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

11

我是Blender和Python的新手。我有一个Blender模型(.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

然后我像这样运行它

blender -b marker_a4.blend --python "marker_a4.py" -o //out -F JPEG -x 1 -f 1 

例如,如果我尝试使用参数来运行Python脚本

...
import sys
...
fov = float(sys.argv[5])
...

然后运行它:

blender -b marker_a4.blend --python "marker_a4.py" 80.0 -o //out -F JPEG -x 1 -f 1 

渲染完成了,但是我在开始时收到了这些消息。
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.
...

有人能告诉我这是什么原因吗?我认为blender也将其解析为模型,但不明白为什么。后来我尝试了更复杂的Python参数解析(argparse),但完全没有起作用。所以我想可能在这个层面上发生了一些奇怪的事情。
谢谢!

请在Blender的StackExchange网站上查看相同的问题:http://blender.stackexchange.com/questions/6817 - ideasman42
2个回答

9
我找到了最初寻找的解决方案。
正如Junuxx所说,“你不能直接将命令行参数传递给Python在这种情况下……”,但实际上您可以在另一种情况下传递参数给Python。
所以达到我想要的效果的方法是在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 ) 

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

> blender -b "demo.blend" -P script.py -- 50

感谢我发现的这个链接: http://www.blender.org/forum/viewtopic.php?t=19102&highlight=batch+render 它与批量渲染有关的IT技术有关。请注意保留HTML标记。

当我运行这段代码时,出现了错误,因为RenderSettings类没有file_format字段。看起来它已经被弃用并删除了。有什么解决方法吗? - fdermishin
哦,我已经想出如何做了。file_format现在在ImageFormatSettings中,所以应该是bpy.data.scenes["Scene"].render.image_settings.file_format = 'PNG' - fdermishin
好的!抱歉我错过了你的问题。如果你省略那一行,它会默认保存为PNG格式。但是,如果你需要另一种格式,那就应该走这条路。 - roho
2
float(sys.argv[6]) 改为 float(sys.argv[-1]) 更可靠地获取最后一个参数。 - ideasman42

4
在这种情况下,您无法直接将命令行参数传递给Python,因为它们会被解释为Blender的参数。绕过此问题的方法是设置环境变量,然后调用Blender/Python,如下所示(假设您使用的是Windows - 在其他操作系统上也可能是可能的,但语法不同):
set arg1='foo' & set arg2='bar' & python envvar.py

注意:等号两侧不应有空格!

在名为envvar.py的Python脚本中,您可以使用os.getenv() 来访问这些变量。

import os
print 'arg1 = ', os.getenv('arg1')
print 'arg2 = ', os.getenv('arg2')

输出:

arg1 = 'foo'
arg2 = 'bar'

我实际上在使用Linux,但我也可以在Mac上使用它。这是一个很酷的解决方法,而且有效,但我对于依赖于操作系统的方法并不完全信服。我还考虑使用辅助文本文件。我在这里找到了更多信息(它适用于Blender 2.49,但我认为它很有用)http://www.blender.org/documentation/249PythonDoc/API_related-module.html - roho
是的,从文本文件中读取也可以,但我认为这比这更复杂。 - Junuxx

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接