Skip to content

Instantly share code, notes, and snippets.

@IRCSS
Created June 8, 2025 20:10
Show Gist options
  • Save IRCSS/d1cfe7841be300618a365a30b3528974 to your computer and use it in GitHub Desktop.
Save IRCSS/d1cfe7841be300618a365a30b3528974 to your computer and use it in GitHub Desktop.
Blender Python Project Image from Camera
import bpy
import os
def project_image_from_camera(image_path, camera_name):
# Validate image path
if not os.path.isfile(image_path):
print(f"Image not found: {image_path}")
return
# Load image if not already loaded
image_name = os.path.basename(image_path)
image = bpy.data.images.get(image_name)
if not image:
image = bpy.data.images.load(image_path)
# Get camera
camera = bpy.data.objects.get(camera_name)
if not camera or camera.type != 'CAMERA':
print(f"Camera '{camera_name}' not found or not a camera.")
return
# Set the scene camera
bpy.context.scene.camera = camera
# Get the active object (must be selected and in Texture Paint mode)
obj = bpy.context.active_object
if obj is None or obj.type != 'MESH':
print("No active mesh object selected.")
return
# Switch to Texture Paint mode
bpy.ops.object.mode_set(mode='TEXTURE_PAINT')
# Ensure the object has an image texture to paint on
mat = obj.active_material
if not mat:
mat = bpy.data.materials.new(name="ProjectionMaterial")
obj.data.materials.append(mat)
obj.active_material = mat
if not mat.use_nodes:
mat.use_nodes = True
nodes = mat.node_tree.nodes
links = mat.node_tree.links
# Check for existing Image Texture node or create one
img_node = None
for node in nodes:
if node.type == 'TEX_IMAGE' and node.image == image:
img_node = node
break
if not img_node:
img_node = nodes.new(type='ShaderNodeTexImage')
img_node.image = image
img_node.interpolation = 'Linear'
# Set the image as active for painting
bpy.ops.paint.project_image(image=image.name)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment