1

I've built Blender as a bpy-only module meaning I can call it directly from Python. I was able to run some commands, but at the moment is failing with simply opening a main file.

My code is super simple:

@app.route('/')
def hello():
   bpy.context.scene.render.engine = 'CYCLES' 
   bpy.ops.wm.open_mainfile(filepath='/home/yuranos/development/3D/model_all_parts.blend')

And yet I'm failing when trying to open my file:

Process finished with exit code 139 (interrupted by signal 11: SIGSEGV)

The interesting bit is that the same python version with the same bpy-related code runs perfectly well from console. I'm new to Python and don't understand if it's a problem with the way I use bpy from Flask or with bpy itself.

yuranos
  • 141
  • 5
  • You cannot just run bpy outside of Blender, it's an API. It needs to run in Blender. You can start Blender, open a file and run a script. This might be helpfull: https://blender.stackexchange.com/questions/1365/how-can-i-run-blender-from-command-line-or-a-python-script-without-opening-a-gui – Martynas Žiemys May 21 '20 at 08:20
  • Also see this: https://docs.blender.org/manual/en/latest/advanced/command_line/arguments.html – Martynas Žiemys May 21 '20 at 08:22
  • You'd be surprised, @MartynasŽiemys, but you definitely can. And if you read my question till the end you'd see that I do run it in bash console as bpy module. You just need to compile the module manually, which is what I did. And it actually works for some commands, but fails for others. – yuranos May 21 '20 at 17:20
  • Also see: https://blender.stackexchange.com/questions/117200/how-to-build-blender-as-a-python-module/178746 – yuranos May 21 '20 at 17:22
  • 1
    IMO Could be made clearer in question. Picked it as bpy module re your previous question (have same issue re draco) An observation, setting scene render to cycles then opening mainfile will set render to whatever the context is of the new file's context scene won't it?. – batFINGER May 21 '20 at 17:58
  • Ok, guys, duly noted. I added a bit more text. – yuranos May 21 '20 at 18:01
  • Not that much surprised, to be honest since I linked to a question with an answer that mentions running as module... – Martynas Žiemys May 22 '20 at 01:04
  • @MartynasŽiemys, I saw that question a few times and both the question and the answers in it can be a bit misleading. It's about running Blender headless, but still starting it as a separate process you have little control over, while if you build it as a module, you use it as any other library without explicitly spawning processes and with an ability to manipulate things via methods and variables as if in Blender native console. – yuranos May 22 '20 at 05:41

1 Answers1

1

Haven't done much with flask, will offer this up as a suggested workaround.

Try loading the mainfile outside of @route decorated methods.

Putting the load operator in any route method also caused a segfault for me (Ubuntu 18, blender 2.90, python 3.82) Suspect a threading issue.

The following works as expected,

# batTOOLS...........

from flask import Flask
import bpy

app = Flask(__name__)
bpy.ops.wm.open_mainfile(filepath="/home/batfinger/Documents/blender/tests/BISECTO.blend")


@app.route('/')
def hello_world():
    lines = []
    for o in bpy.data.objects:
        lines.append(f"{o.name}</br>")

    return "\n".join(lines)
batFINGER
  • 84,216
  • 10
  • 108
  • 233
  • It does work, but I'm still to figure out if it's about threading or something else. We tried to disable threading(https://devtalk.blender.org/t/segmentation-fault-when-running-custom-built-bpy/11284/12?u=yuranos) and in itself it's not enough because there's another strange thing: code with app.run(threaded=False) runs Ok when I start the app as: python3.7 app.py, but it fails when I run it as: flask run. I'm new to both Python and Flask, been a JVM fun boy for a decade, so will need to research. – yuranos May 22 '20 at 05:36