Skip to content

Instantly share code, notes, and snippets.

@Onefabis
Created October 3, 2019 16:21
Show Gist options
  • Save Onefabis/67bc339bf8549d85bb941e3d49f4efeb to your computer and use it in GitHub Desktop.
Save Onefabis/67bc339bf8549d85bb941e3d49f4efeb to your computer and use it in GitHub Desktop.
Plugin for colorizer script in order to attribute color in the channel box
import ast
import sys
import maya.api.OpenMaya as om2
import maya.cmds as mc
import colorsys
# Plug-in information:
kPluginNodeName = 'colorAttributes'
kPluginNodeClassify = 'utility/general'
kPluginNodeId = om2.MTypeId( 0x86025 )
def maya_useNewAPI():
pass
class colorAttributes( om2.MPxNode ):
messageIDs = []
allCustomCBColorAttributes = {}
def __init__( self ):
om2.MPxNode.__init__( self )
self.messageIDs.append( om2.MEventMessage.addEventCallback( 'SelectionChanged', self.callbackSelection ) )
self.messageIDs.append( om2.MDGMessage.addNodeRemovedCallback( self.remove, 'dependNode' ) )
def callbackSelection( self, *args ):
sel = om2.MGlobal.getActiveSelectionList()
if sel.length() > 0:
selNames = [ s for s in sel.getSelectionStrings() ]
attribs = mc.listAttr( selNames[-1], m=1 )
if len(attribs) > 0:
colorAttributes = {}
if mc.attributeQuery( 'notes', n=selNames[-1], ex=1 ):
notesString = mc.getAttr( selNames[-1] + '.notes' )
if 'attributeColorBegin' in notesString:
colorAttributes = ast.literal_eval( notesString.split( ' attributeColorBegin ', 1 )[-1].split( ' attributeColorEnd ', 1 )[0] )
for a in xrange( len( attribs ) ):
if mc.attributeQuery( attribs[a], n=selNames[-1], ex=1 ):
inCb = mc.getAttr( '{0}.{1}'.format( selNames[-1], attribs[a] ), cb=1 )
isKey = mc.getAttr( '{0}.{1}'.format( selNames[-1], attribs[a] ), k=1 )
if inCb or isKey:
mc.channelBox( 'mainChannelBox', e=1, attrRegex=attribs[a], attrColor=( 0.75, 0.75, 0.75 ), attrBgColor=( 0.265, 0.265, 0.265 ) )
if len(colorAttributes)>0 and attribs[a] in colorAttributes:
self.allCustomCBColorAttributes[attribs[a]] = colorAttributes[attribs[a]]
mc.channelBox( 'mainChannelBox', e=1, attrRegex=attribs[a], attrColor=( 0.75, 0.75, 0.75 ), attrBgColor=( colorAttributes[attribs[a]] ) )
lightness = colorsys.rgb_to_hls( colorAttributes[attribs[a]][0], colorAttributes[attribs[a]][1], colorAttributes[attribs[a]][2] )[1]
if lightness > 0.45:
mc.channelBox( 'mainChannelBox', e=1, attrRegex=attribs[a], attrColor=(0.0, 0.0, 0.0) )
else:
mc.channelBox( 'mainChannelBox', e=1, attrRegex=attribs[a], attrColor=(1.0, 1.0, 1.0) )
def remove( self, *args ):
try:
om2.MSelectionList.add( self.thisMobject() )
except:
#Remove custom color attribute callcback
for x in xrange( len(self.messageIDs) ):
try:
if self.allCustomCBColorAttributes and len(self.allCustomCBColorAttributes)>0:
for k, v in self.allCustomCBColorAttributes.iteritems():
mc.channelBox( 'mainChannelBox', e=1, attrRegex=k, attrColor=( 0.75, 0.75, 0.75 ), attrBgColor=( 0.265, 0.265, 0.265 ) )
except:
pass
try:
om2.MEventMessage.removeCallback( self.messageIDs[x] )
except:
pass
try:
om2.MDGMessage.removeCallback( self.messageIDs[x] )
except:
pass
def compute( self, pPlug, data ):
pass
# Plug-in initialization.
def nodeCreator():
""" Creates an instance of our node class and delivers it to Maya as a pointer. """
return colorAttributes()
def nodeInitializer():
pass
def initializePlugin(mobject):
""" Initialize the plug-in """
mplugin = om2.MFnPlugin( mobject, 'Alexander Smirnov (onefabis)', '1.0' )
try:
mplugin.registerNode( kPluginNodeName, kPluginNodeId, nodeCreator, nodeInitializer )
except:
sys.stderr.write( "Failed to register node: " + kPluginNodeName )
raise
def uninitializePlugin( mobject ):
""" Uninitializes the plug-in """
mplugin = om2.MFnPlugin( mobject )
try:
mplugin.deregisterNode( kPluginNodeId )
except:
sys.stderr.write( 'Failed to deregister node: ' + kPluginNodeName )
raise
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment