1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67
| # Import built-in modules import json import os import traceback
# Import third-party modules import pymel.core as pm
DIR = os.path.dirname(os.path.abspath(__file__))
def progress(seq, status="", title=""): pm.progressWindow(status=status, title=title, progress=0.0, isInterruptable=True) total = len(seq) for i, item in enumerate(seq): try: if pm.progressWindow(query=True, isCancelled=True): break pm.progressWindow(e=True, progress=float(i) / total * 100) yield item # with body executes here except: traceback.print_exc() pm.progressWindow(ep=1) pm.progressWindow(ep=1)
def main():
# NOTE: 读取数据 with open(os.path.join(DIR, "BP_metahuman_001.json"), "r") as rf: data = json.load(rf)
attr_map = {"location": "t", "rotation": "r"} status = "Import Keyframe to metahuman controller" # NOTE: undo 支持 pm.undoInfo(ock=1) for channel, frame_list in progress(data.items(), status=status): # NOTE: 解析 channel_name has_attr = channel.count(".") if not has_attr: # NOTE: 处理 `CTRL_C_eye_parallelLook_4311` 格式 ctrl_name = channel.rsplit("_", 1)[0] attr = "ty" else: parts = iter(channel.split(".")) ctrl_name = next(parts, "") param = next(parts, "") axis = next(parts, "") if not axis: # NOTE: 处理 `CTRL_C_teethD.Y_4330` 格式 attr = "t" axis = param else: # NOTE: 处理 `CTRL_L_eyeAim.Rotation.Y_4387` 格式 attr = attr_map.get(param.lower()) attr += axis.split("_")[0].lower()
# NOTE: 解析出控制器属性设置关键帧 attribute = pm.PyNode(".".join([ctrl_name, attr])) for frame_data in frame_list: frame = frame_data.get("frame") value = frame_data.get("value") attribute.setKey(t=frame, v=value)
pm.undoInfo(cck=1)
|