Skip to content

Instantly share code, notes, and snippets.

@tin2tin
Created October 13, 2023 06:33
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tin2tin/b73b577ac129d2c6fbe42b2212b495e1 to your computer and use it in GitHub Desktop.
Save tin2tin/b73b577ac129d2c6fbe42b2212b495e1 to your computer and use it in GitHub Desktop.
Copy source material to folder for Blender
import bpy
import shutil
import os
from bpy.types import Operator
class CopyStripsOperator(Operator):
bl_idname = "sequencer.copy_strips"
bl_label = "Copy Strips to Folder"
def execute(self, context):
# Set the source folder where your .blend file is located
source_folder = bpy.path.abspath("//")
# Set the destination folder where you want to copy the files
destination_folder = "/path/to/your/destination/folder"
# Create a list of supported strip types (Movie, Sound, and Image)
supported_strip_types = {'MOVIE', 'SOUND', 'IMAGE'}
# Iterate through all sequences in the Video Sequence Editor
for sequence in bpy.context.scene.sequence_editor.sequences:
if sequence.type in supported_strip_types:
if sequence.type == 'IMAGE':
# Check if the image strip has a directory and name
if sequence.directory and sequence.name:
# Create the destination file path
destination_path = os.path.join(destination_folder, sequence.name)
source_path = os.path.join(source_folder, sequence.directory, sequence.name)
try:
# Copy the file to the destination folder
shutil.copy(source_path, destination_path)
print(f"Copied '{sequence.name}' to '{destination_folder}'")
# Update the strip's directory and name
sequence.directory = os.path.relpath(destination_folder, source_folder)
sequence.name = os.path.basename(destination_path)
except Exception as e:
print(f"Error copying '{sequence.name}': {str(e)}")
elif sequence.type == 'SOUND':
# For sound strips, use the strip.sound.filepath property
sound_filepath = bpy.path.abspath(sequence.sound.filepath)
if sound_filepath:
filename = os.path.basename(sound_filepath)
destination_path = os.path.join(destination_folder, filename)
source_path = os.path.join(source_folder, filename)
try:
# Copy the file to the destination folder
shutil.copy(source_path, destination_path)
print(f"Copied '{filename}' to '{destination_folder}'")
# Update the strip's sound filepath
sequence.sound.filepath = os.path.relpath(destination_path, source_folder)
except Exception as e:
print(f"Error copying '{filename}': {str(e)}")
else:
# For movie strips, consider the entire filepath as the filename
file_path = bpy.path.abspath(sequence.filepath)
if file_path:
filename = os.path.basename(file_path)
destination_path = os.path.join(destination_folder, filename)
source_path = os.path.join(source_folder, filename)
try:
# Copy the file to the destination folder
shutil.copy(source_path, destination_path)
print(f"Copied '{filename}' to '{destination_folder}'")
# Update the strip's filepath
sequence.filepath = os.path.relpath(destination_path, source_folder)
except Exception as e:
print(f"Error copying '{filename}': {str(e)}")
print("Copy and update process completed.")
return {'FINISHED'}
def register():
bpy.utils.register_class(CopyStripsOperator)
def unregister():
bpy.utils.unregister_class(CopyStripsOperator)
if __name__ == "__main__":
register()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment