3
$\begingroup$

so i'm creating a macro out of separating selection in editor mode, and then select the separated mesh afterwards in object mode... i have very little knowledge of what i'm doing, but i'm doing it never the less...

so i need some help. what would the code be for selecting the last separated mesh after coming out of object mode? sorry if this doesn't make sense. i wish blender would just select the separated mesh in object mode right after you separate it. i don't know how to find the separated mesh basically.

How would i go about doing that so it would work every time with any object? Thanks

$\endgroup$
1

1 Answer 1

4
$\begingroup$

A solution is to store your selected object name first and then deselect them after you separate the mesh.

Something like this:

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

Not sure if this is the best practice, but it do work.


A custom operator:

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()

After register it as an Operator, you can search and run this operator in edit mode within 3d space:

enter image description here

Or in View3d > edit mode > mesh > Separate Selection Active after append a new menu function to make it searchable in Blender 2.90.

enter image description here

You should see a prompt asking a method for separating:

enter image description here

These options is exactly the same with original separate operator.

And this operator will separate it, deselected original mesh, make the new created mesh active:

enter image description here

Note: If the separate process make the one you want to be active unchanged, this operator will active the wrong mesh since it's still the original mesh with the same name.

$\endgroup$
9
  • $\begingroup$ hey HikariTW, thanks for the reply. uh, i've tried running this code but it doesn't show up when i search for it. fyi i'm still in "learning mode" with python and blender. would you mind telling me what i should do specifically to have it show up in the search? i checked the register and ran the script to no avail. thanks $\endgroup$ Commented Aug 19, 2020 at 5:09
  • $\begingroup$ @tinypeacock Did you enter the edit mode and search in the 3d view area? Notice that: def poll(cls, context): return context.object is not None and context.mode == 'EDIT_MESH' line. This is a safety for checking if context is correct to run this operator. And search method in Blender will hide those method which failed this test. $\endgroup$
    – HikariTW
    Commented Aug 19, 2020 at 5:30
  • $\begingroup$ i think i'm doing something very wrong. fyi this is my first month with blender, so i'm still somewhat clueless. here's a gif of me failing to run it: imgur.com/a/PEddW1L $\endgroup$ Commented Aug 19, 2020 at 5:57
  • $\begingroup$ @tinypeacock Wait... what is that search? Is that Blender 2.90? Imgur. Yes that is a 2.90 search, then I need to change the addon to let new search operator find it. $\endgroup$
    – HikariTW
    Commented Aug 19, 2020 at 6:15
  • $\begingroup$ Would you want to use Blender 2.80~2.83 instead of 2.90 beta? The API change might not be that much but it will increase the problem you might meet when scripting, modeling as a new Blenderer $\endgroup$
    – HikariTW
    Commented Aug 19, 2020 at 6:24

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .