import Blender
from Blender.Mathutils import *
import os,math,struct,shutil
from Blender.Scene import Render
from Blender import Draw

def init_cameras():
    scn=Blender.Scene.GetCurrent()
    sels=Blender.Object.GetSelected()
    
    if len(sels)!=1:
        menu_dummy=Draw.PupMenu("Error%t|Need to select one camera")
        return
    if sels[0].type!='Camera':
        menu_dummy=Draw.PupMenu("Error%t|Need to select one camera")
        return

    o=sels[0]
    ry=Draw.Create(32)
    rx=Draw.Create(1)
    block=[]
    block.append(("#Y",ry,1,128,"Rotation in y-axis"))
    block.append(("#X",rx,1,32,"Rotation in x-axis"))
    ret=Draw.PupBlock("Camera Arrangement",block)
    
    cam=o.getData()
    iry=int(str(ry))
    irx=int(str(rx))
    for j in xrange(irx):
        xang=-70*(j/float(irx));

        # choose the number of cameras on each ring to be proportional to circumference
        # of the ring.
        ny=int(math.ceil(math.cos(xang*math.pi/180.0)*iry));
        for i in xrange(ny):
            newobj=scn.objects.new(cam,'Camera-%d'%i)
            m=Matrix([1,0,0,0],[0,1,0,0],[0,0,1,0],[0,-10,0,1])

            #Camera is initially pointing down, so we need to point it along the y-axis,
            # then translate it to -10 on y axis, then rotate elevation (rotation about x),
            # followed by rotation about the azimuth (rotation about up axis, which is z in blender)
            newobj.setMatrix(RotationMatrix(90,4,'x')*m*RotationMatrix(xang,4,'x')*RotationMatrix(360*float(i)/float(ny),4,'z'))
            Blender.Redraw()

init_cameras()
