简体   繁体   English

带有搅拌器的简单python脚本

[英]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. 必须在代码中指定这4个不同的摄像机角度来渲染此椅子,尽管这4个摄像机角度在整个渲染期间都将保持稳定。 After the 4 images have been rendered, the chair has to change material, and then another 4 images are to be rendered. 渲染完4张图像后,椅子必须更换材质,然后再渲染另外4张图像。

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. 因此,这基本上是运行两个循环,一个while循环会更改椅子的材质,一个for循环会在4个不同的角度上进行迭代,然后再渲染该角度。 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. 首先,您应该了解一些python脚本。 Start with the official Python3 tutorial . 从官方Python3教程开始 Stick to python3 tutorials so you don't learn some old ways that will fail with blender's python v3.3. 坚持使用python3教程,这样您就不会学到搅拌器的python v3.3失败的一些旧方法。

Then look at how you access blender's data through python. 然后看看如何通过python访问Blender的数据。 You can start with the official quickstart tutorial . 您可以从官方快速入门教程开始 From that page you can also find access to all of blenders python documentation. 在该页面上,您还可以访问所有Blenders python文档。

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. 您还可以设置对象可见性的动画,复制对象(使用Alt D将使它们共享相同的网格数据 ),然后使一个对象可见,每个对象使用不同的材质。 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. TrackTo约束可以让您放置摄像头并使其始终指向椅子,因此您也不必设置角度。

You can also get more blender specific help at blender.stackexchange 您还可以在blender.stackexchange获得更多特定于Blender的帮助

The following could start you off in what you want to do, once you understand a little python. 一旦您了解了一些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()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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