def separate_by_materials(context, mesh):
    set_default_stage()

    # Remove Rigidbodies and joints
    for obj in bpy.data.objects:
        if 'rigidbodies' in obj.name or 'joints' in obj.name:
            tools.common.delete_hierarchy(obj)

    select(mesh)
    ShapekeyOrder.save(mesh.name)

    for mod in mesh.modifiers:
        if mod.type == 'DECIMATE':
            mesh.modifiers.remove(mod)
        else:
            mod.show_expanded = False

    utils.separateByMaterials(mesh)

    for ob in context.selected_objects:
        if ob.type == 'MESH' and ob.data.shape_keys:
            for kb in ob.data.shape_keys.key_blocks:
                if can_remove(kb):
                    ob.shape_key_remove(kb)

    utils.clearUnusedMeshes()
    def execute(self, context):
        obj = context.active_object
        root = mmd_model.Model.findRoot(obj)
        if root:
            bpy.ops.mmd_tools.clear_temp_materials()
            bpy.ops.mmd_tools.clear_uv_morph_view()
        if root:
            # Store the current material names
            rig = mmd_model.Model(root)
            mat_names = [getattr(mat, 'name', None) for mat in rig.materials()]
        utils.separateByMaterials(obj)
        if self.clean_shape_keys:
            bpy.ops.mmd_tools.clean_shape_keys()
        if root:
            rig = mmd_model.Model(root)
            # The material morphs store the name of the mesh, not of the object.
            # So they will not be out of sync
            for mesh in rig.meshes():
                FnMorph.clean_uv_morph_vertex_groups(mesh)
                if len(mesh.data.materials) > 0:
                    mat = mesh.data.materials[0]
                    idx = mat_names.index(getattr(mat, 'name', None))
                    MoveObject.set_index(mesh, idx)

        if root and len(root.mmd_root.material_morphs) > 0:
            for morph in root.mmd_root.material_morphs:
                mo = FnMorph(morph, mmd_model.Model(root))
                mo.update_mat_related_mesh()
        utils.clearUnusedMeshes()
        return {'FINISHED'}
def separate_by_materials(context, mesh):
    set_default_stage()

    # Remove Rigidbodies and joints
    for obj in bpy.data.objects:
        if 'rigidbodies' in obj.name or 'joints' in obj.name:
            tools.common.delete_hierarchy(obj)

    save_shapekey_order(mesh.name)
    select(mesh)

    for mod in mesh.modifiers:
        if mod.type == 'DECIMATE':
            mesh.modifiers.remove(mod)
        else:
            mod.show_expanded = False

    clean_material_names(mesh)

    # Correctly put mesh together. This is done to prevent extremely small pieces.
    # This essentially does nothing but merges the extremely small parts together.
    switch('EDIT')
    bpy.ops.mesh.select_all(action='DESELECT')
    bpy.ops.mesh.remove_doubles(threshold=0)
    switch('OBJECT')

    utils.separateByMaterials(mesh)

    for ob in context.selected_objects:
        if ob.type == 'MESH':
            clean_shapekeys(ob)

    utils.clearUnusedMeshes()
Exemple #4
0
    def execute(self, context):
        obj = context.active_object

        if not obj or (obj and obj.type != 'MESH'):
            tools.common.unselect_all()
            meshes = tools.common.get_meshes_objects()
            if len(meshes) == 0:
                return {'FINISHED'}
            obj = meshes[0]
            tools.common.select(obj)

        for mod in obj.modifiers:
            if 'Decimate' in mod.name:
                bpy.ops.object.modifier_remove(modifier=mod.name)
            else:
                mod.show_expanded = False

        tools.common.set_default_stage()

        utils.separateByMaterials(obj)
        for ob in context.selected_objects:
            if ob.type != 'MESH' or ob.data.shape_keys is None:
                continue
            if not ob.data.shape_keys.use_relative:
                continue  # not be considered yet
            key_blocks = ob.data.shape_keys.key_blocks
            counts = len(key_blocks)
            self.__do_shape_key_clean(context, ob, key_blocks)
            counts -= len(key_blocks)

        utils.clearUnusedMeshes()
        return {'FINISHED'}
def separate_by_loose_parts(context, mesh):
    set_default_stage()

    # Remove Rigidbodies and joints
    for obj in bpy.data.objects:
        if 'rigidbodies' in obj.name or 'joints' in obj.name:
            tools.common.delete_hierarchy(obj)

    select(mesh)
    print("DEBUG")
    ShapekeyOrder.save(mesh.name)
    print("DEBUG2")

    for mod in mesh.modifiers:
        if mod.type == 'DECIMATE':
            mesh.modifiers.remove(mod)
        else:
            mod.show_expanded = False

    utils.separateByMaterials(mesh)
    meshes = []
    for ob in context.selected_objects:
        if ob.type == 'MESH':
            meshes.append(ob)

    wm = bpy.context.window_manager
    current_step = 0
    wm.progress_begin(current_step, len(meshes))

    for mesh in meshes:
        unselect_all()
        select(mesh)
        bpy.ops.mesh.separate(type='LOOSE')

        meshes2 = []
        for ob in context.selected_objects:
            if ob.type == 'MESH':
                meshes2.append(ob)

        ## This crashes blender, but would be better
        # unselect_all()
        # for mesh2 in meshes2:
        #     if len(mesh2.data.vertices) <= 3:
        #         select(mesh2)
        #     elif bpy.ops.object.join.poll():
        #         bpy.ops.object.join()
        #         unselect_all()

        for mesh2 in meshes2:
            if mesh2 and mesh2.data.shape_keys:
                for kb in mesh2.data.shape_keys.key_blocks:
                    if can_remove(kb):
                        mesh2.shape_key_remove(kb)

        current_step += 1
        wm.progress_update(current_step)

    wm.progress_end()

    ## Old separate method
    # print("DEBUG3")
    # bpy.ops.mesh.separate(type='LOOSE')
    # print("DEBUG4")
    #
    # for ob in context.selected_objects:
    #     print(ob.name)
    #     if ob.type == 'MESH':
    #         if ob.data.shape_keys:
    #             for kb in ob.data.shape_keys.key_blocks:
    #                 if can_remove(kb):
    #                     ob.shape_key_remove(kb)
    #
    #         mesh = ob.data
    #         materials = mesh.materials
    #         if len(mesh.polygons) > 0:
    #             if len(materials) > 1:
    #                 mat_index = mesh.polygons[0].material_index
    #                 for x in reversed(range(len(materials))):
    #                     if x != mat_index:
    #                         materials.pop(index=x, update_data=True)
    #         ob.name = getattr(materials[0], 'name', 'None') if len(materials) else 'None'
    #
    #         if '. 001' in ob.name:
    #             ob.name = ob.name.replace('. 001', '')
    #         if '.000' in ob.name:
    #             ob.name = ob.name.replace('.000', '')

    utils.clearUnusedMeshes()
Exemple #6
0
 def __separate_by_materials(self, obj):
     utils.separateByMaterials(obj)
     if self.clean_shape_keys:
         bpy.ops.mmd_tools.clean_shape_keys()