2021-07-13xulie

该博客介绍了如何使用Python脚本在Vicon光学系统中递归获取所有对象,并创建角色。通过获取顶级模型及其子模型,定义关节名称,并匹配预设的骨骼姿势。同时,它还涉及到了角色的创建、骨骼的字符化以及从配置文件获取路径的过程。
摘要由CSDN通过智能技术生成
# 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()

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值