Ejemplo n.º 1
0
 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 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
Ejemplo n.º 3
0
 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
Ejemplo n.º 4
0
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
Ejemplo n.º 5
0
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
Ejemplo n.º 6
0
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)
Ejemplo n.º 7
0
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 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
Ejemplo n.º 9
0
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
Ejemplo n.º 10
0
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
Ejemplo n.º 11
0
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
Ejemplo n.º 12
0
                        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)
    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()