This topic presents a series of steps which allow you to use the Python FBX module ("fbx") in Eclipse - a cross-platform integrated development environment (IDE). Python code can be written in any text editor, however Eclipse provides an environment to write, debug, and execute Python code.
The following steps have been tested on Windows 7 x64 using FBX SDK 2013, Python 2.6, Java JRE 7.4, Eclipse Indigo SR2, and PyDev 2.5.0.2012040618. Some steps may change depending on your operating system and architecture.
Work with" field to the following url: http://pydev.org/updates, and press the "Add" button. This url was obtained from: http://pydev.org/download.html.pydev", or another arbitrary name.PyDev", and "PyDev Mylyn Integration (optional)" checkboxes. Check the "PyDev" checkbox and press the "Next" button.Finish" button, which will prompt Eclipse to install the PyDev plug-in. Eclipse will likely need to restart at the end of the plug-in installation.PyDev" folder, select "PyDev Project", and press "Next".Please configure an interpreter in the related preferences before proceeding link near the bottom of the window.New".Browse" and select the installed Python 2.6 interpreter executable. By default on Windows, this file can be found at: C:\Python26\python.exe . Press "Ok" to select the interpreter.Ok" to add the presented folders to the SYSTEM pythonpath. Press "Ok" again to exit the current window.myFbxPoject". Press "Finish", and press "Yes" to enable the PyDev perspective in Eclipse.Now that we have created a new PyDev project, we can get to coding our first Python FBX file:
Pressing "Finish" will generate the myFbxProgram.py file within your PyDev project, whose contents resemble the following template:
'''
Created on ******
@author: ******
'''
if __name__ == '__main__':
pass
Note The if _\_name_\_ == '_\_main_\_': statement (and all the code below it) is executed when you right-click and select Run As > Python Run on your myFbxProgram.py file. This code can also be debugged by selecting Debug As > Python Run, which will open the Eclipse debugging perspective. For now, the code will do nothing, as illustrated by the "pass" filler keyword. The code surrounded by the three single-quotes is a multi-line comment.
To use the functions provided by the Python FBX bindings, write the following import statement near the top of the file. Eclipse recognizes this module because it was copied it into the Python interpreter's /site-packages/ directory in step 1.2 of the installation above.
import fbx
We can now add a few lines of code which use the fbx module to create and export a simple scene containing a cubic mesh. Replace the code currently contained in myFbxProgram.py with the following code. As a brief overview, the code below performs the following steps:
FbxManager, which is used to instantiate the rest of the FBX-related objects.FbxScene using the FbxManager.myFbxSaveFile.fbx in the FBX ASCII file format. '''
myFbxProgram.py
> Creates a cube in a scene, and saves the scene to 'myFbxSaveFile.fbx' in ASCII format.
'''
import fbx
# A set of vertices which we will use to create a cube in the scene.
cubeVertices = [ fbx.FbxVector4( -5, -5, 5 ), # 0 - vertex index.
fbx.FbxVector4( 5, -5, 5 ), # 1
fbx.FbxVector4( 5, 5, 5 ), # 2
fbx.FbxVector4( -5, 5, 5 ), # 3
fbx.FbxVector4( -5, -5, -5 ), # 4
fbx.FbxVector4( 5, -5, -5 ), # 5
fbx.FbxVector4( 5, 5, -5 ), # 6
fbx.FbxVector4( -5, 5, -5 )] # 7
# The polygons (faces) of the cube.
polygonVertices = [ ( 0, 1, 2, 3 ), # Face 1 - composed of the vertex index sequence: 0, 1, 2, and 3.
( 4, 5, 6, 7 ), # Face 2
( 8, 9, 10, 11 ), # Face 3
( 12, 13, 14, 15 ), # Face 4
( 16, 17, 18, 19 ), # Face 5
( 20, 21, 22, 23 )] # Face 6
saveFilename = 'myFbxSaveFile.fbx'
###############################################################
# Helper Function(s). #
###############################################################
def addCube( pScene ):
''' Adds a cubic mesh to the scene. '''
# Obtain a reference to the scene's root node.
rootNode = pScene.GetRootNode()
# Create a new node in the scene.
newNode = fbx.FbxNode.Create( pScene, 'myNode' )
rootNode.AddChild( newNode )
# Create a new mesh node attribute in the scene, and set it as the new node's attribute
newMesh = fbx.FbxMesh.Create( pScene, 'myMesh' )
newNode.SetNodeAttribute( newMesh )
# Define the new mesh's control points. Since we are defining a cubic mesh,
# there are 4 control points per face, and there are six faces, for a total
# of 24 control points.
global cubeVertices
newMesh.InitControlPoints( 24 )
# Face 1
newMesh.SetControlPointAt( cubeVertices[0], 0 )
newMesh.SetControlPointAt( cubeVertices[1], 1 )
newMesh.SetControlPointAt( cubeVertices[2], 2 )
newMesh.SetControlPointAt( cubeVertices[3], 3 )
# Face 2
newMesh.SetControlPointAt( cubeVertices[1], 4 )
newMesh.SetControlPointAt( cubeVertices[5], 5 )
newMesh.SetControlPointAt( cubeVertices[6], 6 )
newMesh.SetControlPointAt( cubeVertices[2], 7 )
# Face 3
newMesh.SetControlPointAt( cubeVertices[5], 8 )
newMesh.SetControlPointAt( cubeVertices[4], 9 )
newMesh.SetControlPointAt( cubeVertices[7], 10 )
newMesh.SetControlPointAt( cubeVertices[6], 11 )
# Face 4
newMesh.SetControlPointAt( cubeVertices[4], 12 )
newMesh.SetControlPointAt( cubeVertices[0], 13 )
newMesh.SetControlPointAt( cubeVertices[3], 14 )
newMesh.SetControlPointAt( cubeVertices[7], 15 )
# Face 5
newMesh.SetControlPointAt( cubeVertices[3], 16 )
newMesh.SetControlPointAt( cubeVertices[2], 17 )
newMesh.SetControlPointAt( cubeVertices[6], 18 )
newMesh.SetControlPointAt( cubeVertices[7], 19 )
# Face 6
newMesh.SetControlPointAt( cubeVertices[1], 20 )
newMesh.SetControlPointAt( cubeVertices[0], 21 )
newMesh.SetControlPointAt( cubeVertices[4], 22 )
newMesh.SetControlPointAt( cubeVertices[5], 23 )
# Now that the control points per polygon have been defined, we can create
# the actual polygons within the mesh.
global polygonVertices
for i in range( 0, len( polygonVertices ) ):
newMesh.BeginPolygon( i )
for j in range( 0, len( polygonVertices[i] ) ):
newMesh.AddPolygon( polygonVertices[i][j] )
newMesh.EndPolygon()
def saveScene( pFilename, pFbxManager, pFbxScene, pAsASCII=False ):
''' Save the scene using the Python FBX API '''
exporter = fbx.FbxExporter.Create( pFbxManager, '' )
if pAsASCII:
#DEBUG: Initialize the FbxExporter object to export in ASCII.
asciiFormatIndex = getASCIIFormatIndex( pFbxManager )
isInitialized = exporter.Initialize( pFilename, asciiFormatIndex )
else:
isInitialized = exporter.Initialize( pFilename )
if( isInitialized == False ):
raise Exception( 'Exporter failed to initialize. Error returned: ' + str( exporter.GetStatus().GetErrorString() ) )
exporter.Export( pFbxScene )
exporter.Destroy()
def getASCIIFormatIndex( pManager ):
''' Obtain the index of the ASCII export format. '''
# Count the number of formats we can write to.
numFormats = pManager.GetIOPluginRegistry().GetWriterFormatCount()
# Set the default format to the native binary format.
formatIndex = pManager.GetIOPluginRegistry().GetNativeWriterFormat()
# Get the FBX format index whose corresponding description contains "ascii".
for i in range( 0, numFormats ):
# First check if the writer is an FBX writer.
if pManager.GetIOPluginRegistry().WriterIsFBX( i ):
# Obtain the description of the FBX writer.
description = pManager.GetIOPluginRegistry().GetWriterFormatDescription( i )
# Check if the description contains 'ascii'.
if 'ascii' in description:
formatIndex = i
break
# Return the file format.
return formatIndex
###############################################################
# Main. #
###############################################################
if __name__ == '__main__':
# Create the required FBX SDK data structures.
fbxManager = fbx.FbxManager.Create()
fbxScene = fbx.FbxScene.Create( fbxManager, '' )
# Add a cube to the scene.
addCube( fbxScene )
# Save the scene.
saveScene( saveFilename, fbxManager, fbxScene, pAsASCII=True )
### CLEANUP ###
#
# Destroy the fbx manager explicitly, which recursively destroys
# all the objects that have been created with it.
fbxManager.Destroy()
#
# Once the memory has been freed, it is good practice to delete
# the currently invalid references contained in our variables.
del fbxManager, fbxScene
#
###############
myFbxProgram.py file and select Run As > Python RunmyFbxProject project folder and select Refresh. You will notice that the myFbxSaveFile.fbx file has been added to your project folder. This file contains the scene produced by running myFbxProgram.py.myFbxSaveFile.fbx file, and select Properties. In the Resource tab, the Location field specifies the path of your newly generated .fbx file. This file can be viewed in any Autodesk program which supports the .fbx file format.