PyQt4 camera presets Tool

Hi there. This post is going to be about building a Camera Presets Tool. Sure we can build on maya’s own presets button. But this way you can share it with your coworkers. If by the end of this post you feel confortable, change the code to introduce your own presets in there.

This time instead of using standard cmds to build the interface i used PyQt which is a much better solution for designing ui’s. It’s based on QT (a tech owned by nokia ). As far as my knowledge goes this would work on maya 2011+ due to its new QT interface integration. I’ve heard the about it working on older versions but you have to be a more experienced developer to make it work i guess.

here is the documentation for the all classes available in PyQT4

So let’s go!!

1. First Step is to get the PyQt installer. You can get it here. Thank’s Justin Israel for that.

2. Run this code in Maya. I’ve included all the necessary comments for you to follow along.

from PyQt4 import QtGui, QtCore
import maya.cmds as cmds

#data specs on the cameras name and sensor sizes [name,(width,height)]

camListSpecs = [ ["Arri Alexa", (0.935, 0.526)],
["Red Mysterium-X 4k", (0.870, 0.489) ],
["Red Mysterium-X 2k", (0.435, 0.244) ],
["Canon 5D MkII", (1.417, 0.799)],
["Canon 7D", (0.877, 0.492)],
["Phantom HD Gold 1080p", (0.944, 0.531)]
]

# camera creation function
def createCam():
    camName = str(cameraSel.currentText())
    index = cameraSel.currentIndex()
    focal = float(focalLengthInput.text())
    cmds.camera(name=camName, horizontalFilmAperture=camListSpecs[index][1][0],
    verticalFilmAperture=camListSpecs[index][1][1], focalLength=focal)
    mainWin.close()

#list to store only the camera names

camList = []

for i in range(len(camListSpecs)):
    camList.append(camListSpecs[i][0]) 

#SETUP PYQT WINDOW/WIDGETS

#setup mainwindow
mainWin = QtGui.QWidget()
mainWin.setGeometry(300, 300, 160, 200) #pos x,pos y,width,height
mainWin.setWindowTitle("Camera")

#camera Type combobox
cameraLabel = QtGui.QLabel("Camera Type", parent=mainWin)
cameraLabel.move(5, 5)
cameraSel = QtGui.QComboBox(parent=mainWin)
cameraSel.setGeometry(5, 25, 150, 20)

#add camera list to comboBox
cameraSel.addItems(camList)

#focalLenght field
focalLabel = QtGui.QLabel("Focal Length", parent=mainWin)
focalLabel.setGeometry(5, 50, 150, 20)
focalLengthInput = QtGui.QLineEdit(parent=mainWin)
focalLengthInput.setGeometry(75, 50, 50, 20)

focalLengthInput.setText("35")
mmLabel = QtGui.QLabel("mm", parent=mainWin)
mmLabel.setGeometry(130, 50, 50, 20)

#create camera button
createCambtn = QtGui.QPushButton("Create Camera", parent=mainWin)
createCambtn.setGeometry(5, 75, 150, 20)

#interface actions/signals
QtCore.QObject.connect(createCambtn, QtCore.SIGNAL("clicked()"), createCam)

#show QT window
mainWin.show()

Advertisements