# How to use python to copy video settings from the compositor to the 3D viewport

I have 15 short videos, created in Celestia (which will grow to 100+), that I'm using as backgrounds for a music video.

I previously used one long video for the background, but it was inefficient. I'm constantly going back and forth between Celestia and Blender making fine adjustments to short sections.

So I changed my workflow so that I now have short video backgrounds listed in the Compositor, and use a python script to select which video becomes the background for each mini-scene.

Unfortunately, there is no way in Celestia to start video capture frame-accurately, so I have to adjust the offset in Blender each time I make changes to the Celestia backgrounds.

That much is unavoidable, but I would really like to avoid the next step, which is to select the video and adjust the offset AGAIN in the 3D viewport.

By naming the clips and the background, I've managed to change the clip in the 3D view using python e.g.

bpy.data.images["background_b"].filepath = bpy.data.images["Moon.b1"].filepath


However, I'm stuck with the start frame and offset. The tooltip says

bpy.data.screens["Animation.back"]..frame_start


but that gives a syntax error.

Can anyone tell me the proper syntax to reference the frame_start and frame_offset for the background in a 3D viewport, and an image node in the compositor?

• Not sure to understand, are you using VSE to chain many videos, then you would like that video to be the scene background? You could perhaps render the chained video into a single longer video, then use that video as background (mapping it over a background plane) – m.ardito Sep 19 '17 at 8:00
• Thanks for your response @m.ardito I've edited the question to give a bit more background and, hopefully, clarity. – Thailandian Sep 19 '17 at 10:08
• How is your script altering the background? You should be able to add setting the frame offset to it. – sambler Sep 19 '17 at 11:23
• Thanks @sambler I had just come to a similar conclusion. Actually, I'm now trying to copy the offset from the version in the compositor to the one in the 3D viewport. I think I need to rephrase my question again. – Thailandian Sep 19 '17 at 11:37

OK!!! I finally worked it out.

In case anyone else needs this, here are examples:

Set-up Variables

clip = 'your.video.filename.mp4'

bg = bpy.data.screens['your.screen'].areas[n].spaces[0].background_images[0]

node_im = bpy.data.scenes['your.scene'].node_tree.nodes[clip]


Copy Video and Properties

bg.image = bpy.data.images[clip]
bg.image_user.frame_start = node_im.frame_start
back_im.image_user.frame_offset = node_im.frame_offset


Notes

Blender automatically assigns e.g. 'your.video.filename.mp4' as the image reference when you load a video into an image node. You can change that if you want, but beware that if you reload the clip, the field will revert to the clip's filename.

Finding 'n' from 'areas[n]' takes a bit of trial and error, especially if you have a complex screen.

for area in bpy.context.window.screen.areas: print(area.type)


outputs a list of the areas of your screen, so at least you can narrow down which are viewports.

After that:

bpy.data.screens["your.screen"].areas[n].spaces[0].background_images[0]


will tell you what,if any, image has been assigned to areas[n]'s background.

I have to say I'm surprised that this hasn't come up before - seems to me that duplicating a video background node in a 3D viewport would be a common need.

• I should mention that the answer from @batFINGER in this thread: blenderartists.org/forum/… was instrumental in heading me in the right direction. Thank you. – Thailandian Sep 21 '17 at 6:36