def load(self): self.watcher_pmx = pmxutil.Pmxio() self.watcher_pmx.load(self.watcher_pmx_name) self.watcher_vmd = vmdutil.Vmdio() self.watcher_vmd.load(self.watcher_vmd_name) self.bone_defs[self.WATCHER] = self.watcher_pmx.get_elements('bones') self.watcher_motions = self.watcher_vmd.get_frames('bones') if self.target_vmd_name: self.target_vmd = vmdutil.Vmdio() self.target_vmd.load(self.target_vmd_name) if vmdutil.is_camera_header(self.target_vmd.header): self.target_mode = 'CAMERA' self.target_motions = self.target_vmd.get_frames('cameras') else: if not self.target_pmx_name: raise Exception('pmx not setted') else: self.target_pmx = pmxutil.Pmxio() self.target_pmx.load(self.target_pmx_name) self.target_mode = 'MODEL' self.target_motions = self.target_vmd.get_frames('bones') self.bone_defs[self.TARGET] = self.target_pmx.get_elements( 'bones') if self.watcher_extlink is not None: self.watcher_extlink_pmx = pmxutil.Pmxio() self.watcher_extlink_pmx.load(self.watcher_extlink[1]) self.watcher_extlink_vmd = vmdutil.Vmdio() self.watcher_extlink_vmd.load(self.watcher_extlink[2]) self.bone_defs[self.WATCHER_EX] = ( self.watcher_extlink_pmx.get_elements('bones'))
def turret_and_face(): tasks = [ upper_to_camera_seq1, upper_to_drone_seq2, lower_to_drone_seq2, upper_to_camera_seq3 ] executor = futures.ProcessPoolExecutor() future_list = [executor.submit(task) for task in tasks] face1 = executor.submit(face_camera) face2 = executor.submit(face_drone) turret_frames = [] for future in futures.as_completed(future_list): turret_frames.extend(future.result()) vmdout = vmdutil.Vmdio() vmdout.set_frames('bones', turret_frames) vmdout.store('motion\\turret.vmd') del vmdout face_frames = face1.result() face_frames.extend(face2.result()) vmdout = vmdutil.Vmdio() vmdout.set_frames('bones', face_frames) vmdout.store('motion\\face.vmd') del vmdout
def scale_motion(args): vmdin = vmdutil.Vmdio() vmdin.load_fd(args.infile) is_camera = vmdutil.is_camera_header(vmdin.header) vmdo = vmdutil.Vmdio() if is_camera: vmdo = scale_camera(vmdin, args) else: vmdo = scale_bone(vmdin, args) vmdo.store_fd(args.outfile)
def vmd_concatenate_fd(infile, outfile): header, new_frames = do_concat(infile) p = vmdutil.Vmdio() p.header = header for element in vmddef.VMD_ELEMENTS: p.set_frames(element, new_frames[element]) p.store_fd(outfile)
def store_vmd(filename, bones, morphs, config): vmdo = vmdutil.Vmdio() vmdo.header = vmdo.header._replace( model_name=config['METER_MODEL_NAME'].encode(vmddef.ENCODING)) vmdo.set_frames('bones', bones) vmdo.set_frames('morphs', morphs) vmdo.store(filename)
def camlight(vmdin, against=None, rx=0.0, ry=0.0, rgb=RGB, y_only=False, add_frames=None, auto_add_frames=None): light_frames = [] for camera_frame in vmdin.get_frames('cameras'): light_frames.append( camera_to_light(camera_frame, against, rx, ry, rgb, y_only)) if auto_add_frames: a_frames = check_camera_rotation(vmdin.get_frames('cameras'), math.radians(auto_add_frames)) add_frames = merge_list(add_frames, a_frames) if add_frames is not None: camera_frames = vmdin.get_frames('cameras') camera_motion = vmdmotion.VmdMotion(camera_frames) for frame_no in add_frames: frame_no = int(frame_no) if camera_motion.get_vmd_frame(frame_no) is None: rotation, position, distance, angle_of_view = ( camera_motion.get_vmd_transform(frame_no)) camera_frame = vmddef.camera(frame_no, distance, position, rotation, None, None, None) light_frames.append( camera_to_light(camera_frame, against, rx, ry, rgb, y_only)) out = vmdutil.Vmdio() out.header = vmddef.header(vmddef.HEADER1, vmddef.HEADER2_CAMERA) out.set_frames('lights', light_frames) return out
def write_camera_diff(vmd_b, camera_diff, light_diff, args): camera_motions = list() camera_dict = vmdutil.frames_to_dict(vmd_b.get_frames('cameras')) frame_dict = camera_diff.get('', None) if frame_dict is not None: for frame_no, diff_data in frame_dict.items(): if (diff_data == DiffResult.B_ONLY or diff_data == DiffResult.NOT_EQUAL): camera_motions.append(camera_dict[frame_no][0]) light_motions = list() light_dict = vmdutil.frames_to_dict(vmd_b.get_frames('lights')) frame_dict = light_diff.get('', None) if frame_dict is not None: for frame_no, diff_data in frame_dict.items(): if (diff_data == DiffResult.B_ONLY or diff_data == DiffResult.NOT_EQUAL): light_motions.append(light_dict[frame_no][0]) vmdout = vmdutil.Vmdio() vmdout.header = vmd_b.header vmdout.set_frames('cameras', camera_motions) vmdout.set_frames('lights', light_motions) vmdout.store_fd(args.o) return
def merge(args): all_frames = defaultdict(list) vmd_header = None def load_vmd(infile): nonlocal vmd_header vmdin = vmdutil.Vmdio() try: vmdin.load_fd(infile) except: sys.stderr.write('cannot load {0}\n'.format(infile.name)) return if vmd_header is None: vmd_header = vmdin.header for element in vmddef.VMD_ELEMENTS: frames = all_frames[element] vmd_frames = vmdin.get_frames(element) frames.extend(vmd_frames) if type(args.infile) == type([]): for file in args.infile: if file.name == '<stdin>': file = sys.stdin.buffer load_vmd(file) else: load_vmd(args.infile) vmdout = vmdutil.Vmdio() for element in vmddef.VMD_ELEMENTS: frames = all_frames[element] if len(frames) > 0: vmdout.set_frames(element, frames) if vmd_header is not None: vmdout.header = vmd_header vmdout.store_fd(args.outfile)
def transmotion_fname(infile, outfile, leftfoot, ry=0, scale=1.0, offset=(0, 0, 0)): vmdin = vmdutil.Vmdio() vmdin.load(infile) vmdout = transmotion(vmdin, leftfoot, ry, scale, offset) vmdout.store(outfile)
def remove_motion(args): vmd = vmdutil.Vmdio() vmd.load_fd(args.infile) args.infile.close() t = [args.bone, args.morph, [], [], [], []] for i, n in enumerate(t): e = vmddef.VMD_ELEMENTS[i] vmd.set_frames( e, remove_frames_by_name(vmd.get_frames(e), n, args.inverse)) vmd.store_fd(args.outfile) return
def trace_camera(args): l = lookat.LookAt(args.from_pmx, args.from_vmd) l.set_target_vmd(args.cam_vmd) if args.omega: omega = math.radians(float(args.omega)) l.set_omega_limit(omega) set_common_options(args, l) heading_frames = l.look_at() vmdout = vmdutil.Vmdio() vmdout.set_frames('bones', heading_frames) vmdout.store(args.outfile) return
def trace_model(args): l = lookat.LookAt(args.from_pmx, args.from_vmd) l.set_target_pmx(args.to_pmx) l.set_target_vmd(args.to_vmd) if args.target_bone: target_bone = args.target_bone l.set_target_bone(target_bone) trace_camera.set_common_options(args, l) heading_frames = l.look_at() vmdout = vmdutil.Vmdio() vmdout.set_frames('bones', heading_frames) vmdout.store(args.outfile) return
def camlight_fname(infile, outfile, against=None, rx=0.0, ry=0.0, rgb=RGB, y_only=False, add_frames=None, auto_add_frames=False): vmdin = vmdutil.Vmdio() vmdin.load(infile) vmdout = camlight(vmdin, against, rx, ry, rgb, y_only, add_frames, auto_add_frames) vmdout.store(outfile)
def load_vmd(infile): nonlocal vmd_header vmdin = vmdutil.Vmdio() try: vmdin.load_fd(infile) except: sys.stderr.write('cannot load {0}\n'.format(infile.name)) return if vmd_header is None: vmd_header = vmdin.header for element in vmddef.VMD_ELEMENTS: frames = all_frames[element] vmd_frames = vmdin.get_frames(element) frames.extend(vmd_frames)
def write_motion_diff(vmd_b, bone_diff, morph_diff, args): def collect_diff_frames(key_type, motions_b, diff_info, args): result = list() index_dict_b = vmdutil.make_index_dict(motions_b, True) for name, frame_dict in diff_info.items(): for frame_no, diff_data in frame_dict.items(): if (diff_data == DiffResult.B_ONLY or diff_data == DiffResult.NOT_EQUAL): result.append(motions_b[index_dict_b[name][frame_no]]) return result bone_motions = collect_diff_frames('bones', vmd_b.get_frames('bones'), bone_diff, args) morph_motions = collect_diff_frames('morphs', vmd_b.get_frames('morphs'), morph_diff, args) vmdout = vmdutil.Vmdio() vmdout.header = vmd_b.header vmdout.set_frames('bones', bone_motions) vmdout.set_frames('morphs', morph_motions) vmdout.store_fd(args.o) return
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 load_vmds(file_list): camera_motion = True first_file = True vmds = dict() for file_name in file_list: if file_name == '': sys.stderr.write('no file_name') continue vmd_in = vmdutil.Vmdio() vmd_in.load(file_name) if vmdutil.is_vmd_header(vmd_in.header): is_camera = vmdutil.is_camera_header(vmd_in.header) if first_file: camera_motion = is_camera header = vmd_in.header first_file = False elif camera_motion != is_camera: sys.stderr.write('skip {0}'.format(file_name)) continue vmds[file_name] = vmd_in else: sys.stderr.write('skip {0}'.format(file_name)) continue return header, vmds
a = name_dict['あ'] a_keys = [frame.frame for frame in a] pelo = name_dict['ぺろっ'] pelo_keys = [frame.frame for frame in pelo] for i, frame in enumerate(morphs): if vmdutil.b_to_str(frame.name) == 'あ2': a_weight = get_morph_weight(frame.frame, a, a_keys) pelo_weight = get_morph_weight(frame.frame, pelo, pelo_keys) plus = a_weight + pelo_weight #if plus > 1: # pass if frame.weight + plus > 0.8: d = 0.8 - plus if d < 0: d = 0 morphs[i] = frame._replace(weight=d) return morphs if __name__ == '__main__': vmd = vmdutil.Vmdio() vmd.load(ORIGINAL) morphs = vmd.get_frames('morphs') morphs = にやり2(morphs) morphs = あplusあ2plusぺろっ(morphs) morphs = あplusぺろっ(morphs) vmdout = vmdutil.Vmdio() vmdout.set_frames('morphs', morphs) vmdout.store('morph_edited.vmd')
import sys sys.path.append('../../../vmdgadgets') import vmdutil OFFSET = 5 #RANGE = [(1, 3127), (3467, 6978)] RANGE = [(1, 3100), (3467, 6978)] def in_range(frame, offset=0): for r in RANGE: if r[0] <= frame <= (r[1] + offset): return True else: return False if '__main__' == __name__ : vmdin = vmdutil.Vmdio() vmdin.load(sys.argv[1]) bones = vmdin.get_frames('bones') for i in range(3): offset = (i + 1) * OFFSET output = list() for bone_frame in bones: if in_range(bone_frame.frame, offset): output.append(bone_frame._replace(frame=bone_frame.frame + offset)) else: output.append(bone_frame) vmdin.set_frames('bones', output) vmdin.store('plus_{}.vmd'.format(offset))
def step_back_fname(infile, outfile, kind='bones'): vmdin = vmdutil.Vmdio() vmdin.load(infile) vmdout = step_back(vmdin, kind) vmdout.store(outfile)
def fft_wave(infile, config): # main f = wave.open(infile, 'r') wave_data = read_wave(f) f.close() filename, ext = os.path.splitext(os.path.basename(infile)) band_def = config['BAND_DEFS'][config['BANDS']] n_bands = len(band_def[0]) freqs, ranges = freq_ranges(config['FFT_SAMPLE'], wave_data.rate, band_def) mmd_frame_max = int(wave_data.n_frames / wave_data.rate * MMD_FRAME_PER_SEC) interpolation = meter_interpolation() config['INTERPOLATION'] = interpolation vmorph = 'USE_VIEW_MORPH' in config and config['USE_VIEW_MORPH'] is True mmorph = 'USE_METER_MORPH' in config and config['USE_METER_MORPH'] is True mmd_frame_no = 0 maxf = config['VMD_MAX_FRAME'] if \ 'VMD_MAX_FRAME' in config else DEFAULT_VMD_MAX_FRAME vmd_per_frame = 2 * n_bands if vmorph else n_bands zero_paddings = vmd_per_frame * 2 spare = vmd_per_frame * 1 # omajinai def overflow(vmd_recorded): return vmd_recorded >= maxf - zero_paddings - spare def vmd_name(kind, no): return '{0}_{1}_{2}{3:02d}.vmd'.format(config['METER_MODEL_NAME'], filename, kind, no) def vmd_name_all(kind): return '{0}_{1}_{2}.vmd'.format(config['METER_MODEL_NAME'], filename, kind) def insert_zero_frames(mmd_frame_no, bones, morphs): if mmorph: morphs += zero_metermorphs(mmd_frame_no, n_bands, config) else: bones += zero_bones(mmd_frame_no, n_bands, config) if vmorph: morphs += zero_viewmorphs(mmd_frame_no, n_bands, config) return ### bone_list = [list() for i in range(n_bands)] mmorph_list = [list() for i in range(n_bands)] vmorph_list = [list() for i in range(n_bands)] while mmd_frame_no <= mmd_frame_max: wave_frame = mmd2wave_frame(mmd_frame_no, wave_data.rate) mags = get_magnitudes(wave_frame, config['FFT_SAMPLE'], wave_data, band_def, ranges) if mags: for band_no in range(n_bands): scale = mag2scale(mags[band_no], config['MIN_DB']) scale = int(scale * RESOLUTION) scale /= RESOLUTION info = (mmd_frame_no, band_no, scale) if not mmorph: bone_list[band_no].append(mk_bone_motion(info, config)) else: mmorph_list[band_no].append(mk_meter_morph(info, config)) if vmorph: vmorph_list[band_no].append(mk_view_morph(info, config)) mmd_frame_no += config['FRAME_INCR'] # normalize for band_no in range(n_bands): bone_list[band_no] = vmdutil.remove_redundant_frames( bone_list[band_no]) mmorph_list[band_no] = vmdutil.remove_redundant_frames( mmorph_list[band_no]) vmorph_list[band_no] = vmdutil.remove_redundant_frames( vmorph_list[band_no]) # vmd out vmdout = vmdutil.Vmdio() store_vmd(vmd_name_all('b'), [f for sublist in bone_list for f in sublist], [], config) if mmorph: for band_no in range(n_bands): store_vmd(vmd_name('mm', band_no), [], mmorph_list[band_no], config) if vmorph: for band_no in range(n_bands): store_vmd(vmd_name('vm', band_no), [], vmorph_list[band_no], config)
def cut_motion(sorted_motion, cut_frames, cut_len): cut_set = set() for c_frame in cut_frames: for i in range(cut_len): cut_set.add(c_frame + i) new_frames = list() for frame in sorted_motion: if not frame.frame in cut_set: new_frames.append(frame) return new_frames if '__main__' == __name__: head_f = sys.argv[1] # neck head eyes motion cam_f = sys.argv[2] # camera motion head_out = sys.argv[3] # output camera = vmdutil.Vmdio() camera.load(cam_f) motion = vmdutil.Vmdio() motion.load(head_f) cut_frames = search_cut(camera.get_frames('cameras')) name_dict = vmdutil.make_name_dict( vmdutil.frames_to_dict(motion.get_frames('bones')), True) head_motion = name_dict['頭'] head_motion = cut_motion(head_motion, cut_frames, 3) head_motion.extend(name_dict['両目']) head_motion.extend(name_dict['首']) vmdout = vmdutil.Vmdio() vmdout.set_frames('bones', head_motion) vmdout.store(head_out)
def concat_bullet_frames(): vmds = glob.glob(r'motion\bullets\*.vmd') vmd_frames = { PUL: { 'bones': [], 'showiks': [] }, PUU: { 'bones': [], 'showiks': [] }, SUL: { 'bones': [], 'showiks': [] }, SUU: { 'bones': [], 'showiks': [] }, PLL: { 'bones': [], 'showiks': [] }, PLU: { 'bones': [], 'showiks': [] }, SLL: { 'bones': [], 'showiks': [] }, SLU: { 'bones': [], 'showiks': [] } } def in_fire_frames(bone_name, frame_no): for fire_frames in [ SEQ1_FIRE_FRAMES, SEQ2_FIRE_FRAMES, SEQ3_FIRE_FRAMES ]: if bone_name in fire_frames and frame_no in fire_frames[bone_name]: return True return False for vmd in vmds: o = RE.match(vmd) if o is not None: vmdfile, frame = o.groups() bone = os.path.basename(vmdfile) frame = int(frame) if in_fire_frames(bone, frame): vmdin = vmdutil.Vmdio() vmdin.load(vmd) motion_frames = vmdin.get_frames('bones') vmd_frames[bone]['bones'].extend(motion_frames) iks = vmdin.get_frames('showiks') vmd_frames[bone]['showiks'].extend(iks) del vmdin for bone in vmd_frames: if len(vmd_frames[bone]['bones']) > 0: vmdout = vmdutil.Vmdio() vmdout.set_frames('bones', vmd_frames[bone]['bones']) vmdout.set_frames('showiks', vmd_frames[bone]['showiks']) vmdout.store('motion\\' + bone + '.vmd')
def make_argument_parser(): parser = argparse.ArgumentParser() parser.add_argument('vmd', help='vmd filename') parser.add_argument('pmx', nargs='?', help='pmx filename') return parser if '__main__' == __name__: parser = make_argument_parser() args = parser.parse_args() if args.pmx is not None: pmx = pmxutil.Pmxio() pmx.load(args.pmx) vmd = vmdutil.Vmdio() vmd.load(args.vmd) bone_motions = vmd.get_frames('bones') morph_motions = vmd.get_frames('morphs') print('========') print(vmdutil.b_to_str(vmd.header.model_name)) print('========') for motion_type in ['bones', 'morphs']: if args.pmx is not None: pmx_dict = pmxutil.make_index_dict(pmx.get_elements(motion_type)) else: pmx_dict = None vmd_dict = vmdutil.make_name_dict( vmdutil.frames_to_dict(vmd.get_frames(motion_type)), True)
if args.short is True: frames_a, frames_b = (omit_unnecessary_frames(frames_a, frames_b, key_type)) index_dict_a = vmdutil.make_index_dict(frames_a, True) index_dict_b = vmdutil.make_index_dict(frames_b, True) if names is None: names = set(index_dict_a).union(index_dict_b) return diff_named_frames(names, frames_a, index_dict_a, frames_b, index_dict_b) # {key_type: name: {frame: result}} if __name__ == '__main__': parser = make_argumentparser() args = parser.parse_args() vmd_a = vmdutil.Vmdio() vmd_b = vmdutil.Vmdio() vmd_a.load(args.vmd_a) vmd_b.load(args.vmd_b) is_camera = vmdutil.is_camera_header(vmd_b.header) print_file_info(args, vmd_a, vmd_b) if is_camera is True: camera_diff = diff_noname('cameras', vmd_a, vmd_b) light_diff = diff_noname('lights', vmd_a, vmd_b) print_summary('cameras', camera_diff, args) print_summary('lights', light_diff, args) if args.o is not None: write_camera_diff(vmd_b, camera_diff, light_diff, args) else: if args.names is not None: names = args.names