Example #1
0
def omit_unnecessary_frames(frames_a, frames_b, key_type):
    def keys_used(nd, key_type):
        keys_to_omit = vmdutil.enum_unnecessary_keys(nd, key_type)
        return set(nd).difference(keys_to_omit)

    def update_frames(nd, keys):
        result = list()
        for key, value in nd.items():
            if key in keys:
                result.extend(value)
        return result

    nd_a = vmdutil.make_name_dict(vmdutil.frames_to_dict(frames_a), True)
    nd_b = vmdutil.make_name_dict(vmdutil.frames_to_dict(frames_b), True)
    keys_a = keys_used(nd_a, key_type)
    keys_b = keys_used(nd_b, key_type)
    need_keys = keys_a.union(keys_b)
    result_a = update_frames(nd_a, need_keys)
    result_b = update_frames(nd_b, need_keys)
    return result_a, result_b
Example #2
0
def あplusぺろっ(morphs):
    frame_dict = vmdutil.frames_to_dict(morphs)
    name_dict = vmdutil.make_name_dict(frame_dict, True)
    pelo = name_dict['ぺろっ']
    pelo_keys = [frame.frame for frame in pelo]
    for i, frame in enumerate(morphs):
        if vmdutil.b_to_str(frame.name) == 'あ':
            pelo_weight = get_morph_weight(frame.frame, pelo, pelo_keys)
            if frame.weight + pelo_weight > 0.9:
                d = 0.9 - pelo_weight
                if d < 0:
                    d = 0
                morphs[i] = frame._replace(weight=d)
    return morphs
Example #3
0
def あplusあ2plusぺろっ(morphs):
    frame_dict = vmdutil.frames_to_dict(morphs)
    name_dict = vmdutil.make_name_dict(frame_dict, True)
    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
Example #4
0
def scale_bone(vmdin, args):
    scale = get_scale(args, False)
    bone_frames = vmdin.get_frames('bones')
    nd = vmdutil.make_name_dict(vmdutil.frames_to_dict(bone_frames), True)
    out_frames = list()
    if args.bones:
        targets = args.bones
    else:
        targets = DEFAULT_TARGETS

    for key in nd:
        if key in targets:
            for frame in nd[key]:
                pos = scale_pos(frame, scale, args.lr)
                out_frames.append(frame._replace(position=pos))
        else:
            out_frames.extend(nd[key])

    vmdin.set_frames('bones', out_frames)
    return vmdin
Example #5
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
Example #6
0
def transmotion(vmdin, leftfoot, ry=0, scale=1.0, offset=(0, 0, 0)):
    rad = math.radians(ry)
    bone_frames = vmdin.get_frames('bones')
    frame_dict = vmdutil.frames_to_dict(bone_frames)
    name_dict = vmdutil.make_name_dict(frame_dict, decode=True)
    center0 = name_dict[CENTER][0].position

    center_frames = move_center_frames(name_dict, rad, scale, offset)
    rik, lik = move_LRIKs(name_dict, leftfoot, rad, scale, offset)

    for name in center_frames.keys():
        name_dict[name] = center_frames[name]
    name_dict[LEFT] = lik
    name_dict[RIGHT] = rik

    new_frames = list()
    for key in name_dict.keys():
        new_frames.extend(name_dict[key])
    vmdout = vmdin.copy()
    vmdout.set_frames('bones', new_frames)
    return vmdout
Example #7
0
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)
Example #8
0
    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)

        not_used_keys = set(
            vmdutil.enum_unnecessary_keys(vmd_dict, motion_type, "greedy"))
        all_keys = set(vmd_dict.keys())

        for key in sorted(all_keys - not_used_keys):
            if pmx_dict is None:
                s = ''
            else:
                s = ' 〇' if key in pmx_dict else ' ×'
            print('{}: {}{}'.format(key, len(vmd_dict[key]), s))
        print('========')
Example #9
0
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)
Example #10
0
        else:
            position = begin.position
            rotation = begin.rotation
    return rotation, position


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)
Example #11
0
import vmdutil
from vmdutil import vmdmotion

vmd = vmdutil.Vmdio()
vmd.load('sozai\\口パク.vmd')

morphs = vmd.get_frames('morphs')
info = vmdmotion.VmdMotion(morphs)
nd = vmdutil.make_name_dict(vmdutil.frames_to_dict(morphs), True)

o_frames = list()
for frame in nd['お']:
    u = info.get_vmd_transform(frame.frame, 'う')
    o = frame.weight
    t = o + u
    if t > 1.0:
        o = 1.0 - u
        o_frames.append(frame._replace(weight=o))
    else:
        o_frames.append(frame)

vmdout = vmdutil.Vmdio()
vmdout.set_frames('morphs', o_frames)
vmdout.store('口パク_お変更.vmd')