1
$\begingroup$

I got a .blend file where I got a simple cube and a plane. The Cube has some attributes like being a rigid body etc.

Now I let this cube fall on to the plane and then render an Image. This is my code:

scene = bpy.context.scene
cam = bpy.data.objects['Camera']
obj = bpy.data.objects['Cube']

scene.render.image_settings.file_format = 'PNG'
scene.render.filepath = '/path/testImage.png'
scene.render.resolution_x = 640
scene.render.resolution_y = 640

scene.frame_set(scene.frame_end)
bpy.ops.render.render(write_still = True)

I would like to exchange the cube-object with another object coming from an .stl file. So loading this stl object and having it instead of the cube but with the same attributes that I set to the cube and at the same position.

How can I do this?

$\endgroup$
  • $\begingroup$ After the import the newly imported object(s) are context.selected_objects one of which is context.object Making obj.data = context.object.data after import will give cube above mesh of imported object. It will naff materials tho. $\endgroup$ – batFINGER Jan 28 at 11:21
  • $\begingroup$ @batFINGER I don't care about the materials so this would be fine. How do I import it? $\endgroup$ – blenderNewbie Jan 28 at 11:33
  • $\begingroup$ blender.stackexchange.com/questions/127883/… $\endgroup$ – batFINGER Jan 28 at 11:37
  • $\begingroup$ Thanks! It kind of works now. But my problem is, that on my rendered Image I can't see anything. I guess the size of my stl-file object is much bigger then the one from the cube. Is there a possibility to scale it to the same size? $\endgroup$ – blenderNewbie Jan 28 at 11:46
  • $\begingroup$ blender.stackexchange.com/questions/160976/… $\endgroup$ – batFINGER Jan 28 at 11:47
1
$\begingroup$

With help from @batFinger I derived this working solution:

import bpy
import os

scene = bpy.context.scene
cam = bpy.data.objects['Camera']
obj = bpy.data.objects['Cube']

scene.render.image_settings.file_format = 'PNG'
scene.render.filepath = 'testImage.png'
scene.render.resolution_x = 640
scene.render.resolution_y = 640

# change mesh
path = r"assembly_upsampled.stl"
bpy.ops.import_mesh.stl(filepath=path)
obj.data = bpy.context.object.data
bpy.data.objects.remove(bpy.context.object)

# scale size
max_dim = 2
if obj.dimensions.length:
    obj.scale *= max_dim / max(obj.dimensions)

# render
scene.frame_set(scene.frame_end)
bpy.ops.render.render(write_still = True)
| improve this answer | |
$\endgroup$
  • $\begingroup$ For single mesh could remove meshobs and loop for obj.scale *= max_dim / max(obj.dimensions) still test it has dimension $\endgroup$ – batFINGER Jan 28 at 11:57
  • $\begingroup$ then loop over what? $\endgroup$ – blenderNewbie Jan 28 at 12:06
  • 1
    $\begingroup$ Put that badly,.. no loop required its like mesh_obs = [obj] So instead of looping simply if obj.dimensions.length: (normalize dimension of obj) $\endgroup$ – batFINGER Jan 28 at 12:08
  • $\begingroup$ @batFINGER I edited the answer. Thank you so much for your help! One other question: Is it possible to delete the imported stl after copying the mesh, without deleting my cube and plane? I only know bpy.ops.object.delete() $\endgroup$ – blenderNewbie Jan 28 at 12:15
  • 1
    $\begingroup$ yes. Already used to swap mesh, the imported object ob = context.object then bpy.data.objects.remove(ob) $\endgroup$ – batFINGER Jan 28 at 12:23

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.