Skip to content

Instantly share code, notes, and snippets.

@duskvirkus
Created March 12, 2020 05:23
Show Gist options
  • Select an option

  • Save duskvirkus/b05825281bfd60b7e480f2a403cf9c60 to your computer and use it in GitHub Desktop.

Select an option

Save duskvirkus/b05825281bfd60b7e480f2a403cf9c60 to your computer and use it in GitHub Desktop.
import bpy
import math
import re
from collections import namedtuple
def lookat(location, camera):
xIn = camera.location.x - location.x
yIn = camera.location.y - location.y
zIn = camera.location.z - location.z
x = math.pi / 2
y = 0
z = 0
try:
z = math.atan(yIn/xIn)
except ZeroDivisionError:
print('div error')
z = 0
if xIn >= 0:
z += math.pi / 2
else:
z -= math.pi / 2
try:
x = math.atan(math.sqrt(yIn * yIn + xIn * xIn) / zIn)
except ZeroDivisionError:
print('div error 2')
x = 0
if zIn < 0:
x += math.pi
elif zIn == 0:
x += math.pi / 2
camera.rotation_euler = (x, y, z)
def save(name):
bpy.context.scene.render.image_settings.file_format='PNG'
bpy.context.scene.render.filepath = 'out/' + name
bpy.ops.render.render(write_still=True)
def deselectAll():
for obj in bpy.data.objects:
obj.select_set(False)
def printObjects():
for obj in bpy.data.objects:
print(obj.name)
def map(value, sourceMin, sourceMax, outMin, outMax):
source = sourceMax - sourceMin
out = outMax - outMin
return outMin + ((value - sourceMin) / source) * out
def getCameras():
cameras = []
for obj in bpy.data.objects:
if (re.match("Camera", obj.name)):
# print(obj.name)
cameras.append(obj)
return cameras
Vector = namedtuple('Vector', ['x', 'y', 'z'])
deselectAll()
bpy.data.objects['Camera'].select_set(True)
# bpy.data.objects['Cube'].select_set(True)
bpy.ops.object.delete()
rings = 10
segments = 24
radius = 20
for j in range(rings):
a = map(j, 0, rings, 0, math.pi)
r = radius * math.sin(a)
z = map(j, 0, rings, -radius, radius)
for i in range(segments):
aXY = map(i, 0, segments, 0, math.pi * 2)
x = r * math.cos(aXY)
y = r * math.sin(aXY)
bpy.ops.object.camera_add(location=(x, y, z))
cameras = getCameras()
for cam in cameras:
# print(cam)
lookat(Vector(0, 0, 0), cam)
i = 0
for cam in cameras:
bpy.context.scene.camera = cam
save('out/lookat-' + str(i))
i += 1
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment