コード例 #1
0
    def filter_only_in_mmd_model(
            key_object: bpy.types.Object) -> Iterable[bpy.types.Object]:
        mmd_tools = import_mmd_tools()
        mmd_root = mmd_tools.core.model.Model.findRoot(key_object)
        if mmd_root is None:
            return

        return mmd_tools.core.model.Model(mmd_root).allObjects()
コード例 #2
0
    def execute(self, context):
        mmd_tools = import_mmd_tools()
        for obj in (x for x in context.selected_objects if x.type == 'MESH'):
            mmd_tools.cycles_converter.convertToCyclesShader(obj, use_principled=True, clean_nodes=True)

        if context.scene.render.engine != 'BLENDER_EEVEE':
            context.scene.render.engine = 'BLENDER_EEVEE'

        return {'FINISHED'}
コード例 #3
0
    def is_mmd_armature_object(obj: bpy.types.Object):
        if obj is None:
            return False

        if obj.type != 'ARMATURE':
            return False

        if import_mmd_tools().core.model.Model.findRoot(obj) is None:
            return False

        return True
コード例 #4
0
    def execute(self, context: bpy.types.Context):
        try:
            mmd_find_root = import_mmd_tools().core.model.Model.findRoot

            target_mmd_root_object = None
            rigid_body_objects: List[bpy.types.Object] = []
            mesh_objects: List[bpy.types.Object] = []

            obj: bpy.types.Object
            for obj in context.selected_objects:
                if obj.type != 'MESH':
                    continue

                mmd_root_object = mmd_find_root(obj)
                if mmd_root_object is None:
                    continue

                if target_mmd_root_object is None:
                    target_mmd_root_object = mmd_root_object
                elif target_mmd_root_object != mmd_root_object:
                    raise MessageException(
                        _('Multiple MMD models selected. Please select single model at a time.'
                          ))

                if obj.mmd_type == 'RIGID_BODY':
                    rigid_body_objects.append(obj)
                elif obj.mmd_type == 'NONE':
                    mesh_objects.append(obj)

            RigidBodyToClothConverter.convert(target_mmd_root_object,
                                              rigid_body_objects, mesh_objects,
                                              self.subdivision_level,
                                              self.ribbon_stiffness,
                                              PhysicsMode[self.physics_mode],
                                              self.extend_ribbon_area)

        except MessageException as ex:
            self.report(type={'ERROR'}, message=str(ex))
            return {'CANCELLED'}

        return {'FINISHED'}
コード例 #5
0
    def poll(cls, context: bpy.types.Context):
        if context.mode != 'OBJECT':
            return False

        selected_mesh_mmd_root = None
        selected_rigid_body_mmd_root = None

        mmd_find_root = import_mmd_tools().core.model.Model.findRoot
        for obj in context.selected_objects:
            if obj.type != 'MESH':
                return False

            if obj.mmd_type == 'RIGID_BODY':
                selected_rigid_body_mmd_root = mmd_find_root(obj)
            elif obj.mmd_type == 'NONE':
                selected_mesh_mmd_root = mmd_find_root(obj)

            if selected_rigid_body_mmd_root == selected_mesh_mmd_root:
                return selected_rigid_body_mmd_root is not None

        return False
コード例 #6
0
    def __init__(self, mmd_armature_object: bpy.types.Object):
        super().__init__(mmd_armature_object)

        self.mmd_model_name = import_mmd_tools().core.model.Model.findRoot(
            mmd_armature_object).mmd_root.name

        self.mmd_bone_names: Set[str] = set(
            {b.mmd_bone_name
             for b in MMDBoneInfo})
        self.exist_strict_bone_names: Set[str] = {
            b.mmd_bone.name_j
            for b in self.raw_object.pose.bones
            if b.mmd_bone.name_j in self.mmd_bone_names
        }
        self.exist_actual_bone_names: Set[str] = {
            b.name
            for b in self.raw_object.pose.bones
            if b.mmd_bone.name_j in self.mmd_bone_names
        }
        self.exist_bone_types: Set[MMDBoneType] = {
            b.bone_type
            for b in MMDBoneInfo
            if b.mmd_bone_name in self.exist_strict_bone_names
        }
コード例 #7
0
# -*- coding: utf-8 -*-
# Copyright 2021 UuuNyaa <*****@*****.**>
# This file is part of MMD UuuNyaa Tools.

from typing import Iterable, List

import bpy
from mmd_uuunyaa_tools.editors.meshes import MeshEditor
from mmd_uuunyaa_tools.converters.physics.rigid_body_to_cloth import RigidBodyToClothConverter, PhysicsMode
from mmd_uuunyaa_tools.m17n import _
from mmd_uuunyaa_tools.tuners import TunerABC, TunerRegistry
from mmd_uuunyaa_tools.utilities import MessageException, import_mmd_tools

mmd_tools = import_mmd_tools()
if not hasattr(mmd_tools.core.model.Model, 'clothGroupObject'):

    def mmd_model_cloth_group_object_method(self):
        # pylint: disable=protected-access
        if not hasattr(self, '_cloth_grp'):
            self._cloth_grp = None
            for i in self.rootObject().children:
                if i.name == 'cloths':
                    self._cloth_grp = i
                    break

            if self._cloth_grp is None:
                cloths = bpy.data.objects.new(name='cloths', object_data=None)
                mmd_tools.bpyutils.SceneOp(bpy.context).link_object(cloths)
                cloths.parent = self.rootObject()
                cloths.hide = cloths.hide_select = True
                cloths.lock_rotation = cloths.lock_location = cloths.lock_scale = [
コード例 #8
0
    def convert(cls, mmd_root_object: bpy.types.Object,
                rigid_body_objects: List[bpy.types.Object],
                mesh_objects: List[bpy.types.Object], subdivision_level: int,
                ribbon_stiffness: float, physics_mode: PhysicsMode,
                extend_ribbon_area: bool):  # pylint: disable=too-many-arguments
        # pylint: disable=too-many-locals, too-many-statements
        mmd_model = import_mmd_tools().core.model.Model(mmd_root_object)
        mmd_mesh_object = mesh_objects[0]
        mmd_armature_object = mmd_model.armature()

        rigid_bodys_count = len(rigid_body_objects)
        rigid_body_index_dict = {
            rigid_body_objects[i]: i
            for i in range(rigid_bodys_count)
        }

        pose_bones: List[bpy.types.PoseBone] = []
        for rigid_body_object in rigid_body_objects:
            pose_bone = mmd_armature_object.pose.bones.get(
                rigid_body_object.mmd_rigid.bone)

            if pose_bone is None:
                raise MessageException(
                    iface_(
                        'No bones related with {rigid_body_name}, Please relate a bone to the Rigid Body.'
                    ).format(rigid_body_name=rigid_body_object.name))

            pose_bones.append(pose_bone)

        def remove_objects(objects: Iterable[bpy.types.Object]):
            for obj in objects:
                bpy.data.objects.remove(obj)

        joint_objects, joint_edge_indices, side_joint_objects = cls.collect_joints(
            mmd_model, rigid_body_index_dict)

        remove_objects(joint_objects)

        cloth_mesh = bpy.data.meshes.new('physics_cloth')
        cloth_mesh.from_pydata([r.location for r in rigid_body_objects],
                               joint_edge_indices, [])
        cloth_mesh.validate()

        cloth_mesh_object = bpy.data.objects.new('physics_cloth', cloth_mesh)
        cloth_mesh_object.parent = mmd_model.clothGroupObject()
        cloth_mesh_object.hide_render = True
        cloth_mesh_object.display_type = 'WIRE'

        # 标记出特殊边和点
        # These are special edge and vertex
        cloth_bm: bmesh.types.BMesh = bmesh.new()
        cloth_bm.from_mesh(cloth_mesh)

        cls.clean_mesh(cloth_bm, joint_edge_indices)

        # 标出头部,尾部,飘带顶点
        # try mark head,tail,ribbon vertex
        cloth_bm.verts.ensure_lookup_table()
        cloth_bm.edges.ensure_lookup_table()

        vertices = cls.collect_vertices(cloth_bm, pose_bones, physics_mode,
                                        extend_ribbon_area)
        edges = cls.collect_edges(cloth_bm, vertices)

        new_up_verts = cls.extend_up_edges(cloth_bm, pose_bones, vertices,
                                           edges, physics_mode)
        new_down_verts = cls.extend_down_edges(cloth_bm, pose_bones, vertices,
                                               edges)

        cls.fill_faces(cloth_bm, edges.up_edges, new_up_verts)
        cls.fill_faces(cloth_bm, edges.down_edges, new_down_verts)

        cloth_bm.verts.index_update()
        cloth_bm.faces.ensure_lookup_table()

        new_side_verts = cls.extend_side_vertices(cloth_bm, vertices, edges)
        cls.fill_faces(cloth_bm, edges.side_edges, new_side_verts)

        new_ribbon_verts = cls.extend_ribbon_vertices(cloth_bm)
        cls.fill_faces(cloth_bm, [e for e in cloth_bm.edges if e.is_wire],
                       new_ribbon_verts)

        cls.normals_make_consistent(cloth_bm)
        cloth_bm.verts.ensure_lookup_table()
        cloth_bm.edges.ensure_lookup_table()
        cloth_bm.to_mesh(cloth_mesh)

        pin_vertex_group = cls.new_pin_vertex_group(cloth_mesh_object,
                                                    side_joint_objects,
                                                    new_up_verts,
                                                    new_side_verts,
                                                    rigid_body_index_dict)

        remove_objects(side_joint_objects)

        deform_vertex_group: bpy.types.VertexGroup = mmd_mesh_object.vertex_groups.new(
            name='physics_cloth_deform')

        mesh_editor = MeshEditor(cloth_mesh_object)
        mesh_editor.link_to_active_collection()
        mesh_editor.add_subsurface_modifier('physics_cloth_subsurface',
                                            subdivision_level,
                                            subdivision_level)
        mesh_editor.add_armature_modifier('physics_cloth_armature',
                                          mmd_armature_object,
                                          vertex_group=pin_vertex_group.name)
        mesh_editor.edit_cloth_modifier(
            'physics_cloth', vertex_group_mass=pin_vertex_group.name)

        corrective_smooth_modifier = mesh_editor.add_corrective_smooth_modifier(
            'physics_cloth_smooth',
            smooth_type='LENGTH_WEIGHTED',
            rest_source='BIND')
        bpy.ops.object.correctivesmooth_bind(
            modifier=corrective_smooth_modifier.name)
        if subdivision_level == 0:
            corrective_smooth_modifier.show_viewport = False

        deform_vertex_group_index = deform_vertex_group.index
        vertices_ribbon_verts = vertices.ribbon_verts

        cls.bind_mmd_mesh(mmd_mesh_object, cloth_mesh_object, cloth_bm,
                          pose_bones, deform_vertex_group_index,
                          vertices_ribbon_verts, physics_mode)
        cls.set_pin_vertex_weight(pin_vertex_group, vertices_ribbon_verts,
                                  ribbon_stiffness, physics_mode)

        remove_objects(rigid_body_objects)

        if not vertices.all_ribbon and physics_mode in {
                PhysicsMode.AUTO, PhysicsMode.SURFACE_DEFORM
        }:
            bpy.context.view_layer.objects.active = mmd_mesh_object
            bpy.ops.object.surfacedeform_bind(modifier=MeshEditor(
                mmd_mesh_object).add_surface_deform_modifier(
                    'physics_cloth_deform',
                    target=cloth_mesh_object,
                    vertex_group=deform_vertex_group.name).name)

        cloth_bm.free()