FbxCommon.py 3.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. from fbx import *
  2. import sys
  3. def InitializeSdkObjects():
  4. # The first thing to do is to create the FBX SDK manager which is the
  5. # object allocator for almost all the classes in the SDK.
  6. lSdkManager = KFbxSdkManager.Create()
  7. if not lSdkManager:
  8. sys.exit(0)
  9. # Create an IOSettings object
  10. ios = KFbxIOSettings.Create(lSdkManager, IOSROOT)
  11. lSdkManager.SetIOSettings(ios)
  12. # Create the entity that will hold the scene.
  13. lScene = KFbxScene.Create(lSdkManager, "")
  14. return (lSdkManager, lScene)
  15. def SaveScene(pSdkManager, pScene, pFilename, pFileFormat = -1, pEmbedMedia = False):
  16. lExporter = KFbxExporter.Create(pSdkManager, "")
  17. if pFileFormat < 0 or pFileFormat >= pSdkManager.GetIOPluginRegistry().GetWriteFormatCount():
  18. pFileFormat = pSdkManager.GetIOPluginRegistry().GetNativeWriterFormat()
  19. if not pEmbedMedia:
  20. lFormatCount = pSdkManager.GetIOPluginRegistry().GetWriterFormatCount()
  21. for lFormatIndex in range(lFormatCount):
  22. if pSdkManager.GetIOPluginRegistry().WriterIsFBX(lFormatIndex):
  23. lDesc = KString(pSdkManager.GetIOPluginRegistry().GetWriterFormatDescription(lFormatIndex))
  24. if lDesc.Find("ascii") >= 0:
  25. pFileFormat = lFormatIndex
  26. break
  27. if not pSdkManager.GetIOSettings():
  28. ios = KFbxIOSettings.Create(pSdkManager, IOSROOT)
  29. pSdkManager.SetIOSettings(ios)
  30. pSdkManager.GetIOSettings().SetBoolProp(EXP_FBX_MATERIAL, True)
  31. pSdkManager.GetIOSettings().SetBoolProp(EXP_FBX_TEXTURE, True)
  32. pSdkManager.GetIOSettings().SetBoolProp(EXP_FBX_EMBEDDED, pEmbedMedia)
  33. pSdkManager.GetIOSettings().SetBoolProp(EXP_FBX_SHAPE, True)
  34. pSdkManager.GetIOSettings().SetBoolProp(EXP_FBX_GOBO, True)
  35. pSdkManager.GetIOSettings().SetBoolProp(EXP_FBX_ANIMATION, True)
  36. pSdkManager.GetIOSettings().SetBoolProp(EXP_FBX_GLOBAL_SETTINGS, True)
  37. if lExporter.Initialize(pFilename, pFileFormat, pSdkManager.GetIOSettings()):
  38. lExporter.Export(pScene)
  39. lExporter.Destroy()
  40. def LoadScene(pSdkManager, pScene, pFileName):
  41. lImporter = KFbxImporter.Create(pSdkManager, "")
  42. result = lImporter.Initialize(pFileName, -1, pSdkManager.GetIOSettings())
  43. if not result:
  44. return False
  45. if lImporter.IsFBX():
  46. pSdkManager.GetIOSettings().SetBoolProp(EXP_FBX_MATERIAL, True)
  47. pSdkManager.GetIOSettings().SetBoolProp(EXP_FBX_TEXTURE, True)
  48. pSdkManager.GetIOSettings().SetBoolProp(EXP_FBX_EMBEDDED, True)
  49. pSdkManager.GetIOSettings().SetBoolProp(EXP_FBX_SHAPE, True)
  50. pSdkManager.GetIOSettings().SetBoolProp(EXP_FBX_GOBO, True)
  51. pSdkManager.GetIOSettings().SetBoolProp(EXP_FBX_ANIMATION, True)
  52. pSdkManager.GetIOSettings().SetBoolProp(EXP_FBX_GLOBAL_SETTINGS, True)
  53. result = lImporter.Import(pScene)
  54. lImporter.Destroy()
  55. return result