简体   繁体   English

在搅拌机之外运行 Blender python 脚本

[英]Running Blender python script outside of blender

I want to visualize some scientific data, which is, at the moment, only an animation of some spheres with different colours and sizes.我想可视化一些科学数据,目前只是一些不同颜色和大小的球体的动画。

I already created a script within Mathematica which creates the input for POV-Ray.我已经在 Mathematica 中创建了一个为 POV-Ray 创建输入的脚本。

My problem is, that I'm not satisfied with the quality of POV-Ray's results and wanted to write a Python script, which could set up the corresponding blender scenes and render them.我的问题是,我对 POV-Ray 的结果质量不满意,想编写一个 Python 脚本,它可以设置相应的搅拌机场景并渲染它们。 An important constraint is, that I want to render on a headless machine, so I can't use Blender's internal console.一个重要的限制是,我想在无头机器上渲染,所以我不能使用 Blender 的内部控制台。

My question: Is it possible to use Blender's api from an external console to get POV-Ray-like behaviour?我的问题:是否可以从外部控制台使用 Blender 的 API 来获得类似 POV-Ray 的行为?


After reading George Profenza's answer I did further research and found Don't Use Blender!阅读 George Profenza 的回答后,我做了进一步的研究,发现不要使用 Blender! in Blender's documentation.在 Blender 的文档中。

In contrast to my thoughts, one has to create a python script which is executed by blender in background mode.与我的想法相反,必须创建一个由blender在后台模式下执行的python脚本。

./blender --background --python myscript.py

As far as I remember you can run Blender from the command line without opening the interface/windowing system, which I hope works for your setup.据我所知,您可以在不打开界面/窗口系统的情况下从命令行运行 Blender,我希望这对您的设置有用。 Not only you can tell Blender to render a document, but you can also run a script that generates that document/populates the scene with geometry, lights, etc.您不仅可以告诉 Blender 渲染一个文档,还可以运行一个脚本来生成该文档/使用几何图形、灯光等填充场景。

Alternatively you could generate content straight for a renderer (and skip a 3D Editor/Blender completely).或者,您可以直接为渲染器生成内容(并完全跳过 3D 编辑器/搅拌器)。 There are quite a few free renderers out there like:那里有很多免费的渲染器,例如:

  1. Yafaray亚法瑞
  2. Sunflow向日葵
  3. Mitsuba三叶
  4. LuxRender力士渲染
  5. Pixie小精灵

yafaray亚法瑞

雅法瑞

sunflow向日葵

向日葵

luxRender勒克斯渲染勒克斯

pixie小精灵

小精灵

There are two options,有两种选择,

  • The first which you have come across is which is to run Blender in background mode,您遇到的第一个是在后台模式下运行 Blender,
  • The second, which is very experimental, using Blender as a python module - BlenderAsPyModule第二个是非常实验性的,使用 Blender 作为 python 模块 - BlenderAsPyModule

In both cases this should be a simple operation to import the Mathematica scene - bpy.ops.import_scene.* Then render the scene bpy.ops.render.render在这两种情况下,这应该是导入 Mathematica 场景的简单操作 - bpy.ops.import_scene.*然后渲染场景bpy.ops.render.render

Blender can be run "headless" inside a Docker container. Blender 可以在 Docker 容器内“无头”运行。 Check the Dockerfile here此处检查 Dockerfile

In case you are not familiar with Docker, download the Dockerfile and run:如果您不熟悉 Docker,请下载 Dockerfile 并运行:

docker build -t blender .
docker run -it -v your_folder/:/scripts bash

Now you can run your scripts inside a headless Blender现在您可以在无头 Blender 中运行您的脚本

While the question has been answered, there may be slightly more to a headless render than has been mentioned above.虽然已经回答了这个问题,但无头渲染可能比上面提到的要多一些。

Ensure you have Blender downloaded from the command line.确保您从命令行下载了 Blender。 For mac I use brew对于 mac,我使用 brew

$ brew install blender

Then you can run然后你可以运行

$ blender --background --python <your_script>.py

However, it is important to note that because the script has run headlessly, if you are rendering you will need to save the output within the python script.但是,重要的是要注意,因为脚本是无头运行的,所以如果要渲染,则需要将输出保存在 python 脚本中。

        working_dir = getcwd()
        bpy.context.scene.render.filepath = (working_dir + "/output/render")
        bpy.context.scene.cycles.device = 'GPU'
        bpy.ops.render.render('INVOKE_DEFAULT', write_still=True)

Hope this helps.希望这可以帮助。

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

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