List cameras v2

Version 2 of my python script that list cameras and spotlights in the scene and provides an easy interface for switching between them.

Rewrote most of the old code and added some extra functions. The checkbox next to each camera/spotlight locks the selected item so that it can’t be moved in the viewport. Deselect the checkbox to unlock it. There is also a feature for setting up custom timeranges for each camera to allow for a form of shotlist. Haven’t made a good interface for it yet so for the meantime this is edited directly in the code below. I’m sure you’ll figure it out by looking at the comments below. If not give me a shout at jonne@jonne.net

The “Dup” button duplicates the selected cam/spot with all inputs (keys etc).

As always this is made for my own needs so if you have something you want added drop me a mail.

camSelect

##
## listCameras v2
## -------------
## 1 nov 2009
## Jan-Erik "Jonne" Östman (jonne@jonne.net)
##
## www.jonne.net
##
## Description:
## ------------
## PYTHON script. Lists cameras and spotlights in the scene and provides
## an easy interface for switching between them
##
##
## Instructions:
## -------------
## There are two variables below that need editing:
##
## sceneTimerange - Set this to the range of your whole clip. Start and endframe.
## cams - this is the list that is used to define custom timetranges for each camera.
##
## -------------------------------------------------------------------------
import maya.cmds as mc

#====================== start editable area ==============================

# Edit to match your cameranames and desired timeranges
sceneTimerange = [1,250]
cams = [
		#cameraName,startRange,endRange
		['camera1', 1, 30],
		['camera2', 31, 45],
		['camera3', 46, 100],
		['camera4', 101, 140],
		['camera5', 141, 240],
		['camera6', 241, 250],
		['camera7', 280, 350],
	]

#====================== end editable area ================================

##########################################
# List cameras and spotlights in the scene
##########################################
def getCameras():

	cams = mc.listCameras(p=1)

	try:
		lights = mc.listRelatives(mc.ls(lights=1), p=1)
		return cams + lights
	except:
		return cams

##########################################
## Look thru selected
##########################################
def lookThruCam(value):
	def b(*args):
		mc.lookThru('perspView', value)

		#set timerange for the selected camera
		j =0
		match = 0

		for i in cams:
			if value == cams[j][0]:
				setCurrenttimerange(cams[j][1], cams[j][2])
				match = 1
			else:
				setCurrenttimerange(sceneTimerange[0] , sceneTimerange[1] )
			j += 1
			if match == 1:
				break

	return b

##########################################
##Lock camera or spotlight
##########################################
def checkBoxOn(value):
	def b(*args):

		camAttributes = mc.listAttr(value, v=1,k=1)
		for i in camAttributes:
			mc.setAttr(value + '.' + i, lock=1)

	return b

##########################################
## Unlock camera or spotlight
##########################################
def checkBoxOff(value):
	def b(*args):
		camAttributes = mc.listAttr(value, v=1,k=1)
		for i in camAttributes:
			mc.setAttr(value + '.' + i, lock=0)		

	return b

##########################################
## check if locked or not
##########################################
def checkState(value):

	def b(*args):
		try:
			return mc.getAttr(value + '.translateX', lock=1)
		except:
			return 0
	returnValue = b()
	return returnValue

##########################################
# set the current timerange for selected
##########################################
def setCurrenttimerange(startRange, endRange):

	mc.playbackOptions( minTime=startRange)
	mc.playbackOptions( maxTime=endRange)

##########################################
## Check for existing window
##########################################
def createWindow():
	windowName = 'camListWin'
	if (mc.window(windowName, exists=True)):
		mc.deleteUI(windowName , window=True)

	mc.window( windowName, title=windowName, widthHeight=(350, 400) )
	mc.rowColumnLayout(numberOfColumns=3, columnWidth=[(1, 80), (2, 20), (3, 40)])

	for i in getCameras():
		mc.text(i)
		mc.checkBox( label='', onc=checkBoxOn(i), ofc=checkBoxOff(i), value=checkState(i) )
		mc.button( label='select', command=lookThruCam(i), width=20 )

	mc.button(label='Refresh', command=initWindow, bgc=[1,1,1])
	mc.text(' ')
	mc.button(label='Dup', command=duplicateObj, bgc=[1,1,1])

##########################################
## Initialize window
##########################################
def initWindow(*args):
	createWindow()
	mc.showWindow()

##########################################
## Duplicate an object with it's settings
##########################################
def duplicateObj(*args):
	sel = mc.ls(sl=1)
	sourceObj = sel[0]
	mc.duplicate(sourceObj, un=1)

## init script
initWindow()
I wish to do something Great and Wonderful, but I must start by doing the little things like they were Great and Wonderful”
~ Albert Einstein