Last active
October 3, 2019 16:19
-
-
Save Onefabis/3285bcbe47a7bf4399d0ce7df2c9d2e2 to your computer and use it in GitHub Desktop.
controller colorizer for maya
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
''' | |
Author: Alexander Smirnov | |
Version: 1.0 | |
Instruction: | |
Just paste in script editor this two lines to launch the script: | |
import colorizer | |
colorizer.start() | |
''' | |
import maya.cmds as mc | |
from functools import partial | |
import ast | |
import colorsys | |
class colorizer(object): | |
# Initial buttons colors | |
initColors = [ | |
(39, 255, 181), (23, 132, 228), (159, 23, 228), (228, 23, 149), (228, 23, 64), (228, 125, 23), (210, 228, 23), (64, 228, 23), (23, 47, 228), (246, 255, 83), | |
(255, 33, 90), (255, 96, 15), (194, 255, 64), (86, 242, 128), (0, 168, 182), (126, 165, 255), (95, 86, 242), (147, 58, 255), (255, 217, 73), (159, 209, 255), | |
(255, 132, 109), (0, 205, 17), (255, 34, 11), (73, 41, 255), (235, 124, 84), (189, 255, 163), (220, 177, 0), (175, 62, 75), (110, 255, 73), (255, 161, 251) | |
] | |
def _init_(self): | |
pass | |
# UI | |
def colorizerUI(self): | |
winName = 'Colorizer' | |
if mc.window( winName, ex=1 ): | |
mc.deleteUI( winName ) | |
mainWin = mc.window( winName, s=1 ) | |
mainLayout = mc.formLayout( p=mainWin ) | |
# UI controllers | |
self.colSlider = mc.colorSliderGrp( rgb=(0, 0, 1), adj=3, cw=( 1, 40 ), p=mainLayout, dc=partial(self.changeCol, None ), cc=lambda *args: self.saveSel() ) | |
if mc.optionVar( exists='colorizerRGB' ): | |
col = mc.optionVar( q='colorizerRGB' ) | |
mc.colorSliderGrp( self.colSlider, e=1, rgb=col ) | |
self.outCheckbox = mc.checkBox( l='Outliner', p=mainLayout, v=0, cc=lambda *args: self.changeData() ) | |
mc.popupMenu() | |
mc.menuItem( l='Reset shape', c=lambda *args: self.resetObjColor('outlinerShape') ) | |
mc.menuItem( l='Reset transform', c=lambda *args: self.resetObjColor('outlinerTransform') ) | |
self.shapeCheckbox = mc.checkBox( l='Shape', p=mainLayout, v=0, cc=lambda *args: self.changeData() ) | |
mc.popupMenu() | |
mc.menuItem( l='Reset', c=lambda *args: self.resetObjColor('shape') ) | |
self.transCheckbox = mc.checkBox( l='Transform', p=mainLayout, v=1, cc=lambda *args: self.changeData() ) | |
mc.popupMenu() | |
mc.menuItem( l='Reset', c=lambda *args: self.resetObjColor('transform') ) | |
self.hiCheckbox = mc.checkBox( l='Hi', p=mainLayout, v=0, cc=lambda *args: self.changeData() ) | |
mc.popupMenu() | |
mc.menuItem( l='Reset transforms', c=lambda *args: self.resetObjColor('hiTransform') ) | |
mc.menuItem( l='Reset shapes', c=lambda *args: self.resetObjColor('hiShape') ) | |
mc.menuItem( l='Reset outliner', c=lambda *args: self.resetObjColor('hiOutliner') ) | |
self.cbCheckbox = mc.checkBox( l='Ch Box', p=mainLayout, v=0, cc=lambda *args: self.changeData() ) | |
mc.popupMenu() | |
mc.menuItem( l='Integrate CB colorizer into scene', c=lambda *args: self.changeCBIntegration('add') ) | |
mc.menuItem( l='Remove CB colorizer from scene', c=lambda *args: self.changeCBIntegration('remove') ) | |
mc.menuItem( l='Reset channels', c=lambda *args: self.changeCBColor() ) | |
if mc.optionVar( exists='colorizerBottons' ): | |
colList = mc.optionVar( q='colorizerBottons' ) | |
if len(ast.literal_eval(colList)) == 30: | |
bCol = ast.literal_eval(colList) | |
else: | |
bCol = self.initColors | |
mc.optionVar( sv=( 'colorizerBottons', str(bCol) ) ) | |
else: | |
bCol = self.initColors | |
mc.optionVar( sv=( 'colorizerBottons', str(bCol) ) ) | |
for x in xrange(30): | |
mult = 1 | |
if mc.colorManagementPrefs(q=1, cme=1): | |
mult = 0.454 | |
exec( 'self.colButton%i = mc.button( bgc=%s, w=30, l="", c=partial(self.changeCol,%s) )' % ( x, str( [ (c / 255.0)**mult for c in bCol[x]] ), str(bCol[x]) ) ) in globals(), locals() | |
mc.popupMenu() | |
mc.menuItem( l='Save', c=partial(self.saveCol, x) ) | |
mc.menuItem( l='Reset', c=partial(self.resetCol, x) ) | |
mc.menuItem( l='Reset all', c=partial(self.resetCol, -1) ) | |
if mc.optionVar( exists='colorizerData' ): | |
cb = mc.optionVar( q='colorizerData' ) | |
if len( cb ) != 5: | |
if mc.optionVar( ex='colorizerData' ): | |
mc.optionVar( rm='colorizerData' ) | |
cb = [ 1, 0, 0, 0, 0 ] | |
mc.checkBox( self.outCheckbox, e=1, v=cb[0] ) | |
mc.checkBox( self.shapeCheckbox, e=1, v=cb[1] ) | |
mc.checkBox( self.transCheckbox, e=1, v=cb[2] ) | |
mc.checkBox( self.hiCheckbox, e=1, v=cb[3] ) | |
mc.checkBox( self.cbCheckbox, e=1, v=cb[4] ) | |
if cb[4]: | |
mc.checkBox( self.outCheckbox, e=1, en=0 ) | |
mc.checkBox( self.shapeCheckbox, e=1, en=0 ) | |
mc.checkBox( self.transCheckbox, e=1, en=0 ) | |
mc.checkBox( self.hiCheckbox, e=1, en=0 ) | |
mc.formLayout( mainLayout, e=1, af=( self.colSlider, "top", 15 ) ) | |
mc.formLayout( mainLayout, e=1, af=( self.colSlider, "right", 10 ) ) | |
mc.formLayout( mainLayout, e=1, af=( self.colSlider, "left", 10 ) ) | |
lays = {} | |
for l in xrange(3): | |
lays[l] = mc.formLayout( p=mainLayout ) | |
mc.formLayout( mainLayout, e=1, af=( lays[l], "left", 10 ) ) | |
mc.formLayout( mainLayout, e=1, af=( lays[l], "right", 10 ) ) | |
eval( 'mc.formLayout( mainLayout, e=1, af=( "%s", "left", 1 ) ) ' % lays[l] ) | |
eval( 'mc.formLayout( mainLayout, e=1, af=( "%s", "right", 1 ) ) ' % lays[l] ) | |
if l == 0: | |
eval( 'mc.formLayout( mainLayout, e=1, ac=( "%s", "top", 2, self.colSlider) ) ' % lays[l] ) | |
else: | |
eval( 'mc.formLayout( mainLayout, e=1, ac=( "%s", "top", 2, "%s") ) ' % (lays[l], lays[l - 1]) ) | |
for x in xrange(10): | |
eval( 'mc.button(self.colButton%i, e=1, p="%s")' % ( (x + 10 * l), lays[l] ) ) | |
eval( 'mc.formLayout( "%s", e=1, af=( self.colButton%i, "top", 2 ) ) ' % ( lays[l], (x + 10 * l) ) ) | |
if x == 0: | |
eval( 'mc.formLayout( "%s", e=1, af=( self.colButton%i, "left", 10 ) ) ' % ( lays[l], x + 10 * l ) ) | |
else: | |
eval( 'mc.formLayout( "%s", e=1, ac=( self.colButton%i, "left", 3, self.colButton%i ) ) ' % ( lays[l], x + 10 * l, (x + 10 * l) - 1) ) | |
mc.formLayout( mainLayout, e=1, ac=( self.transCheckbox, "top", 10, lays[2] ) ) | |
mc.formLayout( mainLayout, e=1, af=( self.transCheckbox, "left", 10 ) ) | |
mc.formLayout( mainLayout, e=1, ac=( self.shapeCheckbox, "top", 10, lays[2] ) ) | |
mc.formLayout( mainLayout, e=1, ac=( self.shapeCheckbox, "left", 10, self.transCheckbox ) ) | |
mc.formLayout( mainLayout, e=1, ac=( self.outCheckbox, "top", 10, lays[2] ) ) | |
mc.formLayout( mainLayout, e=1, ac=( self.outCheckbox, "left", 10, self.shapeCheckbox ) ) | |
mc.formLayout( mainLayout, e=1, ac=( self.hiCheckbox, "top", 10, lays[2] ) ) | |
mc.formLayout( mainLayout, e=1, ac=( self.hiCheckbox, "left", 10, self.outCheckbox ) ) | |
mc.formLayout( mainLayout, e=1, ac=( self.cbCheckbox, "top", 10, lays[2] ) ) | |
mc.formLayout( mainLayout, e=1, ac=( self.cbCheckbox, "left", 10, self.hiCheckbox ) ) | |
mc.showWindow(mainWin) | |
mc.window( winName, e=1, wh=(347, 150) ) | |
def changeCol(self, color, part): | |
global colorizerSelection | |
global colorizerChangedColor | |
sel = mc.ls( sl=1, l=1 ) | |
if color: | |
col = [ c / 255.0 for c in color ] | |
mc.colorSliderGrp(self.colSlider, e=1, rgb=col) | |
else: | |
col = mc.colorSliderGrp(self.colSlider, q=1, rgb=1 ) | |
if 'colorizerSelection' not in globals() or len(colorizerSelection) == 0: | |
colorizerSelection = mc.ls( sl=1, l=1 ) | |
mc.undoInfo(ock=1) | |
if mc.checkBox( self.cbCheckbox, q=1, v=1 ) == 0: | |
mc.select( cl=1 ) | |
colorizerChangedColor = col | |
sel = colorizerSelection | |
if mc.checkBox( self.cbCheckbox, q=1, v=1 ): | |
self.changeCBColor( col ) | |
colAttrNodes = mc.ls( typ='colorAttributes' ) | |
if len(colAttrNodes) == 0: | |
self.changeCBIntegration('add') | |
else: | |
valOut = mc.checkBox( self.outCheckbox, q=1, v=1 ) | |
shapeOut = mc.checkBox( self.shapeCheckbox, q=1, v=1 ) | |
transOut = mc.checkBox( self.transCheckbox, q=1, v=1 ) | |
hiOut = mc.checkBox( self.hiCheckbox, q=1, v=1 ) | |
# Save color to option variable | |
mc.optionVar( fv=( 'colorizerRGB', col[0] ) ) | |
mc.optionVar( fva=( 'colorizerRGB', col[1] ) ) | |
mc.optionVar( fva=( 'colorizerRGB', col[2] ) ) | |
mult = 1 | |
if mc.colorManagementPrefs(q=1, cme=1): | |
mult = 0.454 | |
if hiOut: | |
chTransList = [] | |
for k in sel: | |
chTransList.extend( mc.listRelatives( k, c=1, typ='transform', ad=1, f=1 ) ) | |
chTransList.extend( mc.listRelatives( k, c=1, typ='joint', ad=1, f=1 ) ) | |
sel = list( set(sel + chTransList) ) | |
# Apply slider color to selected transform and shape nodes | |
for s in sel: | |
if mc.ls( s, st=1 )[-1] == 'transform' or mc.ls( s, st=1 )[-1] == 'joint': | |
if transOut: | |
if valOut: | |
mc.setAttr( s + '.useOutlinerColor', 1 ) | |
mc.setAttr( s + '.outlinerColor', col[0]**mult, col[1]**mult, col[2]**mult, type='float3' ) | |
mc.setAttr( s + '.overrideEnabled', 1) | |
mc.setAttr( s + '.overrideRGBColors', 1) | |
mc.setAttr( s + '.overrideColorRGB', col[0], col[1], col[2], type='float3' ) | |
shape = mc.listRelatives( s, s=1, f=1 ) | |
if shape and shapeOut: | |
for sh in shape: | |
if valOut: | |
mc.setAttr( sh + '.outlinerColor', col[0]**mult, col[1]**mult, col[2]**mult, type='float3' ) | |
mc.setAttr( sh + '.useOutlinerColor', 1) | |
mc.setAttr( sh + '.overrideEnabled', 1) | |
mc.setAttr( sh + '.overrideRGBColors', 1) | |
mc.setAttr( sh + '.overrideColorRGB', col[0], col[1], col[2], type='float3' ) | |
colorizerChangedColor = None | |
def changeCBColor( self, attrColor=(0.265, 0.265, 0.265) ): | |
selAttr = mc.channelBox( "mainChannelBox", q=1, sma=1 ) | |
if not selAttr: | |
return | |
replaceDefaultAttr = { | |
'tx': 'translateX', | |
'ty': 'translateY', | |
'tz': 'translateZ', | |
'rx': 'rotateX', | |
'ry': 'rotateY', | |
'rz': 'rotateZ', | |
'sx': 'scaleX', | |
'sy': 'scaleY', | |
'sz': 'scaleZ', | |
'v': 'visibility'} | |
#print selAttr | |
colorAttributes = {} | |
if mc.checkBox( self.cbCheckbox, q=1, v=1 ): | |
colAttrNodes = mc.ls( typ='colorAttributes' ) | |
if len(colAttrNodes) == 0: | |
self.changeCBIntegration('add') | |
for s in selAttr: | |
if s in replaceDefaultAttr.keys(): | |
s = replaceDefaultAttr[s] | |
colorAttributes[s] = attrColor | |
mc.channelBox( "mainChannelBox", e=1, attrRegex=s, attrColor=attrColor, attrBgColor=attrColor ) | |
if all( x == 0.265 for x in attrColor ): | |
mc.channelBox( "mainChannelBox", e=1, attrRegex=s, attrColor=(0.75, 0.75, 0.75) ) | |
colorAttributes[s] = (); | |
else: | |
lightness = colorsys.rgb_to_hls( attrColor[0], attrColor[1], attrColor[2] )[1] | |
if lightness > 0.45: | |
mc.channelBox( "mainChannelBox", e=1, attrRegex=s, attrColor=(0.0, 0.0, 0.0) ) | |
else: | |
mc.channelBox( "mainChannelBox", e=1, attrRegex=s, attrColor=(1.0, 1.0, 1.0) ) | |
sel = mc.ls( sl=1, l=1 ) | |
for so in sel: | |
notesStart = notesEnd = '' | |
if mc.attributeQuery( 'notes', n=so, ex=1 ): | |
notesString = mc.getAttr( so + '.notes' ) | |
if 'attributeColorBegin' in notesString: | |
notesStart = notesString.split( ' attributeColorBegin ', 1 )[0] | |
notesEnd = notesString.split( ' attributeColorEnd ', 1 )[-1] | |
notesColors = ast.literal_eval( notesString.split( ' attributeColorBegin ', 1 )[-1].split( ' attributeColorEnd ', 1 )[0] ) | |
z = notesColors.copy() | |
z.update(colorAttributes) | |
itemsToDelete = [] | |
for k, v in z.iteritems(): | |
if v == (): | |
itemsToDelete.append( k ) | |
for i in itemsToDelete: | |
z.pop( i, None ) | |
colorAttributes = z | |
else: | |
mc.addAttr( so, longName='notes', dt='string' ) | |
if colorAttributes: | |
mc.setAttr( so + '.notes', notesStart + ' attributeColorBegin ' + str(colorAttributes) + ' attributeColorEnd ' + notesEnd, type='string' ) | |
else: | |
mc.setAttr( so + '.notes', notesStart + notesEnd, type='string' ) | |
mc.channelBox( "mainChannelBox", e=1, u=1 ) | |
def changeCBIntegration(self, mode): | |
colAttrNodes = mc.ls( typ='colorAttributes' ) | |
if len(colAttrNodes) > 0: | |
mc.delete(colAttrNodes) | |
if mode == 'add': | |
if not mc.pluginInfo( 'colorAttributes', q=1, l=1 ): | |
try: | |
mc.loadPlugin( 'colorAttributes' ) | |
except: | |
mc.warning( "colorAttributes plugin isn't installed" ) | |
return | |
mc.createNode( 'colorAttributes', ss=1 ) | |
def saveCol( self, button, part): | |
bCol = mc.optionVar( q='colorizerBottons' ) | |
col = ast.literal_eval(bCol) | |
colSlider = mc.colorSliderGrp(self.colSlider, q=1, rgb=1 ) | |
newCol = (int(colSlider[0] * 255), int(colSlider[1] * 255), int(colSlider[2] * 255)) | |
col[button] = ( newCol ) | |
if mc.colorManagementPrefs(q=1, cme=1): | |
colSlider = tuple([ x**0.454 for x in colSlider ]) | |
eval( 'mc.button( self.colButton%i, e=1, bgc=%s, c=partial(self.changeCol,%s) )' % ( button, str( colSlider ), str(newCol) ) ) | |
mc.optionVar( sv=( 'colorizerBottons', str(col) ) ) | |
def resetCol( self, button, part ): | |
mult = 1.0 | |
if mc.colorManagementPrefs(q=1, cme=1): | |
mult = 0.454 | |
if button >= 0: | |
eval( 'mc.button( self.colButton%i, e=1, bgc=%s, c=partial(self.changeCol,%s) )' % ( button, str( [(m / 255.0)**mult for m in self.initColors[button] ] ), self.initColors[button] ) ) | |
bCol = mc.optionVar( q='colorizerBottons' ) | |
col = ast.literal_eval(bCol) | |
col[button] = self.initColors[button] | |
mc.optionVar( sv=( 'colorizerBottons', str(col) ) ) | |
else: | |
for x in xrange(30): | |
eval( 'mc.button( self.colButton%i, e=1, bgc=%s, c=partial(self.changeCol,%s) )' % ( x, str( [(m / 255.0)**mult for m in self.initColors[x] ] ), self.initColors[x] ) ) | |
mc.optionVar( sv=( 'colorizerBottons', str(self.initColors) ) ) | |
def changeData( self ): | |
valOut = mc.checkBox( self.outCheckbox, q=1, v=1 ) | |
shapeOut = mc.checkBox( self.shapeCheckbox, q=1, v=1 ) | |
transOut = mc.checkBox( self.transCheckbox, q=1, v=1 ) | |
hiOut = mc.checkBox( self.hiCheckbox, q=1, v=1 ) | |
cbOut = mc.checkBox( self.cbCheckbox, q=1, v=1 ) | |
if cbOut: | |
mc.checkBox( self.outCheckbox, e=1, en=0) | |
mc.checkBox( self.shapeCheckbox, e=1, en=0) | |
mc.checkBox( self.transCheckbox, e=1, en=0) | |
mc.checkBox( self.hiCheckbox, e=1, en=0) | |
else: | |
mc.checkBox( self.outCheckbox, e=1, en=1) | |
mc.checkBox( self.shapeCheckbox, e=1, en=1) | |
mc.checkBox( self.transCheckbox, e=1, en=1) | |
mc.checkBox( self.hiCheckbox, e=1, en=1) | |
mc.optionVar( iv=( 'colorizerData', valOut ) ) | |
mc.optionVar( iva=( 'colorizerData', shapeOut ) ) | |
mc.optionVar( iva=( 'colorizerData', transOut ) ) | |
mc.optionVar( iva=( 'colorizerData', hiOut ) ) | |
mc.optionVar( iva=( 'colorizerData', cbOut ) ) | |
def saveSel( self ): | |
global colorizerSelection | |
global colorizerChangedColor | |
if 'colorizerChangedColor' in globals(): | |
if colorizerChangedColor is None: | |
mc.select( colorizerSelection, r=1 ) | |
mc.undoInfo(cck=1) | |
colorizerChangedColor = 1 | |
colorizerSelection = [] | |
def resetObjColor( self, obj ): | |
sel = mc.ls( sl=1, l=1 ) | |
if obj == 'hiTransform' or obj == 'hiShape' or obj == 'hiOutliner': | |
chTransList = [] | |
for k in sel: | |
chTrans = mc.listRelatives( k, c=1, typ='transform', ad=1, f=1 ) | |
if chTrans: | |
chTransList.extend(chTrans) | |
sel = list( set(sel + chTransList) ) | |
for s in sel: | |
if mc.ls( s, st=1 )[-1] == 'transform': | |
if obj == 'transform' or obj == 'hiTransform': | |
mc.setAttr( s + '.overrideRGBColors', 0) | |
mc.setAttr( s + '.overrideColor', 0 ) | |
elif obj == 'outlinerTransform': | |
mc.setAttr( s + '.useOutlinerColor', 0 ) | |
elif obj == 'shape' or obj == 'hiShape': | |
shape = mc.listRelatives( s, s=1, f=1 ) | |
for sh in shape: | |
mc.setAttr( sh + '.overrideRGBColors', 0) | |
mc.setAttr( sh + '.overrideColor', 0 ) | |
elif obj == 'outlinerShape': | |
shape = mc.listRelatives( s, s=1, f=1 ) | |
for sh in shape: | |
mc.setAttr( sh + '.useOutlinerColor', 0 ) | |
elif obj == 'hiOutliner': | |
mc.setAttr( s + '.useOutlinerColor', 0 ) | |
shape = mc.listRelatives( s, s=1, f=1 ) | |
for sh in shape: | |
mc.setAttr( sh + '.useOutlinerColor', 0 ) | |
def start(): | |
colorizer().colorizerUI() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment