def retarget_motion_in_db(db_url, retargeting, motion_id, motion_name, collection, skeleton_model_name, is_aligned=False, session=None): motion_data = get_motion_by_id_from_remote_db(db_url, motion_id, is_processed=is_aligned) if motion_data is None: print("Error: motion data is empty") return meta_info_str = get_annotation_by_id_from_remote_db( db_url, motion_id, is_processed=is_aligned) motion_vector = MotionVector() motion_vector.from_custom_db_format(motion_data) motion_vector.skeleton = retargeting.src_skeleton new_frames = retargeting.run(motion_vector.frames, frame_range=None) target_motion = MotionVector() target_motion.frames = new_frames target_motion.skeleton = retargeting.target_skeleton target_motion.frame_time = motion_vector.frame_time target_motion.n_frames = len(new_frames) m_data = target_motion.to_db_format() upload_motion_to_db(db_url, motion_name, m_data, collection, skeleton_model_name, meta_info_str, is_processed=is_aligned, session=session)
def load_bvh_file(builder, path, scale=1.0, draw_mode=2, offset=None, reference_frame=None, skeleton_model=None, use_clip=False, color=None, visualize=True): bvh_reader = BVHReader(path) bvh_reader.scale(scale) animated_joints = [ key for key in list(bvh_reader.node_names.keys()) if not key.endswith("EndSite") ] o = None if bvh_reader.frames is not None: skeleton = SkeletonBuilder().load_from_bvh( bvh_reader, animated_joints, reference_frame=reference_frame, skeleton_model=skeleton_model) motion_vector = MotionVector() motion_vector.from_bvh_reader(bvh_reader, False) motion_vector.skeleton = skeleton if offset is not None: motion_vector.translate_root(offset) name = path.split("/")[-1] o = builder.create_object("animation_controller", name, skeleton, motion_vector, bvh_reader.frame_time, draw_mode, visualize, color) return o
def load_motion_from_json(builder, skeleton_data, motion_data, name, collection_id, motion_id, meta_data_str="", skeleton_model=None, is_processed=False, draw_mode=2, visualize=True, color=None, visible=True): if color is None: color = get_random_color() skeleton = SkeletonBuilder().load_from_custom_unity_format(skeleton_data) skeleton.skeleton_model = skeleton_model motion_vector = MotionVector() motion_vector.from_custom_db_format(motion_data) motion_vector.skeleton = skeleton skeleton.frame_time = motion_vector.frame_time #motion_vector.scale_root(scale) o = builder.create_object("animation_controller", name, skeleton, motion_vector, motion_vector.frame_time, draw_mode, visualize, color) o.visible = visible if "data_base_ids" not in builder._scene.internal_vars: builder._scene.internal_vars["data_base_ids"] = dict() builder._scene.internal_vars["data_base_ids"][o.node_id] = (collection_id, motion_id, is_processed) if meta_data_str != "": c = o._components["animation_controller"] meta_data = json.loads(meta_data_str) if "sections" in meta_data: sections = meta_data["sections"] print("sections", sections) sections = meta_data["sections"] if type(sections) == list: semantic_annotation = create_annotation_from_sections_list( sections, motion_vector.n_frames) else: semantic_annotation = create_annotation_from_sections_dict( sections, motion_vector.n_frames) color_map = dict() for key in semantic_annotation.keys(): color_map[key] = get_random_color() c.set_color_annotation(semantic_annotation, color_map) if "time_function" in meta_data: print("set time_function") time_function = meta_data["time_function"] print(meta_data["time_function"]) c.set_time_function(time_function) else: print("meta_data", meta_data) return o
def load_motion_from_bvh(filename): bvh_reader = BVHReader(filename) motion_vector = MotionVector() motion_vector.from_bvh_reader(bvh_reader, False) animated_joints = list(bvh_reader.get_animated_joints()) motion_vector.skeleton = SkeletonBuilder().load_from_bvh( bvh_reader, animated_joints=animated_joints) return motion_vector
def load_file(self, filename): bvh_reader = BVHReader(str(self.folder_path) + os.sep + filename) mv = MotionVector() mv.from_bvh_reader(bvh_reader, False) animated_joints = list(bvh_reader.get_animated_joints()) mv.skeleton = SkeletonBuilder().load_from_bvh( bvh_reader, animated_joints=animated_joints) self.motion_cache[filename] = mv
def get_motion_vector(skeleton, frames): print("generate motion vector", len(skeleton.animated_joints)) frames = np.array(frames) #frames = skeleton.add_fixed_joint_parameters_to_motion(frames) frame_time = skeleton.frame_time mv = MotionVector() mv.frames = frames mv.n_frames = len(frames) mv.skeleton = skeleton return mv
def create_motion_vector_from_bvh(bvh_str, animated_joints=None): bvh_reader = get_bvh_from_str(bvh_str) print("loaded motion", bvh_reader.frames.shape) if animated_joints is None: animated_joints = [key for key in list(bvh_reader.node_names.keys()) if not key.endswith("EndSite")] skeleton = SkeletonBuilder().load_from_bvh(bvh_reader, animated_joints) motion_vector = MotionVector() motion_vector.from_bvh_reader(bvh_reader, False, animated_joints) motion_vector.skeleton = skeleton return motion_vector
def import_motion(db,new_id, skeleton, skeleton_name, filename): bvh = BVHReader(filename) name = filename.split(os.sep)[-1] mv = MotionVector() mv.from_bvh_reader(bvh) mv.skeleton = skeleton data = mv.to_db_format() public = 0 n_frames = mv.n_frames data = bson.dumps(data) meta_data = b"x00" db.insert_motion(new_id, skeleton_name, name, data, meta_data, n_frames, public)
def load_motion_from_str(builder, bvh_str, name, node_key, motion_id, meta_info_str="", draw_mode=2, visualize=True, color=None): if color is None: color = get_random_color() bvh_reader = get_bvh_from_str(bvh_str) print("loaded motion", bvh_reader.frames.shape) animated_joints = [ key for key in list(bvh_reader.node_names.keys()) if not key.endswith("EndSite") ] skeleton = SkeletonBuilder().load_from_bvh(bvh_reader, animated_joints) motion_vector = MotionVector() motion_vector.from_bvh_reader(bvh_reader, False) motion_vector.skeleton = skeleton motion_vector.scale_root(0.01) o = builder.create_object("animation_controller", name, skeleton, motion_vector, bvh_reader.frame_time, draw_mode, visualize, color) if "data_base_ids" not in builder._scene.internal_vars: builder._scene.internal_vars["data_base_ids"] = dict() builder._scene.internal_vars["data_base_ids"][o.node_id] = (node_key, motion_id) if meta_info_str != "": c = o._components["animation_controller"] meta_info = json.loads(meta_info_str) if "sections" in meta_info: sections = meta_info["sections"] if type(sections) == list: semantic_annotation = create_annotation_from_sections_list( sections, motion_vector.n_frames) else: semantic_annotation = create_annotation_from_sections_dict( sections, motion_vector.n_frames) color_map = dict() for key in semantic_annotation.keys(): color_map[key] = get_random_color() c.set_color_annotation(semantic_annotation, color_map) return o
nargs='?', help='skeleton model name') parser.add_argument('src_motion', nargs='?', help='bvh filename') parser.add_argument('src_skeleton_type', nargs='?', help='skeleton model name') parser.add_argument('out_filename', nargs='?', help='filename') parser.add_argument('src_scale', nargs='?', default=1.0, help='float') parser.add_argument('place_on_ground', nargs='?', default=1, help='int') args = parser.parse_args() if args.src_motion is not None and args.dest_skeleton is not None and args.out_filename is not None: src_skeleton, src_motion = load_motion(args.src_motion, args.src_skeleton_type) dest_skeleton = load_skeleton(args.dest_skeleton, args.dest_skeleton_type) joint_map = generate_joint_map(src_skeleton.skeleton_model, dest_skeleton.skeleton_model) retargeting = Retargeting(src_skeleton, dest_skeleton, joint_map, float(args.src_scale), additional_rotation_map=None, place_on_ground=bool(args.place_on_ground)) new_frames = retargeting.run(src_motion.frames, frame_range=None) target_motion = MotionVector() target_motion.frames = new_frames target_motion.skeleton = retargeting.target_skeleton target_motion.frame_time = src_motion.frame_time target_motion.n_frames = len(new_frames) target_motion.export(dest_skeleton, args.out_filename)