How to replace a mesh

Hi,

I would like to make a script that changes a mesh of some object. I know how to create a list of vertexes and faces and I know I can create a new object with a new mesh using:

mesh = bpy.data.meshes.new(meshname)
object = bpy.data.objects.new(name,mesh)
bpy.context.scene.objects.link(object)
mesh.from_pydata(verts,[],faces)

But now I have an existing object that already has a mesh and I want to replace the mesh with an other mesh. But I can not find out how to do that.

Hi mehcamania


object.data = mesh

1 Like

I didn’t expect it is so simple!
Will the old mesh be removed from memory automatically?

B.T.W.: I tried calling mesh.from_pydata(verts,[],faces) again, but that results in an error.
Can I throw away the existing vertexes, edges and faces of a mesh?

Anyway the object.data = mesh works perfect. I feel a little stupid.

Will the old mesh be removed from memory automatically?

No, the old mesh will remain in memory and if you continue creating new meshes you will be leaking memory.

Here is how I do it.
This little code snippet also transfers the material from the previous mesh to the new mesh.


# Make sure we are linked to the scene.
try:
    scene.objects.link(ob)
except:
    pass
    
# Generate a new mesh to re-link to this passed object.
me_new = bpy.data.meshes.new(ob.data.name)            # Create new mesh.
if me_new != None:
    size = (1, ln, sz)
    verts, faces = self.create_multi_side_box(size, t, s)
    me_new = self.returnMeshCreated("me_%s" % ob_name, verts, faces)

    # Remove the old mesh, it is no longer needed.
    old_mesh = ob.data
    try:
        temp_material = old_mesh.materials[0]         # Transfer the first material from the old mesh to the new mesh.
        if temp_material != None:
            me_new.materials.append(temp_material)
    except:
        pass
    me_new.update()                                   # Update mesh geometry after doing stuff.

    ob.data = me_new                                  # Assign the new mesh to the object.
    removeMeshFromMemory (old_mesh.name)



def removeMeshFromMemory (passedName):
    print("removeMeshFromMemory:[%s]." % passedName)
    # Extra test because this can crash Blender if not done correctly.
    result = False
    mesh = bpy.data.meshes.get(passedName)
    if mesh != None:
        if mesh.users == 0:
            try:
                mesh.user_clear()
                can_continue = True
            except:
                can_continue = False
            
            if can_continue == True:
                try:
                    bpy.data.meshes.remove(mesh)
                    result = True
                    print("removeMeshFromMemory: MESH [" + passedName + "] removed from memory.")
                except:
                    result = False
                    print("removeMeshFromMemory: FAILED to remove [" + passedName + "] from memory.")
            else:
                # Unable to clear users, something is holding a reference to it.
                # Can't risk removing. Favor leaving it in memory instead of risking a crash.
                print("removeMeshFromMemory: Unable to clear users for MESH, something is holding a reference to it.")
                result = False
        else:
            print ("removeMeshFromMemory: Unable to remove MESH because it still has [" + str(mesh.users) + "] users.")
    else:
        # We could not fetch it, it does not exist in memory, essentially removed.
        print("We could not fetch MESH [%s], it does not exist in memory, essentially removed." % passedName)
        result = True
    return result