Skip to content

tahv/mayafbx

Repository files navigation

mayafbx

Warning:

Release 1.0.0 include many API breaking changes and dropped Python 2 support. If you are looking for the legacy version of mayafbx, see this commit and release 0.1.0.

License - MIT Maya Version PyPI - Python Version PyPI - Version Linter - Ruff Types - Mypy CI - Tests Documentation Status

Python wrapper of Maya FBX plugin.

Installation

Install mayafbx with pip.

python -m pip install mayafbx

You can also download and extract mayafbx-<version>.zip from latest release.

The zip archive is created using hatch-zipped-directory and has the following structure:

.
├── LICENSE
├── METADATA.json
├── README.md
└── mayafbx/
    ├── __init__.py
    └── ...

Comparison

Below is an example of how to export an FBX file using standard Maya commands:

from maya import mel

mel.eval("FBXResetExport")  # Reset options.
mel.eval("FBXProperty Export|IncludeGrp|Animation -v true")
mel.eval("FBXProperty Export|IncludeGrp|Animation|ExtraGrp|RemoveSingleKey -v true")
mel.eval("FBXProperty Export|IncludeGrp|CameraGrp|Camera -v false")

mel.eval('FBXExport -f "C:/outfile.fbx" -s')  # '-s' for selected.

And here is how to achieve the same using mayafbx:

from mayafbx import FbxExportOptions, export_fbx

options = FbxExportOptions()
options.animation = True
options.remove_single_key = True
options.cameras = True

export_fbx("C:/outfile.fbx", options, selection=True)

Alternatively, you can write it in a more concise way:

from mayafbx import FbxExportOptions, export_fbx

options = FbxExportOptions(animation=True, remove_single_key=True, cameras=True)

export_fbx("C:/outfile.fbx", options, selection=True)

Quickstart

In this example, we export the animation from a cube and import it back.

import os
import tempfile
from maya import cmds
from mayafbx import (
    FbxExportOptions, 
    FbxImportOptions, 
    MergeMode,
    export_fbx,
    import_fbx, 
)

# Start from an empty scene.
cmds.file(new=True, force=True)

# Create a cube with 2 keyframes.
cube = cmds.polyCube()[0]
cmds.setKeyframe(cube, attribute="translateX", time=1, value=0)
cmds.setKeyframe(cube, attribute="translateX", time=24, value=10)

# Prepare options to export baked animation.
options = FbxExportOptions()
options.animation = True
options.bake_animation = True
options.bake_resample_all = True

# Export the scene as FBX.
filepath = os.path.join(tempfile.gettempdir(), "testcube.fbx")
export_fbx(filepath, options)

# Remove all keys from the cube.
cmds.cutKey(cube, attribute="translateX", option="keys")

# Prepare options to import animation back to the cube.
options = FbxImportOptions()
options.merge_mode = MergeMode.kMerge
options.animation = True

# Import the previously exported FBX.
import_fbx(filepath, options)

Documentation

See mayafbx documentation for more details.

Contributing

For guidance on setting up a development environment and contributing to the project, see the contributing section.