0
$\begingroup$

I'm writing a script that set ups a compositing tree with the MovieClip node. I would like to automate it to take the movieclip background image on the active camera in the scene and set it to be the same in the movieclip node. The only thing holding me up is that I'm not sure how to access the value of the MovieClip data block on the active camera in python.

Any help would be very appreciated! Thanks!

Active Movieclip Data Block

$\endgroup$

1 Answer 1

0
$\begingroup$

You can use the following python code to add a movie clip to the selected camera in blender


# ========================================================
# Determine whether Blender is 3.2 or newer and requires
# the temp_override function, or is older and requires
# the context override dictionary
# ========================================================


import bpy
import os


def use_temp_override():
    version = bpy.app.version
    major = version[0]
    minor = version[1]
    return not (major < 3 or (major == 3 and minor < 2))

window = bpy.context.window
screen = window.screen

def get_areas(type):
    return [area for area in screen.areas if area.type == type]

def get_regions(areas):
    return [region for region in areas[0].regions if region.type == 'WINDOW']

def execute_main_script():
    area_type = 'VIEW_3D' # change this to use the correct Area Type context you want to process in
    areas  = get_areas(area_type)

    if len(areas) <= 0:
        raise Exception(f"Make sure an Area of type {area_type} is open or visible in your screen!")

    if use_temp_override(): # execute using new temp override
        with bpy.context.temp_override(window=window, area=areas[0], regions=get_regions(areas)[0], screen=screen):
            bpy.ops.view3d.background_image_add()

    else: # execute using legacy override
        override = {
            'window': window,
            'screen': screen,
            'area': areas[0],
            'region': get_regions(areas)[0],
        }

        # bpy.ops.view3d.background_image_add(override)
        bpy.ops.view3d.background_image_add(override)



def set_camera_background_movieclip(camera, movFile=""):
    mov_dirname = os.path.dirname(movFile)
    mov_basename = os.path.basename(movFile)

    camera.data.show_background_images = True
    execute_main_script()

    background_images = camera.data.background_images
    if background_images:
        background_images[0].source = 'MOVIE_CLIP'
        
        clip = bpy.data.movieclips.get(mov_basename)
        if not clip:
            bpy.ops.clip.open('INVOKE_DEFAULT', 
                            directory=mov_dirname, 
                            files=[{"name":mov_basename, "name":mov_basename}], 
                            relative_path=True)

        clip = bpy.data.movieclips.get(mov_basename)
        if clip:
            clip.frame_start = 101
            background_images[0].clip = clip



movFile = "D:/my_movive.mov"
camera = bpy.context.selected_objects[0]

set_camera_background_movieclip(camera, movFile)



Reference Link

$\endgroup$

You must log in to answer this question.

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