Hi folks. A while ago I decided to start learning to use Python in Blender to make a basic modal operator. Since Blender has the newly added feature of being able to use multiple objects in edit mode, I thought I’d try to make use of this new feature at the same time as learning about Python in Blender. So I started putting together the basics of an operator that would allow a user to choose which objects to join whilst tweaking the geometry. Iceythe gave me some very helpful advice with regards to using Python in blender, including the advice that I should not try to use bpy.ops to join the objects (a recommendation that I’ve seen re-enforced in almost every single search result relating to this topic). Iceythe advised me to attempt to use lower level calls to connect meshes that the user chooses to join and, according to the documentation, the bmesh module makes calls directly to the underlying C, so I guess that’s about as low level as I can hope to get (which is good because it’s what I was intending to use anyway).
I’m trying to put together a few lines of Python that I can use as the basis of an object joining method. To set it up, all I do is have two objects in an otherwise empty blend file and put a basic material on each of them, then I just move each object a couple of BUs from the origin in different directions. Based on what was in the bmesh documentation, I managed to put together a few lines that does take the mesh of each object that’s in edit mode and puts them into into a single object that gets linked back to each collection that any of the objects came from, but it doesn’t copy over any of the non-mesh information: materials, translations, etc are all missing. It took me a while to realise that it was actually copying the data of both meshes because, when the translation information is missing, all the meshes are sitting in exactly the same place at the origin. I guess I shouldn’t really have been surprised that only the mesh data was being used, given that the methods I could get to work were called .from_edit_mesh()
and .from_mesh()
, but the method I pinned my hopes on when I went back to the documentation .from_object()
demands an argument I’ve never heard of, have no idea how to get, and google doesn’t look like it brings up anything useful when I ask it.
Here’s the Python I’ve been using that gets me an object containing just the bare bones meshes without copying any of the other data I want.
import bmesh
m = bmesh.from_edit_mesh(C.objects_in_mode[0].data)
m.from_mesh(C.objects_in_mode[1].data)
dm = bpy.data.meshes.new('z')
m.to_mesh(dm)
mo = bpy.data.objects.new('s', dm)
cs = set()
for o in C.objects_in_mode:
cs.update(o.users_collection)
for c in cs:
c.objects.link(mo)
Am I right to expect that from_object() is the method that will copy over all the other data? If so, then what is a depsgraph and how do I get a reference to the correct one? I’ve been using Blender for well over a year now and I’ve never even heard of it before.