0
$\begingroup$

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:

$\endgroup$
6
  • $\begingroup$ blender.stackexchange.com/questions/6101/… $\endgroup$
    – Harry McKenzie
    Commented Aug 26, 2023 at 17:51
  • $\begingroup$ I appreciate the attempt, but this link hasn't helped me so far. It's one of the few I keep revisiting and I've correctly change the area's type and ui_type to no avail. $\endgroup$ Commented Aug 26, 2023 at 17:54
  • $\begingroup$ can you share your blend file? or a simple setup that reproduces the problem blend-exchange.com $\endgroup$
    – Harry McKenzie
    Commented Aug 26, 2023 at 18:08
  • $\begingroup$ Added it in an edit. This is what I'm able to generate in Python so far. $\endgroup$ Commented Aug 26, 2023 at 18:13
  • $\begingroup$ what is the file in bpy.ops.graph.sound_bake(filepath=file) it's not defined in your code. $\endgroup$
    – Harry McKenzie
    Commented Aug 26, 2023 at 18:18

1 Answer 1

0
$\begingroup$

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

$\endgroup$
2
  • $\begingroup$ 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. $\endgroup$ Commented Aug 26, 2023 at 18:50
  • $\begingroup$ @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. $\endgroup$
    – Harry McKenzie
    Commented Aug 27, 2023 at 3:18

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .