def calc_finger(bf: VmdBoneFrame, motion: VmdMotion, model: PmxModel, jname: str, default_joints: dict, frame_joints: dict, name_list: list, parent_list: list): rotation = calc_direction_qq(bf.fno, motion, frame_joints, *name_list) bone_initial = calc_bone_direction_qq(bf, motion, model, jname, *name_list) qq = MQuaternion() for parent_name in reversed(parent_list): qq *= motion.calc_bf(parent_name, bf.fno).rotation.inverted() qq = qq * rotation * bone_initial.inverted() _, _, z_qq, _ = separate_local_qq(bf.fno, bf.name, qq, model.get_local_x_axis(bf.name)) z_limited_qq = MQuaternion.fromAxisAndAngle( MVector3D(0, 0, -1 * (-1 if "right" in jname else 1)), min(90, z_qq.toDegree())) bf.rotation = z_limited_qq motion.regist_bf(bf, bf.name, bf.fno)
def calc_bone_direction_qq(bf: VmdBoneFrame, motion: VmdMotion, model: PmxModel, jname: str, direction_from_name: str, direction_to_name: str, up_from_name: str, up_to_name: str): direction_from_vec = get_bone_vec3(model, direction_from_name) direction_to_vec = get_bone_vec3(model, direction_to_name) up_from_vec = get_bone_vec3(model, up_from_name) up_to_vec = get_bone_vec3(model, up_to_name) direction = (direction_to_vec - direction_from_vec).normalized() up = (up_to_vec - up_from_vec).normalized() qq = MQuaternion.fromDirection(direction, up) return qq
def calc_direction_qq(bf: VmdBoneFrame, motion: VmdMotion, joints: dict, direction_from_name: str, direction_to_name: str, up_from_name: str, up_to_name: str): direction_from_vec = get_vec3(joints["joints"], direction_from_name) direction_to_vec = get_vec3(joints["joints"], direction_to_name) up_from_vec = get_vec3(joints["joints"], up_from_name) up_to_vec = get_vec3(joints["joints"], up_to_name) direction = (direction_to_vec - direction_from_vec).normalized() up = (up_to_vec - up_from_vec).normalized() cross = MVector3D.crossProduct(direction, up) qq = MQuaternion.fromDirection(direction, cross) return qq
def calc_bone_direction_qq2(bf: VmdBoneFrame, motion: VmdMotion, model: PmxModel, jname: str, direction_from_name: str, direction_to_name: str, up_from_name: str, up_to_name: str, cross_from_name: str, cross_to_name: str): direction_from_vec = get_bone_vec3(model, direction_from_name) direction_to_vec = get_bone_vec3(model, direction_to_name) up_from_vec = get_bone_vec3(model, up_from_name) up_to_vec = get_bone_vec3(model, up_to_name) cross_from_vec = get_bone_vec3(model, cross_from_name) cross_to_vec = get_bone_vec3(model, cross_to_name) direction = (direction_to_vec - direction_from_vec).normalized() up = (up_to_vec - up_from_vec).normalized() cross = (cross_to_vec - cross_from_vec).normalized() qq = MQuaternion.fromDirection(direction, MVector3D.crossProduct(up, cross)) return qq
def execute(cmd_args): folder_path = cmd_args.folder_path bone_csv_path = cmd_args.bone_csv_path model = read_bone_csv(bone_csv_path) logger.info(model) motion = VmdMotion() # 動画上の関節位置 for fno, joints_path in enumerate( glob.glob(osp.join(folder_path, '**/*_joints.json'))): logger.info(f"■ fno: {fno} -----") frame_joints = {} with open(joints_path, 'r') as f: frame_joints = json.load(f) bf = VmdBoneFrame(fno) bf.set_name("センター") bf.position = calc_center(frame_joints) motion.regist_bf(bf, bf.name, bf.fno) for jname, (bone_name, calc_bone, name_list, parent_list, ranges) in VMD_CONNECTIONS.items(): if name_list is None: continue bf = VmdBoneFrame(fno) bf.set_name(bone_name) if calc_bone is None: if len(name_list) == 4: rotation = calc_direction_qq(bf.fno, motion, frame_joints, *name_list) initial = calc_bone_direction_qq(bf, motion, model, jname, *name_list) else: rotation = calc_direction_qq2(bf.fno, motion, frame_joints, *name_list) initial = calc_bone_direction_qq2(bf, motion, model, jname, *name_list) qq = MQuaternion() for parent_name in reversed(parent_list): qq *= motion.calc_bf(parent_name, bf.fno).rotation.inverted() qq = qq * rotation * initial.inverted() if ranges: # 可動域指定がある場合 x_qq, y_qq, z_qq, _ = separate_local_qq( bf.fno, bf.name, qq, model.get_local_x_axis(bf.name)) local_x_axis = model.get_local_x_axis(bf.name) local_z_axis = MVector3D( 0, 0, -1 * (-1 if "right" in jname else 1)) local_y_axis = MVector3D.crossProduct( local_x_axis, local_z_axis) x_limited_qq = MQuaternion.fromAxisAndAngle( local_x_axis, max( ranges["x"]["min"], min( ranges["x"]["max"], x_qq.toDegree() * MVector3D.dotProduct( local_x_axis, x_qq.vector())))) y_limited_qq = MQuaternion.fromAxisAndAngle( local_y_axis, max( ranges["y"]["min"], min( ranges["y"]["max"], y_qq.toDegree() * MVector3D.dotProduct( local_y_axis, y_qq.vector())))) z_limited_qq = MQuaternion.fromAxisAndAngle( local_z_axis, max( ranges["z"]["min"], min( ranges["z"]["max"], z_qq.toDegree() * MVector3D.dotProduct( local_z_axis, z_qq.vector())))) bf.rotation = y_limited_qq * x_limited_qq * z_limited_qq else: bf.rotation = qq motion.regist_bf(bf, bf.name, bf.fno) # 動画内の半分は地面に足が着いていると見なす center_values = np.zeros((1, 3)) for bf in motion.bones["センター"].values(): center_values = np.insert( center_values, 0, np.array([bf.position.x(), bf.position.y(), bf.position.z()]), axis=0) center_median = np.median(center_values, axis=0) for bf in motion.bones["センター"].values(): bf.position.setY(bf.position.y() - center_median[1]) writer = VmdWriter( motion, model, osp.join( folder_path, "output_{0}.vmd".format( datetime.datetime.now().strftime('%Y%m%d_%H%M%S')))) writer.write()
def convert_leg_fk2ik(direction: str, motion: VmdMotion, model: PmxModel): logger.info("足IK変換 【%s足IK】", direction, decoration=MLogger.DECORATION_LINE) leg_ik_bone_name = "{0}足IK".format(direction) toe_ik_bone_name = "{0}つま先IK".format(direction) leg_bone_name = "{0}足".format(direction) knee_bone_name = "{0}ひざ".format(direction) ankle_bone_name = "{0}足首".format(direction) # 足FK末端までのリンク fk_links = model.create_link_2_top_one(ankle_bone_name, is_defined=False) # 足IK末端までのリンク ik_links = model.create_link_2_top_one(leg_ik_bone_name, is_defined=False) # つま先IK末端までのリンク toe_ik_links = model.create_link_2_top_one(toe_ik_bone_name, is_defined=False) # つま先(足首の子ボーン)の名前 ankle_child_bone_name = "{0}つま先".format(direction) # つま先末端までのリンク toe_fk_links = model.create_link_2_top_one(ankle_child_bone_name, is_defined=False) fnos = motion.get_bone_fnos(leg_bone_name, knee_bone_name, ankle_bone_name) # まずキー登録 prev_sep_fno = 0 for fno in fnos: bf = motion.calc_bf(leg_ik_bone_name, fno) motion.regist_bf(bf, leg_ik_bone_name, fno) if fno // 2000 > prev_sep_fno and fnos[-1] > 0: logger.info("-- %sフレーム目:終了(%s%)【準備 - %s】", fno, round((fno / fnos[-1]) * 100, 3), leg_ik_bone_name) prev_sep_fno = fno // 2000 if len(fnos) > 0 and fnos[-1] > 0: logger.info("-- %sフレーム目:終了(%s%)【準備 - %s】", fnos[-1], round((fnos[-1] / fnos[-1]) * 100, 3), leg_ik_bone_name) logger.info("準備完了 【%s足IK】", direction, decoration=MLogger.DECORATION_LINE) ik_parent_name = ik_links.get(leg_ik_bone_name, offset=-1).name # 足IKの移植 prev_sep_fno = 0 # 移植 for fno in fnos: leg_fk_3ds_dic = calc_global_pos(model, fk_links, motion, fno) _, leg_ik_matrixs = calc_global_pos(model, ik_links, motion, fno, return_matrix=True) # IKの親から見た相対位置 leg_ik_parent_matrix = leg_ik_matrixs[ik_parent_name] bf = motion.calc_bf(leg_ik_bone_name, fno) # 足IKの位置は、足IKの親から見た足首のローカル位置(足首位置マイナス) bf.position = leg_ik_parent_matrix.inverted() * ( leg_fk_3ds_dic[ankle_bone_name] - (model.bones[ankle_bone_name].position - model.bones[ik_parent_name].position)) # 足首の角度がある状態での、つま先までのグローバル位置 leg_toe_fk_3ds_dic = calc_global_pos(model, toe_fk_links, motion, fno) # 一旦足IKの位置が決まった時点で登録 motion.regist_bf(bf, leg_ik_bone_name, fno) # 足IK回転なし状態でのつま先までのグローバル位置 leg_ik_3ds_dic, leg_ik_matrisxs = calc_global_pos(model, toe_ik_links, motion, fno, return_matrix=True) # つま先のローカル位置 ankle_child_initial_local_pos = leg_ik_matrisxs[ leg_ik_bone_name].inverted() * leg_ik_3ds_dic[toe_ik_bone_name] ankle_child_local_pos = leg_ik_matrisxs[leg_ik_bone_name].inverted( ) * leg_toe_fk_3ds_dic[ankle_child_bone_name] # 足IKの回転は、足首から見たつま先の方向 bf.rotation = MQuaternion.rotationTo(ankle_child_initial_local_pos, ankle_child_local_pos) motion.regist_bf(bf, leg_ik_bone_name, fno) if fno // 2000 > prev_sep_fno and fnos[-1] > 0: logger.info("-- %sフレーム目:終了(%s%)【足IK変換 - %s】", fno, round((fno / fnos[-1]) * 100, 3), leg_ik_bone_name) prev_sep_fno = fno // 2000 if len(fnos) > 0 and fnos[-1] > 0: logger.info("-- %sフレーム目:終了(%s%)【足IK変換 - %s】", fnos[-1], round((fnos[-1] / fnos[-1]) * 100, 3), leg_ik_bone_name) logger.info("変換完了 【%s足IK】", direction, decoration=MLogger.DECORATION_LINE)