Integrating Blender into scripting languages e.g. Python, Matlab

I want to be able to call Blender from another environment which is capable of handling images. Ideally, I would like to think of my call to Blender as being not-so-different from my call to any other function. e.g. I would like to be able to write a wrapper that would give me a syntax like: image = Blender(args). Which would mean “call blender with args and return the rendered image”.

I know several ways to get the arguments into Blender and I know how to get it to render images; I am not asking about either of those. I want to know: Is there any way to get it to return the rendered image? For now, in my wrapper implementation I use the clunky method of having Blender save the image in a particular temporary file from which my script then loads it. I want to know if there is a better way to accomplish the same goal.

Separately from that question, another feature that would be extremely useful would be to have some way for the external script to start a Blender session and then keep it running and waiting for additional commands. That way it could render multiple images without having to start and stop Blender each time.

I suspect that neither of these are possible at this time… But I wonder if anyone else has any thoughts or workarounds.?

render data.
bpy.data.images[‘Render Result’].
http://www.blender.org/documentation/blender_python_api_2_66a_release/bpy.types.Image.html

this executes after render.
bpy.app.handlers.render_post
http://www.blender.org/documentation/blender_python_api_2_66a_release/bpy.app.handlers.html

‘keep it running and waiting for additional commands’
dont know, though they did lots of stuff like this with the network render.

I should be a bit more clear. For the case that I’m interested in, there is a script running externally to Blender. Let’s say it is in Matlab so we don’t get confused by the fact that I’m calling Blender with a Python script. I can call Blender from Matlab using:

!blender -b file.blend -P render.py

The only way I know how to get the result of the render out of this and back into Matlab is to have render.py save the image someplace where Matlab can then load it back in.

Maybe what I am really looking for is a matlab2python interface. Googling a bit just now, I find http://algoholic.eu/matpy/. Which maybe could work. Does anyone know a better way?

sorry. i dont know much about this. feel odd posting.

matlab:
matpy looks like one option.
i found: ‘mlabwrap’ and https://github.com/jaderberg/python-matlab-bridge
from: http://stackoverflow.com/questions/2255942/how-do-i-interact-with-matlab-from-python

blender:
i found some examples in: background_job.py
adding commands + arguments.

guess you want to send the render result data directly to matlab via python.

Ideasman has made a build of Blender compiled as a Python module:

http://wiki.blender.org/index.php/User:Ideasman42/BlenderAsPyModule

However, this is highly experimental, and many Blender features won’t work.

Also, I see no need to use Matlab for anything unless you use Simulink or have lots of legacy code. With numpy/scipy/matplotlib you can do the same things using a much saner language.

I don’t know why use matlab,but for high performance (python is a fail) so I use C++ ,compile it as .dll ,then call its functions from ctypes module inside python , this is pretty fast and C++ got everything you may do inside matlab

That’s exactly how numpy and scipy do it too…

Edit: well, not using ctypes, but you get the idea. Use C/C++/Fortran libraries for speed-critical computations and control them through a Python interface.

You are correct of course, I wish I could just use Python primarily. Though I will admit, Matlab has been my primary language for years now. I can still do almost everything significantly faster in Matlab than I can with Scipy, but that’s mostly because I’ve never really had the opportunity to do a larger project in Python. Right now I am working with a lot of people who also only use Matlab so switching isn’t even an option.

Thanks for telling me about Ideasman’s Blender Python module. That sounds like it would solve my problem if it works well enough. I’ll post here again after I look in more detail.


By the way, Dirty-stick (and whoever else may be listening), I realized that some of the options for matlab2python interfaces may not work with Python 3. So I became less enthusiastic about them. I know that pymex https://github.com/kw/pymex does not support Python 3. I am not sure about matpy yet; though the author told me that it should be fine as long as the Python C API has not changed (and I don’t know whether it has or not).