带有参数的Python脚本,用于命令行Blender

roh*_*oho 10 python rendering batch-file blender-2.50

我是blender和python的新手.我有一个搅拌器模型(.blend),我想批量渲染为几个图像,为每个图像提供一些属性.

我用这些参数编写了一个python脚本,如:

import bpy

pi = 3.14159265
fov = 50

scene = bpy.data.scenes["Scene"]

# Set render resolution
scene.render.resolution_x = 480
scene.render.resolution_y = 359

# Set camera fov in degrees
scene.camera.data.angle = fov*(pi/180.0)

# Set camera rotation in euler angles
scene.camera.rotation_mode = 'XYZ'
scene.camera.rotation_euler[0] = 0.0*(pi/180.0)
scene.camera.rotation_euler[1] = 0.0*(pi/180.0)
scene.camera.rotation_euler[2] = -30.0*(pi/180.0)

# Set camera translation
scene.camera.location.x = 0.0
scene.camera.location.y = 0.0
scene.camera.location.z = 80.0
Run Code Online (Sandbox Code Playgroud)

那么我就像运行它一样

blender -b marker_a4.blend --python "marker_a4.py" -o //out -F JPEG -x 1 -f 1 
Run Code Online (Sandbox Code Playgroud)

然后,例如,如果我尝试使用python脚本的参数

...
import sys
...
fov = float(sys.argv[5])
...
Run Code Online (Sandbox Code Playgroud)

并运行它:

blender -b marker_a4.blend --python "marker_a4.py" 80.0 -o //out -F JPEG -x 1 -f 1 
Run Code Online (Sandbox Code Playgroud)

渲染完成但我在开始时收到此消息.

read blend: /home/roho/workspace/encuadro/renders/marker/model/marker_a4.blend
read blend: /home/roho/workspace/encuadro/renders/marker/model/80.0
Unable to open "/home/roho/workspace/encuadro/renders/marker/model/80.0": No such file or directory.
...
Run Code Online (Sandbox Code Playgroud)

有谁能告诉我这是什么造成的?我认为blender也将其解析为模型,但不明白为什么.我后来尝试了一些更加软化的东西,用于在python(argparse)中解析参数,但它根本不起作用.所以我在想这个级别可能会发生一些奇怪的事情.

谢谢!

roh*_*oho 8

我找到了我正在寻找的解决方案.

正如Junuxx所说:"在这种情况下你无法直接将命令行参数传递给python ......"但你实际上可以将参数传递给python,但在另一种情况下.

因此,我想要的方法是RENDER并直接保存在python脚本中

import sys

fov = float(sys.argv[-1])   
...
# Set Scenes camera and output filename 
bpy.data.scenes["Scene"].render.file_format = 'PNG'
bpy.data.scenes["Scene"].render.filepath = '//out'

# Render Scene and store the scene 
bpy.ops.render.render( write_still=True ) 
Run Code Online (Sandbox Code Playgroud)

--python选项(或-P)必须在最后,您可以使用 - 指定参数,只需加载模型并运行脚本.

> blender -b "demo.blend" -P script.py -- 50
Run Code Online (Sandbox Code Playgroud)

相信这个链接,我发现:http: //www.blender.org/forum/viewtopic.php?t = 19102&highlight = batch +render

  • `float(sys.argv [6])`最好写成`float(sys.argv [-1])`来可靠地得到最后一个arg. (2认同)