Replacing objects inside mesh

Hi

can I replace objects inside mesh with Python?

For example:
I have 3 cubes joined together and I wish to replace two cubes with two cylinders.

My idea is:

  • select both cubes by material index
  • get location of both cubes [x,y,z]
  • delete cubes
  • position cylinders to the same location as were cubes before

Attachments


Sounds like a great plan, and it should work!
What have you tried?

You’ll also need to take into account each post’s bounding box so you can size the cylinders accordingly.

HOWEVER… the tricky part you’re both forgetting is… how do you separate the two posts from one another? They both have the same material index.

You could do this using some mesh operators… off the top of my head I think it’s called ‘separate by material index’, then you’d select the resulting object, and do separate by loose parts… THEN you have your blue boxes as nice atomic pieces.

Would be more interesting, but more difficult to accomplish this solely in python… not using any of those silly mesh operators… feels so dirty using them… :frowning:

Well not much.
I will try something with Empty objects.

Select -> Separate by Material

ob_foo = bpy.data.objects.get(objname_foo)
ob_foo.select = True
bpy.context.scene.objects.active = ob_foo
bpy.ops.object.mode_set(mode = 'EDIT')
bpy.ops.mesh.separate(type='MATERIAL')
bpy.ops.object.mode_set(mode = 'OBJECT')
bpy.ops.object.select_all(action='DESELECT')

I wish there could be a REPLACE/CTRL+H function like in text editors.:cool: