Created
July 15, 2019 10:03
-
-
Save adrienkaiser/086b0de7f301fb8903b03a47e60357d9 to your computer and use it in GitHub Desktop.
Blender script to generate a NURBS curve from a camera's path.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import bpy | |
from mathutils import Vector | |
# Create the curve data | |
# https://blender.stackexchange.com/a/6751 | |
curveData = bpy.data.curves.new('CameraCurve', type='CURVE') | |
curveData.dimensions = '3D' | |
curveData.resolution_u = 2 | |
curveData.bevel_depth = 0.01 # curve radius in meters | |
polyline = curveData.splines.new('NURBS') | |
polyline.points.add(bpy.context.scene.frame_end) | |
# Loop over camera poses and add coords to curve | |
# https://blender.stackexchange.com/a/58922 | |
camera = bpy.data.objects["Camera"] | |
sensorHeightMet = camera.data.sensor_height * 0.001 # used to hide curve tube from camera frame | |
frame = bpy.context.scene.frame_start | |
while frame <= bpy.context.scene.frame_end: | |
bpy.context.scene.frame_set(frame) | |
pos = camera.matrix_world.decompose()[0] # https://blender.stackexchange.com/a/38210 | |
rot = camera.matrix_world.decompose()[1].to_matrix() | |
camUp = Vector((rot[0][1], rot[1][1], rot[2][1])) # camera Up vector | |
# lower curve to avoid showing inside of curve tube close to camera | |
pos = pos - camUp * sensorHeightMet * 3 # empirical factor if sensor height is not enough | |
# map coords to spline | |
polyline.points[frame].co = (pos[0], pos[1], pos[2], 1) | |
frame += 1 | |
# create object and add to main collection | |
curveOB = bpy.data.objects.new('CameraCurve', curveData) | |
bpy.data.collections['Collection'].objects.link(curveOB) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment