Skip to content

Instantly share code, notes, and snippets.

@hsab
Created August 29, 2018 15:48
Show Gist options
  • Save hsab/5e80366bd588bccdb247851949f6daa9 to your computer and use it in GitHub Desktop.
Save hsab/5e80366bd588bccdb247851949f6daa9 to your computer and use it in GitHub Desktop.
Blender: Fix depth and ratio of CameraImagePlanes for all scenes
import bpy
from bpy_extras.image_utils import load_image
from mathutils import Vector, Euler
import math
from math import sqrt
C = bpy.context
D = bpy.data
def SetupDriverVariables(scene, driver, imageplane):
camAngle = driver.variables.new()
camAngle.name = 'camAngle'
camAngle.type = 'SINGLE_PROP'
camAngle.targets[0].id = imageplane.parent
camAngle.targets[0].data_path="data.angle"
scX = driver.variables.new()
scX.name = 'scX'
scX.type = 'SINGLE_PROP'
scX.targets[0].id_type = 'SCENE'
scX.targets[0].id = scene
scX.targets[0].data_path="render.resolution_x"
scY = driver.variables.new()
scY.name = 'scY'
scY.type = 'SINGLE_PROP'
scY.targets[0].id_type = 'SCENE'
scY.targets[0].id = scene
scY.targets[0].data_path="render.resolution_y"
aspX = driver.variables.new()
aspX.name = 'aspX'
aspX.type = 'SINGLE_PROP'
aspX.targets[0].id_type = 'SCENE'
aspX.targets[0].id = scene
aspX.targets[0].data_path="render.pixel_aspect_x"
aspY = driver.variables.new()
aspY.name = 'aspY'
aspY.type = 'SINGLE_PROP'
aspY.targets[0].id_type = 'SCENE'
aspY.targets[0].id = scene
aspY.targets[0].data_path="render.pixel_aspect_y"
depth = driver.variables.new()
depth.name = 'depth'
depth.type = 'TRANSFORMS'
depth.targets[0].id = imageplane
depth.targets[0].data_path = 'location'
depth.targets[0].transform_type = 'LOC_Z'
depth.targets[0].transform_space = 'LOCAL_SPACE'
def SetupDriversForImagePlane(scene, imageplane):
driver = imageplane.driver_add('scale',1).driver
driver.type = 'SCRIPTED'
SetupDriverVariables(scene, driver, imageplane)
#driver.expression ="-depth*math.tan(camAngle/2)*resolution_y*pixel_y/(resolution_x*pixel_x)"
driver.expression ="-depth*tan(camAngle/2)*scY * aspY/(scX * aspX) if scX>=scY else -depth*tan(camAngle/2)"
driver = imageplane.driver_add('scale',0).driver
driver.type= 'SCRIPTED'
SetupDriverVariables(scene, driver, imageplane)
driver.expression ="-depth*tan(camAngle/2) if scX>=scY else -depth*tan(camAngle/2)*scX * aspX/(scY * aspY)"
def distance(first, second):
locx = second[0] - first[0]
locy = second[1] - first[1]
locz = second[2] - first[2]
distance = sqrt((locx)**2 + (locy)**2 + (locz)**2)
return distance
def createImagePlaneForCamera(scene, camera):
imageplane = bpy.data.objects['Figure-'+scene.name]
stairs = bpy.data.objects['Stairs-'+scene.name]
imageplane.scale = (1,1,1)
imageplane.location[2] = -distance(camera.location, stairs.location)*3/4
SetupDriversForImagePlane(scene, imageplane)
for sc in D.scenes:
if sc.name != 'Import':
C.screen.scene = sc
scene = sc
camera = scene.camera
camera.scale = (1,1,1)
createImagePlaneForCamera(scene, camera)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment