"개체 모드에서 분리 및 선택"에 대한 코드

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> 편집 모드> 메쉬> 블렌더 2.90에서 검색 할 수 있도록 새 메뉴 기능을 추가 한 후 선택 분리 활성화 .

분리 방법을 묻는 메시지가 표시되어야합니다.

이러한 옵션은 원래의 개별 연산자와 정확히 동일합니다.

그리고이 연산자는이를 분리하고 원래 메시를 선택 해제하고 새로 생성 된 메시를 활성화합니다.

참고 : 별도의 프로세스에서 활성화하려는 메시를 변경하지 않은 경우이 연산자는 여전히 동일한 이름을 가진 원래 메시이기 때문에 잘못된 메시를 활성화합니다.