Advertisement
sanfx

RenderUI.py

Dec 7th, 2012
538
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 15.49 KB | None | 0 0
  1. from PyQt4 import QtCore,QtGui
  2. from functools import partial
  3. import sys,os,subprocess,pickle
  4. import Render
  5. import ast,datetime
  6.  
  7. class Window(QtGui.QWidget):
  8.     renderer={
  9.             'Mental Ray':'mr',
  10.             'Maya Software':'sw',
  11.             'Maya Vecter':'vr',
  12.             'Maya Hardware 2.0':'mayaHardware2',
  13.             'Maya Hardware':'mayaHardware'
  14.                 }
  15.  
  16.     browsBtnSig=QtCore.pyqtSignal(int)
  17.  
  18.     def __init__(self,*args,**kwargs):
  19.           super(Window,self).__init__(*args,**kwargs)
  20.  
  21.           self.setWindowTitle("Snowball Batch Render")
  22.           self.resize(500,400)
  23.           self.setMinimumSize(400,300)
  24.           self.setMaximumSize(600,400)
  25.           self.setWindowIcon(QtGui.QIcon('images/Snowball-icon.png'))
  26.           self.accessAction = Render.Actions()
  27.  
  28.  
  29.           self.settingsDir=os.path.join(os.path.split(__file__)[0],"settings")
  30.           self.maSetfile=os.path.join(self.settingsDir,"mayaSettings.txt")
  31.  
  32.           if os.path.isfile(self.maSetfile):
  33.              self.items = self.readSettingFile()
  34.           else:
  35.                self.items={
  36.                 'Maya Executable':'',
  37.                 'Render':'',
  38.                 'Mayapy Interpreter':'',
  39.                 'imgcvt':'',
  40.                 'IMConvert':''}
  41.  
  42.           self.createUI()
  43.  
  44.     def initializeSetting(self):
  45.         if os.path.isfile(self.maSetfile):
  46.            if str(self.appExeCB.currentText())=='Maya Executable':
  47.               self.appExeEdt.setText(self.items['Maya Executable'])
  48.  
  49.     def appExeCBSelChange(self):
  50.         """
  51.        Sets the path of selected items to self.appExeEdt(QLineEdit)
  52.        """
  53.         self.appExeEdt.setText(self.items[str(self.appExeCB.currentText())])
  54.  
  55.  
  56.     def getSelectedRendertype(self):
  57.         if self.mayachkBox.checkState() == QtCore.Qt.Checked:
  58.  
  59.             self.label.setText("Maya Scene File")
  60.             self.rndCB.setEnabled(True)
  61.             self.camlbl.setText("Select Camera")
  62.  
  63.         elif self.nukechkBox.checkState() == QtCore.Qt.Checked:
  64.             self.label.setText("Nuke Script File")
  65.             self.rndCB.setEnabled(False)
  66.             self.camlbl.setText("Write node")
  67.  
  68.     def createUI(self):
  69.           quitAction=QtGui.QAction('&Exit',self)
  70.           quitAction.triggered.connect(self.close)
  71.           fileMenu=QtGui.QMenuBar()
  72.  
  73.           phile=fileMenu.addMenu('&File')
  74.           help=fileMenu.addMenu('&Help')
  75.  
  76.           phile.addAction(quitAction)
  77.  
  78.           #add my checkboxes to select render type task
  79.           self.mayachkBox=QtGui.QCheckBox()
  80.           self.mayachkBox.setCheckState(QtCore.Qt.Checked)
  81.           self.mayachkBox.stateChanged.connect(self.getSelectedRendertype)
  82.           mayaIcon=QtGui.QPixmap("images/Maya_Icon.png")
  83.           self.mayachkBox.setIcon(QtGui.QIcon(mayaIcon))
  84.           self.mayachkBox.setIconSize(QtCore.QSize(25,25))
  85.           self.nukechkBox=QtGui.QCheckBox()
  86.           nukeIcon= QtGui.QPixmap("images/nuke_icon.png")
  87.           self.nukechkBox.setIcon(QtGui.QIcon(nukeIcon))
  88.           self.nukechkBox.stateChanged.connect(self.getSelectedRendertype)
  89.           self.nukechkBox.setIconSize(QtCore.QSize(25,25))
  90.           chkboxLayout=QtGui.QHBoxLayout()
  91.           chkboxLayout.addStretch(1)
  92.           chkboxLayout.addWidget(self.mayachkBox)
  93.           chkboxLayout.addWidget(self.nukechkBox)
  94.           chkboxLayout.addStretch(1)
  95.           #make button exclusive
  96.           self.chkBoxGrp=QtGui.QButtonGroup()
  97.           self.chkBoxGrp.setExclusive(True)
  98.           # add checkBoxes to exclusive groups
  99.           self.chkBoxGrp.addButton(self.mayachkBox)
  100.           self.chkBoxGrp.addButton(self.nukechkBox)
  101.  
  102.           # initialize tab widget
  103.           tab_widget = QtGui.QTabWidget()
  104.           # Everything will go inside the below QWidgets
  105.           tab1 = QtGui.QWidget()
  106.           tab2 = QtGui.QWidget()
  107.           tab3 = QtGui.QWidget()
  108.  
  109.           p1_vertical = QtGui.QVBoxLayout(tab1)
  110.           p2_vertical = QtGui.QVBoxLayout(tab2)
  111.           self.p3_vertical = QtGui.QVBoxLayout(tab3)
  112.  
  113.           tab_widget.addTab(tab1, "Render Setup")
  114.           tab_widget.addTab(tab2, "Task Queue")
  115.           tab_widget.addTab(tab3, "Settings")
  116.  
  117.  
  118.           gridLayout  = QtGui.QGridLayout()
  119.           self.label=QtGui.QLabel("Maya Scene File")
  120.           gridLayout.addWidget(self.label,0,0)
  121.           self.scnFilePath=QtGui.QLineEdit()
  122.           self.scnFilePath.setToolTip("Enter scene file name")
  123.           gridLayout.addWidget(self.scnFilePath,0,1)
  124.           self.browseBtn=QtGui.QPushButton("Browse")
  125.           self.browseBtn.setObjectName('browsbtn')
  126.           gridLayout.addWidget(self.browseBtn,0,2)
  127.           dirlabel=QtGui.QLabel("Render Directory")
  128.           gridLayout.addWidget(dirlabel,1,0)
  129.           self.renDir=QtGui.QLineEdit()
  130.           self.renDir.setToolTip("Specify the directory you want render to go")
  131.           gridLayout.addWidget(self.renDir,1,1)
  132.           self.rdBtn=QtGui.QPushButton("Browse")
  133.           gridLayout.addWidget(self.rdBtn,1,2)
  134.  
  135.           gridLayout2=QtGui.QGridLayout()
  136.           gridLayout2.setSpacing(1)
  137.           #First Row
  138.           sfLbl=QtGui.QLabel("Start Frame")
  139.           gridLayout2.addWidget(sfLbl,0,0)
  140.           self.sfEdt=QtGui.QLineEdit()
  141.           gridLayout2.addWidget(self.sfEdt,0,1)
  142.  
  143.           eflbl=QtGui.QLabel("End Frame")
  144.           gridLayout2.addWidget(eflbl,0,2)
  145.           self.efEdt=QtGui.QLineEdit()
  146.           gridLayout2.addWidget(self.efEdt,0,3)
  147.           byfrlbl=QtGui.QLabel("Interval")
  148.           gridLayout2.addWidget(byfrlbl,0,4)
  149.           self.byfrEdt=QtGui.QLineEdit()
  150.           gridLayout2.addWidget(self.byfrEdt,0,5)
  151.           rndlbl=QtGui.QLabel("Renderer")
  152.           #Second Row
  153.           gridLayout2.addWidget(rndlbl,1,0)
  154.           self.rndCB=QtGui.QComboBox()
  155.           self.rndCB.addItems(self.renderer.keys())
  156.           self.rndCB.setCurrentIndex(self.renderer.keys().index('Maya Software'))
  157.           gridLayout2.addWidget(self.rndCB,1,1)
  158.           self.camlbl=QtGui.QLabel("Select Camera")
  159.           self.camlbl.setFixedWidth(70)
  160.           gridLayout2.addWidget(self.camlbl,1,2)
  161.  
  162.           self.camCB=QtGui.QComboBox()
  163.           gridLayout2.addWidget(self.camCB,1,3)
  164.           fmtlbl=QtGui.QLabel("Format")
  165.           gridLayout2.addWidget(fmtlbl,1,4)
  166.           self.fmtCB=QtGui.QComboBox()
  167.           self.fmtCB.addItems(self.accessAction.readImageFormats().values())
  168.           gridLayout2.addWidget(self.fmtCB,1,5)
  169.  
  170.           #Create a validator for each frame number text box
  171.           self.sfEdt.setValidator(QtGui.QIntValidator())
  172.           self.efEdt.setValidator(QtGui.QIntValidator())
  173.           self.byfrEdt.setValidator(QtGui.QIntValidator())
  174.  
  175.           hbox=QtGui.QHBoxLayout()
  176.           rgbchkBox=QtGui.QCheckBox("RGB")
  177.           rgbchkBox.setCheckState(QtCore.Qt.Checked)
  178.           alpchkBox=QtGui.QCheckBox("Alpha")
  179.           depchkBox=QtGui.QCheckBox("Z-Depth")
  180.           addCmdlbl=QtGui.QLabel("Additional Commands")
  181.           addCmdEdt=QtGui.QLineEdit()
  182.  
  183.           hLine=QtGui.QFrame(self)
  184.           hLine.setFrameShape(QtGui.QFrame.HLine)
  185.           hLine.setFrameShadow(QtGui.QFrame.Raised)
  186.           nxtbtnbox=QtGui.QHBoxLayout()
  187.           ## Render Buttons
  188.           self.bgnBatRndBtn=QtGui.QPushButton("Render Current")
  189.           addTaskBtn=QtGui.QPushButton("Add to Render Queue")
  190.           nxtbtnbox.addWidget(self.bgnBatRndBtn)
  191.           nxtbtnbox.addWidget(addTaskBtn)
  192.  
  193.           hbox.addWidget(rgbchkBox)
  194.           hbox.addWidget(alpchkBox)
  195.           hbox.addWidget(depchkBox)
  196.           hbox.addStretch(1)
  197.           hbox.addWidget(addCmdlbl)
  198.  
  199.           hbox.addWidget(addCmdEdt)
  200.  
  201.           #add status bar
  202.           self.statusbar=QtGui.QStatusBar()
  203.           self.statusbar.showMessage("Ready")
  204.  
  205.           # assign signals to slots
  206.           scnFile=partial(self.showFileDialog,"scnFile")
  207.           self.browseBtn.clicked.connect(scnFile)
  208.           rndPath=partial(self.showFileDialog,"rdPath")
  209.           self.rdBtn.clicked.connect(rndPath)
  210.           self.scnFilePath.textChanged.connect(self.execMayapy)
  211.           self.bgnBatRndBtn.clicked.connect(self.writeBatFile)
  212.  
  213.           # add layouts that contain inside of tab1 widgets
  214.           p1_vertical.addLayout(gridLayout)
  215.           p1_vertical.addLayout(gridLayout2)
  216.           p1_vertical.addLayout(hbox)
  217.           p1_vertical.addWidget(hLine)
  218.           p1_vertical.addLayout(nxtbtnbox)
  219.           self.settingTabContent()
  220.           vbox = QtGui.QVBoxLayout()
  221.           vbox.setMargin(0)
  222.           vbox.addWidget(fileMenu)
  223.  
  224.           vbox.addLayout(chkboxLayout)
  225.           vbox.addWidget(tab_widget)
  226.           vbox.addWidget(self.statusbar)
  227.           self.setLayout(vbox)
  228.  
  229.     def makebatFileTasks(self):
  230.  
  231.         pass
  232.  
  233.     def writeBatFile(self):
  234.         now = datetime.datetime.now()
  235.  
  236.         buildCrntTime=str(now.hour) +"_" + str(now.minute)
  237.         selected=str(self.scnFilePath.text())
  238.         quikBatNam=os.path.basename(selected).split(".")[0]+"_"+buildCrntTime+".bat"
  239.         self.batfiletoSave=os.path.join(os.path.split(selected)[0],quikBatNam)
  240.         try:
  241.             writeBat=open(self.batfiletoSave,'w')
  242.         except: pass
  243.         finally: writeBat.close()
  244.  
  245.  
  246.     def execMayapy(self):
  247.         ## call to readMayaFile.py using mayapy.exe interpreter
  248.         fileToOpen=str(self.scnFilePath.text())
  249.         fileName=os.path.join(os.path.split(__file__)[0],"readMayaFile.py "+fileToOpen)
  250.         test="mayapy "+ fileName
  251.         process = subprocess.Popen(test, shell=True, stdout=subprocess.PIPE)
  252.         process.wait()
  253.         if process.returncode==0: # 0 = success, optional check
  254.            print "Success Reading: %s" % os.path.basename(fileToOpen)
  255.            # read the result to a string
  256.            self.objRead = ast.literal_eval(process.stdout.read())
  257.            print self.objRead
  258.            self.fillInputs()
  259.  
  260.         else: print "Unable to read maya scene file: %s" % os.path.basename(fileToOpen)
  261.  
  262.     def fillInputs(self):
  263.  
  264.         self.camCB.clear()
  265.         self.camCB.addItems(self.objRead['camsLst'])
  266.         self.sfEdt.setText(str(self.objRead['startFrame']))
  267.         self.efEdt.setText(str(self.objRead['endFrame']))
  268.         self.byfrEdt.setText(str(self.objRead['stepByFrame']))
  269.         ## Select the default renderer
  270.         if str(self.objRead['defaultRenderer'])=='mayaSoftware':
  271.            self.rndCB.setCurrentIndex(self.renderer.keys().index('Maya Software'))
  272.  
  273.         elif str(self.objRead['defaultRenderer'])=='mayaHardware':
  274.              self.rndCB.setCurrentIndex(self.renderer.keys().index('Maya Hardware'))
  275.  
  276.         elif str(self.objRead['defaultRenderer'])=='mentalRay':
  277.              self.rndCB.setCurrentIndex(self.renderer.keys().index('Mental Ray'))
  278.  
  279.         elif str(self.objRead['defaultRenderer'])=='mayaVector':
  280.              self.rndCB.setCurrentIndex(self.renderer.keys().index('Maya Vecter'))
  281.  
  282.         elif str(self.objRead['defaultRenderer'])=='mayaHardware2':
  283.              self.rndCB.setCurrentIndex(self.renderer.keys().index('Maya Hardware 2.0'))
  284.  
  285.         else: self.rndCB.setCurrentIndex(self.renderer.keys().index(str(self.objRead['defaultRenderer'])))
  286.  
  287.  
  288.  
  289.  
  290.     def showFileDialog(self,*args):
  291.         directory=os.path.expanduser('~')
  292.         fileFilters="Maya Ascii/Binary(*.ma *.mb);; Maya Ascii(*.ma);; Maya Binary( *.mb )"
  293.  
  294.         if args[0]=="scnFile":
  295.             fname=str(QtGui.QFileDialog.getOpenFileName(self,'Open File',directory,self.tr(fileFilters)))
  296.             if os.path.isfile(fname):
  297.                 fname=os.path.normpath(fname)
  298.                 if args[1]=="appFile":
  299.                     self.appExeEdt.setText(fname)
  300.                 else:
  301.                     self.scnFilePath.setText(fname)
  302.         elif args[0]=="rdPath":
  303.             selectedDir=str(QtGui.QFileDialog.getExistingDirectory(self,"Select Render Directory",directory))
  304.             if not selectedDir:
  305.                 print "No directory selected"
  306.             else:
  307.                 selectedDir=os.path.normpath(selectedDir)
  308.                 self.renDir.setText(selectedDir)
  309.  
  310.  
  311.     def updateAppPaths(self):
  312.         appSel = str(self.appExeEdt.text())
  313.         if os.path.isfile(appSel):
  314.            if os.path.split(appSel)[-1]=="maya.exe":
  315.               self.items['Maya Executable']=appSel
  316.            else:
  317.                 appSel=os.path.join(os.path.split(appSel)[0],"maya.exe")
  318.                 if os.path.isfile(appSel):
  319.                    self.items['Maya Executable']=appSel
  320.            mayapy=os.path.join(os.path.split(appSel)[0],"mayapy.exe")
  321.            if os.path.isfile(mayapy):
  322.              self.items['Mayapy Interpreter']=mayapy
  323.            imgcvt=os.path.join(os.path.split(appSel)[0],"imgcvt.exe")
  324.            if os.path.isfile(imgcvt):
  325.              self.items['imgcvt']=imgcvt
  326.            IMConvert=os.path.join(os.path.split(appSel)[0],"imconvert.exe")
  327.            if os.path.isfile(IMConvert):
  328.              self.items['IMConvert']=IMConvert
  329.            Render=os.path.join(os.path.split(appSel)[0],"render.exe")
  330.            if os.path.isfile(Render):
  331.              self.items['Render']=Render
  332.  
  333.  
  334.     def savSettingFile(self):
  335.         """Saves Maya Application paths to setting file on disk"""
  336.         try:
  337.             if not os.path.isdir(self.settingsDir):
  338.                os.makedirs(self.settingsDir)
  339.  
  340.             output_phile=open(self.maSetfile,"w")
  341.             pickle.dump(self.items,output_phile,1)
  342.         except Exception as e:
  343.                print e
  344.         finally:
  345.                 output_phile.close()
  346.  
  347.     def readSettingFile(self):
  348.         """ Read the setting file that contains the Maya Application paths"""
  349.         try:
  350.             read_phile=open(self.maSetfile,"r")
  351.             self.items=pickle.load(read_phile)
  352.         except Exception as e:
  353.                print e
  354.         finally:
  355.                 read_phile.close()
  356.         return self.items
  357.  
  358.     def settingTabContent(self):
  359.         """
  360.        Content of the settings tab and signal to slot conntections
  361.        """
  362.         p3_HBox=QtGui.QHBoxLayout()
  363.         self.appExeCB=QtGui.QComboBox()
  364.         self.appExeCB.addItems(self.items.keys())
  365.         self.appExeCB.setCurrentIndex(self.items.keys().index('Maya Executable'))
  366.         self.appExeEdt=QtGui.QLineEdit()
  367.         self.brwBtn=QtGui.QPushButton("Browse")
  368.         p3_HBox.addWidget(self.appExeCB)
  369.         p3_HBox.addWidget(self.appExeEdt)
  370.         p3_HBox.addWidget(self.brwBtn)
  371.  
  372.  
  373.         p3_HBox2=QtGui.QHBoxLayout()
  374.         self.pylbl=QtGui.QLabel("Python Script")
  375.         self.pyEdt=QtGui.QLineEdit()
  376.         self.pybrwBtn=QtGui.QPushButton("Browse")
  377.  
  378.         p3_HBox3=QtGui.QHBoxLayout()
  379.         self.savSetBtn=QtGui.QPushButton("Save Settings")
  380.  
  381.         p3_HBox3.addStretch(1)
  382.         p3_HBox3.addWidget(self.savSetBtn)
  383.         p3_HBox2.addStretch(1)
  384.  
  385.  
  386.         #Connect buttons to slots
  387.         self.appFiles=partial(self.showFileDialog,"scnFile","appFile")
  388.         self.brwBtn.clicked.connect(self.appFiles)
  389.         self.appExeEdt.textChanged.connect(self.updateAppPaths)
  390.         self.appExeCB.currentIndexChanged.connect(self.appExeCBSelChange)
  391.         #Save maya application paths (self.items) to a file on disk
  392.         self.savSetBtn.clicked.connect(self.savSettingFile)
  393.  
  394.  
  395.         [p3_HBox2.addWidget(each) for each in [self.pylbl,self.pyEdt,self.pybrwBtn]]
  396.         self.pyEdt.setFixedWidth(261)
  397.  
  398.  
  399.         self.p3_vertical.addLayout(p3_HBox)
  400.         self.p3_vertical.addLayout(p3_HBox2)
  401.         self.p3_vertical.addStretch(1)
  402.         self.p3_vertical.addLayout(p3_HBox3)
  403.         self.initializeSetting()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement