Access Resolve via Python without 'module not found' and endless attmpts to make the environment variables work.


import os

import sys

# Add the path to the file to the PYTHONPATH

sys.path.append("C:/ProgramData/Blackmagic Design/DaVinci Resolve/Support/Developer/Scripting/Modules")

# Set the RESOLVE_SCRIPT_API_NET environment variable

os.environ["RESOLVE_SCRIPT_API_NET"] = "1"

# Import the DaVinci Resolve Python API

import DaVinciResolveScript as dvr

resolve = dvr.scriptapp("Resolve")

project_manager = resolve.GetProjectManager()

project = project_manager.CreateProject("Test_Project")

if project:

    print("New project created successfully.")


    print("Failed to create a new project.")

Add files from a specified folder to media and add each to a track named same as part of their clipname/filename

#resolve file sequence added to media, added to new tracks based on name form part of filename/clipname 9hole#thanks to roger.magnusson for the Undocumented Resolve API functionsimport osimport sysimport refrom pathlib import Path
def normalize_file_path(file_path):    return file_path.replace('/', '\\')
def to_range_format(file_path, start_index, end_index):    regex = re.compile(r'\.%04d\.')    file_ext = Path(file_path).suffix    return regex.sub(f'.[{start_index:04d}-{end_index:04d}]{file_ext}', file_path)
def populate_timeline(folder_path, project_name, timeline_name, video_track_names):    sys.path.append("C:/ProgramData/Blackmagic Design/DaVinci Resolve/Support/Developer/Scripting/Modules")    os.environ["RESOLVE_SCRIPT_API_NET"] = "1"
    import DaVinciResolveScript as dvr
    resolve = dvr.scriptapp("Resolve")    project_manager = resolve.GetProjectManager()    project = project_manager.LoadProject(project_name)
    if not project_name:        print("Failed to load project")        return
    media_storage = resolve.GetMediaStorage()    media_pool = project.GetMediaPool()
    timeline = media_pool.CreateEmptyTimeline(timeline_name)    if not timeline:        print(f"Failed to create a timeline named '{timeline_name}'")        return
    for index, track_name in enumerate(video_track_names, 1):        video_track = timeline.AddTrack("video")        timeline.SetTrackName("video", index, track_name)    timeline.DeleteTrack("video", index + 1
    image_sequence_pattern = re.compile(r"\.\d{4}\.")
    sequences_imported = set()
    for file in Path(folder_path).iterdir():        media_pool_item = None          if file.is_file():            file_name =            file_path = str(file.resolve())
            match =            if match:                sequence_base_name = file_name[:match.start()] + file_name[match.end():]
                if sequence_base_name in sequences_imported:                    continue                sequence_files = sorted([f for f in Path(folder_path).glob(file_name[:match.start()] + ".*" + file_name[match.end():])])                #print(f"sequence_files",sequence_files)                if len(sequence_files) > 0:                    start_index = int(sequence_files[0].stem.split(".")[-1])                    end_index = int(sequence_files[-1].stem.split(".")[-1])                    file_path = os.path.join(str(sequence_files[0].resolve().parent), sequence_base_name + ".%04d" + sequence_files[0].suffix).replace('\\', '/')                    file_path = re.sub(r'\.\d{4}\.', '.%04d.', file_path)                    file_path = file_path.replace("png.%04d", ".%04d")                      #print("file_path",file_path)                    media_pool.ImportMedia([{"FilePath": file_path, "StartIndex": start_index, "EndIndex": end_index}])                    sequences_imported.add(sequence_base_name)                    #print(f"Imported file path: {file_path}")
                else:                    print(f"No sequence files found for base name: {sequence_base_name}")            else:                print(f"File not part of image sequence: {file_name}")
    first_track_clip = None
    for pool_clip in media_pool.GetRootFolder().GetClipList():        print("pool_clip", pool_clip)        pool_clip_file_path = pool_clip.GetClipProperty()["File Path"]        normalized_pool_clip_file_path = normalize_file_path(pool_clip_file_path)
        for index, track_name in enumerate(video_track_names, 1):            if index == 1# Skip the first track                if ('.' + track_name.lower() + '.') in normalized_pool_clip_file_path.lower():                    first_track_clip = pool_clip                continue
            if ('.' + track_name.lower() + '.') in normalized_pool_clip_file_path.lower():                subclip = {                    "mediaPoolItem": pool_clip,                    "startFrame": 0,                    "endFrame": pool_clip.GetClipProperty("End"),                    "mediaType": 1,                    "trackIndex": index                }                print(subclip)                media_pool.AppendToTimeline([subclip])                break
    # Add the first track clip    if first_track_clip is not None:        subclip = {            "mediaPoolItem": first_track_clip,            "startFrame": 0,            "endFrame": first_track_clip.GetClipProperty("End"),            "mediaType": 1,            "trackIndex": 1        }        media_pool.AppendToTimeline([subclip])
    print(f"Timeline '{timeline_name}' has been populated with the video tracks.")
folder_path = "D:/yourPathToClipSequenceFolder"  project_name = "yourProjectName"timeline_name = "yourTimelineName"video_track_names = ["Background", "BG", "MG", "FG"] #these will be in your filenames preferably bewteen "." for less error unless your names are super unique ;)populate_timeline(folder_path, project_name, timeline_name, video_track_names)