def make_bone_model(index, pmx): bones = pmx.get_elements('bones') disp_dir = bones[index].disp_dir top_weight = None if tuple == type(disp_dir): if disp_dir == (0, 0, 0): to_v = None else: to_v = vmdutil.add_v(bones[index].position, disp_dir) else: if disp_dir == -1: to_v = None else: to_v = bones[disp_dir].position top_weight = disp_dir vertexes, normals, faces = corn(0.1, bones[index].position, to_v) weight = pmxdef.vertex_bdef1(bone1=index) vertex_base = pmxdef.vertex(position=(0, 0, 0), normal=(0, 0, 0), uv=(0, 0), ex_uvs=(), weight_type=0, weight=weight, edge_mag=1.0) pmx_vertexes = [ vertex_base._replace(position=i, normal=j) for i, j in zip(vertexes, normals) ] if top_weight is not None: pmx_vertexes[-1] = pmx_vertexes[-1]._replace( weight=pmxdef.vertex_bdef1(bone1=top_weight)) return pmx_vertexes, faces
def replace_position(frames, diff): new_frames = [] p = (0, 0, 0) for i, frame in enumerate(frames): p = vmdutil.add_v(p, (diff[i][0], diff[i][1], -diff[i][2])) new_frames.append(frame._replace(position=tuple(p))) return new_frames
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
def translate_camera(motion_def, instruction): if len(instruction) < 4: return motion_def else: position = motion_def.position delta = instruction[3:6] r = vmdutil.add_v(position, delta) motion_def = motion_def._replace(position=tuple(r)) return motion_def
def transform(v, scale, translation, rotation): # scale v = [i * j for i, j in zip(v, scale)] # rotate quaternion = vmdutil.euler_to_quaternion(rotation) v = vmdutil.rotate_v3q(v, quaternion) # translate v = vmdutil.add_v(v, translation) return tuple(v)
def project_ontime(pos_target, v_target, t): vx, vy, vz = v_target px, py, pz = pos_target # vy*t + 1/2*g*t*t = py vy = py / t - HALFG * t vx = px / t vz = pz / t collision_pos = vmdutil.add_v(pos_target, [t * i for i in v_target]) return (vx, vy, vz), t, collision_pos
def translate_bone(motion_def, instruction): if len(instruction) < 4: return motion_def else: bone_name = vmdutil.b_to_str(motion_def.name) if bone_name in TRANSLATE_BONES: position = motion_def.position delta = instruction[3:6] r = vmdutil.add_v(position, delta) motion_def = motion_def._replace(position=tuple(r)) return motion_def
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
def move_center_frames(name_dict, rad=0, scale=1.0, offset=(0, 0, 0)): result = {} offset = (offset[0], offset[2]) for key in CENTERS: result[key] = list() for frame in name_dict[key]: rotation = vmdutil.compose_quaternion(frame.rotation, (0, 1, 0), -rad, True) pos_xz = vmdutil.rotate_v2((frame.position[0], frame.position[2]), rad) pos_xz = vmdutil.scale_v(pos_xz, scale) pos_xz = vmdutil.add_v(pos_xz, offset) result[key].append( frame._replace(rotation=tuple(rotation), position=(pos_xz[0], frame.position[1], pos_xz[1]))) return result
def project_asap(pos_target, v_target, v_bullet): # ||pos + v_t * t - 1/2gt**2|| = v_b * t g = vmdutil.scale_v(GV, -1) a = vmdutil.dot_v(g, g) * 0.25 # TODO constant b = vmdutil.dot_v(v_target, g) c = (vmdutil.dot_v(pos_target, g) + vmdutil.dot_v(v_target, v_target) - v_bullet**2) d = vmdutil.dot_v(pos_target, v_target) * 2 e = vmdutil.dot_v(pos_target, pos_target) coeff = [a, b, c, d, e] roots = np.roots(coeff) roots = sorted([i for i in roots if np.isreal(i) and i > 0]) # TODO + 0j if len(roots) == 0: # TODO (differential() == 0) & ((-) -> (+)) print('no positive real root') # TODO return None t = roots[0] collision_pos = vmdutil.add_v(pos_target, [t * i for i in v_target]) vx = collision_pos[0] / t vz = collision_pos[2] / t vy = math.sqrt(v_bullet * v_bullet - vx * vx - vz * vz) return (vx, vy, vz), t, collision_pos
def move_root(args): if args.pos: position = tuple(args.pos) else: position = (0, 0, 0) if args.angles: angles = (args.angles[0], -args.angles[1], -args.angles[2]) else: angles = (0, 0, 0) if angles == (0, 0, 0) and position == (0, 0, 0): sys.stderr.write('do nothing.') return vmdin = vmdutil.Vmdio() vmdin.load_fd(args.infile) bones = vmdin.get_frames('bones') frame_dict = vmdutil.frames_to_dict(bones) name_dict = vmdutil.make_name_dict(frame_dict, True) parent_frames = name_dict.get('全ての親') new_frames = [] if parent_frames: for frame in parent_frames: rotation = vmdutil.euler_to_quaternion( tuple([math.radians(r) for r in angles])) rotation = vmdutil.multiply_quaternion(frame.rotation, rotation) frame = frame._replace(position=tuple( vmdutil.add_v(frame.position, position)), rotation=tuple(rotation)) new_frames.append(frame) else: rotation = vmdutil.euler_to_quaternion( tuple([math.radians(r) for r in angles])) new_frames.append( vmddef.BONE_SAMPLE._replace(position=position, rotation=rotation)) for key in name_dict: if key != '全ての親': new_frames.extend(name_dict[key]) vmdin.set_frames('bones', new_frames) vmdin.store_fd(args.outfile)
def export_bullet_motion(self, frame_no, bone_index, from_pos, bullet_v, collision_time, collision_pos): vx, vy, vz = bullet_v bone_defs = self.watcher_transform.bone_defs bone_def = bone_defs[bone_index] bone_name = bone_def.name_jp extreme_time, extreme_pos = extreme_value(from_pos, bullet_v) if extreme_time > 0: # vy > 0 b_bone = 'センター'.encode(vmddef.ENCODING) frame0 = vmddef.BONE_SAMPLE._replace(name=b_bone, frame=frame_no, position=tuple(from_pos)) end_frame = math.ceil(frame_no + extreme_time) frame1 = frame0._replace(frame=end_frame, position=tuple(extreme_pos), interpolation=PARABOLA2) bullet_frames = [frame0, frame1] if collision_time > extreme_time: collision_pos = tuple(vmdutil.add_v(from_pos, collision_pos)) end_frame = math.ceil(frame_no + collision_time) frame2 = frame0._replace(frame=end_frame, position=tuple(collision_pos), interpolation=PARABOLA1) bullet_frames.append(frame2) vmd_name = '{0}_{1}_{2}.vmd'.format( bone_name, frame_no, math.ceil(frame_no + collision_time)) vmdo = vmdutil.Vmdio() vmdo.set_frames('bones', bullet_frames) if self.export_showik is True: ik_frame0 = vmddef.showik(frame_no, 1, 0, ()) ik_frame1 = ik_frame0._replace(frame=end_frame + 1, show=0) vmdo.set_frames('showiks', [ik_frame0, ik_frame1]) vmdo.store(os.path.join(self.bullets_dir, vmd_name)) del vmdo else: pass # TODO angles of depression return
def get_camera_pos(self, rotation, position, distance): direction = vmdutil.camera_direction(rotation, distance) return vmdutil.add_v(position, direction)
if __name__ == '__main__': vmd = vmdutil.Vmdio() if len(sys.argv) > 1: vmd.load(sys.argv[1]) else: vmd.load_fd(sys.stdin.buffer) bones = vmd.get_frames('bones') frame_dict = vmdutil.frames_to_dict(bones) name_dict = vmdutil.make_name_dict(frame_dict, True) target_bones = {'センター', '右足IK', '左足IK'} root_frames = name_dict['全ての親'] root_key_frames = [frame.frame for frame in root_frames] new_frames = [] for move_bone in target_bones: for frame in name_dict[move_bone]: p_rot, p_pos = get_vmdtransformation(frame.frame, root_key_frames, root_frames) frame = frame._replace( position=tuple(vmdutil.add_v(frame.position, p_pos))) new_frames.append(frame) for bone_name in name_dict: if bone_name not in target_bones and bone_name != '全ての親': new_frames.extend(name_dict[bone_name]) vmd.set_frames('bones', new_frames) if len(sys.argv) > 2: vmd.store(sys.argv[2]) else: vmd.store_fd(sys.stdout.buffer)