「オブジェクトモードで分離して選択する」のコード

Aug 18 2020

だから私はエディターモードで分離選択からマクロを作成し、その後オブジェクトモードで分離メッシュを選択しています...私は自分が何をしているのかほとんど知識がありませんが、それでもそれをやっています。 ..

だから私はいくつかの助けが必要です。オブジェクトモードを終了した後、最後に分離されたメッシュを選択するためのコードは何でしょうか?これが意味をなさない場合は申し訳ありません。ブレンダーが分離した直後にオブジェクトモードで分離したメッシュを選択することを望みます。基本的に分離したメッシュを見つける方法がわかりません。

どのオブジェクトでも毎回機能するように、どうすればよいですか?ありがとう

回答

2 HikariTW Aug 18 2020 at 15:07

解決策は、最初に選択したオブジェクト名を保存し、メッシュを分離した後でそれらの選択を解除することです。

このようなもの:

org_obj_list = {obj.name for obj in context.selected_objects}
# This is a Set comprehension in Python,
# which create a set of name from the context.selected_objects
# context.selected_objects will be a Iterable collection of some object

bpy.ops.mesh.separate(type = 'SELECTED')
# This will call the separate operator in your code directly
# the type can be a enum string in ['SELECTED', 'LOOSE', 'MATERIAL']

bpy.ops.object.editmode_toggle()
# Switch back to object mode from edit mode

# Those separated object will also be selected now
# We then check if selected object is the one we saved before, then deselect it.
for obj in context.selected_objects:
    if obj and obj.name in org_obj_list:
        # Deselect selected object
        obj.select_set(False)
    else:
        # Set the new created object to active
        context.view_layer.objects.active = obj

これがベストプラクティスかどうかはわかりませんが、機能します。


カスタム演算子:

import bpy

class SeparateSelectionActive(bpy.types.Operator):
    """Separate object by selection and set it as active object."""
    bl_idname = "mesh.select_separate_active"
    bl_label = "Separate Selection Active"
    
    # An enum for prompt dialog
    separate_method: bpy.props.EnumProperty(
        items = {
            ('SELECTED', 'Selected', "Selected mesh"),
            ('MATERIAL', 'Material', "Based on material"),
            ('LOOSE', 'Loose', "Based on loose part")
        },
        name = "Separate Method",
        description = "Choose a method to separate mesh",
        default = 'SELECTED'
    )
    
    @classmethod
    def poll(cls, context):
        return context.object is not None and context.mode == 'EDIT_MESH'
    
    def invoke(self, context, event):
        # Prompt to ask a method to separate
        return context.window_manager.invoke_props_dialog(self)

    def execute(self, context):
        org_obj_list = {o.name for o in context.selected_objects}
        
        # Separate using selected method
        bpy.ops.mesh.separate(type = self.separate_method)
        bpy.ops.object.editmode_toggle()
        for obj in context.selected_objects:
            if obj and obj.name in org_obj_list:
                # Deselect everything selected before
                obj.select_set(False)
            else:
                # Set the new created object to active
                context.view_layer.objects.active = obj
                self.report({'INFO'},f"Set active object to: {obj.name}")
        return {'FINISHED'}

# A menu inject into View3D > Edit > Mesh tab
def _menu_func(self, context):
    self.layout.operator(SeparateSelectionActive.bl_idname)

def register():
    bpy.utils.register_class(SeparateSelectionActive)
    bpy.types.VIEW3D_MT_edit_mesh.append(_menu_func)

def unregister():
    bpy.utils.unregister_class(SeparateSelectionActive)
    bpy.types.VIEW3D_MT_edit_mesh.remove(_menu_func)

if __name__ == "__main__":
    register()

    # test call
    bpy.ops.mesh.select_separate_active()

オペレーターとして登録した後、このオペレーターを3D空間内で編集モードで検索して実行できます

または、View3d>編集モード>メッシュ>セパレートセレクションアクティブで、新しいメニュー機能を追加して、Blender2.90で検索できるようにします。

分離する方法を尋ねるプロンプトが表示されます。

これらのオプションは、元の個別の演算子とまったく同じです。

そして、このオペレーターはそれを分離し、元のメッシュの選択を解除し、新しく作成されたメッシュをアクティブにします。

注:別のプロセスでアクティブにしたいメッシュを変更しない場合、同じ名前の元のメッシュのままであるため、このオペレーターは間違ったメッシュをアクティブにします。