简体   繁体   中英

Simple python script with blender

I have been rendering a lot images lately for my work and thought that it might be possible to automate a lot of the rendering if I just knew what to do. This would help me and fellows at work and we would save a lot of time in the long run. This is what I need:

I have an item, let's say a chair. This chair has to be rendered in 4 different camera angles which we have to be able to specify in the code, though these 4 camera angles will stay steady for the entire rendering period. After the 4 images have been rendered, the chair has to change material, and then another 4 images are to be rendered.

With a very little coding experience, I imaging something like this:

Pseudo code:

var material = "oak"; //This is the first material
var camPosition = 0;

while(i<12) {

 for(j=0;j<=4;j++) {
    if(i == 0) {
     camPosition = 20 //Example
    }
    if(i == 1) {
     camPosition = 40 //Example
    }
    ... //Do this 4 times, for the 4 different angles

    TakePhoto AT position = camPosition;
    //Render;
 }
material = "Stone";
i++;
}

So this is basically running two loops, a while loop which changes the material for the chair, and a for loop which iterates over the 4 different angles which it shall then render. I hope I made it clear and that there are some help, advice or suggestions to get.

Thanks in advance.

First you should know some python scripting. Start with the official Python3 tutorial . Stick to python3 tutorials so you don't learn some old ways that will fail with blender's python v3.3.

Then look at how you access blender's data through python. You can start with the official quickstart tutorial . From that page you can also find access to all of blenders python documentation.

Also consider you may find it easier to do the task you describe manually. You can position and keyframe the camera location. You can also animate object visibility, duplicate the object (using Alt D will have them share the same mesh data ) then have one object visible, each with a different material. One press of render animation will then do it all.

A TrackTo constraint can let you position the camera and have it always point at the chair, so you don't have to set angles as well.

You can also get more blender specific help at blender.stackexchange

The following could start you off in what you want to do, once you understand a little python.

import bpy
material_list = ['oak','redwood','plastic']
camera_positions = [[2.0,1.0,1.0],[1.0,2.0,1.0],[2.0,2.0,1.0]]

for mat in material_list:
    bpy.data.objects['Chair'].material_slots[0].material = bpy.data.materials[mat]
    for campos in camera_positions:
        bpy.data.objects['Camera'].location = campos
        bpy.ops.render.render()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM