简体   繁体   English

如何运行带有Blender参数的python脚本?

[英]How to run a python script taking arguments with blender?

I have a script I want to run within blender to generate AO maps (script was given to me and the source guarantees it works). 我有一个脚本,我想在Blender中运行以生成AO映射(脚本是提供给我的,并且源保证它可以工作)。

I try to run the script as follows: 我尝试如下运行脚本:

blender --background --python /opt/ff/product_builder/furniture_builder/generate_ao_maps.py --input_dir /tmp/test.obj --output_dir /tmp/test.png --mode ao

Which produces: 产生:

AL lib: (EE) UpdateDeviceParams: Failed to set 44100hz, got 48000hz instead
found bundled python: /usr/share/blender/2.79/python
Traceback (most recent call last):
  File "/opt/ff/product_builder/furniture_builder/generate_ao_maps.py", line 195, in <module>
    main()
  File "/opt/ff/product_builder/furniture_builder/generate_ao_maps.py", line 178, in main
    args = parse_args()
  File "/opt/ff/product_builder/furniture_builder/generate_ao_maps.py", line 21, in parse_args
    return parser.parse_args(os.getenv(BLENDER_ENV).split(' '))
AttributeError: 'NoneType' object has no attribute 'split'
Error: File format is not supported in file '/tmp/test.obj'

Blender quit

If I run this same script without blender (but with the argument) it tells me: 如果我在没有Blender的情况下运行相同的脚本(但带有参数),它将告诉我:

Traceback (most recent call last):
  File "/opt/ff/product_builder/furniture_builder/generate_ao_maps.py", line 5, in <module>
    import bpy
ImportError: No module named bpy

What do I need to do to pass the parameters to the script and get it working? 我需要怎么做才能将参数传递给脚本并使它正常工作?

You are seeing that error because your script is looking for the environment variable BLENDER_ENV , which is not on your system. 您正在看到该错误,因为脚本正在寻找环境变量BLENDER_ENV ,该变量不在您的系统上。 I don't recognize BLENDER_ENV as a standard Blender related environment variable so it's probable that your friend added BLENDER_ENV to his or her environment. 我不认为BLENDER_ENV是与Blender相关的标准环境变量,因此您的朋友很可能将BLENDER_ENV添加到他或她的环境中。

Firstly, blender processes its cli args in the order they are given, so your example will start in the background, run a script, then set the input_dir... This will most likely not have the result you are after. 首先,blender会按照给定的顺序处理其cli参数 ,因此您的示例将从后台开始,运行脚本,然后设置input_dir ...这很可能不会得到您想要的结果。

The problem with your script failing is that the arg passed to os.getenv() needs to be a string that is the name of a shell environment variable, if you are using bash you need to export the variable to put it into the environment before you start blender. 脚本失败的问题在于,传递给os.getenv()的arg必须是一个字符串,它是shell环境变量的名称,如果您使用的是bash,则需要os.getenv()出该变量以将其放入环境中您开始搅拌器。

export BLENDER_ARGS="arg1 arg2"
blender -b myfile.blend --python myscript.py

If you are using a csh then use setenv BLENDER_ARGS "arg1 arg2" 如果使用的是csh,则使用setenv BLENDER_ARGS "arg1 arg2"

Then in your py script, you use os.getenv('BLENDER_ARGS').split(' ') 然后在py脚本中,使用os.getenv('BLENDER_ARGS').split(' ')

Note that each shell instance is a separate environment, you need to set the variables in the same instance that starts blender. 请注意,每个外壳程序实例是一个单独的环境,您需要在启动Blender的同一实例中设置变量。

You may also be interested in passing cli arguments to the script as explained in response to this question . 您可能也有兴趣将cli参数传递给脚本,如针对此问题的解释所述。

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

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