问题描述:
Maya 导出导入 FBX,设置 FBX 的 导入和导出设置
解决方案:
获取FBX设置
python
def getFBXSettings():
""" get current user settings for FBX export and store them """
mel.eval('FBXPushSettings;')
def setFBXSettings():
""" set user-defined FBX settings back after export """
mel.eval('FBXPopSettings;')
导出 FBX
python
def exportFBX(export_file_path, selected=True, **kwargs):
"""
"""
smoothing_groups = kwargs.get('smoothing_groups', 'true')
hard_edges = kwargs.get('hard_edges', 'false')
tangents = kwargs.get('tangents', 'false')
smooth_mesh = kwargs.get('smooth_mesh', 'true')
instances = kwargs.get('instances', 'false')
referenced_assets_content = kwargs.get('referenced_assets_content',
'false')
animation_only = kwargs.get('animation_only', 'false')
bake_complex_animation = kwargs.get('bake_complex_animation', 'false')
bake_complex_step = kwargs.get('bake_complex_step', '1')
use_scene_name = kwargs.get('use_scene_name', 'false')
quaternion = kwargs.get('quaternion', 'euler')
shapes = kwargs.get('shapes', 'false')
skins = kwargs.get('skins', 'false')
skeleton = kwargs.get('skeleton', 'false')
constraints = kwargs.get('constraints', 'false')
cameras = kwargs.get('cameras', 'false')
lights = kwargs.get('lights', 'false')
embed_media = kwargs.get('embed_media', 'false')
connections = kwargs.get('connections', 'false')
up_axis = kwargs.get('up_axis', 'y')
in_ascii = kwargs.get('in_ascii', 'false')
# store current user FBX settings
getFBXSettings()
# export selected as FBX
# Geometry
mel.eval("FBXExportSmoothingGroups -v {}".format(smoothing_groups))
mel.eval("FBXExportHardEdges -v {}".format(hard_edges))
mel.eval("FBXExportTangents -v {}".format(tangents))
mel.eval("FBXExportSmoothMesh -v {}".format(smooth_mesh))
mel.eval("FBXExportInstances -v {}".format(instances))
mel.eval("FBXExportReferencedAssetsContent -v {}".format(
referenced_assets_content))
mel.eval("FBXExportAnimationOnly -v {}".format(animation_only))
mel.eval(
"FBXExportBakeComplexAnimation -v {}".format(bake_complex_animation))
mel.eval("FBXExportBakeComplexStep -v {}".format(bake_complex_step))
mel.eval("FBXExportUseSceneName -v {}".format(use_scene_name))
mel.eval("FBXExportQuaternion -v {}".format(quaternion))
mel.eval("FBXExportShapes -v {}".format(shapes))
mel.eval("FBXExportSkins -v {}".format(skins))
mel.eval('FBXExportSkeletonDefinitions -v {}'.format(skeleton))
# Constraints
mel.eval("FBXExportConstraints -v {}".format(constraints))
# Cameras
mel.eval("FBXExportCameras -v {}".format(cameras))
# Lights
mel.eval("FBXExportLights -v {}".format(lights))
# Embed Media
mel.eval("FBXExportEmbeddedTextures -v {}".format(embed_media))
# Connections
mel.eval("FBXExportInputConnections -v {}".format(connections))
# Axis Conversion
mel.eval("FBXExportUpAxis {}".format(up_axis))
# Version
# mel.eval("FBXExportFileVersion -v FBX201600")
mel.eval("FBXExportInAscii -v {}".format(in_ascii))
try:
# cmds.file(r'{}'.format(export_file_path), exportSelected=selected,
# type="FBX export",
# force=True, prompt=False)
if selected:
cmds.FBXExport('-file', export_file_path, '-s')
else:
cmds.FBXExport('-file', export_file_path)
except Exception as e:
print(e)
print(export_file_path)
# restore current user FBX settings
setFBXSettings()
导入FBX
python
def import_fbx(fbx_file, **kwargs):
log = kwargs.get('log', 'false')
axis_conversion = kwargs.get('axis_conversion', 'true')
cache_file = kwargs.get('cache_file', 'true')
import_camera = kwargs.get('import_camera', 'false')
import_constraints = kwargs.get('import_constraints', 'false')
convert_joint = kwargs.get('convert_joint', 'true')
convert_unit = kwargs.get('convert_unit', 'cm')
fit_timeline = kwargs.get('fit_timeline', 'true')
force_axis = kwargs.get('force_axis', 'y')
hard_edge = kwargs.get('hard_edge', 'false')
import_light = kwargs.get('import_light', 'false')
merge_pivots = kwargs.get('merge_pivots', 'true')
merge_ani_layer = kwargs.get('merge_ani_layer', 'true')
import_mode = kwargs.get('import_mode', 'merge')
import_driven_key = kwargs.get('import_driven_key', 'false')
quaternion = kwargs.get('quaternion', 'resample')
resample_rate = kwargs.get('resample_rate', 'File')
# scale_factor_enable = kwargs.get('scale_factor_enable', 'false')
scale_factor = kwargs.get('scale_factor_enable', 1.0)
frame_rate = kwargs.get('frame_rate', 'true')
lock_attr = kwargs.get('lock_attr', 'false')
set_take = kwargs.get('set_take', 0)
import_shape = kwargs.get('import_shape', 'true')
skeleton_type = kwargs.get('skeleton_type', 'humanik')
import_skin = kwargs.get('import_skin', 'true')
lock_normal = kwargs.get('lock_normal', 'false')
up_axis = kwargs.get('up_axis', 'y')
# store current user FBX settings
getFBXSettings()
# options
mel.eval("FBXImportGenerateLog -v {}".format(log))
mel.eval("FBXImportAxisConversionEnable -v {}".format(axis_conversion))
mel.eval("FBXImportCacheFile -v {}".format(cache_file))
mel.eval("FBXImportCameras -v {}".format(import_camera))
mel.eval("FBXImportConstraints -v {}".format(import_constraints))
mel.eval("FBXImportConvertDeformingNullsToJoint -v {}".format(convert_joint))
mel.eval("FBXImportConvertUnitString {}".format(convert_unit))
mel.eval("FBXImportFillTimeline -v {}".format(fit_timeline))
mel.eval("FBXImportForcedFileAxis -v {}".format(force_axis))
mel.eval("FBXImportHardEdges -v {}".format(hard_edge))
mel.eval("FBXImportLights -v {}".format(import_light))
mel.eval("FBXImportMergeBackNullPivots -v {}".format(merge_pivots))
mel.eval("FBXImportMergeAnimationLayers -v {}".format(merge_ani_layer))
mel.eval("FBXImportMode -v {}".format(import_mode))
mel.eval("FBXImportProtectDrivenKeys -v {}".format(import_driven_key))
mel.eval("FBXImportQuaternion -v {}".format(quaternion))
mel.eval("FBXImportResamplingRateSource -v {}".format(resample_rate))
# mel.eval("FBXImportScaleFactorEnable {}".format(scale_factor_enable))
mel.eval("FBXImportScaleFactor {}".format(scale_factor))
mel.eval("FBXImportSetMayaFrameRate -v {}".format(frame_rate))
mel.eval("FBXImportSetLockedAttribute -v {}".format(lock_attr))
mel.eval("FBXImportSetTake -ti {}".format(set_take))
mel.eval("FBXImportShapes -v {}".format(import_shape))
# mel.eval("FBXImportSkeletonType -v {}".format(skeleton_type))
mel.eval("FBXImportSkins -v {}".format(import_skin))
mel.eval("FBXImportUnlockNormals -v {}".format(lock_normal))
mel.eval("FBXImportUpAxis {}".format(up_axis))
try:
cmds.FBXImport('-file', fbx_file)
except Exception as e:
traceback.print_exc()
print(fbx_file)
# restore current user FBX settings
setFBXSettings()