Skip to content

Instantly share code, notes, and snippets.

@Cdaprod
Last active February 14, 2025 15:02
Show Gist options
  • Save Cdaprod/f2fe8b00bc1812210a6ecae22baf8d3f to your computer and use it in GitHub Desktop.
Save Cdaprod/f2fe8b00bc1812210a6ecae22baf8d3f to your computer and use it in GitHub Desktop.
This setup will give a skewed overlay video on the left, over the background video in a 4:3 frame.
import bpy
# ✅ Fix for deletion (Avoids "Lack of Context" errors)
for obj in list(bpy.data.objects):
bpy.data.objects.remove(obj, do_unlink=True)
# Set the render resolution to 4:3 aspect ratio (1280x960)
bpy.context.scene.render.resolution_x = 1280
bpy.context.scene.render.resolution_y = 960
# Function to create video material
def create_video_material(video_path, use_chroma_key=False):
mat = bpy.data.materials.new(name="VideoMaterial")
mat.use_nodes = True
nodes = mat.node_tree.nodes
# Clear default nodes
for node in nodes:
nodes.remove(node)
# Create new nodes
output_node = nodes.new(type="ShaderNodeOutputMaterial")
principled_node = nodes.new(type="ShaderNodeBsdfPrincipled")
texture_node = nodes.new(type="ShaderNodeTexImage")
# Load video
video = bpy.data.images.load(video_path)
texture_node.image = video
texture_node.image_user.use_auto_refresh = True
# Link nodes
mat.node_tree.links.new(principled_node.outputs["BSDF"], output_node.inputs["Surface"])
mat.node_tree.links.new(texture_node.outputs["Color"], principled_node.inputs["Base Color"])
# Apply chroma key (optional)
if use_chroma_key:
chroma_node = nodes.new(type="ShaderNodeKeying")
chroma_node.inputs["Key Color"].default_value = (0.0, 1.0, 0.0, 1.0) # Green screen
mat.node_tree.links.new(texture_node.outputs["Color"], chroma_node.inputs["Image"])
mat.node_tree.links.new(chroma_node.outputs["Image"], principled_node.inputs["Base Color"])
return mat
# ✅ Fix: Add background video as a plane
bpy.ops.mesh.primitive_plane_add(size=10, location=(0, 0, 0))
bg_plane = bpy.context.object
bg_plane.data.materials.append(create_video_material("//background.mp4"))
# ✅ Fix: Add skewed overlay on left side
bpy.ops.mesh.primitive_plane_add(size=5, location=(-2.5, 0, 1))
overlay_plane = bpy.context.object
overlay_plane.data.materials.append(create_video_material("//overlay.mp4", use_chroma_key=True))
# ✅ Fix: Skew overlay using scale & rotation
overlay_plane.scale.x = 1.2 # Stretch in X
overlay_plane.scale.y = 0.8 # Flatten in Y
overlay_plane.rotation_euler.x = 0.2 # Tilt forward
overlay_plane.rotation_euler.y = -0.3 # Skew left
overlay_plane.rotation_euler.z = 0.1 # Slight rotation
# ✅ Fix: Add camera properly without context issues
bpy.ops.object.camera_add(location=(0, -7, 2))
camera = bpy.context.object
bpy.context.scene.camera = camera
# Set rendering settings
bpy.context.scene.render.engine = 'CYCLES'
bpy.context.scene.render.image_settings.file_format = 'FFMPEG'
bpy.context.scene.render.ffmpeg.format = 'MPEG4'
bpy.context.scene.render.filepath = "//output.mp4"
# ✅ Fix: Render the video
bpy.ops.render.render(animation=True)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment