def load_asf_file(builder, filename): scene_object = SceneObject() scene_object.scene = builder._scene asf_data = parse_asf_file(filename) skeleton = SkeletonBuilder().load_from_asf_data(asf_data) color = get_random_color() builder.create_component("skeleton_vis", scene_object, skeleton, color) motion_vector = MotionVector() motion_vector.frames = [skeleton.get_reduced_reference_frame()] motion_vector.n_frames = 1 scene_object = SceneObject() vis = builder.create_component("skeleton_vis", scene_object, skeleton, color=get_random_color()) animation_controller = SkeletonAnimationController(scene_object) animation_controller.name = filename.split("/")[-1] animation_controller.set_motion(motion_vector) animation_controller.frameTime = 1 animation_controller.set_visualization(vis) scene_object.name = animation_controller.name scene_object.add_component("animation_controller", animation_controller) builder._scene.addAnimationController(scene_object, "animation_controller") return scene_object
def load_motion_vector_from_bvh_file(bvh_file_path, animated_joints): bvh_data = BVHReader(bvh_file_path) mv = MotionVector(None) mv.from_bvh_reader(bvh_data, filter_joints=False, animated_joints=animated_joints) return mv
def export_frames_to_bvh(skeleton, frames, filename): print("export", len(frames[0])) mv = MotionVector() mv.frames = np.array( [skeleton.add_fixed_joint_parameters_to_frame(f) for f in frames]) print(mv.frames.shape) mv.export(skeleton, filename, add_time_stamp=False)
def generate_training_data(motion_data, animated_joints=None): motions = collections.OrderedDict() sections = collections.OrderedDict() temporal_data = collections.OrderedDict() for name, value in motion_data.items(): data = value["data"] motion_vector = MotionVector() motion_vector.from_custom_db_format(data) motions[name] = motion_vector.frames if value["section_annotation"] is not None:# v_type = type(value["section_annotation"]) if v_type == list: sections[name] = value["section_annotation"] elif v_type == dict: sections[name] = list()#create_sections_from_annotation(annotation) print(value["section_annotation"]) for section_key in value["section_annotation"]: n_sections = len(value["section_annotation"][section_key]) if n_sections == 1: # take only the first segment in the list sections[name].append(value["section_annotation"][section_key][0]) else: warnings.warn("number of annotations "+str(section_key)+" "+str(n_sections)) else: warnings.warn("type unknown", name, v_type) if value["time_function"] is not None: temporal_data[name] = value["time_function"] return motions, sections, temporal_data
def load_motion(path, skeleton_type=None): bvh = BVHReader(path) mv = MotionVector() mv.from_bvh_reader(bvh) skeleton = SkeletonBuilder().load_from_bvh(bvh) skeleton.skeleton_model = load_skeleton_model(skeleton_type) return skeleton, mv
def __init__(self, motion_state_graph, mg_input, algorithm_config, start_pose=None, create_ca_vis_data=False): self.elementary_action_list = [] self.steps = [] self.motion_state_graph = motion_state_graph self.step_count = 0 self.mg_input = mg_input self._algorithm_config = algorithm_config self.motion_vector = MotionVector(self.motion_state_graph.skeleton, algorithm_config) if start_pose is None: start_pose = mg_input.get_start_pose() self.motion_vector.start_pose = start_pose smoothing_settings = algorithm_config["smoothing_settings"] self.spatial_smoothing_method = "smoothing" self.apply_smoothing = smoothing_settings[ "spatial_smoothing"] # set whether the exported motion is smoothed at transitions if "spatial_smoothing_method" in smoothing_settings: self.spatial_smoothing_method = smoothing_settings[ "spatial_smoothing_method"] self.motion_vector.apply_spatial_smoothing = False # deactivate smoothing during the synthesis self.use_time_parameters = algorithm_config["activate_time_variation"] self.constrain_place_orientation = algorithm_config[ "inverse_kinematics_settings"]["constrain_place_orientation"] write_message_to_log( "Use time parameters" + str(self.use_time_parameters), LOG_MODE_DEBUG) self.keyframe_event_list = KeyframeEventList(create_ca_vis_data) self.place_action_list = DEFAULT_PLACE_ACTION_LIST
def create_motion_model_in_db(db_url, skeleton_name, c_id, c_name, spline_basis_factor, animated_joints=None, session=None): motion_data = get_motion_vectors(db_url, c_id, skeleton_name, is_aligned=1) skeleton = load_skeleton_from_db(db_url, skeleton_name) n_motions = len(motion_data) if n_motions > 1: print("start modeling", n_motions, spline_basis_factor) config = get_standard_config() config["n_spatial_basis_factor"] = spline_basis_factor if animated_joints is None: animated_joints = skeleton.animated_joints model_data = create_motion_primitive_model(c_name, skeleton, motion_data, config, animated_joints, save_skeleton=True, align_frames=False) print("finished modelling") name = c_name+"_"+skeleton_name+"_"+str(n_motions) upload_motion_model_to_remote_db(db_url, name, c_id, skeleton_name, model_data, config, session) print("uploaded model") elif n_motions == 1: print("Create static motion primitive fromn 1 motion") first_key = list(motion_data.keys())[0] motion_vector = MotionVector() motion_vector.from_custom_db_format(motion_data[first_key]["data"]) config = get_standard_config() n_basis = int(config["n_spatial_basis_factor"]*motion_vector.n_frames) name = c_name+"_"+skeleton_name+"_"+str(n_motions) model_data = convert_motion_to_static_motion_primitive(name, motion_vector.frames, skeleton, n_basis=n_basis, degree=3) upload_motion_model_to_remote_db(db_url, name, c_id, skeleton_name, model_data, config, session) print("uploaded model") else: print("No motion, found")
def load_skeleton_from_json(builder, file_path, scale=1.0): data = load_json_file(file_path) if "skeleton" in data: data = data["skeleton"] #skeleton = SkeletonBuilder().load_from_custom_unity_format(data) skeleton = SkeletonBuilder().load_from_json_data(data) skeleton.scale(scale) motion_vector = MotionVector() motion_vector.frames = [skeleton.get_reduced_reference_frame()] motion_vector.n_frames = 1 scene_object = SceneObject() vis = builder.create_component("skeleton_vis", scene_object, skeleton, color=get_random_color()) animation_controller = SkeletonAnimationController(scene_object) animation_controller.name = file_path.split("/")[-1] animation_controller.set_motion(motion_vector) animation_controller.frameTime = 1 animation_controller.set_visualization(vis) scene_object.name = animation_controller.name scene_object.add_component("animation_controller", animation_controller) print("load mg json from", file_path) if scene_object is not None: builder._scene.addAnimationController(scene_object, "animation_controller") return scene_object
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 to_motion_vector(self): frames = [] for idx in range(self.n_frames): frames.append(self.get_frame(idx)) mv = MotionVector() mv.frames = self.skeleton.add_fixed_joint_parameters_to_motion(frames) mv.n_frames = self.n_frames return mv
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 replace_motion_from_str(self, bvh_str): bvh_reader = BVHReader("") lines = bvh_str.split("\n") print(len(lines)) lines = [l for l in lines if len(l) > 0] bvh_reader.process_lines(lines) motion_vector = MotionVector() motion_vector.from_bvh_reader(bvh_reader, False) self._motion.replace_frames(motion_vector.frames)
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 load_custom_unity_format_file(builder, filename, draw_mode=2): data = load_json_file(filename) if data is not None: frame_time = data["frameTime"] motion_vector = MotionVector() motion_vector.from_custom_unity_format(data) skeleton = SkeletonBuilder().load_from_json_data(data) o = builder.create_object("animation_controller", filename.split("/")[-1], skeleton, motion_vector, motion_vector.frame_time, draw_mode, visualize, color) return o
def export(self, output_filename, add_time_stamp=False, export_details=False): """ Saves the resulting animation frames, the annotation and actions to files. Also exports the input file again to the output directory, where it is used as input for the constraints visualization by the animation server. """ MotionVector.export(self, self.skeleton, output_filename, add_time_stamp) self.export_annotation(output_filename)
def create_clip_from_reference_frame(skeleton): n_joints = len(skeleton.animated_joints) frame = np.zeros(3 + 4 * n_joints) offset = 3 for node_name in skeleton.animated_joints: frame[offset:offset + 4] = skeleton.nodes[node_name].rotation offset += 4 clip = MotionVector(skeleton) clip.n_frames = 1 clip.frames = np.array([frame]) return clip
def replace_motion_from_file(self, filename): if filename.endswith(".bvh"): bvh_reader = BVHReader(filename) motion_vector = MotionVector() motion_vector.from_bvh_reader(bvh_reader, False) self._motion.replace_frames(motion_vector.frames) self.currentFrameNumber = 0 self.updateTransformation() self.update_scene_object.emit(-1) self.updated_animation_frame.emit(self.currentFrameNumber) elif filename.endswith("_mg.zip"): self.scene_object.scene.attach_mg_state_machine( self.scene_object, filename) self._motion = self.scene_object._components[ "morphablegraph_state_machine"] self._motion.set_target_skeleton(self._visualization.skeleton) self.activate_emit = False elif filename.endswith("amc"): amc_frames = parse_amc_file(filename) motion_vector = MotionVector() motion_vector.from_amc_data(self._visualization.skeleton, amc_frames) self._motion.replace_frames(motion_vector.frames) self._motion.mv.frame_time = 1.0 / 120 self.currentFrameNumber = 0 self.updateTransformation() self.update_scene_object.emit(-1) self.updated_animation_frame.emit(self.currentFrameNumber)
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 get_motion_vector_copy(self, start_frame, end_frame, sample_idx=0): if sample_idx < len(self.samples): mv_copy = MotionVector() mv_copy.frames = deepcopy(self.samples[0].frames[start_frame:end_frame]) mv_copy.frames = self.skeleton.add_fixed_joint_parameters_to_motion(mv_copy.frames) mv_copy.n_frames = len(mv_copy.frames) mv_copy.frame_time = self.frameTime return mv_copy
def create_clip_from_animation(skeleton, anim): n_joints = len(skeleton.animated_joints) n_frames = get_n_frames(anim) n_dims = 3 + n_joints * 4 frames = np.zeros((n_frames, n_dims)) offset = 3 for joint_name in skeleton.animated_joints: if joint_name in anim and "rotation" in anim[joint_name]: a_func = anim[joint_name]["rotation"] new_values = np.array(a_func)[:, 1] for frame_idx in range(n_frames): frames[frame_idx][offset:offset + 4] = convert_q( new_values[frame_idx]) else: for frame_idx in range(n_frames): frames[frame_idx][offset:offset + 4] = skeleton.nodes[joint_name].rotation offset += 4 clip = MotionVector(skeleton) clip.n_frames = n_frames clip.frames = frames return clip
def get_motion_vector_copy(self, start_frame, end_frame): mv_copy = MotionVector() mv_copy.frames = deepcopy(self._motion.frames[start_frame:end_frame]) skeleton = self._graph.skeleton mv_copy.frames = skeleton.add_fixed_joint_parameters_to_motion( mv_copy.frames) mv_copy.n_frames = len(mv_copy.frames) mv_copy.frame_time = self.frameTime return mv_copy
def get_motion_vector_copy(self, start_frame=0, end_frame=-1): mv_copy = MotionVector() if end_frame > 0: mv_copy.frames = deepcopy( self._motion.mv.frames[start_frame:end_frame]) else: mv_copy.frames = np.array(self._motion.mv.frames) mv_copy.n_frames = len(mv_copy.frames) mv_copy.frame_time = self._motion.mv.frame_time return mv_copy
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
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 create_motion_vector_from_json(motion_data): motion_vector = MotionVector() motion_vector.from_custom_db_format(motion_data) return motion_vector
def __init__(self, name, skeleton, share_widget, parent=None, enable_line_edit=False, skeleton_model=None): self.initialized = False QDialog.__init__(self, parent) Ui_Dialog.setupUi(self, self) self.view = SceneViewerWidget(parent, share_widget, size=(400, 400)) self.view.setObjectName("left") self.view.setMinimumSize(400, 400) self.view.initializeGL() self.nameLineEdit.setText(name) self.nameLineEdit.setEnabled(enable_line_edit) self.name = name self.view.enable_mouse_interaction = True self.view.mouse_click.connect(self.on_mouse_click) self.viewerLayout.addWidget(self.view) self.radius = 1.0 self.fps = 60 self.dt = 1 / 60 self.timer = QTimer() self.timer.timeout.connect(self.draw) self.timer.start(0) self.timer.setInterval(1000.0 / self.fps) self.skeleton = skeleton self.view.makeCurrent() self.scene = EditorScene(True) self.scene.enable_scene_edit_widget = True if skeleton_model is not None: self.skeleton_model = skeleton_model elif skeleton.skeleton_model is not None: self.skeleton_model = skeleton.skeleton_model else: self.skeleton_model = dict() print("create new skeleton model") if "cos_map" not in self.skeleton_model: self.skeleton_model["cos_map"] = dict() if "joints" not in self.skeleton_model: self.skeleton_model["joints"] = dict() if "joint_constraints" not in self.skeleton_model: self.skeleton_model["joint_constraints"] = dict() motion_vector = MotionVector() self.reference_frame = skeleton.reference_frame print(self.reference_frame[:3]) motion_vector.frames = [skeleton.reference_frame] motion_vector.n_frames = 1 o = self.scene.object_builder.create_object("animation_controller", "skeleton", skeleton, motion_vector, skeleton.frame_time) self.controller = o._components["animation_controller"] self.skeleton = self.controller.get_skeleton() self.skeleton_vis = o._components["skeleton_vis"] self.init_joints(self.controller) self.fill_joint_map() self.selectButton.clicked.connect(self.slot_accept) self.cancelButton.clicked.connect(self.slot_reject) self.applyTwistRotationButton.clicked.connect(self.slot_set_twist) self.applySwingRotationButton.clicked.connect(self.slot_set_swing) self.setOrthogonalTwistButton.clicked.connect( self.slot_set_orthogonal_twist) self.setOrthogonalSwingButton.clicked.connect( self.slot_set_orthogonal_swing) self.rotateTwistButton.clicked.connect(self.slot_rotate_twist) self.rotateSwingButton.clicked.connect(self.slot_rotate_swing) self.flipTwistButton.clicked.connect(self.slot_flip_twist) self.flipSwingButton.clicked.connect(self.slot_flip_swing) self.flipZAxisButton.clicked.connect(self.slot_flip_z_axis) self.alignToUpAxisButton.clicked.connect(self.slot_align_to_up_axis) self.alignToForwardAxisButton.clicked.connect( self.slot_align_to_forward_axis) self.guessSelectedButton.clicked.connect( self.slot_guess_selected_cos_map) self.resetSelectedCosButton.clicked.connect( self.slot_reset_selected_cos_map) self.guessAllCosButton.clicked.connect(self.slot_guess_cos_map) self.resetAllCosButton.clicked.connect(self.slot_reset_cos_map) self.loadDefaultPoseButton.clicked.connect(self.slot_load_default_pose) self.applyScaleButton.clicked.connect(self.slot_apply_scale) self.jointMapComboBox.currentIndexChanged.connect( self.slot_update_joint_map) self.aligningRootComboBox.currentIndexChanged.connect( self.slot_update_aligning_root_joint) self.mirrorLeftButton.clicked.connect(self.slot_mirror_left_to_right) self.mirrorRightButton.clicked.connect(self.slot_mirror_right_to_left) self.is_updating_joint_info = False self.success = False self.initialized = False self.skeleton_data = None self.precision = 3 self.aligning_root_node = self.skeleton.aligning_root_node self.fill_root_combobox() self.init_aligning_root_node()
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)