示例#1
0
 def execute(self, context):
     obj = find_armature(context)
     set_context('POSE', obj)
     scaled = []
     for pose_bone in obj.pose.bones:
         if pose_bone.custom_shape and not pose_bone.custom_shape in scaled:
             pose_bone.custom_shape.empty_draw_size *= self.scale
             scaled.append(pose_bone.custom_shape)
     return {'FINISHED'}
示例#2
0
 def execute(self, context):
     obj = find_armature(context)
     set_context('POSE',obj)
     scaled = []
     for pose_bone in obj.pose.bones:
         if pose_bone.custom_shape and not pose_bone.custom_shape in scaled:
             pose_bone.custom_shape.empty_draw_size *= self.scale
             scaled.append(pose_bone.custom_shape)
     return {'FINISHED'}
示例#3
0
 def execute(self,context):
     obj = find_armature(context)
     if not obj:
         return
     if obj.animation_data and obj.animation_data.action:
         obj.animation_data.action = None
     set_context('POSE',obj)
     bpy.ops.pose.user_transforms_clear()
     for bone in obj.pose.bones: bone.morph_value = 0
     bpy.context.scene.frame_set(0)
     return {'FINISHED'}
示例#4
0
文件: blender.py 项目: garthand/s3py
 def create_armature_bone(bone_name, parent_bone=None, min_bone=.001):
     set_context('EDIT', armature_rig)
     armature_bone = find_bone(armature_rig.data.edit_bones, bone_name)
     if not armature_bone:
         armature_bone = armature_rig.data.edit_bones.new(bone_name)
         armature_bone.use_connect = False
         armature_bone.tail = [0, min_bone, 0]
     if parent_bone:
         armature_bone.parent = armature_rig.data.edit_bones[parent_bone]
     set_context('POSE', armature_rig)
     return armature_bone
示例#5
0
 def make_bone_shape(draw_type, name, size, unlink=True):
     if name in bpy.data.objects:
         return bpy.data.objects[name]
     set_context('OBJECT')
     bpy.ops.object.add(type='EMPTY')
     shape = bpy.context.active_object
     shape.empty_draw_type = draw_type
     shape.name = '%s_bone_shape' % name
     shape.empty_draw_size = size
     if unlink:
         bpy.context.scene.objects.unlink(shape)
     return shape
示例#6
0
 def make_bone_shape(draw_type, name, size, unlink=True):
     if name in bpy.data.objects:
         return bpy.data.objects[name]
     set_context('OBJECT')
     bpy.ops.object.add(type='EMPTY')
     shape = bpy.context.active_object
     shape.empty_draw_type = draw_type
     shape.name = '%s_bone_shape' % name
     shape.empty_draw_size = size
     if unlink:
         bpy.context.scene.objects.unlink(shape)
     return shape
示例#7
0
 def execute(self, context):
     obj = find_armature(context)
     if not obj:
         return
     if obj.animation_data and obj.animation_data.action:
         obj.animation_data.action = None
     set_context('POSE', obj)
     bpy.ops.pose.user_transforms_clear()
     for bone in obj.pose.bones:
         bone.morph_value = 0
     bpy.context.scene.frame_set(0)
     return {'FINISHED'}
示例#8
0
    def execute(self,context):
        obj = find_armature(context)
        if not obj:
            return

        if obj.animation_data and obj.animation_data.action:
            action =obj.animation_data.action
            obj.animation_data.action = None
            action.user_clear()
            bpy.data.actions.remove(action)

        set_context('POSE',obj)
        bpy.ops.pose.user_transforms_clear()
        bpy.context.scene.frame_set(0)
        return {'FINISHED'}
示例#9
0
    def execute(self, context):
        obj = find_armature(context)
        if not obj:
            return

        if obj.animation_data and obj.animation_data.action:
            action = obj.animation_data.action
            obj.animation_data.action = None
            action.user_clear()
            bpy.data.actions.remove(action)

        set_context('POSE', obj)
        bpy.ops.pose.user_transforms_clear()
        bpy.context.scene.frame_set(0)
        return {'FINISHED'}
示例#10
0
def add_constraint_target(armature_rig, bone_name, offset, draw_type, draw_size):
    target = None
    pose_bones = armature_rig.pose.bones
    set_context('POSE', armature_rig)
    bone = pose_bones[bone_name]
    select_bone(armature_rig, bone_name)
    bpy.ops.pose.constraint_add_with_targets(type='DAMPED_TRACK')
    constraint = bone.constraints[-1]
    constraint.track_axis = 'TRACK_Z'
    target = constraint.target
    target.name = 'ik_%s_%s' % (armature_rig.name, bone_name)
    target.empty_draw_type = draw_type
    target.empty_draw_size = draw_size
    set_context('OBJECT', target)
    bpy.ops.transform.translate(value=(0, offset, 0), constraint_axis=(False, True, False))
    return target
示例#11
0
def add_constraint_target(armature_rig, bone_name, offset, draw_type,
                          draw_size):
    target = None
    pose_bones = armature_rig.pose.bones
    set_context('POSE', armature_rig)
    bone = pose_bones[bone_name]
    select_bone(armature_rig, bone_name)
    bpy.ops.pose.constraint_add_with_targets(type='DAMPED_TRACK')
    constraint = bone.constraints[-1]
    constraint.track_axis = 'TRACK_Z'
    target = constraint.target
    target.name = 'ik_%s_%s' % (armature_rig.name, bone_name)
    target.empty_draw_type = draw_type
    target.empty_draw_size = draw_size
    set_context('OBJECT', target)
    bpy.ops.transform.translate(value=(0, offset, 0),
                                constraint_axis=(False, True, False))
    return target
示例#12
0
def load_rig(rig, min_bone=0.1):
    """
    Creates a bpy.types.Armature from a s3py_animation.SkeletonRig (Sims 3 Rig)
    """
    print('Test')
    def make_bone_shape(draw_type, name, size, unlink=True):
        if name in bpy.data.objects:
            return bpy.data.objects[name]
        set_context('OBJECT')
        bpy.ops.object.add(type='EMPTY')
        shape = bpy.context.active_object
        shape.empty_draw_type = draw_type
        shape.name = '%s_bone_shape' % name
        shape.empty_draw_size = size
        if unlink:
            bpy.context.scene.objects.unlink(shape)
        return shape

    def walk_hierarchy(bone_source, func):
        children = []
        if isinstance(bone_source, Bone):
            func(bone_source)
            children = bone_source.children
        elif isinstance(bone_source, SkeletonRig):
            children = filter(lambda bone: not bone.parent, bone_source.bones)
        for child in children:
            walk_hierarchy(child, func)

    def connect_armature_bone(bone):
        bone_name = bone.name
        armature_bone = armature_data.edit_bones.new(bone_name)
        if bone.is_skeletal():
            print('Connecting %s'%bone_name)
            armature_bone.use_connect = True
        else:
            print('Skipping %s. Not skeletal.' %bone_name)

    def create_armature_bone(bone):
        bone_name = bone.name
        original = find_bone(armature_data.edit_bones, bone_name)
        if original:
            print('Skipping %s.  Already created.'%bone_name)
            return

        print('Creating %s (%i)' % (bone_name, len(armature_data.edit_bones)))
        armature_bone = armature_data.edit_bones.new(bone_name)
        armature_bone.tail = [0, 0.05, 0]
        if not bone.is_animated():
            armature_bone.hide = True
        if bone.parent:
            armature_parent = find_bone(armature_data.edit_bones, bone.parent.name)
            armature_bone.parent = armature_parent

    def pose_armature_bone(bone):
        pose_bone = armature_rig.pose.bones[bone.name]
        pose_bone.rotation_quaternion = Quaternion(quat_wxyz(bone.orientation))
        pose_bone.location = bone.position
        if not bone.is_animated() or  (bone.is_root() and not bone.is_export_root()):
            pose_bone.bone.hide = True
            print('Hiding pose bone %s' % bone.name)

    assert isinstance(rig, SkeletonRig)
    # root_shape = make_bone_shape('PLAIN_AXES', 'root', 50)
    # bone_shape = make_bone_shape('SPHERE', 'bone', 2.5)

    set_context('OBJECT')
    armature_name = "%s_Armature" % rig.name
    armature_data = bpy.data.armatures.new(name=armature_name)
    armature_data.draw_type = 'OCTAHEDRAL'
    armature_rig = bpy.data.objects.new(armature_name, armature_data)
    armature_rig.show_x_ray = True
    if rig.name:
        armature_rig.name = rig.name
    else:
        armature_rig.name= "rig"
    armature_rig.data.name = '%s_Armature_Data' % rig.name
    bpy.context.scene.objects.link(armature_rig)

    set_context('EDIT', armature_rig)
    walk_hierarchy(rig, create_armature_bone)

    set_context('POSE', armature_rig)
    walk_hierarchy(rig, pose_armature_bone)
    bpy.ops.pose.armature_apply()

    set_context('EDIT', armature_rig)
    walk_hierarchy(rig, connect_armature_bone)

    set_context('OBJECT', armature_rig)
    bpy.ops.transform.rotate(value=math.pi / 2.0, axis=(1, 0, 0))

    set_context('POSE', armature_rig)
    return armature_rig 
示例#13
0
def save_clip(clip_resource, skeleton, scale=1.0):
    print(skeleton.name, skeleton.type)
    clip = clip_resource.clip
    clip.tracks = []
    action = skeleton.animation_data.action
    clip_resource.actor_name = action.s3py.actor_name
    clip.name = action.s3py.name
    clip.source_file_name = action.s3py.source_name
    clip.max_frame_count = int(action.frame_range[1])

    start_time = time.clock()
    print('Saving CLIP...')
    track_map = {}
    used_bones = []
    for fcurve in action.fcurves:
        s = str(fcurve.data_path).split('.')
        if s[0] != 'pose' or s[1][:5] != 'bones':
            continue
        cname = s[1][7:-2]
        if cname == ROOT_MORPH:
            continue
        if cname in skeleton.pose.bones:
            pose_bone = skeleton.pose.bones[cname]
            track_key = FNV32.hash(cname)
            clip_track_key = FNV64.hash(
                cname) & 0xFFFFFFFF if pose_bone.parent and pose_bone.parent.name == ROOT_MORPH else  track_key
            if not track_key in track_map:
                track = Track(clip_track_key)
                used_bones.append(pose_bone)
                clip.tracks.append(track)
                track_map[track_key] = track

    print(list(ub.name for ub in used_bones))
    print('%i Frames found in %s' % (int(action.frame_range[1]), action.name))

    def write_frame(current_value, track, frame_index):
        write = False
        if not any(track.frames):
            write = True
        else:
            last_value = track.frames[-1].data
            for i in range(len(current_value)):
                difference = math.fabs(current_value[i] - last_value[i])
                if difference > 0.0001:
                    write = True
        if write:
            f = Frame()
            f.frame_index = frame_index
            f.data = current_value
            track.frames.append(f)

    set_context('POSE', skeleton)

    for frame_index in range(int(action.frame_range[0]), int(action.frame_range[1])):
        bpy.context.scene.frame_set(frame_index)
        for pose_bone in used_bones:
            track_key = FNV32.hash(pose_bone.name)
            if not track_key in track_map:
                continue
            track = track_map[track_key]
            if pose_bone.parent and pose_bone.parent.name == ROOT_MORPH:
                if not track.morphs:
                    track.morphs = Curve.create_morph()
                cur_morph = [pose_bone.morph_value]
                write_frame(cur_morph, track.morphs, frame_index)
                continue

            matrix_parent = Matrix() if not pose_bone.parent else pose_bone.parent.matrix
            matrix_delta = matrix_parent.inverted() * pose_bone.matrix

            if not track.orientations:
                track.orientations = Curve.create_orientation()
            rotation = quat_xyzw(matrix_delta.to_quaternion())
            write_frame(rotation, track.orientations, frame_index)

            if not track.positions:
                track.positions = Curve.create_position()
            translation = matrix_delta.to_translation()
#            if pose_bone.name == ROOT_BIND:
#                translation[1] *= scale
            write_frame(translation, track.positions, frame_index)

    print('Finished in %.4f sec.' % (time.clock() - start_time))
示例#14
0
def load_rig(rig, min_bone=0.01):
    """
    Creates a bpy.types.Armature from a s3py_animation.SkeletonRig (Sims 3 Rig)
    """
    def make_bone_shape(draw_type, name, size, unlink=True):
        if name in bpy.data.objects:
            return bpy.data.objects[name]
        set_context('OBJECT')
        bpy.ops.object.add(type='EMPTY')
        shape = bpy.context.active_object
        shape.empty_draw_type = draw_type
        shape.name = '%s_bone_shape' % name
        shape.empty_draw_size = size
        if unlink:
            bpy.context.scene.objects.unlink(shape)
        return shape

    def walk_hierarchy(bone_source, func):
        children = []
        if isinstance(bone_source, Bone):
            func(bone_source)
            children = bone_source.children
        elif isinstance(bone_source, SkeletonRig):
            children = filter(lambda bone: not bone.parent, bone_source.bones)
        for child in children:
            walk_hierarchy(child, func)

    def create_armature_bone(bone):
        bone_name = bone.name
        original = find_bone(armature_data.edit_bones, bone_name)
        if original:
            return

        print('Creating %s (%i)' % (bone_name, len(armature_data.edit_bones)))
        armature_bone = armature_data.edit_bones.new(bone_name)
        armature_bone.tail = [0, min_bone, 0]
        if bone.parent:
            armature_parent = find_bone(armature_data.edit_bones,
                                        bone.parent.name)
            armature_bone.parent = armature_parent

    def pose_armature_bone(bone):
        pose_bone = armature_rig.pose.bones[bone.name]
        pose_bone.rotation_quaternion = Quaternion(quat_wxyz(bone.orientation))
        pose_bone.location = bone.position
        if bone.is_root():
            pose_bone.custom_shape = root_shape
        elif bone.is_slot():
            pose_bone.bone.hide = True
        else:
            pose_bone.custom_shape = bone_shape

    assert isinstance(rig, SkeletonRig)
    root_shape = make_bone_shape('PLAIN_AXES', 'root', 50)
    bone_shape = make_bone_shape('SPHERE', 'bone', 2.5)

    set_context('OBJECT')
    armature_name = "%s_Armature" % rig.name
    armature_data = bpy.data.armatures.new(name=armature_name)
    armature_data.draw_type = 'STICK'
    armature_rig = bpy.data.objects.new(armature_name, armature_data)
    armature_rig.show_x_ray = True
    armature_rig.name = rig.name
    armature_rig.data.name = '%s_Armature_Data' % rig.name
    bpy.context.scene.objects.link(armature_rig)

    set_context('EDIT', armature_rig)
    walk_hierarchy(rig, create_armature_bone)

    set_context('POSE', armature_rig)
    walk_hierarchy(rig, pose_armature_bone)
    bpy.ops.pose.armature_apply()

    set_context('OBJECT', armature_rig)
    bpy.ops.transform.rotate(value=math.pi / 2.0, axis=(1, 0, 0))

    return armature_rig
示例#15
0
def save_clip(clip_resource, skeleton, scale=1.0):
    print(skeleton.name, skeleton.type)
    clip = clip_resource.clip
    clip.tracks = []
    action = skeleton.animation_data.action
    clip_resource.actor_name = action.s3py.actor_name
    clip.name = action.s3py.name
    clip.source_file_name = action.s3py.source_name
    clip.max_frame_count = int(action.frame_range[1])

    start_time = time.clock()
    print('Saving CLIP...')
    track_map = {}
    used_bones = []
    for fcurve in action.fcurves:
        s = str(fcurve.data_path).split('.')
        if s[0] != 'pose' or s[1][:5] != 'bones':
            continue
        cname = s[1][7:-2]
        if cname == ROOT_MORPH:
            continue
        if cname in skeleton.pose.bones:
            pose_bone = skeleton.pose.bones[cname]
            track_key = FNV32.hash(cname)
            clip_track_key = FNV64.hash(
                cname
            ) & 0xFFFFFFFF if pose_bone.parent and pose_bone.parent.name == ROOT_MORPH else track_key
            if not track_key in track_map:
                track = Track(clip_track_key)
                used_bones.append(pose_bone)
                clip.tracks.append(track)
                track_map[track_key] = track

    print(list(ub.name for ub in used_bones))
    print('%i Frames found in %s' % (int(action.frame_range[1]), action.name))

    def write_frame(current_value, track, frame_index):
        write = False
        if not any(track.frames):
            write = True
        else:
            last_value = track.frames[-1].data
            for i in range(len(current_value)):
                difference = math.fabs(current_value[i] - last_value[i])
                if difference > 0.0001:
                    write = True
        if write:
            f = Frame()
            f.frame_index = frame_index
            f.data = current_value
            track.frames.append(f)

    set_context('POSE', skeleton)

    for frame_index in range(int(action.frame_range[0]),
                             int(action.frame_range[1])):
        bpy.context.scene.frame_set(frame_index)
        for pose_bone in used_bones:
            track_key = FNV32.hash(pose_bone.name)
            if not track_key in track_map:
                continue
            track = track_map[track_key]
            if pose_bone.parent and pose_bone.parent.name == ROOT_MORPH:
                if not track.morphs:
                    track.morphs = Curve.create_morph()
                cur_morph = [pose_bone.morph_value]
                write_frame(cur_morph, track.morphs, frame_index)
                continue

            matrix_parent = Matrix(
            ) if not pose_bone.parent else pose_bone.parent.matrix
            matrix_delta = matrix_parent.inverted() * pose_bone.matrix

            if not track.orientations:
                track.orientations = Curve.create_orientation()
            rotation = quat_xyzw(matrix_delta.to_quaternion())
            write_frame(rotation, track.orientations, frame_index)

            if not track.positions:
                track.positions = Curve.create_position()
            translation = matrix_delta.to_translation()
            #            if pose_bone.name == ROOT_BIND:
            #                translation[1] *= scale
            write_frame(translation, track.positions, frame_index)

    print('Finished in %.4f sec.' % (time.clock() - start_time))