Esempio n. 1
0
    def get_face_rotation(self, frame_type, frame_no, bone_index, parent_index,
                          watcher_v, watcher_dir, watcher_pos, up, target_v,
                          target_pos):

        bone_defs = self.watcher_transform.bone_defs
        bone_name = bone_defs[bone_index].name_jp
        look_dir = vmdutil.sub_v(target_pos, watcher_pos)

        if (self.ignore_zone2 is None
                and self.check_ignore_case(watcher_dir, look_dir)):
            return None

        turn = vmdutil.look_at(watcher_dir, up, look_dir, self.global_up)

        if self.ignore_checked is True:
            if 'i' in frame_type:
                return None
        elif self.ignore_zone2 is not None and self.check_ignore_case2(turn):
            return None

        if (self.vmd_lerp
                and bone_index not in self.watcher_transform.leaf_indexes):
            vmd_rot = self.watcher_transform.get_vmd_transform(
                frame_no, bone_index)[0]
            vmd_euler = vmdutil.quaternion_to_euler(vmd_rot)
            turn = [turn[0], turn[1], 0]
            turn = self.scale_turn(bone_name, turn)
            vmd_euler = self.scale_turn(bone_name, vmd_euler, True)
            turn = vmdutil.add_v(turn, vmd_euler)
        else:
            turn = self.scale_turn(bone_name, turn)
        turn = self.apply_constraints(bone_name, turn)
        hrot = tuple(vmdutil.euler_to_quaternion(turn))
        return hrot
Esempio n. 2
0
 def make_arm_dir(self):
     base_dirs = {}
     leaf_indexes = self.watcher_transform.leaf_indexes
     graph = self.watcher_transform.transform_bone_graph
     bone_defs = self.watcher_transform.bone_defs
     for leaf_index in leaf_indexes:
         if leaf_index in self.overwrite_indexes:
             bone_def = bone_defs[leaf_index]
             if (bone_def.flag
                     & pmxdef.BONE_DISP_DIR == pmxdef.BONE_DISP_DIR):
                 disp_to_bone_index = bone_def.disp_dir
                 base_dir = vmdutil.sub_v(
                     bone_defs[disp_to_bone_index].position,
                     bone_def.position)
             else:
                 base_dir = bone_def.disp_dir
             base_dirs[leaf_index] = base_dir
             degree = graph.in_degree(leaf_index)
             if degree <= 0:
                 continue
             parent_index = next(iter(graph.preds[leaf_index]))
             while True:
                 if parent_index in self.overwrite_indexes:
                     base_dirs[parent_index] = base_dir
                 degree = graph.in_degree(parent_index)
                 if degree <= 0:
                     break
                 parent_index = next(iter(graph.preds[parent_index]))
     return base_dirs
Esempio n. 3
0
    def get_arm_rotation(self, frame_type, frame_no, bone_index, parent_index,
                         watcher_v, watcher_dir, watcher_pos, watcher_axis,
                         watcher_up, target_v, target_pos):

        bone_defs = self.watcher_transform.bone_defs
        bone_name = bone_defs[bone_index].name_jp
        look_dir = vmdutil.sub_v(target_pos, watcher_pos)
        turn = vmdutil.look_at_fixed_axis(watcher_dir, watcher_up, look_dir)
        turn = self.apply_constraints(bone_name, [turn, 0, 0])[0]
        hrot = tuple(vmdutil.quaternion(watcher_axis, turn))
        return hrot
Esempio n. 4
0
 def apply_near_mode(self, bone_index, rotation, target_pos):
     bone_defs = self.watcher_transform.bone_defs
     leaves = self.watcher_transform.transform_bone_graph.get_leaves(
         bone_index)
     for ow_index in self.overwrite_indexes:
         if ow_index in leaves:
             delta = vmdutil.sub_v(bone_defs[bone_index].position,
                                   bone_defs[ow_index].position)
             delta = vmdutil.rotate_v3q(delta, rotation)
             target_pos = vmdutil.add_v(target_pos, delta)
             break  # first leaf in sorted overwrite-bones
     return target_pos
Esempio n. 5
0
 def get_projectile_arm_rotation(self, frame_type, frame_no, bone_index,
                                 parent_index, watcher_v, watcher_dir,
                                 watcher_pos, axis, up, target_v,
                                 target_pos):
     look_dir = vmdutil.sub_v(target_pos, watcher_pos)
     relative_v = vmdutil.sub_v(target_v, watcher_v)
     if self.projection_mode == 'A':
         p = project_asap(look_dir, relative_v, self.initial_velocity)
     else:
         p = project_ontime(look_dir, relative_v, self.collision_time)
     if p is None:
         return vmdutil.QUATERNION_IDENTITY
     else:
         v, t, c_pos = p
     angle = vmdutil.look_at_fixed_axis(watcher_dir, up,
                                        vmdutil.normalize_v(v))
     hrot = tuple(vmdutil.quaternion(axis, angle))
     if (bone_index in self.watcher_transform.leaf_indexes
             and 'f' in frame_type):
         self.export_bullet_motion(frame_no, bone_index, watcher_pos, v, t,
                                   c_pos)
     return hrot
Esempio n. 6
0
def corn(radius, from_v, to_v, d=8):
    def return_sphere():
        s = sphere(radius, d, d)
        vertexes = [(x + from_v[0], y + from_v[1], z + from_v[2])
                    for (x, y, z) in s[0]]
        return vertexes, s[1], s[2]

    if to_v is None:
        return return_sphere()

    dir_v = vmdutil.sub_v(to_v, from_v)
    angle = vmdutil.angle_v((0, 1, 0), dir_v)
    if angle is None:
        return return_sphere()

    half_sphere = sphere(radius, d, d, True)
    vertexes = [(x, -y, z) for (x, y, z) in half_sphere[0]]
    normals = [(x, -y, z) for (x, y, z) in half_sphere[1]]
    length = vmdutil.norm_v(dir_v)
    faces = [(f[0], f[2], f[1]) for f in half_sphere[2]]
    vertexes.append((0, length, 0))
    normals.append((0, 1, 0))
    n_vertexes = len(vertexes)

    # insert faces
    for i in range(n_vertexes - d - 1, n_vertexes - 2):
        faces.append((i, i + 1, n_vertexes - 1))
    faces.append((n_vertexes - 2, n_vertexes - d - 1, n_vertexes - 1))
    if (abs(angle) < EPS):
        pass
    else:
        if (abs(math.pi - angle) < EPS):
            q = vmdutil.quaternion((0, 0, 1), math.pi)
        else:
            c = vmdutil.cross_v3((0, 1, 0), dir_v)
            q = vmdutil.quaternion(c, angle)
        vertexes = [tuple(vmdutil.rotate_v3q(v, q)) for v in vertexes]
        normals = [tuple(vmdutil.rotate_v3q(v, q)) for v in normals]
    # translate
    vertexes = [(x + from_v[0], y + from_v[1], z + from_v[2])
                for (x, y, z) in vertexes]
    return vertexes, normals, faces
Esempio n. 7
0
def step_back(vmdin, kind='bones'):
    frames = vmdin.get_frames(kind)
    if kind == 'bones':
        frame_dict = vmdutil.frames_to_dict(frames)
        name_dict = vmdutil.make_name_dict(frame_dict, True)
        for name in MOVE_BONES:
            bone_frames = name_dict[name]
            init_frame = bone_frames[0]._replace(position=(0, 0, 0))
            diff = vmdutil.adjacent_difference(
                [init_frame] + bone_frames,
                lambda x1, x2: vmdutil.sub_v(x1.position, x2.position))
            new_frames = replace_position(bone_frames, diff)
            name_dict[name] = new_frames
        all_frames = list()
        for name in name_dict.keys():
            all_frames.extend(name_dict[name])
        vmdout = vmdin.copy()
        vmdout.set_frames('bones', all_frames)
        return vmdout
    else:
        return None
Esempio n. 8
0
    def make_look_at_frames(self,
                            frame_type,
                            frame_no,
                            target_pos,
                            next_frame_no,
                            next_center_transform,
                            next_target_pos,
                            bone_index=None):

        overwrite_frames = list()
        bone_defs = self.watcher_transform.bone_defs

        if next_frame_no is not None:
            target_v = vmdutil.sub_v(next_target_pos, target_pos)
            target_v = vmdutil.scale_v(target_v,
                                       1 / (next_frame_no - frame_no))

            # center velocity
            global_center, vmd_center, add_center = (
                self.watcher_transform.do_transform(
                    frame_no,
                    self.watcher_transform.bone_name_to_index['センター']))
            cpos = global_center[1]
            watcher_v = vmdutil.sub_v(next_center_transform[1], cpos)
            watcher_v = vmdutil.scale_v(watcher_v,
                                        1 / (next_frame_no - frame_no))
        else:
            target_v = (0, 0, 0)
            cpos = (0, 0, 0)
            watcher_v = (0, 0, 0)

        def get_lookat_frame(b_index):
            result = list()
            bone_def = bone_defs[b_index]
            bone_name = bone_def.name_jp
            hrot = self.get_rotation(frame_no, frame_type, b_index, watcher_v,
                                     target_v, target_pos)
            if hrot is None:  # ignore_case
                if self.use_vmd_interpolation:
                    vmd_frame = self.watcher_transform.get_vmd_frame(
                        frame_no, bone_name)
                    if vmd_frame:
                        result.append(vmd_frame)
                return result
            self.watcher_transform.do_transform(frame_no, b_index,
                                                (hrot, (0, 0, 0)))
            if (not self.use_vmd_interpolation
                    or b_index in self.watcher_transform.leaf_indexes):
                result.append(
                    vmddef.BONE_SAMPLE._replace(frame=frame_no,
                                                name=bone_name.encode(
                                                    vmddef.ENCODING),
                                                rotation=hrot))
            else:
                vmd_frame = self.watcher_transform.get_vmd_frame(
                    frame_no, bone_name)
                if vmd_frame:
                    result.append(vmd_frame._replace(rotation=hrot))
            return result

        if bone_index is not None:
            result = get_lookat_frame(bone_index)
            if 0 == len(result):
                return []
            else:
                overwrite_frames.extend(result)
        else:
            for bone_index in self.overwrite_indexes:
                result = get_lookat_frame(bone_index)
                if 0 == len(result):
                    return []
                else:
                    overwrite_frames.extend(result)
        # vmd_blend
        if self.need_vmd_blend():
            overwrite_frames = self.blend_vmd(frame_no, frame_type,
                                              overwrite_frames, watcher_v,
                                              target_v, target_pos)
        return overwrite_frames