0

I have written several functions in blender scripting mode. I need to pass their variables through Matlab thanks to the py command (Matlab - Python connection). The problem is that these functions work only in Blender (I have written a simple example below). I was wondering if there is a way to call blender in each python function in order to execute them (Python - Blender connection).

I have already read about the possibility to call blender from the script thanks to the "subprocess" function, but I have not understood very well how to do it.

A simple function can be:

def monkey(x_coord, y_coord, z_coord):
    import bpy
    bpy.ops.mesh.primitive_monkey_add(size = 2, enter_editmode= False, align = 'WORLD', location = (x_coord, y_coord, z_coord))

How can I modify it in order to run blender everytime the function is called from matlab?

Maybe it can be understand better by looking to what happen in matlab.

I write my function:

enter image description here

but when I call it from the command window the result is an error:

enter image description here

I think this is due to the fact the the bpy module is only defined in Blender. I was wondering if there was a command to place before import bpy to make Blender run and so, avoid the error.

  • 1
    I think what you want is the other way around: Calling a blender command in matlab to eg. get the geometry of the monkey in matlab right? ...You can run blender using the command line https://docs.blender.org/manual/en/latest/advanced/command_line/arguments.html which allows to subprocess.call() it by python or any other language. – brockmann Aug 21 '20 at 09:28
  • I need matlab just to give the input variables, in my example, the coordinates x,y and z. So everything should start in matlab. My idea was to call the python function from matlab (that is easily done), but the function needs Blender to execute, and here is my problem. The results of my functions are simply rendered images, that at the end I will upload in matlab. – Carmine Buonagura Aug 21 '20 at 10:18
  • 1
    Still don't get it. Please take your time and add any minimal example and all relevant details to your question, thanks. – brockmann Aug 21 '20 at 10:23
  • I've tried to write something else to understand better the problem – Carmine Buonagura Aug 21 '20 at 14:43
  • Right, bpy module is only available in blender so either (again) call blender in matlab using subprocess blender -b your_scene.blend -P your_pyfile.py or compile blender as submodule: https://wiki.blender.org/wiki/Building_Blender/Other/BlenderAsPyModule – brockmann Aug 22 '20 at 01:03

0 Answers0