1

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?

  • 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. – batFINGER Jan 28 '20 at 11:21
  • @batFINGER I don't care about the materials so this would be fine. How do I import it? – blenderNewbie Jan 28 '20 at 11:33
  • https://blender.stackexchange.com/questions/127883/import-stl-and-render-image – batFINGER Jan 28 '20 at 11:37
  • 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? – blenderNewbie Jan 28 '20 at 11:46
  • https://blender.stackexchange.com/questions/160976/how-can-i-normalize-the-scale-of-what-seems-to-be-either-an-infinitely-large-o – batFINGER Jan 28 '20 at 11:47

1 Answers1

1

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)
  • For single mesh could remove meshobs and loop for obj.scale *= max_dim / max(obj.dimensions) still test it has dimension – batFINGER Jan 28 '20 at 11:57
  • then loop over what? – blenderNewbie Jan 28 '20 at 12:06
  • 1
    Put that badly,.. no loop required its like mesh_obs = [obj] So instead of looping simply if obj.dimensions.length: (normalize dimension of obj) – batFINGER Jan 28 '20 at 12:08
  • @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() – blenderNewbie Jan 28 '20 at 12:15
  • @batFINGER Do you mean bpy.data.objects.remove(obj)? But how do I know the obj. import_mesh returns a set, right? – blenderNewbie Jan 28 '20 at 12:22
  • 1
    yes. Already used to swap mesh, the imported object ob = context.object then bpy.data.objects.remove(ob) – batFINGER Jan 28 '20 at 12:23