Running Blender python script outside of blender
Asked Answered
C

4

11

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.

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. An important constraint is, that I want to render on a headless machine, so I can't use Blender's internal console.

My question: Is it possible to use Blender's api from an external console to get POV-Ray-like behaviour?


After reading George Profenza's answer I did further research and found Don’t Use Blender! in Blender's documentation.

In contrast to my thoughts, one has to create a python script which is executed by blender in background mode.

./blender --background --python myscript.py
Cholecalciferol answered 28/2, 2013 at 13:31 Comment(0)
N
8

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. 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.

Alternatively you could generate content straight for a renderer (and skip a 3D Editor/Blender completely). There are quite a few free renderers out there like:

  1. Yafaray
  2. Sunflow
  3. Mitsuba
  4. LuxRender
  5. Pixie

yafaray

YAFARAY

sunflow

SUNFLOW

luxRender LUX

pixie

PIXIE

Numeral answered 28/2, 2013 at 13:51 Comment(4)
I know this is an old thread, but do you have recommendations for generating the content directly? Right now I'm in the middle of trying out cgkit (a python library to generate RenderMan files), but was wondering if there might be a better approach. ThanksRabblement
Are you talking about creating generative models ? If so, it should be a matter of using the Blender Python API to add vertices to faces/faces to meshes and/or modify these meshes. Didn't know about cgkit, thanks for the heads up, that looks interesting. Having a quick look at the docs, you can generate a custom triangle mesh with cgkit. It's hard to say what the better approach would be in your scenario...Numeral
...you can use both cgkit and Blender to generate meshes. You have a few more modifiers and tools in Blender, but in the end I'd go with the simplest solution (not necessarily the most feature rich one): what generates your desired output the simplest and still supports the inputs you need (parameters for generating the geometry I assume). HTHNumeral
I'm using commandline blender to render 1 scene (same camera setting) with different textures (say 100 texture file), but it takes about 2s to render out 1 PNG file. Could you please suggest a faster tool to do so?Incidentally
V
5

There are two options,

  • The first which you have come across is which is to run Blender in background mode,
  • The second, which is very experimental, using Blender as a python module - 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

Vshaped answered 28/2, 2013 at 17:2 Comment(1)
I will stay with the first option. But thanks for pointing out the import_scene operator. This could be very helpful.Cholecalciferol
U
1

Blender can be run "headless" inside a Docker container. Check the Dockerfile here

In case you are not familiar with Docker, download the Dockerfile and run:

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

Now you can run your scripts inside a headless Blender

Umeko answered 12/6, 2020 at 5:44 Comment(0)
M
0

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. For mac I use 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.

        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.

Morrell answered 27/6, 2022 at 14:19 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.