def load_skeleton_and_animations_from_fbx(file_path): mesh_list, skeleton_def, animations = load_fbx_file(file_path) skeleton = SkeletonBuilder().load_from_fbx_data(skeleton_def) anim_names = list(animations.keys()) motion_vectors = [] if len(anim_names) > 0: anim_name = anim_names[0] mv = MotionVector() mv.from_fbx(animations[anim_name], skeleton.animated_joints) motion_vectors.append(mv) return skeleton, motion_vectors
def load_clips(data_path): print("load clips", data_path) loaded_clips = dict() for filepath in glob.glob(data_path+os.sep+"*.bvh"): print("load", filepath) name = filepath.split(os.sep)[-1][:-4] bvh = BVHReader(filepath) mv = MotionVector() mv.from_bvh_reader(bvh, True) state = MotionState(mv) state.play = True print("load", name, mv.n_frames) loaded_clips[name] = state return loaded_clips
def export_motion_clip(self, skeleton, motion_id, name, directory): print("export clip") data, skeleton, meta_data = self.get_motion_by_id(motion_id) if data is None: return data = bz2.decompress(data) motion_dict = bson.loads(data) print("write to file") motion_vector = MotionVector() motion_vector.from_custom_unity_format(motion_dict) bvh_str = get_bvh_string(skeleton, motion_vector.frames) filename = directory + os.sep + name if not name.endswith(".bvh"): filename += ".bvh" with open(filename, "wt") as out_file: out_file.write(bvh_str)
def build_state_old(self, frames, pose_buffer, ignore_rotation=False): if ignore_rotation: pose_buffer[-1][3:7] = [1, 0, 0, 0] m = get_node_aligning_2d_transform(self.skeleton, self.skeleton.aligning_root_node, pose_buffer, frames) frames = transform_quaternion_frames(frames, m) len_pose_buffer = len(pose_buffer) new_frames = smooth_quaternion_frames(np.array(pose_buffer + frames.tolist()), len_pose_buffer, 20, True) new_frames = new_frames[len_pose_buffer:] new_frames = self.apply_smoothing(new_frames, pose_buffer[-1], 0, self.settings.blend_window) mv = MotionVector(self.skeleton) mv.frames = new_frames[2:] mv.frame_time = self.frame_time mv.n_frames = len(mv.frames) state = MotionState(mv) return state
def generate_motion_old(self, trajectory): """generate motion vector by pre-defined trajectory""" node_id = min(self.strong_components) #start from the minimum frame id pose = None last_pose = None trj_type = trajectory.getType() trj_value = trajectory.getValue() trj_frame = 0 direction = [] orientation = 0 if trj_type == "euler": trj_frame = trajectory.getFrame() #compute the translation along the walking direction radian = np.deg2rad(trj_value[0]) direction = np.array([np.sin(radian), 0, -1 * np.cos(radian) ]) #walk toward to [0 0 -1] orientation = -radian out_mv = MotionVector() out_mv.frames = [] for frame_idx in range(trj_frame): node = self.nodes[node_id] if pose is None: pose = np.copy(node.pose) pose[3:7] = quaternion_multiply(pose[3:7], node.velocity[3:7]) pose[3:7] = quaternion_multiply( pose[3:7], quaternion_from_euler(orientation, 0, 0)) last_pose = np.copy(pose) else: pose = self.get_pose_by_trajectory(node, last_pose, trj_type, trj_value, direction, orientation) last_pose = np.copy(pose) out_mv.frames.append( last_pose) #TODO: why mess when using "pose" here? node_id = self.sample_next_node_id(node_id) if DEBUG: if node_id < 0: print('frame ', frame_idx, 'not find any edge, dead end') else: print('frame ', frame_idx, 'current node', node_id) return out_mv
def set_initial_idle_state(self, use_all_joints=False): mv = MotionVector(self.skeleton) mv.frames = self._graph.nodes[ self.current_node].sample().get_motion_vector() mv.frame_time = self.frame_time mv.n_frames = len(mv.frames) print("before", mv.frames.shape, self.skeleton.reference_frame_length) other_animated_joints = self._graph.nodes[ self.current_node].get_animated_joints() if len(other_animated_joints) == 0: other_animated_joints = ANIMATED_JOINTS_CUSTOM if use_all_joints: other_animated_joints = self._graph.nodes[ self.current_node].get_animated_joints() if len(other_animated_joints) == 0: other_animated_joints = ANIMATED_JOINTS_CUSTOM full_frames = np.zeros( (len(mv.frames), self.skeleton.reference_frame_length)) for idx, reduced_frame in enumerate(mv.frames): full_frames[ idx] = self.skeleton.add_fixed_joint_parameters_to_other_frame( reduced_frame, other_animated_joints) mv.frames = full_frames self.state = MotionState(mv) self.state.play = self.play
def save_recording_to_file(self): time_str = datetime.now().strftime("%d%m%y_%H%M%S") filename = "recording_"+time_str+".bvh" n_frames = len(self.recorded_poses) if n_frames > 0: other_animated_joints = self._graph.nodes[self.current_node].get_animated_joints() full_frames = np.zeros((n_frames, self.skeleton.reference_frame_length)) for idx, reduced_frame in enumerate(self.recorded_poses): full_frames[idx] = self.skeleton.add_fixed_joint_parameters_to_other_frame(reduced_frame, other_animated_joints) mv = MotionVector() mv.frames = full_frames mv.n_frames = n_frames mv.frame_time = self.frame_time mv.export(self.skeleton, filename) print("saved recording with", n_frames, "to file", filename) self.is_recording = False
def get_initial_idle_state(self, start_node, use_all_joints=False): mv = MotionVector(self.skeleton) print("node", start_node) mv.frames = self._graph.nodes[start_node].sample().get_motion_vector() mv.frame_time = self.frame_time mv.n_frames = len(mv.frames) if use_all_joints: animated_joints = self._graph.nodes[ start_node].get_animated_joints() assert len(animated_joints) == 0, "Number of joints are zero" full_frames = np.zeros( (len(mv.frames), self.skeleton.reference_frame_length)) for idx, reduced_frame in enumerate(mv.frames): full_frames[ idx] = self.skeleton.add_fixed_joint_parameters_to_other_frame( reduced_frame, animated_joints) mv.frames = full_frames state = MotionState(mv) return state
def build_state(self, frames, pose_buffer, ignore_rotation=False, export=False): if ignore_rotation: pose_buffer[-1][3:7] = [1, 0, 0, 0] if pose_buffer is not None: m = get_node_aligning_2d_transform(self.skeleton, self.skeleton.aligning_root_node, pose_buffer, frames) #if export: # print("aligning state transform",m) frames = transform_quaternion_frames(frames, m) new_frames = smooth_quaternion_frames2(pose_buffer[-1], frames, self.settings.blend_window, True) else: new_frames = frames if export: time_stamp = str(datetime.now().strftime("%d%m%y_%H%M%S")) mv = MotionVector() mv.frames = frames mv.n_frames = len(frames) mv.export(self.skeleton, "data" + os.sep + "global"+time_stamp+"global.bvh") mv = MotionVector(self.skeleton) mv.frames = new_frames#[1:] mv.frame_time = self.frame_time mv.n_frames = len(mv.frames) state = MotionState(mv) return state