# Insert video to sequence editor with python Script

i want to create a button for insert a video to video sequence editor. i have written a script :

import bpy, os

class insertVideo(bpy.types.Operator):
bl_idname = "scene.invideo"
bl_label = "Insert Video"

def execute(self, context):
episode_path = "F:\\Dokumen\\Target\\Develop Apps\\addon_blender\\tes"

class MyPanel(bpy.types.Panel):
"""Creates a Panel in the Object properties window"""
bl_label = "My Tools"
bl_idname = "my.panel"
bl_space_type = 'SEQUENCE_EDITOR'
bl_region_type = 'UI'

def draw(self, context):
layout = self.layout

obj = context.object

row = layout.row()
row.label(text="Movie File")
row = layout.row()
row.operator("scene.invideo", text="Insert Video")

def register():
bpy.utils.register_class(MyPanel)
bpy.utils.register_class(insertVideo)

def unregister():
bpy.utils.unregister_class(MyPanel)
bpy.utils.unregister_class(insertVideo)

if __name__ == "__main__":
register()


In episode_path = "F:\\Dokumen\\Target\\Develop Apps\\addon_blender\\tes"

I have 3 videos :

1. file1.mov
2. file2.mov
3. file3.mov

I hope, when i click this button :

It will insert all videos from episode_path. But, i have a problem. When i click the button, it always show error :

Maybe you can help me, What did i miss? ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

@cmomoney @sambler Thanks for your answer guys. Finally, i can finish my first addon in blender. I add this in execute function :

def execute(self, context):
episode_path = bpy.path.abspath("F:\\Dokumen\\Target\\Develop Apps\\addon_blender\\tes")
episode_list = os.listdir(episode_path)
episode_sort = sorted(episode_list)
first_frame=0
last_frame=0
for v in [f for f in episode_sort if f.endswith('.mov')]:
movs = os.path.join(episode_path,v)
start_frame = last_frame+first_frame
first_frame = bpy.context.scene.sequence_editor.sequences_all[v].frame_start
last_frame = bpy.context.scene.sequence_editor.sequences_all[v].frame_final_duration
return{'FINISHED'}


Thanks for your help guys

When invoking an operator in python, there are three positional arguments that are generic to all operators, all other arguments that pass settings specific to the operator must use keyword arguments.

So to call the operator you should use -

bpy.ops.sequencer.movie_strip_add(filepath=video_file)


Note that movie_strip_add will add one video file as a strip, so to add a directory of movies you will want to use a loop.

def execute(self, context):
episode_path = "/path/to/videos/"
for v in [f for f in os.listdir(episode_path) if f.endswith('.mov')]:
return {'FINISHED'}

• Wow... it's work!!!! Thankyou sambler. You saved me... Hahaha – Aditya Dec 25 '16 at 2:52
• Now, i need to set the script. Because, the video always in the frame 0. Thanks for help sambler – Aditya Dec 25 '16 at 2:58

You could skip using ops altogether: new_movie(name, filepath, channel, frame_start)

def execute(self, context):
#create sequence_editor if none exists
if not bpy.context.scene.sequence_editor:
bpy.context.scene.sequence_editor_create()
episode_path = "F:\\Dokumen\\Target\\Develop Apps\\addon_blender\\tes"
movs = [os.path.join(path,v) for v in os.listdir(episode_path) if v.endswith('.mov')]
for i in range(len(movs)):
bpy.context.scene.sequence_editor.sequences.new_movie(os.path.basename(movs[i]), movs[i], i, 1)


Doing it this way also gives you more control, like name and what channel and frame for placement.

• Hi cmomoney, i follow your tips but... uhmmm. this script doesn't work... – Aditya Dec 25 '16 at 3:08
• Maybe you can explain, why you define variable v in movs variable. Because, it's always error. error : variable v is not define. – Aditya Dec 25 '16 at 3:26
• So, i modified to this : for v in [f for f in os.listdir(episode_path) if f.endswith('.mov')]: movs = os.path.join(episode_path,v) for i in range(len(movs)): bpy.context.scene.sequence_editor.sequences.new_movie(os.path.basename(movs[i]), movs[i], i, 1) – Aditya Dec 25 '16 at 3:31
• Wow, looks messy.. and, i got this error : sequences.new_movie: unable to open movie file – Aditya Dec 25 '16 at 3:32
• v is the variable for the filename found in the episode path that ends with '.mov'. Its joined together with the path and added to the movs list. – cmomoney Dec 25 '16 at 4:50