access a separated object with python

Hi,

I’m separating objects in python. How can I properly access the object, that has been just separated? I could go for something like name convention (Plane -> Plane.001, Plane.002), but this would be so ugly. There must be another way! WHY DOESN’T the separate operator return the object it separated? That would be so much easier.

Here’s the code I had, just before I realised, that I am adding the wrong object to my list…


# here I select my faces/edges/vertices 
# (why do I need to do this in object mode?!? my code is full of toggles...)
bpy.ops.object.editmode_toggle()
bpy.ops.mesh.separate()
#here I would like to append the newly created object I just separated...
kids.append(bpy.context.active_object) 
bpy.ops.object.editmode_toggle()

Here’s the file: dreieck_2.blend (354 KB)

you can make a list of mesh before separate the mesh and another list after.
Then you remove all what is in the first list from the sconde and your have your new mesh

WHY DOESN’T the separate operator return the object it separated?
Because you are using bpy.ops. Eventually you will realize you can write your own functions that manipulate bpy.data directly. You are taking a shortcut to coding a solution by using bpy.ops. When you write your own functions you can offer a return value such as the object just separated.

But what kgeogeo says is valid and would probably work.
For instance…


import bpy
l_before = len(bpy.data.meshes)
# Now issue your bpy.ops command.
l_after = len(bpy.data.meshes)
print(l_before, l_after)

How would I do this? And why when the “separate” command is already there?

Thank you for the hint on len(bpy.data.meshes). I guess that’s my workaround for now.

lo_b = [ob for ob in bpy.data.objects if ob.type == ‘MESH’]
bpy.ops.mesh.separate(type=‘SELECTED’)
lo_a = [ob for ob in bpy.data.objects if ob.type == ‘MESH’]

for i in lo_b:
lo_a.remove(i)
separate_object = lo_a[0]

For Atom: yes It’s possible to write your own function but it’ll be slower because Python is slower als C I think.

Just an FYI. I have found many bpy.ops operations to be much slower than operating directly upon bpy.data. I never really worry about speed, I just focus on functionality. Make it work first, then optimize (if needed). Most python scripts are just a way to get to the next project and they have a shelf life of about 3 months before the API changes and they fall out of scope. If you are writing game code for the game engine then speed is more of a concern.

And why when the “separate” command is already there?
It is there to offer the feature for the interface. Which is user driven. Thus the context is provided by the user at run time.

Here is a way to navigate mesh data.


import bpy
ob = bpy.data.objects.get("Cube")
if ob != None:
    me = ob.data
    for face in me.polygons:
        print(face.select)
        if face.select == True:
            print(dir(face))

When I am writing scripts I often use the Help menu of Blender to bring up the search website for the API.
http://www.blender.org/documentation/blender_python_api_2_63_release/bpy.types.MeshVertex.html?highlight=vertices

To separate a face from an object. You are really creating two objects from a single source. One object contains all the faces except the selection set and the other is the inverse. Then you remove the source object from the scene.