Working with fbx files with python in maya

fbx, or fbx(why are you such a pain?)

Working with fbx files in maya using python isn’t the most frustrating format to work with, just very picky.

When using python, try to stay away from using maya file command:

import maya.cmds as Mc

Mc.file(strFile, i=True, type='fbx')

You can by all means use that command, but it’s buggy when you want to have certain options for your import. i.e. if your open file preferences are set to maya native compared to os native, the fbx import options windows doesn’t always come up before import.

A better way is to use maya.mel commands for fbx:

import maya.mel as Mm
import maya.cmds as Mc

Mm.eval('FBXImportMode -v Exmerge')
Mm.eval('FBXImportMergeAnimationLayers -v true')
Mm.eval('FBXImportProtectDrivenKeys -v true')
Mm.eval('FBXImportConvertDeformingNullsToJoint -v true')
Mm.eval('FBXImportMergeBackNullPivots -v false')
Mm.eval('FBXImportSetLockedAttribute -v true')
Mm.eval('FBXImportConstraints -v false')
		
Mm.eval('FBXImport -f "yourFileHere.fbx"')

Strange right?

The way fbx code works is kinda like building up what you want for options and then importing your file.

It would make more sense to have a command with options built into it, i.e:

import maya.mel as Mm

Mm.eval('FBXImport -f "yourFileHere" -importMode "exmerge", -protectKeys')

Alas, we can’t have something nice to work with. Now, since we are using mel commands in python, we need a way to transfer between python and mel variables.

To store python data in a mel variable we can use:

float $melDir = `python "pythonDir"`;

To store mel data in a python variable we can use:

import maya.cmds as Mc
import maya.mel as Mm

pythonDir = Mm.eval("$temp=$melDir")

Now that’s all fine and dandy, but we are working with python from the start. So we need a way to store a python variable in mel, from python.

So, to do that:

import maya.cmds as Mc
import maya.mel as Mm

strDir = "C:\Something\Something\Darkside"

Mm.eval('string $strDir = `python "strDir"`;')

So we can finally put it all together in a python function:

import maya.cmds as Mc
import maya.mel as Mm


def importFbx():
    strDir = "C:\Something\Something\Darkside.fbx"

    Mm.eval('string $strDir = `python "strDir"`;')

    Mm.eval('FBXImportMode -v Exmerge')
    Mm.eval('FBXImportMergeAnimationLayers -v true')
    Mm.eval('FBXImportProtectDrivenKeys -v true')
    Mm.eval('FBXImportConvertDeformingNullsToJoint -v true')
    Mm.eval('FBXImportMergeBackNullPivots -v false')
    Mm.eval('FBXImportSetLockedAttribute -v true')
    Mm.eval('FBXImportConstraints -v false')

    Mm.eval('FBXImport -f $strDir')

You can find references for commands here:
http://knowledge.autodesk.com/support/maya/learn-explore/caas/CloudHelp/cloudhelp/2015/ENU/Maya/files/GUID-699CDF74-3D64-44B0-967E-7427DF800290-htm.html

2 thoughts on “Working with fbx files with python in maya”

  1. very helpful post. I really appreciate the reminder on Python variables in Mel. I’ve been searching for about 30 minutes for this exact page.

    Thanks!

Leave a Reply to mike Cancel reply

Your email address will not be published. Required fields are marked *