# coding=utf-8
import os
import json
from pyfbsdk import *
global allNodes
def getBranchObj(topModel):
'''
递归获取所有的对象
:param topModel:
:return:
'''
global allNodes
for childModel in topModel.Children:
getBranchObj(childModel)
jointName = str(topModel.LongName).split("_")[-1]
allNodes[jointName] = topModel
def getTopModel(model):
if model.Parent.LongName == "Vicon:Optical":
return model
elif not model.Parent:
return None
for parentModel in model.Parent:
getTopModel(parentModel)
def createCharacter():
'''
为vicon骨骼创建角色
:return:
'''
global allNodes
viconGrp = FBFindModelByLabelName("Vicon:Optical")
if not viconGrp:
return
selectedModels = FBModelList()
FBGetSelectedModels(selectedModels, None, True, True)
if selectedModels:
foundComponents = list()
for model in selectedModels:
ns = model.LongName.split(":")[0]
temp = FBComponentList()
FBFindObjectsByName('%s:*_Hips' % ns, temp, True, True)
for unit in temp:
if unit:
foundComponents.append(unit)
foundComponents = list(set(foundComponents))
else:
foundComponents = FBComponentList()
includeNamespace = True
modelsOnly = True
FBFindObjectsByName('*:*_Hips', foundComponents, includeNamespace, modelsOnly)
for comp in foundComponents:
if not comp:
continue
allNodes = dict()
getBranchObj(comp)
if not allNodes:
continue
namespace = allNodes.values()[0].LongName.split(":")[0]
pCharacterName = namespace
matchSkeFromPreset(allNodes.values())
characterizeSkeleton(pCharacterName, allNodes)
# lScene.Evaluate()
def characterizeSkeleton(pCharacterName, pSkeleton):
'''
定义fb角色
:return:
'''
if FBApplication().CurrentCharacter and FBApplication().CurrentCharacter.LongName == pCharacterName:
return
# Create a character.
character = FBCharacter(pCharacterName)
FBApplication().CurrentCharacter = character
for jointName, joint in pSkeleton.iteritems():
slot = character.PropertyList.Find(jointName + 'Link')
if slot is None:
continue
slot.append(joint)
# character.SetCharacterizeOn(True)
# character.CreateControlRig(True)
character.ActiveInput = None
def getXtoolDir():
'''
获取xtoolDir路径
:return:
'''
xToolDir = None
configPath = FBSystem().UserConfigPath
xToolPath = os.path.join(configPath, "PythonStartup", "xToolConfig.txt").replace("\\", "/")
with open(xToolPath, "r") as f:
for unit in f.readlines():
if unit.strip().startswith("#"):
continue
if os.path.exists(unit.strip()):
xToolDir = unit
break
return xToolDir
def matchSkeFromPreset(allNodes):
'''
匹配骨骼从预设的pose值
:return:
'''
if not allNodes:
return
xtoolDir = getXtoolDir()
with open(xtoolDir + "/Files/MacapPresrtSke/standard_pose_macap.json", "r") as f:
allContent = json.loads(f.read())
lScene = FBSystem().Scene
for unit in allNodes:
if str(unit.__class__) == "<class 'pyfbsdk.FBModelSkeleton'>":
for key, value in allContent.items():
if unit.LongName.endswith(key):
globalRotation = FBVector3d(value[0], value[1], value[2])
# 设置对象的全局坐标
unit.SetVector(globalRotation, FBModelTransformationType.kModelRotation, True)
break
# createCharacter()
2021-07-13xulie
最新推荐文章于 2021-09-07 15:13:45 发布
该博客介绍了如何使用Python脚本在Vicon光学系统中递归获取所有对象,并创建角色。通过获取顶级模型及其子模型,定义关节名称,并匹配预设的骨骼姿势。同时,它还涉及到了角色的创建、骨骼的字符化以及从配置文件获取路径的过程。
摘要由CSDN通过智能技术生成