0

I'm working on an audio visualizer mostly from scratch. I'm trying to override the context so I can run bpy.ops.graph.sound_bake() but for some reason I can't get the context right. The code is below, as well as the stack trace. What am I doing wrong?

Code:

for window in bpy.context.window_manager.windows:
    screen = window.screen
    for area in screen.areas:
        if area.type == 'VIEW_3D':
            area_type = area.type
            area_ui_type = area.ui_type
            area.type = 'GRAPH_EDITOR'
            area.ui_type = 'FCURVES'
            with bpy.context.temp_override(window=window, area=area):
                bpy.ops.graph.sound_bake(filepath=file)
            area.type = area_type
            area.ui_type = area_ui_type

Stack trace:

Traceback (most recent call last):                                                                                                                                                                               File "E:\Git\music\blender.py", line 348, in <module>
main(music_file)
File "E:\Git\music\blender.py", line 336, in main
create_centerpiece(file, 16)
File "E:\Git\music\blender.py", line 319, in create_centerpiece
bpy.ops.graph.sound_bake(filepath=file)
File "C:\Users\devin gardner\AppData\Local\Programs\Python\Python310\lib\site-packages\bpy\3.6\scripts\modules\bpy\ops.py", line 113, in __call__
ret = _op_call(self.idname_py(), None, kw)
RuntimeError: Operator bpy.ops.graph.sound_bake.poll() failed, context is incorrect    

Added sample .blend file:

1 Answers1

0

Since I could not find any animations or fcurves in your blend file, I added a random animation for 1 object so I could generate an fcurve just for demonstration purposes. Then you have to select an fcurve in the Graph Editor before you execute the script.

enter image description here

So basically you were not in the right context because nothing was selected. That is evident in that you can see that the command for bpy.ops.graph.sound_bake is grayed out in the menu until you select an fcurve.

enter image description here

Harry McKenzie
  • 10,995
  • 8
  • 23
  • 51
  • I see. In that case, I'll need to figure out how to programmatically generate an fcurve and then select it before I try to bake the sound. I'm doing all of this from a Python script, instead of inside Blender. – Devin Gardner Aug 26 '23 at 18:50
  • @DevinGardner Yes it's easy to select an fcurve via python. Like for example (assuming the 'Plane' object had animation data), you can use bpy.data.objects['Plane'].animation_data.action.fcurves[0].select = True. Put that line before you execute the script then it will also work. So no need to manually select an fcurve. You just have to make sure you select the right fcurve. – Harry McKenzie Aug 27 '23 at 03:18