1
$\begingroup$

I am new to blender but wonder if it is possible to programmatically position the viewport and then render images of each visible component.

Pseudo code below:

model = load("my_model.blend")
textures = ["stone", "wood", "glass"]
view_port.set_position(100,200,300)
view_port.set_rotation(-42,0)
for each component in model do:
  for each texture in textures do:
    component.set_texture(texture)
    component.save_png()

Can something like that be accomplished with blender and if yes, how?

$\endgroup$

1 Answer 1

3
$\begingroup$

Your question is too broad for a complete answer in one place. Below I outline the solution and suggest areas of research for specific steps. Please try to put together a complete script on your own and then come back with specific questions about areas where you are stuck.

Yes it is programmatically possible. I assume by "position the viewport" you mean "position the camera". Here's a step by step commentary on your pseudocode. With a little additional research you should be able to piece together the script you need.

model = load("my_model.blend")

Your model can be stored in a separate Blend file in one of two ways. You'll have to decide between

  • library linking; or
  • the new asset library

For now I recommend library linking. There are several questions with good answers about how to gather a model from library by linking

textures = ["stone", "wood", "glass"]

You've forgotten to mention how the materials that these textures name will be created. You could place them in a separate blend file; you could create them manually; or you could create them using Python. There are several questions with good answers about creating materials and modifying the material node trees to get what you want. But in your case I would recommend storing them in a separate blend file and then linking them, just as with the model.

view_port.set_position(100,200,300)
view_port.set_rotation(-42,0)

I've treated the above lines together. You want to create a camera, using code like this:

camera_data = bpy.data.cameras.new(name="Camera")
my_camera = bpy.data.objects.new("Camera", camera_data)
bpy.context.scene.collection.objects.link(my_camera)

and then move it using the transform operations, as you would with any object. There are a lot of questions about how to use Python to move objects.

for each component in model do:

Let's assume you've placed your model's components in a specific collection. This becomes for each object in bpy.data.collections[MODEL_COLLECTION_NAME_HERE].objects

  for each texture in textures do:

This becomes for texture in textures:

    component.set_texture(texture)

This becomes object.material_slots[0] = bpy.data.materials[texture]

    component.save_png()

This requires some setup. You'll need to specify render properties like which render engine, render resolution, et cetera. Again, you can find questions here discussing each of these.

After the setup comes the render: bpy.ops.render... See the manual page for render for details.

Having rendered the model, you need to save the render file. Look for questions about saving images for specifics.

$\endgroup$
3
  • 1
    $\begingroup$ That is an awesome answer. I was hoping to learn if it's possible and a few links. You have given me almost an entire guide. Thank you very much!!! $\endgroup$
    – RndmSymbl
    Commented May 1, 2022 at 16:00
  • $\begingroup$ @RndmSymbl Thank you very much. I hope you won't have too much struggle with finding what you need to write your script. $\endgroup$ Commented May 1, 2022 at 19:45
  • 1
    $\begingroup$ It will probably take a while but with confidence that it can be done I will eventually get there :) $\endgroup$
    – RndmSymbl
    Commented May 3, 2022 at 6:52

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .