1
$\begingroup$

In a loop, I'm calling bpy.ops.mesh.separate(type="SELECTED") to separate some faces into new meshes. The newly created mesh doesn't seem to automatically become the "active" object, and so it seems like I need to know the name of the newly created mesh before I can rename it.

I'd really like to have the script be able to give these new meshes a meaningful name. However, trying to figure out what they're getting called by default is proving very tedious.

Is there any easy way to do this?

$\endgroup$
1

2 Answers 2

1
$\begingroup$

I made http://web.purplefrog.com/~thoth/blender/python-cookbook/find-new-objects.html to solve this problem:

import bpy


scn = bpy.context.scene
names = [ obj.name for obj in scn.objects]

bpy.ops.mesh.separate(type="SELECTED")

new_objs = [ obj for obj in scn.objects if not obj.name in names]

print( [ obj.name for obj in new_objs ] )

There are shortcomings with this approach if your scene has objects linked from external .blend files (because the .name is no longer guaranteed to be unique).

$\endgroup$
1
$\begingroup$

I think it's pretty simple. Let's take an object called cubes which is one mesh consisting of several disjoint cubes that you want to separate.

# with some selection, running: separate()
cubes -> cubes.001

# with some selection, running: separate()
cubes.001 -> cubes.002

if you then deleted cubes.001 and ran separate() on cubes.002 blender will automatically find the earliest unused object name

# with some selection, running: separate()
cubes.002 -> cubes.001

I think this is how it works, at least there's a logic to it. (Tested by seeing what happens in the outliner if I do this manually for a few iterations.

The reason this isn't a very convenient process is because you are calling a function which is used by the user interface, and doesn't need to return the names of the new objects.


A simple snippet using Python's Set class to find out which scene objects are new in the scene after such an operation is:

# before
A = set(bpy.data.objects[:])

# after operation
B = set(bpy.data.objects[:])

# whats the difference between sets
new_objects = A ^ B
print([o.name for o in new_objects])
$\endgroup$
1
  • $\begingroup$ I did not know about ^ or that it would work on objects as well as strings. $\endgroup$
    – Mutant Bob
    Commented Jun 16, 2015 at 17:55

You must log in to answer this question.

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