-
Notifications
You must be signed in to change notification settings - Fork 0
/
k_center.py
83 lines (75 loc) · 2.48 KB
/
k_center.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
#!BPY
""" Registration info for Blender menus:
Name: 'k+) Center'
Blender: 242
Group: 'Object'
Tooltip: 'Recenter the camera to look at object(s).'
"""
__author__ = "Kurt"
__url__ = ""
__version__ = "1.0"
__bpydoc__ = """
Recenter and then zoom the camera to frame the current object(s).
A good idea, but me and math are not getting along well.
"""
# $Id: k_center.py,v 1.0 2007/12/25 23:14:48 kurt Exp $
import Blender
from Blender import *
from math import *
def center(objs=None,camera=None):
"""
Using the given camera, current camera (or new camera) frame the shot so the given object(s) are centered
If no object(s) are specified, it will try to frame all existing objects.
"""
scene=Scene.GetCurrent()
if objs==None:
objs=scene.objects
if objs==None:
return
if type(objs).__name__!='list':
objs = [objs]
elif len(objs)<1:
return
if camera==None:
camera = scene.objects.camera
if camera==None:
camera = scene.objects.new (Blender.Camera.New())
camera.setLocation (10.0, 10.0, 10.0)
camera.setEuler (Euler(-45.0,0.0,45.0))
# figure out the space occupied by everything togehter
xmax=-65535;xmin=65535;ymax=-65535;ymin=65535;zmax=-65535;zmin=65535;
for obj in objs:
bb=obj.getBoundBox()
if bb == None:
print dir(obj)
pass
else:
for pt in bb:
xmin=min(xmin,pt[0])
xmax=max(xmax,pt[0])
ymin=min(ymin,pt[1])
ymax=max(ymax,pt[1])
zmin=min(zmin,pt[2])
zmax=max(zmax,pt[2])
if xmax == None:
print "ERROR: No objects found to center to."
return -1
xspan=xmax-xmin
yspan=ymax-ymin
zspan=zmax-zmin
xcenter=xmin+(xspan/2)
ycenter=xmin+(yspan/2)
zcenter=xmin+(zspan/2)
camera.clearTrack()
camx,camy,camz=camera.getLocation()
# Rotate camera to face center point
rx=cos((camy-ycenter)/(camz-zcenter))
ry=cos((camz-zcenter)/(camx-xcenter))
rz=cos((camx-xcenter)/(camy-ycenter))
camera.RotX=rx
camera.RotY=ry
camera.RotZ=rz
# TODO: Zoom in/out until we fill the frame
if __name__ == '__main__':
print "-----------"
center(Object.GetSelected())