Hi All,
I have this small routine which accepts two meshes as input. One mesh is the host mesh and the other is the addition mesh. The goal is to combine both meshes into a single mesh, like join. But this routine will be run in a frame change so it needs to be event friendly and be able to execute while rendering as well.
For some reason, this code completely pulls down Blender. Not even a crash. Blender just goes bye-bye when I run this code. The crash happens when I issue a polygons.add().
Does anyone have an alternate way to join meshes? Or any insights as to how to fix this code?
import bpy
def addToMesh (passedHostMesh, passedAdditionMesh):
fl = len(passedHostMesh.polygons)
print("Initial face length:" + str(fl) + " for [" + passedHostMesh.name + "].")
# Transfer the vertices from the append to the host.
num_v = len(passedHostMesh.vertices)
num_append_v = len(passedAdditionMesh.vertices)
passedHostMesh.vertices.add(num_append_v)
for v in range(num_append_v):
# However, we need to offset the index by the number of verts in the host mesh we are appending to.
passedHostMesh.vertices[num_v + v].co = passedAdditionMesh.vertices[v].co
print("Appending face...")
# Now append the faces...
num_f = len(passedHostMesh.polygons)
num_append_f = len(passedAdditionMesh.polygons)
print("Count %i" % num_append_f)
# When I issue this next line, Blender disappears completely.
#-->
passedHostMesh.polygons.add(num_append_f)
#<--
print(str(num_f) + ", " + str(num_append_f))
for f in range(num_append_f):
print("1")
x_fv = passedAdditionMesh.polygons[f].vertices
print("2")
o_fv = [i + num_v for i in x_fv]
# However, we need to offset the index by the number of faces in the host mesh we are appending to.
if len(x_fv) == 4:
print("3a")
passedHostMesh.polygons[num_f + f].vertices = o_fv
else:
print("3b")
passedHostMesh.polygons[num_f + f].vertices = o_fv
print("Attempting to update and calculate edges.")
#passedHostMesh.update()
#passedHostMesh.update(calc_edges=True)
fl = len(passedHostMesh.polygons)
print("Final face length:" + str(fl) + " for [" + passedHostMesh.name + "].")
The attached BLEND file has the whole scene setup for testing. Open up the BLEND and run the script to observe the crash.
Thanks
Attachments
266_addToMesh.blend (85.2 KB)