View unanswered posts
View active topics
It is currently Tue Dec 12, 2017 12:30 pm


Author Message
ymangolds
Post  Post subject: FB matte control tool for Maya  |  Posted: Sun Feb 14, 2016 6:47 am
User avatar

Joined: Fri Aug 17, 2012 8:23 am
Posts: 89

Offline
Attachment:
fbMatte.JPG
fbMatte.JPG [ 20.72 KiB | Viewed 12011 times ]

Here's a simple script to easily make 8 channel + alpha multi-matte passes for easy to key mattes during compositing. Especially useful when combined with either of these multi-matte gizmos in Nuke:


Instructions:
  1. Create a new file named 'fbMatte.py' in your maya scripts directory (usually "[My Documents]\maya\20xx\scripts")
  2. Open the file in a text editor and copy the code below into that file and save it
  3. In a python tab in your script editor in Maya, type 'import fbMatte' and run it (can also highlight it and middle mouse button drag it onto a shelf to make yourself a button for it)
  4. Select objects (and/or fb materials) in your scene you want to change the matte color on
  5. Press the button of the color (or alpha value) you want it to be in the control window
  6. In your passes tab of your render settings, make sure to enable 'Matte'

Important Notes!!!
  • Requires pyside (included in Maya 2014+ I believe)
  • Only works on FurryBall materials
  • Even though this is meant to be used by selecting objects, it's important to remember that it gets applied to the FB materials that are assigned to them, so other objects that also use those materials will also be the chosen matte color!
  • Buttons with 'All' written on it will be applied to ALL furryball materials in the scene
  • 'Random All' will give make the matte of every FB material in the scene a different random color (basically makes you a 'material ID pass')
  • Alpha can be very useful because you can group things together that have different matte colors!

Code:
from PySide import QtCore, QtGui
from shiboken import wrapInstance
import maya.OpenMayaUI as omui
import pymel.core as pm

def maya_main_window():
   main_window_ptr = omui.MQtUtil.mainWindow()
   return wrapInstance(long(main_window_ptr), QtGui.QWidget)

class ControlMainWindow(QtGui.QMainWindow):

   def __init__(self, parent=None):
      self.used =[]

      super(ControlMainWindow, self).__init__(parent)
      self.setWindowFlags(QtCore.Qt.Tool)
      self.ui = Ui_MainWindow()
      self.ui.setupUi(self)

      self.ui.redBtn.clicked.connect(lambda: self.setMatte([1,0,0]))
      self.ui.grnBtn.clicked.connect(lambda: self.setMatte([0,1,0]))
      self.ui.bluBtn.clicked.connect(lambda: self.setMatte([0,0,1]))
      self.ui.magBtn.clicked.connect(lambda: self.setMatte([1,0,1]))
      self.ui.yelBtn.clicked.connect(lambda: self.setMatte([1,1,0]))
      self.ui.cyaBtn.clicked.connect(lambda: self.setMatte([0,1,1]))
      self.ui.whtBtn.clicked.connect(lambda: self.setMatte([1,1,1]))
      self.ui.blkBtn.clicked.connect(lambda: self.setMatte([0,0,0]))
      self.ui.resetBtn.clicked.connect(self.resetMatte)
      self.ui.randomBtn.clicked.connect(self.randomMatte)
      self.ui.a0Btn.clicked.connect(lambda: self.setMatteAlpha(0))
      self.ui.a1Btn.clicked.connect(lambda: self.setMatteAlpha(1))
      self.ui.a0aBtn.clicked.connect(lambda: self.setAllMatteAlpha(0))
      self.ui.a1aBtn.clicked.connect(lambda: self.setAllMatteAlpha(1))


   def setMatte(self, color = [0,0,0]):
      fbmats = self.getShadersFromObj(pm.ls(sl=1), type='furryballMaterial')
      if fbmats:
         for fbmat in fbmats:
            fbmat.matteColor.set(color)

   def setMatteAlpha(self, color = 0):
      fbmats = self.getShadersFromObj(pm.ls(sl=1), type='furryballMaterial')
      if fbmats:
         for fbmat in fbmats:
            fbmat.matteOpacity.set(color)
   def setAllMatteAlpha(self, color = 0):
      fbmats = pm.ls(type='furryballMaterial')
      for fbmat in fbmats:
         fbmat.matteOpacity.set(color)
   def resetMatte(self):
      fbmats = pm.ls(type='furryballMaterial')
      for fbmat in fbmats:
         fbmat.matteColor.set([0,0,0])

   def randomMatte(self):
      fbmats = pm.ls(type='furryballMaterial')
      self.used =[]
      for fbmat in fbmats:
         attempt = 0
         while True:
            attempt += 1
            red = random.uniform(0.1,1)
            green = random.uniform(0.1,1)
            blue = random.uniform(0.1,1)
            if isNotUsed([red,green,blue]) or attempt >= 20:
               break
         self.used.append([red,green,blue])
         fbmat.matteColor.set([red,green,blue])

   def isNotUsed(self,new):
      if not len(self.used):
         return True
      for use in self.used:
         if abs(new[0]-use[0]) <=0.1 and abs(new[1]-use[1]) <=0.1 and abs(new[2]-use[2]) <=0.1:
            return False
         elif round(new[0]-use[0],1) == round(new[1]-use[1],1) == round(new[2]-use[2],1):
            return False
      return True
   def getShadersFromObj(self,objs,type=None):
      if type is None:
         mats = set(pm.ls(objs,mat=1))
      else:
         mats = set(pm.ls(objs,type=type))
      shapes = pm.ls(objs,o=1,dag=1)
      if shapes:
         sg = pm.listConnections(shapes,type='shadingEngine')
         if type is None:
            mats |= set(pm.ls(pm.listConnections(sg),mat=1))
         else:
            mats |= set(pm.ls(pm.listConnections(sg),type=type))
      result = list(mats)
      return result

class Ui_MainWindow(object):
   def setupUi(self, MainWindow):
      MainWindow.setObjectName("MainWindow")
      MainWindow.resize(364, 75)
      self.centralwidget = QtGui.QWidget(MainWindow)
      self.centralwidget.setObjectName("centralwidget")
      self.redBtn = QtGui.QPushButton(self.centralwidget)
      self.redBtn.setGeometry(QtCore.QRect(0, 0, 71, 21))
      self.redBtn.setStyleSheet("background-color: rgb(255, 0, 0);\n"
"color: rgb(0, 0, 0);")
      self.redBtn.setObjectName("redBtn")
      self.grnBtn = QtGui.QPushButton(self.centralwidget)
      self.grnBtn.setGeometry(QtCore.QRect(70, 0, 71, 21))
      self.grnBtn.setStyleSheet("background-color: rgb(0, 255, 0);\n"
"color: rgb(0, 0, 0);")
      self.grnBtn.setObjectName("grnBtn")
      self.bluBtn = QtGui.QPushButton(self.centralwidget)
      self.bluBtn.setGeometry(QtCore.QRect(140, 0, 71, 21))
      self.bluBtn.setStyleSheet("background-color: rgb(0, 0, 255);\n"
"color: rgb(255, 255, 255);")
      self.bluBtn.setObjectName("bluBtn")
      self.magBtn = QtGui.QPushButton(self.centralwidget)
      self.magBtn.setGeometry(QtCore.QRect(0, 20, 71, 21))
      self.magBtn.setStyleSheet("background-color: rgb(255, 0, 255);\n"
"color: rgb(0, 0, 0);")
      self.magBtn.setObjectName("magBtn")
      self.cyaBtn = QtGui.QPushButton(self.centralwidget)
      self.cyaBtn.setGeometry(QtCore.QRect(140, 20, 71, 21))
      self.cyaBtn.setStyleSheet("background-color: rgb(0, 255, 255);\n"
"color: rgb(0, 0, 0);")
      self.cyaBtn.setObjectName("cyaBtn")
      self.yelBtn = QtGui.QPushButton(self.centralwidget)
      self.yelBtn.setGeometry(QtCore.QRect(70, 20, 71, 21))
      self.yelBtn.setStyleSheet("background-color: rgb(255, 255, 0);\n"
"color: rgb(0, 0, 0);")
      self.yelBtn.setObjectName("yelBtn")
      self.blkBtn = QtGui.QPushButton(self.centralwidget)
      self.blkBtn.setGeometry(QtCore.QRect(210, 20, 71, 21))
      self.blkBtn.setStyleSheet("background-color: rgb(0, 0, 0);\n"
"color: rgb(255, 255, 255);")
      self.blkBtn.setObjectName("blkBtn")
      self.whtBtn = QtGui.QPushButton(self.centralwidget)
      self.whtBtn.setGeometry(QtCore.QRect(210, 0, 71, 21))
      self.whtBtn.setStyleSheet("background-color: rgb(255, 255, 255);\n"
"color: rgb(0, 0, 0);")
      self.whtBtn.setObjectName("whtBtn")
      self.resetBtn = QtGui.QPushButton(self.centralwidget)
      self.resetBtn.setGeometry(QtCore.QRect(290, 0, 71, 21))
      self.resetBtn.setStyleSheet("color: rgb(255, 255, 255);\n"
"text-decoration: underline;\n"
"font: 75 8pt \"MS Shell Dlg 2\";\n"
"background-color: rgb(125, 125, 125);")
      self.resetBtn.setObjectName("resetBtn")
      self.randomBtn = QtGui.QPushButton(self.centralwidget)
      self.randomBtn.setGeometry(QtCore.QRect(290, 20, 71, 21))
      self.randomBtn.setStyleSheet("background-color: qlineargradient(spread:pad, x1:0, y1:0, x2:1, y2:0, stop:0 rgba(255, 0, 0, 255), stop:0.166 rgba(255, 255, 0, 255), stop:0.333 rgba(0, 255, 0, 255), stop:0.5 rgba(0, 255, 255, 255), stop:0.666 rgba(0, 0, 255, 255), stop:0.833 rgba(255, 0, 255, 255), stop:1 rgba(255, 0, 0, 255));\n"
"text-decoration: underline;\n"
"font: 75 8pt \"MS Shell Dlg 2\";\n"
"color: rgb(0, 0, 0);")
      self.randomBtn.setObjectName("randomBtn")
      self.a0Btn = QtGui.QPushButton(self.centralwidget)
      self.a0Btn.setGeometry(QtCore.QRect(0, 50, 71, 21))
      self.a0Btn.setStyleSheet("background-color: rgb(0, 0, 0);\n"
"color: rgb(255, 255, 255);")
      self.a0Btn.setObjectName("a0Btn")
      self.a1Btn = QtGui.QPushButton(self.centralwidget)
      self.a1Btn.setGeometry(QtCore.QRect(70, 50, 71, 21))
      self.a1Btn.setStyleSheet("background-color: rgb(255, 255, 255);\n"
"color: rgb(0, 0, 0);")
      self.a1Btn.setObjectName("a1Btn")
      self.a0aBtn = QtGui.QPushButton(self.centralwidget)
      self.a0aBtn.setGeometry(QtCore.QRect(150, 50, 71, 21))
      self.a0aBtn.setStyleSheet("background-color: qlineargradient(spread:pad, x1:0, y1:0, x2:0, y2:1, stop:0 rgba(0, 0, 0, 255), stop:0.505682 rgba(126, 126, 126, 255), stop:1 rgba(0, 0, 0, 255));\n"
"text-decoration: underline;\n"
"font: 75 8pt \"MS Shell Dlg 2\";\n"
"color: rgb(255, 255, 255);")
      self.a0aBtn.setObjectName("a0aBtn")
      self.a1aBtn = QtGui.QPushButton(self.centralwidget)
      self.a1aBtn.setGeometry(QtCore.QRect(220, 50, 71, 21))
      self.a1aBtn.setStyleSheet("color: rgb(0, 0, 0);\n"
"text-decoration: underline;\n"
"font: 75 8pt \"MS Shell Dlg 2\";\n"
"background-color: qlineargradient(spread:pad, x1:0, y1:0, x2:0, y2:1, stop:0 rgba(255, 255, 255, 255), stop:0.482955 rgba(178, 178, 178, 255), stop:1 rgba(255, 255, 255, 255));")
      self.a1aBtn.setObjectName("a1aBtn")
      MainWindow.setCentralWidget(self.centralwidget)

      self.retranslateUi(MainWindow)
      QtCore.QMetaObject.connectSlotsByName(MainWindow)

   def retranslateUi(self, MainWindow):
      MainWindow.setWindowTitle(QtGui.QApplication.translate("MainWindow", "FurryBall Matte Control", None, QtGui.QApplication.UnicodeUTF8))
      self.redBtn.setText(QtGui.QApplication.translate("MainWindow", "Red", None, QtGui.QApplication.UnicodeUTF8))
      self.grnBtn.setText(QtGui.QApplication.translate("MainWindow", "Green", None, QtGui.QApplication.UnicodeUTF8))
      self.bluBtn.setText(QtGui.QApplication.translate("MainWindow", "Blue", None, QtGui.QApplication.UnicodeUTF8))
      self.magBtn.setText(QtGui.QApplication.translate("MainWindow", "Magenta", None, QtGui.QApplication.UnicodeUTF8))
      self.cyaBtn.setText(QtGui.QApplication.translate("MainWindow", "Cyan", None, QtGui.QApplication.UnicodeUTF8))
      self.yelBtn.setText(QtGui.QApplication.translate("MainWindow", "Yellow", None, QtGui.QApplication.UnicodeUTF8))
      self.blkBtn.setText(QtGui.QApplication.translate("MainWindow", "Black", None, QtGui.QApplication.UnicodeUTF8))
      self.whtBtn.setText(QtGui.QApplication.translate("MainWindow", "White", None, QtGui.QApplication.UnicodeUTF8))
      self.resetBtn.setText(QtGui.QApplication.translate("MainWindow", "Reset All", None, QtGui.QApplication.UnicodeUTF8))
      self.randomBtn.setText(QtGui.QApplication.translate("MainWindow", "Random All", None, QtGui.QApplication.UnicodeUTF8))
      self.a0Btn.setText(QtGui.QApplication.translate("MainWindow", "Alpha 0", None, QtGui.QApplication.UnicodeUTF8))
      self.a1Btn.setText(QtGui.QApplication.translate("MainWindow", "Alpha 1", None, QtGui.QApplication.UnicodeUTF8))
      self.a0aBtn.setText(QtGui.QApplication.translate("MainWindow", "Alpha 0 All", None, QtGui.QApplication.UnicodeUTF8))
      self.a1aBtn.setText(QtGui.QApplication.translate("MainWindow", "Alpha 1 All", None, QtGui.QApplication.UnicodeUTF8))

myWin = ControlMainWindow(parent=maya_main_window())
myWin.show()


Last edited by ymangolds on Thu Feb 25, 2016 5:20 pm, edited 1 time in total.


Top
Jan_Tomanek
Post  Post subject: Re: FB matte control tool for Maya  |  Posted: Thu Feb 18, 2016 11:54 pm
User avatar

Joined: Tue Oct 27, 2009 11:03 pm
Posts: 1568
Location: Prague - Czech republic

Offline
Thanks - this is great work!

_________________
Thank you for contacting us.
If you have any further questions, please do not hesitate to contact us.

All the Best
Jan


Top
Display posts from previous:  Sort by  
Print view

Who is online
Users browsing this forum: No registered users and 1 guest
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum
Jump to:   
cron

Delete all board cookies | The team | All times are UTC + 1 hour [ DST ]

Powered by phpBB® Forum Software © phpBB Group
DAJ Glass 2 template created by Dustin Baccetti