Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

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
like image 868
Stefan Avatar asked Feb 28 '13 13:02

Stefan


People also ask

How do I autorun a python script in blender?

In the Preferences, there is the toggle to Auto Run Python Scripts. This means the Trusted Source option in the File Browser will be enabled by default, and scripts can run when blend-files are loaded without using the File Browser.

How do you use Bpy outside a blender?

You can start it with cli options to run in the background (without gui) and to run a python script. You can also start rendering from the cli without a script. The other option to get access to bpy without a normal blender install means you will need to compile your own copy of blender as a pymodule.

How do I run a Python script from the command line in blender?

Command-line / subprocess You can use subprocess to run blender (like any other application) from python. Use the -b / --background switch to run blender in the backgroud (GUI-less). Use the -P <filename> / --python <filename> switch to load desired python script. Or use --python-console to run python from stdin.


2 Answers

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

like image 99
George Profenza Avatar answered Sep 21 '22 11:09

George Profenza


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

like image 23
neomonkeus Avatar answered Sep 19 '22 11:09

neomonkeus