コード例 #1
0
ファイル: model.py プロジェクト: crazysnowboy/blender_python
    def create(name,
               name_e='',
               scale=1,
               obj_name=None,
               armature=None,
               add_root_bone=False):
        scene = SceneOp(bpy.context)
        if obj_name is None:
            obj_name = name

        root = bpy.data.objects.new(name=obj_name, object_data=None)
        root.mmd_type = 'ROOT'
        root.mmd_root.name = name
        root.mmd_root.name_e = name_e
        root.empty_draw_size = scale / 0.2
        scene.link_object(root)

        armObj = armature
        if armObj:
            m = armObj.matrix_world
            armObj.parent_type = 'OBJECT'
            armObj.parent = root
            #armObj.matrix_world = m
            root.matrix_world = m
            armObj.matrix_local.identity()
        else:
            arm = bpy.data.armatures.new(name=obj_name)
            #arm.draw_type = 'STICK'
            armObj = bpy.data.objects.new(name=obj_name + '_arm',
                                          object_data=arm)
            armObj.parent = root
            scene.link_object(armObj)
        armObj.lock_rotation = armObj.lock_location = armObj.lock_scale = [
            True, True, True
        ]
        armObj.show_x_ray = True
        armObj.draw_type = 'WIRE'

        if add_root_bone:
            bone_name = u'全ての親'
            with bpyutils.edit_object(armObj) as data:
                bone = data.edit_bones.new(name=bone_name)
                bone.head = [0.0, 0.0, 0.0]
                bone.tail = [0.0, 0.0, root.empty_draw_size]
            armObj.pose.bones[bone_name].mmd_bone.name_j = bone_name
            armObj.pose.bones[bone_name].mmd_bone.name_e = 'Root'

        bpyutils.select_object(root)
        return Model(root)
コード例 #2
0
 def get_selected_pose_bones(armature):
     if armature.mode == 'EDIT':
         with bpyutils.select_object(armature):  # update selected bones
             bpy.ops.object.mode_set(mode='EDIT')  # back to edit mode
     context_selected_bones = bpy.context.selected_pose_bones or bpy.context.selected_bones or []
     bones = armature.pose.bones
     return (bones[b.name] for b in context_selected_bones
             if not bones[b.name].is_mmd_shadow_bone)
コード例 #3
0
    def apply_bone_groups(mmd_root, armature):
        arm_bone_groups = armature.pose.bone_groups
        if not hasattr(arm_bone_groups,
                       'remove'):  #bpy.app.version < (2, 72, 0):
            from core.mmd_tools import bpyutils
            bpyutils.select_object(armature)
            bpy.ops.object.mode_set(mode='POSE')

            class arm_bone_groups:
                values = armature.pose.bone_groups.values
                get = armature.pose.bone_groups.get

                @staticmethod
                def new(name):
                    bpy.ops.pose.group_add()
                    group = armature.pose.bone_groups.active
                    group.name = name
                    return group

                @staticmethod
                def remove(group):
                    armature.pose.bone_groups.active = group
                    bpy.ops.pose.group_remove()

        pose_bones = armature.pose.bones
        used_groups = set()
        unassigned_bones = {b.name for b in pose_bones}
        for frame in mmd_root.display_item_frames:
            for item in frame.data:
                if item.type == 'BONE' and item.name in unassigned_bones:
                    unassigned_bones.remove(item.name)
                    group_name = frame.name
                    used_groups.add(group_name)
                    group = arm_bone_groups.get(group_name)
                    if group is None:
                        group = arm_bone_groups.new(name=group_name)
                    pose_bones[item.name].bone_group = group

        for name in unassigned_bones:
            pose_bones[name].bone_group = None

        # remove unused bone groups
        for group in arm_bone_groups.values():
            if group.name not in used_groups:
                arm_bone_groups.remove(group)
コード例 #4
0
ファイル: model.py プロジェクト: crazysnowboy/blender_python
    def __makeSpring(self, target, base_obj, spring_stiffness):
        with bpyutils.select_object(target):
            bpy.ops.object.duplicate()
            spring_target = SceneOp(bpy.context).active_object
        t = spring_target.constraints.get('mmd_tools_rigid_parent')
        if t is not None:
            spring_target.constraints.remove(t)
        spring_target.mmd_type = 'SPRING_GOAL'
        spring_target.rigid_body.kinematic = True
        spring_target.rigid_body.collision_groups = (False, False, False,
                                                     False, False, False,
                                                     False, False, False,
                                                     False, False, False,
                                                     False, False, False,
                                                     False, False, False,
                                                     False, True)
        spring_target.parent = base_obj
        spring_target.matrix_parent_inverse = mathutils.Matrix(
            base_obj.matrix_basis).inverted()
        spring_target.hide = True

        obj = bpy.data.objects.new('S.' + target.name, None)
        SceneOp(bpy.context).link_object(obj)
        obj.location = target.location
        obj.empty_draw_size = 0.1
        obj.empty_draw_type = 'ARROWS'
        obj.hide_render = True
        obj.select = False
        obj.hide = True
        obj.mmd_type = 'SPRING_CONSTRAINT'
        obj.parent = self.temporaryGroupObject()

        with bpyutils.select_object(obj):
            bpy.ops.rigidbody.constraint_add(type='GENERIC_SPRING')
        rbc = obj.rigid_body_constraint
        rbc.object1 = target
        rbc.object2 = spring_target

        rbc.use_spring_x = True
        rbc.use_spring_y = True
        rbc.use_spring_z = True

        rbc.spring_stiffness_x = spring_stiffness[0]
        rbc.spring_stiffness_y = spring_stiffness[1]
        rbc.spring_stiffness_z = spring_stiffness[2]
コード例 #5
0
ファイル: misc.py プロジェクト: crazysnowboy/blender_python
    def execute(self, context):
        obj = context.active_object
        root = mmd_model.Model.findRoot(obj)
        if root is None:
            self.report({'ERROR'}, 'Select a MMD model')
            return {'CANCELLED'}

        if root:
            bpy.ops.mmd_tools.clear_temp_materials()
            bpy.ops.mmd_tools.clear_uv_morph_view()

        # Find all the meshes in mmd_root
        rig = mmd_model.Model(root)
        meshes_list = sorted(rig.meshes(), key=lambda x: x.name)
        if not meshes_list:
            self.report({'ERROR'}, 'The model does not have any meshes')
            return {'CANCELLED'}
        active_mesh = meshes_list[0]

        from core.mmd_tools import bpyutils
        bpyutils.select_object(active_mesh, objects=meshes_list)

        # Store the current order of the materials
        for m in meshes_list[1:]:
            for mat in m.data.materials:
                if getattr(mat, 'name',
                           None) not in active_mesh.data.materials[:]:
                    active_mesh.data.materials.append(mat)

        # Join selected meshes
        bpy.ops.object.join()

        if self.sort_shape_keys:
            FnMorph.fixShapeKeyOrder(active_mesh,
                                     root.mmd_root.vertex_morphs.keys())
            active_mesh.active_shape_key_index = 0

        if len(root.mmd_root.material_morphs) > 0:
            for morph in root.mmd_root.material_morphs:
                mo = FnMorph(morph, rig)
                mo.update_mat_related_mesh(active_mesh)

        utils.clearUnusedMeshes()
        return {'FINISHED'}
コード例 #6
0
 def assign(self, obj):
     if obj is None:
         return
     if obj.type == 'ARMATURE':
         with bpyutils.select_object(obj):
             bpy.ops.object.mode_set(mode='POSE')
             self.__assignToArmature(obj)
     elif obj.type == 'MESH':
         self.__assignToMesh(obj)
     else:
         pass
コード例 #7
0
ファイル: morph.py プロジェクト: crazysnowboy/blender_python
    def execute(self, context):
        obj = context.active_object
        root = mmd_model.Model.findRoot(obj)
        rig = mmd_model.Model(root)
        mmd_root = root.mmd_root
        meshObj = obj

        selected = meshObj.select
        with bpyutils.select_object(meshObj) as data:
            morph = mmd_root.uv_morphs[mmd_root.active_morph]
            mesh = meshObj.data

            base_uv_name = mesh.uv_layers.active.name[5:]
            if base_uv_name not in mesh.uv_layers:
                self.report({'ERROR'},
                            ' * UV map "%s" not found' % base_uv_name)
                return {'CANCELLED'}

            base_uv_data = mesh.uv_layers[base_uv_name].data
            temp_uv_data = mesh.uv_layers.active.data
            axis_type = 'ZW' if base_uv_name.startswith('_') else 'XY'

            from collections import namedtuple
            __OffsetData = namedtuple('OffsetData', 'index, offset')
            offsets = {}
            vertices = mesh.vertices
            for l, i0, i1 in zip(mesh.loops, base_uv_data, temp_uv_data):
                if vertices[
                        l.
                        vertex_index].select and l.vertex_index not in offsets:
                    dx, dy = i1.uv - i0.uv
                    if abs(dx) > 0.0001 or abs(dy) > 0.0001:
                        offsets[l.vertex_index] = __OffsetData(
                            l.vertex_index, (dx, dy, dx, dy))

            FnMorph.store_uv_morph_data(meshObj, morph, offsets.values(),
                                        axis_type)
            morph.data_type = 'VERTEX_GROUP'

        meshObj.select = selected
        return {'FINISHED'}
コード例 #8
0
    def __exportPoseLib(self, armObj, pose_type, filepath, use_pose_mode=False):
        if armObj is None:
            return None
        if armObj.pose_library is None:
            return None

        pose_bones = armObj.pose.bones
        converters = self.__getConverters(pose_bones)

        backup = {b:(b.matrix_basis.copy(), b.bone.select) for b in pose_bones}
        for b in pose_bones:
            b.bone.select = False

        matrix_basis_map = {}
        if use_pose_mode:
            matrix_basis_map = {b:bak[0] for b, bak in backup.items()}

        def __export_index(index, filepath):
            bpy.ops.poselib.apply_pose(pose_index=index)
            vpd_bones = self.__exportBones(armObj, converters, matrix_basis_map)
            self.__exportVPDFile(filepath, vpd_bones)

        try:
            pose_markers = armObj.pose_library.pose_markers
            with bpyutils.select_object(armObj):
                bpy.ops.object.mode_set(mode='POSE')
                if pose_type == 'ACTIVE':
                    if 0 <= pose_markers.active_index < len(pose_markers):
                        __export_index(pose_markers.active_index, filepath)
                else:
                    folder = os.path.dirname(filepath)
                    for i, m in enumerate(pose_markers):
                        __export_index(i, os.path.join(folder, m.name+'.vpd'))
        finally:
            for b, bak in backup.items():
                b.matrix_basis, b.bone.select = bak
コード例 #9
0
ファイル: morph.py プロジェクト: crazysnowboy/blender_python
    def execute(self, context):
        obj = context.active_object
        root = mmd_model.Model.findRoot(obj)
        rig = mmd_model.Model(root)
        mmd_root = root.mmd_root
        meshObj = obj

        selected = meshObj.select
        with bpyutils.select_object(meshObj) as data:
            bpy.ops.object.mode_set(mode='EDIT')
            bpy.ops.mesh.select_mode(type='VERT', action='ENABLE')
            bpy.ops.mesh.reveal()  # unhide all vertices
            bpy.ops.mesh.select_all(action='DESELECT')
            bpy.ops.object.mode_set(mode='OBJECT')

            vertices = meshObj.data.vertices
            for l, d in zip(meshObj.data.loops,
                            meshObj.data.uv_layers.active.data):
                if d.select:
                    vertices[l.vertex_index].select = True

            bpy.ops.object.mode_set(mode='EDIT')
        meshObj.select = selected
        return {'FINISHED'}
コード例 #10
0
ファイル: morph.py プロジェクト: crazysnowboy/blender_python
    def execute(self, context):
        obj = context.active_object
        root = mmd_model.Model.findRoot(obj)
        rig = mmd_model.Model(root)
        mmd_root = root.mmd_root

        meshes = tuple(rig.meshes())
        if len(meshes) == 1:
            obj = meshes[0]
        elif obj not in meshes:
            self.report({'ERROR'}, 'Please select a mesh object')
            return {'CANCELLED'}
        meshObj = obj

        bpy.ops.mmd_tools.clear_uv_morph_view()

        selected = meshObj.select
        with bpyutils.select_object(meshObj) as data:
            morph = mmd_root.uv_morphs[mmd_root.active_morph]
            mesh = meshObj.data
            uv_textures = getattr(mesh, 'uv_textures', mesh.uv_layers)

            base_uv_layers = [
                l for l in mesh.uv_layers if not l.name.startswith('_')
            ]
            if morph.uv_index >= len(base_uv_layers):
                self.report({'ERROR'}, "Invalid uv index: %d" % morph.uv_index)
                return {'CANCELLED'}

            uv_layer_name = base_uv_layers[morph.uv_index].name
            if morph.uv_index == 0 or uv_textures.active.name not in {
                    uv_layer_name, '_' + uv_layer_name
            }:
                uv_textures.active = uv_textures[uv_layer_name]

            uv_layer_name = uv_textures.active.name
            uv_tex = uv_textures.new(name='__uv.%s' % uv_layer_name)
            if uv_tex is None:
                self.report({'ERROR'}, "Failed to create a temporary uv layer")
                return {'CANCELLED'}

            offsets = FnMorph.get_uv_morph_offset_map(meshObj, morph).items()
            offsets = {
                k: getattr(Vector(v),
                           'zw' if uv_layer_name.startswith('_') else 'xy')
                for k, v in offsets
            }
            if len(offsets) > 0:
                base_uv_data = mesh.uv_layers.active.data
                temp_uv_data = mesh.uv_layers[uv_tex.name].data
                for i, l in enumerate(mesh.loops):
                    select = temp_uv_data[i].select = (l.vertex_index
                                                       in offsets)
                    if select:
                        temp_uv_data[i].uv = base_uv_data[i].uv + offsets[
                            l.vertex_index]

            uv_textures.active = uv_tex
            uv_tex.active_render = True
        meshObj.hide = False
        meshObj.select = selected
        return {'FINISHED'}