3

I want to add music via python. I tried:

bpy.ops.sequencer.sound_strip_add(bpy.ops.sequencer.sound_strip_add(filepath="//mymusic.mp3", frame_start=0, channel=1)

but somehow it gives me the error:

drop file blabla
Traceback (most recent call last):
  File "blabla", line 60, in <module>
  File "C:\Program Files\Blender Foundation\Blender\2.77\scripts\modules\bpy\ops.py", line 189, in __call__
    ret = op_call(self.idname_py(), None, kw)
RuntimeError: Operator bpy.ops.sequencer.sound_strip_add.poll() failed, context is incorrect

Error: Python script fail, look in the console for now...

Any ideas?

Ray Mairlot
  • 29,192
  • 11
  • 103
  • 125
froggyman
  • 503
  • 5
  • 16
  • 1
    Have a look at http://blender.stackexchange.com/questions/6101/poll-failed-context-incorrect-example-bpy-ops-view3d-background-image-add In this case you need to be in the 'SEQUENCE_EDITOR' space. – batFINGER Sep 27 '16 at 18:43
  • since i dont really want to go there and just add the sound is there no workaround? or can i switch to the editor space with python and then back? – froggyman Sep 27 '16 at 18:44
  • scene.sequence_editor.sequences.new_sound(name, filepath, channel, frame_start) – batFINGER Sep 27 '16 at 18:47
  • NameError: name 'scene' is not defined – froggyman Sep 27 '16 at 18:50
  • related: http://blender.stackexchange.com/questions/62163/extract-audio-to-file-from-movie-clip-with-blender/62858#62858 – zeffii Sep 28 '16 at 07:05

1 Answers1

5
import bpy
from bpy import context

scene = context.scene 

if not scene.sequence_editor:
    scene.sequence_editor_create()

#Sequences.new_sound(name, filepath, channel, frame_start)    
soundstrip = scene.sequence_editor.sequences.new_sound("toad", "~/toad_sound.mp3", 3, 1)
batFINGER
  • 84,216
  • 10
  • 108
  • 233