Re-assigning meshes in Python?

I’ve spent hours googling, reading manual docs and looking through fora and tutorialsand I can’t find this one. (Well, I’ve found a few old years-old discussions that use deprecated code.)

It seems simple. I have Cube selected. If I go to the Links and Materials block in Editing, I can simply pick from the listbox and switch the mesh to Sphere, or Cube.001, or whatever.

How do I do that with a Python script?

Here’s what I’ve got, in simplest form:

import Blender as B

cube = B.Object.Get('Cube')                 # Cube has mesh 'cubemesh'
deadcube = B.Object.Get('DeadCube')         # DeadCube has mesh 'deadmesh'

I’m trying to assign deadmesh – the mesh of object DeadCube – to object Cube. This has to be possible, right?

Apologies if I’m ignoring something obvious.

I think this is what you want


import Blender as B

cube = B.Object.Get('Cube')                 # Cube has mesh 'cubemesh'
deadcube = B.Object.Get('DeadCube')         # DeadCube has mesh 'deadmesh'
me = deadcube.getData(mesh=1)

cube.link(me)
cube.makeDisplayList()
B.Window.RedrawAll()