Getting References to Objects In Edit Mode?

Hi folks. I’m just starting out with Python in Blender (and in general) so I’m starting small and just trying to write a script that joins vertices with edges, and thought I’d try playing with the new feature of modifying more than one object in edit mode. I thought I try to connect a vertex from the the default cube to a plane, but I 'm not sure what the right (and generalised) way to get references to each selected object in edit mode is. From looking at the Blender Python documentation for 2.79, the recommended way looks to have been C.edit_object. The documentation says to pass this to a bmesh constructor to be able to fiddle with the vertices and edges etc.

If I use the same thing in the build I’m using of 2.8, however, I only see a single object listed when I check the C.edit_object property when I have two objects selected in edit mode. I used C.dir() to have a look for other properties that might provide what I’m looking for and I found C.objects_in_mode, but I’m not sure if it’s supposed to be used for what I’m looking for. To test it out, I added a cone to the scene (in object mode) and then made sure I selected only the cube and the plane and switched back to edit mode so it was the same as before, but now contained an unselected cone. I used C.objects_in_mode it it did list the plane and the cube without the cone, but the thing that makes me unsure that this property is what I’m looking for is that when I check the same property from object mode, it just returns and empty list.

My understanding of the documentation is that bpy.context is intended to provide information about the various modes that might be used, so I’d expect this propery to list all 3 objects in object mode, but the fact that it just returns an emty list in object mode makes me think I must me barking up the wrong tree. Does anyone have any suggestions about what I’m doing wrong or know which page of the Blender Python manual I should be reading to do figure this out?

code please

Unless something has changed recently, elements from multiple objects cannot be physically joined. You can snap a vertex from one mesh to another, but connecting a vertex from one object’s mesh to a different object wouldn’t work (what happens when you exit edit mode with two objects if they are joined, etc.).

C.edit_object only references the active object in edit mode.
C.object will reference the object which holds the active selection in edit mode. In other words the object which active vert/face/edge has landed on.

The naming of the property is not clear, but C.objects_in_mode is specifically for referencing all objects which have their meshes in an exclusive mode like edit mode. It’s probably called in_mode in order to reuse this property on future multi-object modes like sculpting, painting etc.

You can use obj.data.total_vert_sel to filter only objects which have any of their elements selected (a value of 0 is considered False). Something like:

multi = bpy.context.objects_in_mode
objs = [o for o in multi if o.data.total_vert_sel]

You’ll also want to use the interactive console in Blender and ctrl-space to get a list of all properties and functions available in the current namespace.

2 Likes

Thanks for the highly informative response, iceythe. I forgot to say in my OP that I’m aware that you can’t connect two separate objects with an edge in edit mode and that you have to bpy.ops.object.join() them in object mode first. I’m just trying to give myself a simple but non-trivial task that would require me to bring together a few different operations so I could gradually build up a picture in my head of how one thing relates to another. Figuring out the best way to do the joining step was going to be my next step (something to do with that context override thing I saw in the documentation?) but I needed a bit of reassurance that I was not using the wrong methods to obtain object references before I try to join them.

Thanks.

1 Like

Ah ok. I was going by the “join vertices” part of your post, but I wasn’t entirely sure.

There are low-level ways of doing what bpy.ops.object.join() does in blender so you don’t have to override contexts. If you’re really looking to know the ins and outs of blender’s api, avoiding operators as much as possible is the best thing to do.

Well thanks for the tips. I might have a peek at things below the level of the operators in the future, but I should probably stick to the basics for the moment because, as well as being new to the Python API for Blender, I’m also pretty much new to Python in general. The syntax drives me up the wall, but that’s just tough luck.