MotionBuilder插件开发常用功能------python

from pyfbsdk import *

1 获取character controls的character的名字
FullName  = FBApplication().CurrentCharacter.FullName 
Name  = FBApplication().CurrentCharacter.Name 
2 选择当前模型character control的character
# 新建一个列表
ComponentList = FBComponentList()
# 把NS:Character放到ComponentList列表中
FBFindObjectsByName('NS:Character', ComponentList, True, False)
# 找参数可以打印出来查看的几个参数
MilaCharacter = ComponentList[0]
# 选中NS:Character
MilaCharacter.Selected = True
3 获取并截取take名字
Takename = FBSystem().CurrentTake.Name
4 查找并选中
Node= FBFindModelByLabelName(‘Role:Name’)
# 选中
Node.Selected = True
5 K帧(旋转、位移)
Node.Translation.Key()
Node.Rotation.Key()
6 从开始帧播放
FBPlayerControl().GotoStart()
FBPlayerControl().Play()
7 获取首尾帧
Syst= FBSystem()
# 首帧
StartTime = Syst.CurrentTake.LocalTimeSpan.GetStart()
StartFrame = Syst.CurrentTake.LocalTimeSpan.GetStart().GetFrame()
# 尾帧
EndTime = Syst.CurrentTake.LocalTimeSpan.GetStop()
EndFrame = Syst.CurrentTake.LocalTimeSpan.GetStop().GetFrame()
8 从尾帧回到首帧
FBPlayerControl().GotoStart()
FBPlayerControl().Stop()
9 获取度数,查找节点(Rotation为例)
RotationNode = Node.Rotation.GetAnimationNode()
# 获取RotationNode节点的值(Nodes下标表示X,Y,Z;Keys下标表示第几帧)
Num = RotationNode.Nodes[0].FCurve.Keys[EndFrame].Value
# 删除RotationNode节点的某一轴(0--x,1--y,2--z)
RotationNode.Nodes[1].FBDelete()
10 修改旋转度数(旋转Y轴为例)
Nameref.Rotation = FBVector3d( 90, 0, -Num)
11 选中position----properties----position----constrain Axes(Affect Y为例)
AffectZProp = constraint.PropertyList.Find( 'AffectY' )
# 选择
AffectZProp.Data = True
12 渲染(plot all…)
# 对plot all属性进行设置并plot
lOptions = FBPlotOptions()
lOptions.PlotAllTakes = False
lOptions.PlotOnFrame = True
lOptions.PlotPeriod = FBTime( 0, 0, 0, 1 ) # 表示首帧
lOptions.PlotLockedProperties = True
lOptions.UseConstantKeyReducer = True
lOptions.ConstantKeyReducerKeepOneKey = True
FBSystem().CurrentTake.PlotTakeOnSelected(lOptions)   # 使上面的设置生效
13 清除节点多余的设置(clear extra effector )
def ClearAuxEffectors(pCharacter, pEffectorId):
    lAuxToClear = []
    for lSetId in FBEffectorSetID.values.values():
        # 默认设置不删除
        if lSetId == FBEffectorSetID.FBEffectorSetDefault: 
            continue
        lAuxEffector = pCharacter.GetEffectorModel(pEffectorId,lSetId)
        if lAuxEffector: 
            lAuxToClear.append(lAuxEffector)
        else:  
            break
    # 清除列表
    for lModel in reversed(lAuxToClear):
        lModel.FBDelete()
# pCharacter
MilaChar = FBSystem().Scene.Characters[1]
lEffectorID =  FBEffectorId.values.values() #[0] 
# run 
for effectorID in lEffectorID:
    ClearAuxEffectors(MilaChar, effectorID)
14 创建position,并判断position是否存在,存在删除
FBDeleteObjectsByName('Position')
constraint = FBConstraintManager().TypeCreateConstraint('Position')
15 创建marker,并判断删除
if not FBFindModelByLabelName('MilaMarker'):
    #创建
    MarkerDevice = FBCreateObject( "Browsing/Templates/Elements", "Marker", "MilaMarker" )
    MarkerDevice.Show = True   # 显示
    MarkerDevice.Size = 1000      # 设置大小
else:
    FBDeleteObjectsByName('MilaMarker')
    MarkerDevice = FBCreateObject( "Browsing/Templates/Elements", "Marker", "MilaMarker" )
    MarkerDevice.Show = True
    MarkerDevice.Size = 1000

######16 约束绑定

# constraint object
constraint.ReferenceAdd(0,MarkerDevice)
# Source
constraint.ReferenceAdd(1,NodeName)
17 约束的 snap 吸附
FBConstraint.Snap(constraint)
18 约束中的Active选择
# 取消
constraint.Active = False
# 勾选
constraint.Active = True
19 Position二次约束
# 移除旧的Marker
constraint.ReferenceRemove(0,MarkerDevice)
# 新的对象
constraint.ReferenceAdd(0,MilaRoot)
constraint.ReferenceRemove(1,MilaHips)
constraint.ReferenceAdd(1,MarkerDevice)
FBConstraint.Snap(constraint)
20 创建新的动画层(Animation Layers)
# 创建
FBSystem().CurrentTake.CreateNewLayer()
# 获取layers数量
mCount = FBSystem().CurrentTake.GetLayerCount()
# 获取创建的动画层位置下标(最后一个)
AnimLayer = FBSystem().CurrentTake.GetLayer(mCount-1)
# 选中
AnimLayer.SelectLayer(True, True)
# 重命名
mSystem.CurrentTake.GetLayer(mCount-1).Name= "MilaLayer"
21 重命名/添加名字(Add/Remove Namespace…)
NS = FBStringList()
Count = FBSystem().Scene.NamespaceGetChildrenList(NS)
for i in NS:
    localNS = FBSystem().Scene.NamespaceGet(i)
    try:
        if "Name" in localNS.Name:
            localNS.Name = "haha" 
        elif localNS.Name == None:
            print "ok"
    except:
        print i
22 打开文件
FBApplication().FileOpen('C:\Path\test.fbx',False)
23 Merge
# 文件路径
nativeFile = r"C:\path\test.fbx"
options = FBFbxOptions(True, nativeFile)
options.SetAll(FBElementAction.kFBElementActionMerge, True)
for takeIndex in range( 0, options.GetTakeCount() ):
    # 取消选择options
    options.SetTakeSelect( takeIndex, False )
FBApplication().FileMerge( nativeFile, False, options )
24 merge文件前缀命名
nativeFile = r"C:\Path" 
options = FBFbxOptions(True, nativeFile)
options.NamespaceList = Names
25 break plot to Skeleton
myPlotOptions = FBPlotOptions ()
myPlotOptions.ConstantKeyReducerKeepOneKey = False
myPlotOptions.PlotAllTakes = False
myPlotOptions.PlotOnFrame = True
myPlotOptions.PlotPeriod = FBTime( 0, 0, 0, 1 )
myPlotOptions.PlotTranslationOnRootOnly = False
myPlotOptions.PreciseTimeDiscontinuities = False
myPlotOptions.RotationFilterToApply = FBRotationFilter.kFBRotationFilterNone
myPlotOptions.UseConstantKeyReducer = False
TheChar = FBApplication().CurrentCharacter
if TheChar.ActiveInput == True:
    TheChar.PlotAnimation(FBCharacterPlotWhere.kFBCharacterPlotOnSkeleton, myPlotOptions)
else:
    TheChar.PlotAnimation(FBCharacterPlotWhere.kFBCharacterPlotOnControlRig, myPlotOptions)
26 character ,source来自不同模型
foundComponents = FBComponentList()
# 选character
FBFindObjectsByName('Mila:Character', foundComponents, True, False)
Character = foundComponents[0]
Character.Selected = True
# 选source
foundComponents = FBComponentList()
FBFindObjectsByName('MilaOld5:Character', foundComponents, True, False)
OldCharacter = foundComponents[0]
Character.InputCharacter = OldCharacter
Character.InputType = FBCharacterInputType.kFBCharacterInputCharacter
Character.ActiveInput = True
27 获取Characters所有文件
name = FBSystem().Scene.Characters
for Name in name:
    print Name
28 IK修改
NodeName= FBFindModelByLabelName('RoleName:NodeName' )
NodeName.Selected = True
# IK Blend T
IK = NodeName.PropertyList[102]
IK.Data = 0   # 修改参数值
# IK Blend R
IK1 = NodeName.PropertyList[103]
IK1.Data = 0
29 获取打开文件名绝对路径
FilePath = FBApplication().FBXFileName
30 选择根模型及子模型
def SelectBranch(topModel):
    for childModel in topModel.Children:
        SelectBranch(childModel)
    topModel.Selected = True
31 导出选中项
itemName = 'C:\Path\Test.fbx'
options = FBFbxOptions(False)
# 只保存选中项,格式为ASCII
options.UseASCIIFormat = True
options.SaveSelectedModelsOnly = True
# 不保存默认选项
options.BaseCameras = False
options.CameraSwitcherSettings = False
options.CurrentCameraSettings = False
options.GlobalLightingSettings = False
options.TransportSettings = False
FBApplication().FileSave(itemName, options)
32 Match Source 的勾选
if FBApplication().CurrentCharacter:
    FBApplication().CurrentCharacter.PropertyList.Find('Match Source').Data=True
33 UI选择保存路径
lFolderPopup = FBFolderPopup()
# 弹框的标题
lFolderPopup.Caption = "Select Save Skeleton File..." 
# 弹框 
lFolderPopup.Execute()   # Execute  执行
# 选择文件的路径
currentFilePath = lFolderPopup.Path
# 拼接的保存后的文件路径及名字
itemName = currentFilePath + '\\' + OpenNF +'.fbx'
options = FBFbxOptions(False)
# 保存
FBApplication().FileSave(itemName, options)
34 对merge文件进行前缀命名
nativeFile = "F:\\WildDogFile" 
options = FBFbxOptions(True, nativeFile)
# 通过命名赋值来实现的导入文件名和之前的名字一样 
options.NamespaceList = Names
35 获取Characters下的所有文件
name = FBSystem().Scene.Characters
# 遍历name列表所有名字
for Name in name:
    print Name
36 修改场景的帧率60帧
FBPlayerControl().SetTransportFps(FBTimeMode.kFBTimeMode60Frames)
37 多个take处理方法
# 保存多余take
OldTakeList = []
for take in FBSystem().Scene.Takes:
    OldTakeList.append(take.Name)
# 删除多余take
for take in FBSystem().Scene.Takes:
    if take.Name in OldTakeList:
        take.FBDelete()
38 获取相机动画所以节点
Camera = FBSystem().Scene.Cameras[-1]  # 获取相机名字
CamAnimNodes = Camera.AnimationNode.Nodes  # 获取节点
39 获取场景的原路径
FBFindObjectByFullName( 'FileReference::CJName' ).ReferenceFilePath
40 获取相机指向目标
Cameraname = FBSystem().Scene.Cameras[-1]
CamInter = Cameraname.Interest  
41 预留帧,起始帧结束帧各预留100帧
paddFrames = 100
if paddFrames:
    StartFrameTime -= paddFrames
    EndFrameTime += paddFrames
return str(StartFrameTime), str(EndFrameTime)
42 设置世界坐标globel(GetVector获取)
SkinName = FBFindModelByLabelName('MilaRig:SkinJnt')
SkinName.Selected = True
SkinName.SetVector (FBEditVector(90,71,180), FBModelTransformationType.kModelTranslation) # 位移
SkinName.SetVector( FBEditVector(1800,71,18), FBModelTransformationType.kModelRotation ) # 旋转
FBSystem().Scene.Evaluate()  # 此步骤必须有,否则修改失败(在最后有一个即可)
43 删除多余帧
def DelFrame(Model, Startframe, Endframe):
    AnimationNodes = Model.AnimationNode.Nodes
    for animNode in AnimationNodes:
        for node in animNode.Nodes:
            Fcurve = node.FCurve
            # 第一个参数是次数,第二个参数是删除的数量,即步数
            Fcurve.KeyDeleteByIndexRange(1, Endframe-Startframe)

DelFrame(SkinName, Startframe, Endframe)
44 选择场景中所有东西
for model in FBSystem().Scene.RootModel.Children:
    SelectBranch(model)
45 删除选择模型
ModelList = FBModelList()
FBGetSelectedModels(ModelList)
for child in ModelList:
    child.FBDelete()
如有缺陷请多多指教
发布了13 篇原创文章 · 获赞 16 · 访问量 1922

猜你喜欢

转载自blog.csdn.net/weixin_43614573/article/details/102635449