importexport/FBXExtension/mbcustomsurfaceplugin.cxx

importexport/FBXExtension/mbcustomsurfaceplugin.cxx
/***************************************************************************************
Autodesk(R) Open Reality(R) Samples
(C) 2009 Autodesk, Inc. and/or its licensors
All rights reserved.
AUTODESK SOFTWARE LICENSE AGREEMENT
Autodesk, Inc. licenses this Software to you only upon the condition that
you accept all of the terms contained in the Software License Agreement ("Agreement")
that is embedded in or that is delivered with this Software. By selecting
the "I ACCEPT" button at the end of the Agreement or by copying, installing,
uploading, accessing or using all or any portion of the Software you agree
to enter into the Agreement. A contract is then formed between Autodesk and
either you personally, if you acquire the Software for yourself, or the company
or other legal entity for which you are acquiring the software.
AUTODESK, INC., MAKES NO WARRANTY, EITHER EXPRESS OR IMPLIED, INCLUDING BUT
NOT LIMITED TO ANY IMPLIED WARRANTIES OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR
PURPOSE REGARDING THESE MATERIALS, AND MAKES SUCH MATERIALS AVAILABLE SOLELY ON AN
"AS-IS" BASIS.
IN NO EVENT SHALL AUTODESK, INC., BE LIABLE TO ANYONE FOR SPECIAL, COLLATERAL,
INCIDENTAL, OR CONSEQUENTIAL DAMAGES IN CONNECTION WITH OR ARISING OUT OF PURCHASE
OR USE OF THESE MATERIALS. THE SOLE AND EXCLUSIVE LIABILITY TO AUTODESK, INC.,
REGARDLESS OF THE FORM OF ACTION, SHALL NOT EXCEED THE PURCHASE PRICE OF THE
MATERIALS DESCRIBED HEREIN.
Autodesk, Inc., reserves the right to revise and improve its products as it sees fit.
Autodesk and Open Reality are registered trademarks or trademarks of Autodesk, Inc.,
in the U.S.A. and/or other countries. All other brand names, product names, or
trademarks belong to their respective holders.
GOVERNMENT USE
Use, duplication, or disclosure by the U.S. Government is subject to restrictions as
set forth in FAR 12.212 (Commercial Computer Software-Restricted Rights) and
DFAR 227.7202 (Rights in Technical Data and Computer Software), as applicable.
Manufacturer is Autodesk, Inc., 10 Duke Street, Montreal, Quebec, Canada, H3C 2L7.
***************************************************************************************/
#include "mbextension.h"
#include "../../miscellaneous/material_template/ormaterial_template_material.h"
#include <fbxsdk/fbxsdk_nsbegin.h>
static FbxManager* gFbxManager = NULL;
static bool gProcessedOnce = false;
// Custom Material declaration
#define MY_CUSTOM_SURFACE_CLASS_NAME "MyCustomSurfaceClass"
#define MY_CUSTOM_SURFACE_CLASS_SUBTYPE "MyCustomSurfaceClass" // need to match the SURFACE_CLASS_NAME so the class can be recognized
// by the FBX reader.
class MyCustomSurface : public FbxSurfaceMaterial
{
FBXSDK_OBJECT_DECLARE(MyCustomSurface, FbxSurfaceMaterial);
protected:
void ConstructProperties(bool pForceSet)
{
ParentClass::ConstructProperties(pForceSet);
Diffuse.StaticInit(this, "Diffuse", FbxColor3DT, FbxDouble3(0,0,0), pForceSet);
}
public:
FbxPropertyT<FbxDouble3> Diffuse;
};
FBXSDK_OBJECT_IMPLEMENT(MyCustomSurface);
class MBCustomSurfacePlugin : public FbxPlugin
{
FBXSDK_PLUGIN_DECLARE(MBCustomSurfacePlugin);
protected:
explicit MBCustomSurfacePlugin(const FbxPluginDef& pDefinition, FbxModule pFbxModule) : FbxPlugin(pDefinition, pFbxModule)
{
}
// Implement kfbxmodules::FbxPlugin
virtual bool SpecificInitialize()
{
gFbxManager = GetData().mSDKManager;
// Register MyCustomSurfacer class with the plug-in's manager
gFbxManager->RegisterFbxClass(MY_CUSTOM_SURFACE_CLASS_NAME, FBX_TYPE(MyCustomSurface),FBX_TYPE(FbxSurfaceMaterial), FIELD_OBJECT_DEFINITION_OBJECT_TYPE_MATERIAL, MY_CUSTOM_SURFACE_CLASS_SUBTYPE);
return true;
}
virtual bool SpecificTerminate()
{
// Unregister MyCustomSurface class with the plug-in's manager
gFbxManager->UnregisterFbxClass(FBX_TYPE(MyCustomSurface));
return true;
}
};
FBXSDK_PLUGIN_IMPLEMENT(MBCustomSurfacePlugin);
extern "C"
{
// The DLL is owner of the plug-in
static MBCustomSurfacePlugin* sPlugin = NULL;
// This function will be called when an application will request the plug-in
EXPORT_DLL void FBXPluginRegistration(FbxPluginContainer& pContainer, FbxModule pFbxModule)
{
if( sPlugin == NULL )
{
// Create the plug-in definition which contains the information about the plug-in
FbxPluginDef sPluginDef;
sPluginDef.mName = "MBCustomSurfacePlugin";
sPluginDef.mVersion = "1.0";
// Create an instance of the plug-in. The DLL has the ownership of the plug-in
sPlugin = MBCustomSurfacePlugin::Create(sPluginDef, pFbxModule);
// Register the plug-in
pContainer.Register(*sPlugin);
}
}
FBX_MB_EXTENSION_DECLARE();
}
bool MBExt_ExportHandled( FBComponent* pFBComponent )
{
// In this example, we aren't replacing any objects in the fbx scene, so we don't have to
// prevent the MB plugin from translating MB objects into fbx objects.
return false;
}
void MBExt_ExportBegin(FbxScene* pFbxScene)
{
// In this example, we will create our custom data during the export process.
// So there is nothing to do here.
}
void MBExt_ExportTranslated(FbxObject* pFbxObject, FBComponent* pFBComponent)
{
// Since we aren't replacing objects in the scene hierarchy in this example, we don't have
// anything to do with this information.
}
bool MBExt_ExportProcess( FbxObject*& pOutputFbxObject, FBComponent* pInputObject, FbxScene* pFbxScene)
{
// In this example, we replace the Maya phong shader with our custom material.
// Because this function is called several times during the export, we need to figure out what
// we are processing and take action only on the objects of interest.
// validate the inputs
if (pOutputFbxObject == NULL || pInputObject == NULL || pFbxScene == NULL)
return false;
// If *pOutputFbxObject is NULL, the caller expect us to create an object and return it back.
// In this case, we replace all the Phong materials we receive with our own with a white color.
if (FBString(pInputObject->ClassName())==ORMATERIALCUSTOM__CLASSSTR)
{
// Make sure our custom object class was correctly registered
static FbxClassId MyCustomSurfaceDataClassId = gFbxManager->FindClass(MY_CUSTOM_SURFACE_CLASS_NAME);
// if not, we notify the caller that we cannot handle the received object
if( !MyCustomSurfaceDataClassId.IsValid() ) return false;
// Create the custom object to hold the custom data
MyCustomSurface* MyCustomSurfaceInstance = MyCustomSurface::Create(pFbxScene, pInputObject->Name);
MyCustomSurfaceInstance->Diffuse.Set(FbxDouble3(1, 1, 1));
// Destroy any object created before
if(pOutputFbxObject) pOutputFbxObject->Destroy();
// Replace with new one
pOutputFbxObject = FbxCast<FbxObject>(MyCustomSurfaceInstance);;
}
// let the process continue to export as usual even if the object was replaced
return false;
}
void MBExt_ExportEnd(FbxScene* pFbxScene)
{
//Since we process all our data in the MBExt_ExportProcess callback, we have nothing to do here.
}
bool MBExt_ImportHandled( FbxObject* pFbxObject )
{
// In this example, we want to intercept the processing pass only. If we return true here, we tell the
// caller that we are responsible for creating the geometry associated with pFbxObject which we don't.
// So we return false and let the caller process the geometry as usual.
return false;
}
void MBExt_ImportBegin(FbxScene* pFbxScene)
{
// In this example we have nothing to do before we translate the FBX scene into
// MB data.
}
bool MBExt_ImportProcess( FBComponent*& pOutputObject, FbxObject* pInputFbxObject, bool pIsAnInstance, bool pMerge)
{
// Validate the input
if (pInputFbxObject == NULL)
return false;
if (pIsAnInstance)
{
// we have been called with an instance...actually this block simply shows how we would check and
// handle the case of an instance. But this case can never happen with the current Maya plug-in and
// the material objects (pIsAnInstance is always false)
if (gProcessedOnce)
return true;
else
gProcessedOnce = true;
}
// check that we have received an object that derives from the FbxSurfaceMaterial class.
FbxSurfaceMaterial* lSurfaceMat = FbxCast<FbxSurfaceMaterial>(pInputFbxObject);
if (lSurfaceMat == NULL)
// we did not: return immediately and let the caller know that we did not process the input object
return false;
// Now that we validated that the input object is some sort of SurfaceMaterial, we make sure that
// it is our own custom material.
if (!lSurfaceMat->Is<MyCustomSurface>())
// nope! again, we return immediately
return false;
// All right! We have our data object...
MyCustomSurface* lCustSurface = (MyCustomSurface*)lSurfaceMat;
// get the only attribute of our custom surface...
FbxDouble3 lFbxColor = lCustSurface->Diffuse.Get();
// Let's create our ORMaterialCustom and give it the Diffuse color of our custom object.
ORMaterialCustom* lTempMaterial;
if (pMerge)
{
// we are importing in a merge-back operation.
if (pOutputObject == NULL)
{
// there is a problem! pOutputObject must be non Null
return false;
}
// let's see if we have a Phong shader interface
lTempMaterial = (FBString(pOutputObject->ClassName())==ORMATERIALCUSTOM__CLASSSTR) ? (ORMaterialCustom*)(pOutputObject) : NULL;
}
else
{
// Let's first delete the object that the importer created, the create our custom one.
pOutputObject->FBDelete();
lTempMaterial = (ORMaterialCustom*)FBCreateObject( "Browsing/Templates/Shading Elements",ORMATERIALCUSTOM__CLASSSTR,lSurfaceMat->GetName());
pOutputObject = lTempMaterial;
}
if(lTempMaterial)
{
// Yep! Let's set the color
lTempMaterial->Diffuse.SetData(&lFbxColor);
}
return false;
}
void MBExt_ImportTranslated( FbxObject* pFbxObject, FBComponent* pFBComponent )
{
// In this example, this doesn't interest us. A typical usage would be to
// record this information to able to re-make connections and such.
}
void MBExt_ImportEnd(FbxScene* pFbxScene)
{
// In this example, this doesn't interest us either. Every thing has been processed in
// the MBExt_ImportProcess callback.
}
#include <fbxsdk/fbxsdk_nsend.h>