目录
完整项目代码:Data-Processing/FBX_SDK_Maya
1--Maya数据解析
在软件Maya中直接拖入FBX文件,可以播放和查看人体各个骨骼关节点的数据:
对于上图来说,平移X、平移Y和平移Z表示关节点的Local Transaction的坐标,而旋转X、旋转Y、旋转Z表示关节点的Euler Rotation坐标;对于一个固定的人体模型,修改每一帧中各个关节点的上述六个坐标,即可改变人体表现的动作;
在Maya中,可以通过以下脚本在Python编辑器(窗口→常规编辑器→脚本编辑器)中打印所有帧所有关节的上述6D坐标数据:
'''
@File : print_joint_6Ddata_maya.py
@Time : 2024/03/07 20:05:00
@Author : Jinfu Liu
@Version : 1.0
@Desc : print 6D data of joint in FBX file
'''
import maya.cmds as cmds
joint_names = ["root", "pelvis", "spine_00", "spine_01", "spine_02", "spine_03", "clavicle_l", "upperarm_l", "lowerarm_l", "hand_l", "index_01_l",
"index_02_l", "index_03_l", "middle_01_l", "middle_02_l", "middle_03_l", "pinky_01_l", "pinky_02_l", "pinky_03_l", "ring_01_l", "ring_02_l",
"ring_03_l", "thumb_01_l", "thumb_02_l", "thumb_03_l", "Slot_hand_L_bone", "clavicle_r", "upperarm_r", "lowerarm_r", "hand_r", "index_01_r",
"index_02_r", "index_03_r", "middle_01_r", "middle_02_r", "middle_03_r", "pinky_01_r", "pinky_02_r", "pinky_03_r", "ring_01_r", "ring_02_r",
"ring_03_r", "thumb_01_r", "thumb_02_r", "thumb_03_r", "Slot_hand_R_bone", "Slot_spine_bone", "neck_01", "head", "thigh_l", "calf_l","foot_l",
"ball_l", "thigh_r", "calf_r", "foot_r", "ball_r", "Slot_waist_L_bone", "Slot_waist_R_bone", "Slot_pelvis_bone", "ik_foot_root", "ik_foot_l",
"ik_foot_r", "ik_hand_root", "ik_hand_gun", "ik_hand_l", "ik_hand_r"]
for joint in joint_names:
obj = cmds.ls(joint)
print("process ", obj)
keyframes = cmds.keyframe(obj, query=True)
for frame in keyframes:
local_trans_X = cmds.getAttr(joint + ".translateX", time = frame)
local_trans_Y = cmds.getAttr(joint + ".translateY", time = frame)
local_trans_Z = cmds.getAttr(joint + ".translateZ", time = frame)
local_rotate_X = cmds.getAttr(joint + ".rotateX", time = frame)
local_rotate_Y = cmds.getAttr(joint + ".rotateY", time = frame)
local_rotate_Z = cmds.getAttr(joint + ".rotateZ", time = frame)
print(local_trans_X, local_trans_Y, local_trans_Z)
print(local_rotate_X, local_rotate_Y, local_rotate_Z)
2--FBX SDK导出6D数据
通过Python FBX SDK,我们可以提取和保存在一个原始FBX文件中对应于Maya可视化的6D坐标,具体的脚本如下:FBX_SDK_Maya/Extract_local_TR.py
3--6D数据映射和Maya可视化
通过第2步的脚本可以提取人体运动的关键6D坐标数据,这些6D坐标数据可以进行一些动作生成任务,生成相同意义的坐标数据。原始6D或生成的6D坐标数据可以使用以下脚本,并在Maya中进行可视化:
'''
@File : set_joint_6Ddata_maya.py
@Time : 2024/03/07 20:10:00
@Author : Jinfu Liu
@Version : 1.0
@Desc : set 6D data of joint in FBX file
'''
# you must install numpy by: mayapy.exe -m pip install numpy
import numpy as np
import maya.cmds as cmds
Joint_to_idx = {
"root": 0,
"pelvis": 1,
"spine_00": 2,
"spine_01": 3,
"spine_02": 4,
"spine_03": 5,
"clavicle_l": 6,
"upperarm_l": 7,
"lowerarm_l": 8,
"hand_l": 9,
"index_01_l": 10,
"index_02_l": 11,
"index_03_l": 12,
"middle_01_l": 13,
"middle_02_l": 14,
"middle_03_l": 15,
"pinky_01_l": 16,
"pinky_02_l": 17,
"pinky_03_l": 18,
"ring_01_l": 19,
"ring_02_l": 20,
"ring_03_l": 21,
"thumb_01_l": 22,
"thumb_02_l": 23,
"thumb_03_l": 24,
"Slot_hand_L_bone": 25,
"clavicle_r": 26,
"upperarm_r": 27,
"lowerarm_r": 28,
"hand_r": 29,
"index_01_r": 30,
"index_02_r": 31,
"index_03_r": 32,
"middle_01_r": 33,
"middle_02_r": 34,
"middle_03_r": 35,
"pinky_01_r": 36,
"pinky_02_r": 37,
"pinky_03_r": 38,
"ring_01_r": 39,
"ring_02_r": 40,
"ring_03_r": 41,
"thumb_01_r": 42,
"thumb_02_r": 43,
"thumb_03_r": 44,
"Slot_hand_R_bone": 45,
"Slot_spine_bone": 46,
"neck_01": 47,
"head": 48,
"thigh_l": 49,
"calf_l": 50,
"foot_l": 51,
"ball_l": 52,
"thigh_r": 53,
"calf_r": 54,
"foot_r": 55,
"ball_r": 56,
"Slot_waist_L_bone": 57,
"Slot_waist_R_bone": 58,
"Slot_pelvis_bone": 59,
"ik_foot_root": 60,
"ik_foot_l": 61,
"ik_foot_r": 62,
"ik_hand_root": 63,
"ik_hand_gun": 64,
"ik_hand_l": 65,
"ik_hand_r": 66
}
Local_Trans_data = np.load("C:/Users/jinfullliu/Desktop/test_maya/Local_Trans.npy", allow_pickle = True)
local_Rotate_data = np.load("C:/Users/jinfullliu/Desktop/test_maya/local_Rotate.npy", allow_pickle = True)
for joint in Joint_to_idx:
joint_idx = Joint_to_idx[joint]
obj = cmds.ls(joint)
print("process ", obj)
for frame in range(Local_Trans_data.shape[0]):
cmds.setKeyframe(joint + '.translateX', value = Local_Trans_data[frame, joint_idx, 0], time=frame)
cmds.setKeyframe(joint + '.translateY', value = Local_Trans_data[frame, joint_idx, 1], time=frame)
cmds.setKeyframe(joint + '.translateZ', value = Local_Trans_data[frame, joint_idx, 2], time=frame)
cmds.setKeyframe(joint + '.rotateX', value = local_Rotate_data[frame, joint_idx, 0], time=frame)
cmds.setKeyframe(joint + '.rotateY', value = local_Rotate_data[frame, joint_idx, 1], time=frame)
cmds.setKeyframe(joint + '.rotateZ', value = local_Rotate_data[frame, joint_idx, 2], time=frame)