KaiKostack / bullet-constraints-builder

Add-on for Blender to connect rigid bodies via constraints in a physical plausible way. (You only need the ZIP file for installation in Blender. Click the filename and at the next page "Download", right click and "Save As" won't work!)
https://inachuslaurea.wordpress.com
GNU General Public License v2.0
135 stars 21 forks source link

bcb.get_config() crashes when run as script #20

Closed ColinMoldenhauer closed 8 months ago

ColinMoldenhauer commented 8 months ago

Dear Kai,

I am new to Blender and scripting, so this might as well be a general Blender thing, but since the affected method belongs to BCB, I thought I'd post here.

When I run the command bpy.ops.bcb.get_config() within the Blender Python console, it works like expected, i.e. the Element Group List is loaded correctly from file.
However, when creating this simple Python script mwe.py

import bpy
bpy.ops.bcb.get_config()

and calling it with blender --background "MY_BLEND_FILE.blend" --python mwe.py will produce the following error:

Traceback (most recent call last):
  File "<string>", line 1, in <module>
  File "C:\Users\...\mwe.py", line 3, in <module>
    bpy.ops.bcb.get_config()
  File "C:\Users\...\Blender 2.79\2.79\scripts\modules\bpy\ops.py", line 189, in __call__
    ret = op_call(self.idname_py(), None, kw)
RuntimeError: Error: Traceback (most recent call last):
  File "C:\Users\...\Blender 2.79\2.79\scripts\addons\kk_bullet_constraints_builder\gui_buttons.py", line 72, in execute
    warning = getConfigDataFromScene(scene)
  File "C:\Users\...\Blender 2.79\2.79\scripts\addons\kk_bullet_constraints_builder\build_data.py", line 409, in getConfigDataFromScene
    elemGrps = mem["elemGrps"]
KeyError: 'elemGrps'

location: C:\Users\...\Blender 2.79\2.79\scripts\modules\bpy\ops.py:189

Any idea why the command only works from within the Blender UI?

KaiKostack commented 8 months ago

Hi Colin, thanks for the report. This is a bug that I had already fixed internally, but it was not committed to the public repository. I have now done this, please update the BCB and try again.

ColinMoldenhauer commented 8 months ago

Thanks a lot!

ColinMoldenhauer commented 8 months ago

Slightly off-topic, but do you by any chance know how to get the Build FM operation to work when running Blender in the background? I am running bpy.ops.bcb.export_ascii_fm() in my custom Python script, but when saving the file, the changes applied to the model through the simulation are not saved. Does it have to do with the animation not being played in background mode?

ColinMoldenhauer commented 8 months ago

NVM, after hours of googling I found a way :)

KaiKostack commented 8 months ago

Try this:

import bpy

# Get config
bpy.ops.bcb.get_config()

# Build
bpy.ops.bcb.export_ascii_fm()

# Simulate frame by frame through the timeline
scene = bpy.context.scene
for frame in range(scene.frame_start, scene.frame_end +1):
    scene.frame_current = frame
    bpy.context.screen.scene = scene  # Scene update invokes BCB monitor event handler

Indeed, Blender doesn't seem to support animation playback in background mode, but this workaround should do the trick. I also updated the BCB to suppress some irrelevant warnings that only appear in background mode for some reason.