Community
Maya Programming
Welcome to Autodesk’s Maya Forums. Share your knowledge, ask questions, and explore popular Maya SDK topics.
cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

Maya Python Script Export FBX

2 REPLIES 2
SOLVED
Reply
Message 1 of 3
dgeeQYWSW
5934 Views, 2 Replies

Maya Python Script Export FBX

When exporting the FBX file it exports multiple meshes instead of exporting the selected duplicate.  Here is the code.

 

 

import maya.cmds as cmds

# Get the currently selected object
selected_objects = cmds.ls(selection=True)

# Check if there is exactly one object selected
if len(selected_objects) != 1:
    print("Please select exactly one object")
else:
    # Get the selected object
    selected_object = selected_objects[0]

    # Check if the selected object is a mesh
    if cmds.objectType(selected_object) != "mesh":
        print("Please select a mesh")
    else:
        # Select the mesh
        cmds.select(selected_object)


# Duplicate the object
duplicated_object = cmds.duplicate(selected_object, name="duplicated_object")[0]

# Export the duplicated object as an FBX file
export_path = "C:/Users/Devin/Desktop/"
cmds.select(duplicated_object)
FileName = (duplicated_object)
for i in FileName:
    #extension = ".FBX"
    mainpath = export_path+ "/" +i+extension
    cmds.file(mainpath, force=True, options="v=0;", typ="FBX export", pr=True,  ea=True)

 

 

2 REPLIES 2
Message 2 of 3
Kahylan
in reply to: dgeeQYWSW

Hi!

 

The reason your script exports multiple files is that you have the exportAll(ea) flag set to true in your file command instead of exportSelected(es)

 

Now there are quite a few other issues with your script. Your checking logic doesn't really stop the program, it just prints out statements but moves on like nothing happend if you have a wrong selection. Also your check for "mesh" if it would work, it would require the user to select the shape node instead of the transform. That would be quite confusing for most users.

Having a check function that makes sure you only have one object selected but then running the File creation from a loop also doesn't really make sense. Especially since you needed to redeclare your string as a tuple with a string in it to work.

The name of your duplicate object is always "duplicate_object" this will result in the file always being called "duplicate_object" which will cause file clashing issues. Adding the actual name of the object instead of "object" can fix this issue to some degree.

Also The variable "extention" gets called without being defined. but I guess that was a last minute try to fix things before you uploaded here.

 

So here is how I would fix that:

import maya.cmds as cmds
import maya.mel as mel

# Get the currently selected object
selected_objects = cmds.ls(selection=True)

# Check if there is exactly one object selected
if len(selected_objects) != 1:
    cmds.error("Please select exactly one object")
else:
    # Get the selected object
    selected_object = selected_objects[0]

    # Check if the selected object is a mesh
    if cmds.objectType(cmds.listRelatives(selected_object,c= True)[0]) != "mesh":
        cmds.error("Please select a mesh")
    else:
        # Select the mesh
        cmds.select(selected_object)


# Duplicate the object
duplicated_object = cmds.duplicate(selected_object, name="duplicated_{0}".format(selected_object))[0]

# Export the duplicated object as an FBX file
export_path = "users/itz/desktop"
cmds.select(duplicated_object, r= True)

mainpath = export_path + "/" + duplicated_object + ".fbx"
mel.eval( 'FBXExport -f "{0}" -s'.format(mainpath) )

 

Now cmds.file(typ= "FBX export") seems to be Maya version dependant. I never really could get it to work, so I used the FBX MEL wrap instead here so I could make sure it worked, but if it works for you, you can change the last line to:

 

cmds.file(mainpath, force=True, options="v=0;", typ="FBX export", pr=True,  es=True)

 

 

I hope it helps!

 

Message 3 of 3
dgeeQYWSW
in reply to: Kahylan

@Kahylan Thank you I'm still new to coding as you can tell!  But adding checks in is some good advice thank you for that.  Is there any reading that you would recommend to help reinforce that method?

Can't find what you're looking for? Ask the community or share your knowledge.

Post to forums  

Autodesk Design & Make Report