Esempio n. 1
0
    def execute(self, context):
        obj = context.active_object
        root = mmd_model.Model.findRoot(obj)
        rig = mmd_model.Model(root)
        arm = rig.armature()
        if obj != arm:
            utils.selectAObject(root)
            root.select = False
        elif arm.mode != 'POSE':
            bpy.ops.object.mode_set(mode='POSE')

        selected_pose_bones = []
        if context.selected_pose_bones:
            selected_pose_bones = context.selected_pose_bones

        arm.select = False
        if len(selected_pose_bones) > 0:
            for pose_bone in selected_pose_bones:
                rigid = self.__add_rigid_body(rig, arm, pose_bone)
                rigid.select = True
        else:
            rigid = self.__add_rigid_body(rig)
            rigid.select = True
        return {'FINISHED'}
Esempio n. 2
0
 def execute(self, context):
     obj = context.active_object
     root = mmd_model.Model.findRoot(obj)
     rig = mmd_model.Model(root)
     for meshObj in rig.meshes():
         mats_to_delete = []
         for mat in meshObj.data.materials:
             if mat and "_temp" in mat.name:
                 mats_to_delete.append(mat)
         for temp_mat in reversed(mats_to_delete):
             base_mat_name = temp_mat.name.split('_temp')[0]
             try:
                 FnMaterial.swap_materials(meshObj, temp_mat.name,
                                           base_mat_name)
             except MaterialNotFoundError:
                 self.report({'WARNING'},
                             'Base material for %s was not found' %
                             temp_mat.name)
             else:
                 temp_idx = meshObj.data.materials.find(temp_mat.name)
                 mat = meshObj.data.materials.pop(index=temp_idx)
                 if mat.users < 1:
                     bpy.data.materials.remove(mat)
     return {'FINISHED'}
Esempio n. 3
0
    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'}
Esempio n. 4
0
 def execute(self, context):
     root = mmd_model.Model.findRoot(context.active_object)
     rig = mmd_model.Model(root)
     rig.build()
     SceneOp(context).active_object = root
     return {'FINISHED'}
Esempio n. 5
0
 def execute(self, context):
     root = mmd_model.Model.findRoot(context.active_object)
     rig = mmd_model.Model(root)
     rig.clean()
     context.scene.objects.active = root
     return {'FINISHED'}
Esempio n. 6
0
def _toggleShowNamesOfRigidBodies(self, context):
    root = self.id_data
    rig = mmd_model.Model(root)
    for i in rig.rigidBodies():
        i.show_name = root.mmd_root.show_names_of_rigid_bodies
Esempio n. 7
0
def _getVisibilityOfMMDRigArmature(prop):
    rig = mmd_model.Model(prop.id_data)
    arm = rig.armature()
    return not (arm is None or arm.hide)
Esempio n. 8
0
def _toggleShowNamesOfJoints(self, context):
    root = self.id_data
    rig = mmd_model.Model(root)
    for i in rig.joints():
        i.show_name = root.mmd_root.show_names_of_joints
Esempio n. 9
0
def _toggleUseSDEF(self, context):
    root = self.id_data
    rig = mmd_model.Model(root)
    mute_sdef = not self.use_sdef
    for i in rig.meshes():
        FnSDEF.mute_sdef_set(i, mute_sdef)
Esempio n. 10
0
    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'}
Esempio n. 11
0
    def execute(self, context):
        obj = context.active_object
        root = mmd_model.Model.findRoot(obj)
        rig = mmd_model.Model(root)
        mmd_root = root.mmd_root
        morph = mmd_root.material_morphs[mmd_root.active_morph]
        mat_data = morph.data[morph.active_data]

        meshObj = rig.findMesh(mat_data.related_mesh)
        if meshObj is None:
            self.report({'ERROR'}, "The model mesh can't be found")
            return {'CANCELLED'}

        base_mat = meshObj.data.materials.get(mat_data.material, None)
        if base_mat is None:
            self.report({'ERROR'},
                        'Material "%s" not found' % mat_data.material)
            return {'CANCELLED'}

        work_mat_name = base_mat.name + '_temp'
        if work_mat_name in bpy.data.materials:
            self.report({'ERROR'},
                        'Temporary material "%s" is in use' % work_mat_name)
            return {'CANCELLED'}

        work_mat = base_mat.copy()
        work_mat.name = work_mat_name
        meshObj.data.materials.append(work_mat)
        FnMaterial.swap_materials(meshObj, base_mat.name, work_mat.name)
        base_mmd_mat = base_mat.mmd_material
        work_mmd_mat = work_mat.mmd_material
        work_mmd_mat.material_id = -1

        # Apply the offsets
        if mat_data.offset_type == "MULT":
            diffuse_offset = multiply_vector_components(
                base_mmd_mat.diffuse_color, mat_data.diffuse_color[0:3])
            specular_offset = multiply_vector_components(
                base_mmd_mat.specular_color, mat_data.specular_color)
            edge_offset = multiply_vector_components(base_mmd_mat.edge_color,
                                                     mat_data.edge_color)
            ambient_offset = multiply_vector_components(
                base_mmd_mat.ambient_color, mat_data.ambient_color)
            work_mmd_mat.diffuse_color = diffuse_offset
            work_mmd_mat.alpha *= mat_data.diffuse_color[3]
            work_mmd_mat.specular_color = specular_offset
            work_mmd_mat.shininess *= mat_data.shininess
            work_mmd_mat.ambient_color = ambient_offset
            work_mmd_mat.edge_color = edge_offset
            work_mmd_mat.edge_weight *= mat_data.edge_weight
        elif mat_data.offset_type == "ADD":
            diffuse_offset = Vector(base_mmd_mat.diffuse_color) + Vector(
                mat_data.diffuse_color[0:3])
            specular_offset = Vector(base_mmd_mat.specular_color) + Vector(
                mat_data.specular_color)
            edge_offset = Vector(base_mmd_mat.edge_color) + Vector(
                mat_data.edge_color)
            ambient_offset = Vector(base_mmd_mat.ambient_color) + Vector(
                mat_data.ambient_color)
            work_mmd_mat.diffuse_color = list(diffuse_offset)
            work_mmd_mat.alpha += mat_data.diffuse_color[3]
            work_mmd_mat.specular_color = list(specular_offset)
            work_mmd_mat.shininess += mat_data.shininess
            work_mmd_mat.ambient_color = list(ambient_offset)
            work_mmd_mat.edge_color = list(edge_offset)
            work_mmd_mat.edge_weight += mat_data.edge_weight

        return {'FINISHED'}
Esempio n. 12
0
    def execute(self, context):
        obj = context.active_object
        root = mmd_model.Model.findRoot(obj)
        rig = mmd_model.Model(root)
        mmd_root = root.mmd_root
        morph = mmd_root.material_morphs[mmd_root.active_morph]
        mat_data = morph.data[morph.active_data]

        meshObj = rig.findMesh(mat_data.related_mesh)
        if meshObj is None:
            self.report({'ERROR'}, "The model mesh can't be found")
            return {'CANCELLED'}
        try:
            work_mat_name = mat_data.material + '_temp'
            work_mat, base_mat = FnMaterial.swap_materials(
                meshObj, work_mat_name, mat_data.material)
        except MaterialNotFoundError:
            self.report({'ERROR'}, "Material not found")
            return {'CANCELLED'}

        copy_idx = meshObj.data.materials.find(work_mat.name)
        base_mmd_mat = base_mat.mmd_material
        work_mmd_mat = work_mat.mmd_material

        if mat_data.offset_type == "MULT":
            try:
                diffuse_offset = divide_vector_components(
                    work_mmd_mat.diffuse_color, base_mmd_mat.diffuse_color
                ) + [special_division(work_mmd_mat.alpha, base_mmd_mat.alpha)]
                specular_offset = divide_vector_components(
                    work_mmd_mat.specular_color, base_mmd_mat.specular_color)
                edge_offset = divide_vector_components(work_mmd_mat.edge_color,
                                                       base_mmd_mat.edge_color)
                mat_data.diffuse_color = diffuse_offset
                mat_data.specular_color = specular_offset
                mat_data.shininess = special_division(work_mmd_mat.shininess,
                                                      base_mmd_mat.shininess)
                mat_data.ambient_color = divide_vector_components(
                    work_mmd_mat.ambient_color, base_mmd_mat.ambient_color)
                mat_data.edge_color = edge_offset
                mat_data.edge_weight = special_division(
                    work_mmd_mat.edge_weight, base_mmd_mat.edge_weight)

            except DivisionError:
                mat_data.offset_type = "ADD"  # If there is any 0 division we automatically switch it to type ADD
            except ValueError:
                self.report({'ERROR'}, 'An unexpected error happened')
                # We should stop on our tracks and re-raise the exception
                raise

        if mat_data.offset_type == "ADD":
            diffuse_offset = list(work_mmd_mat.diffuse_color -
                                  base_mmd_mat.diffuse_color) + [
                                      work_mmd_mat.alpha - base_mmd_mat.alpha
                                  ]
            specular_offset = list(work_mmd_mat.specular_color -
                                   base_mmd_mat.specular_color)
            edge_offset = Vector(work_mmd_mat.edge_color) - Vector(
                base_mmd_mat.edge_color)
            mat_data.diffuse_color = diffuse_offset
            mat_data.specular_color = specular_offset
            mat_data.shininess = work_mmd_mat.shininess - base_mmd_mat.shininess
            mat_data.ambient_color = work_mmd_mat.ambient_color - base_mmd_mat.ambient_color
            mat_data.edge_color = list(edge_offset)
            mat_data.edge_weight = work_mmd_mat.edge_weight - base_mmd_mat.edge_weight

        mat = meshObj.data.materials.pop(index=copy_idx)
        if mat.users < 1:
            bpy.data.materials.remove(mat)
        return {'FINISHED'}
Esempio n. 13
0
def _getVisibilityOfMMDRigArmature(prop):
    if prop.id_data.mmd_type != 'ROOT':
        return False
    rig = mmd_model.Model(prop.id_data)
    arm = rig.armature()
    return not (arm is None or arm.hide)
Esempio n. 14
0
    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 = rig.firstMesh()
        if meshObj is None:
            self.report({'ERROR'}, "The model mesh can't be found")
            return {'CANCELLED'}

        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 = mesh.uv_textures

            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_textures.active = uv_textures[base_uv_layers[
                morph.uv_index].name]
            uv_tex = uv_textures.new(name='__uv.%s' % uv_textures.active.name)
            if uv_tex is None:
                self.report({'ERROR'}, "Failed to create a temporary uv layer")
                return {'CANCELLED'}

            if len(morph.data) > 0:
                base_uv_data = mesh.uv_layers.active.data
                temp_uv_data = mesh.uv_layers[uv_tex.name].data

                uv_id_map = {}
                for uv_idx, l in enumerate(mesh.loops):
                    uv_id_map.setdefault(l.vertex_index, []).append(uv_idx)

                if self.with_animation:
                    morph_name = '__uv.%s' % morph.name
                    a = mesh.animation_data_create()
                    act = bpy.data.actions.new(name=morph_name)
                    old_act = a.action
                    a.action = act

                    for data in morph.data:
                        offset = Vector(data.offset[:2])  # only use dx, dy
                        for i in uv_id_map.get(data.index, []):
                            t = temp_uv_data[i]
                            t.keyframe_insert('uv', frame=0, group=morph_name)
                            t.uv = base_uv_data[i].uv + offset
                            t.keyframe_insert('uv',
                                              frame=100,
                                              group=morph_name)

                    for fcurve in act.fcurves:
                        for kp in fcurve.keyframe_points:
                            kp.interpolation = 'LINEAR'
                        fcurve.lock = True

                    nla = a.nla_tracks.new()
                    nla.name = morph_name
                    nla.strips.new(name=morph_name, start=0, action=act)
                    a.action = old_act
                    context.scene.frame_current = 100
                else:
                    for data in morph.data:
                        offset = Vector(data.offset[:2])  # only use dx, dy
                        for i in uv_id_map.get(data.index, []):
                            temp_uv_data[i].uv = base_uv_data[i].uv + offset

            uv_textures.active = uv_tex
            uv_tex.active_render = True
        meshObj.hide = False
        meshObj.select = selected
        return {'FINISHED'}
Esempio n. 15
0
    def draw(self, context):
        obj = context.active_object

        layout = self.layout
        c = layout.column()
        c.prop(obj.mmd_rigid, 'name_j')
        c.prop(obj.mmd_rigid, 'name_e')

        c = layout.column(align=True)
        row = c.row(align=True)
        row.prop(obj.mmd_rigid, 'type', expand=True)

        root = mmd_model.Model.findRoot(obj)
        if root is None:
            row = c.row(align=True)
            row.enabled = False
            row.prop(obj.mmd_rigid, 'bone', text='', icon='BONE_DATA')
        else:
            row = c.row(align=True)
            armature = mmd_model.Model(root).armature()
            row.prop_search(obj.mmd_rigid,
                            'bone',
                            text='',
                            search_data=armature.pose,
                            search_property='bones',
                            icon='BONE_DATA')

        c = layout.column(align=True)
        c.enabled = obj.mode == 'OBJECT'
        c.row(align=True).prop(obj.mmd_rigid, 'shape', expand=True)
        c.column(align=True).prop(obj.mmd_rigid, 'size', text='')

        row = layout.row()
        if obj.rigid_body is None:
            row.operator('rigidbody.object_add', icon='MESH_ICOSPHERE')
            return

        c = row.column()
        c.prop(obj.rigid_body, 'mass')
        c.prop(obj.mmd_rigid, 'collision_group_number')
        c = row.column()
        c.prop(obj.rigid_body, 'restitution')
        c.prop(obj.rigid_body, 'friction')

        c = layout.column()
        #c.prop(obj.mmd_rigid, 'collision_group_mask')
        col = c.column(align=True)
        col.label('Collision Group Mask:')
        row = col.row(align=True)
        for i in range(0, 8):
            row.prop(obj.mmd_rigid,
                     'collision_group_mask',
                     index=i,
                     text=str(i),
                     toggle=True)
        row = col.row(align=True)
        for i in range(8, 16):
            row.prop(obj.mmd_rigid,
                     'collision_group_mask',
                     index=i,
                     text=str(i),
                     toggle=True)

        c = layout.column()
        c.label('Damping')
        row = c.row()
        row.prop(obj.rigid_body, 'linear_damping')
        row.prop(obj.rigid_body, 'angular_damping')