Sunday, November 5, 2017

Cell Shader Tool

As you all know the alCell shader is not compatible with the Arnold 5. After some research, I found a way to do it in a simple way. Now I will show you my new tool to create the Cell Shader. It is quite easy to use, here is a video of how to use it.

Cell Shader Tool - Arnold 5 - Maya from Steffano Richi on Vimeo.

The code:
"""
########################################################################################################################
AUTHOR:
Steffano Richi (steffano.richi@gmail.com)
https://encci20.blogspot.pe/
31.10.2017
DESCRIPTION:
Create cell shader in arnold 5, using the native nodes from it.
USAGE:
*Just drag and drop into the script editor
*Run it
########################################################################################################################
"""
import maya.cmds as mc
import mtoa.aovs as aovs
import random
#Definitions
def ColorConstant(name,R,G,B):
color = mc.shadingNode('colorConstant', asTexture=1,n=name + '_ColorConstant')
mc.setAttr(color + '.inColor', R,G,B)
def cellRamp(name,clamp=1):
ramp = mc.shadingNode('ramp', asTexture=1,n=name + '_Ramp')
surfLuma = mc.shadingNode('surfaceLuminance', asUtility=1,n=name + '_SurfaceLuminance')
if clamp == 1:
clampNode = mc.shadingNode('clamp', asUtility=1,n=name + '_Clamp')
mc.setAttr(clampNode + '.maxR', 1)
mc.connectAttr(surfLuma + '.outValue', clampNode + '.inputR')
mc.connectAttr(clampNode + '.outputR', ramp + '.vCoord')
else:
mc.connectAttr(surfLuma + '.outValue', ramp + '.vCoord')
mc.setAttr(ramp + '.interpolation', 0)
mc.setAttr(ramp + '.colorEntryList[0].position', 0)
mc.setAttr(ramp + '.colorEntryList[1].position', 0.25)
mc.setAttr(ramp + '.colorEntryList[0].color', 0.5,0.5,0.5)
mc.setAttr(ramp + '.colorEntryList[1].color', 1,1,1)
return ramp
def cellNodes(name,outputFile,hardwareColor,outputRamp='None'):
shader = mc.shadingNode('aiUtility', asShader=1,n=name + '_Mat')
cCorrect = mc.shadingNode('aiColorCorrect', asUtility=1,n=name + '_ColorCorrect')
compositer = mc.shadingNode('aiComposite', asUtility=1,n=name + '_Composite')
mc.setAttr(shader + '.shadeMode', 2)
mc.setAttr(compositer + '.operation', 19)
mc.connectAttr(compositer + '.outColor', shader + '.color')
#RAMP
if outputRamp != 'None':
mc.connectAttr(outputRamp + '.outColor', compositer + '.A')
else:
createRamp = cellRamp(name)
mc.connectAttr(createRamp + '.outColor', compositer + '.A')
#FILE
mc.connectAttr(cCorrect + '.outColor', compositer + '.B')
mc.connectAttr(outputFile + '.outColor', cCorrect + '.input')
#HARDWARE COLOR
if hardwareColor == 1:
if mc.objectType(outputFile,isType='file'):
rawColor = (random.random(),random.random(),random.random())
else:
rawColor = mc.getAttr(outputFile + '.outColor')[0]
mc.setAttr(shader + '.hardwareColor', rawColor[0],rawColor[1],rawColor[2])
if hardwareColor == 2:
mc.connectAttr(outputFile + '.outColor', shader + '.hardwareColor')
if hardwareColor == 3:
pass
#Doing
def ColorConstantDo(self):
name = mc.textFieldGrp(nameConstant,q=1,tx=1)
colorSel = mc.colorSliderGrp(color,q=1,rgb=1)
ColorConstant(name,colorSel[0],colorSel[1],colorSel[2])
mc.button(coloredButton,e=1,bgc=[colorSel[0],colorSel[1],colorSel[2]])
def cellRampDo(self):
name = mc.textFieldGrp(RampGTo,q=1,tx=1)
clamp = mc.checkBox(RampClamp,q=1,v=1)
cellRamp(name,clamp)
def cellNodesDo(self):
outputRamp = mc.textFieldGrp(rampToAuto,q=1,tx=1)
shareBool = mc.checkBox(shareToAuto,q=1,v=1)
radioState = mc.radioButtonGrp(radioColor,q=1,sl=1)
list = mc.ls(sl=1)
for elem in list:
if shareBool == 1:
cellNodes(elem,elem,radioState,outputRamp=outputRamp)
else:
cellNodes(elem,elem,radioState,outputRamp='None')
def aovCell(self):
aovs.AOVInterface().addAOV('cell_color','rgb')
aovs.AOVInterface().addAOV('cell_raw','rgb')
def connectCellAovDo(self):
#Detect Aov New
color = mc.listConnections('aiAOV_cell_color',p=1)[2]
raw = mc.listConnections('aiAOV_cell_raw',p=1)[2]
num_color = color[len(color)-2:len(color)-1]
num_raw = raw[len(raw)-2:len(raw)-1]
#Connect
constant = mc.ls(typ='aiUtility')
for cons in constant:
engineNone = mc.listConnections(cons,t='shadingEngine')
compoNone = mc.listConnections(cons,t='aiComposite')
if engineNone != None and compoNone != None:
engine = engineNone[0]
compo = compoNone[0]
ramp = mc.listConnections(compo,t='ramp')[0]
cc = mc.listConnections(compo,t='aiColorCorrect')[0]
try:
mc.connectAttr(ramp + '.outColor', engine + '.aiCustomAOVs[' + num_raw + '].aovInput',f=1)
mc.connectAttr(cc + '.outColor', engine + '.aiCustomAOVs[' + num_color + '].aovInput',f=1)
except:
pass
def updateCellCC(self):
textTypes = mc.textFieldGrp(searchUpdate,q=1,tx=1)
types = textTypes.split(', ')
for eachType in types:
constant = mc.ls(typ='aiUtility')
for cons in constant:
compoNone = mc.listConnections(cons,t='aiComposite')
if compoNone != None:
color = mc.listConnections(compoNone,c=1,t=eachType)
if color != None:
cCorrect = mc.shadingNode('aiColorCorrect', asUtility=1,n=color[1] + '_ColorCorrect')
mc.connectAttr(cCorrect + '.outColor', color[0],f=1)
mc.connectAttr(color[1] + '.outColor', cCorrect + '.input',f=1)
#UI
width = 400
height = 400
#UI Restorer
if mc.window('mainWin',ex=1):
mc.deleteUI('mainWin')
cell = mc.window('mainWin',sizeable=0,t='Cell Creator - Arnold 5 - 1.0.0 - sTF ')
mc.window(cell,e=1,width=width,height=height)
mc.columnLayout(rs=8)
##Constant
mc.columnLayout(rs=8)
nameConstant = mc.textFieldGrp(text='Color', w=width, label='Constant Name ')
color = mc.colorSliderGrp(rgb=(1, 1, 1), w=width, label="Color ") #rgb=(0.071, 0.071, 1)
mc.rowLayout(nc=3)
mc.separator(style="none", w=width/3)
coloredButton = mc.button(c=ColorConstantDo, l="Make Constant",width=width/3,al='center')
mc.separator(style="none", w=width/3)
mc.setParent('..')
mc.separator(style="out", w=width, height=10)
##Ramp
mc.columnLayout(rs=8)
RampGTo = mc.textFieldGrp(text="Common", label="Ramp Name")
mc.rowLayout(nc=3)
mc.separator(style="none", w=width/3)
RampClamp = mc.checkBox(align='center', value=1, label="Clamp")
mc.separator(style="none", w=width/3)
mc.setParent('..')
mc.rowLayout(nc=3)
mc.separator(style="none", w=width/3)
mc.button(c=cellRampDo,l="Make Ramp",w=width/3,bgc=(0.3, 0.2, 0.2))
mc.separator(style="none", w=width/3)
mc.setParent('..')
mc.separator(style="out", w=width, height=10)
##File
mc.columnLayout(rs=8)
rampToAuto = mc.textFieldGrp(text='Common_Ramp', label='Ramp to search')
mc.rowLayout(nc=1)
radioColor = mc.radioButtonGrp(label='Hardware color',labelArray3=['Color','Texture','Default'],numberOfRadioButtons=3,sl=2,w=width,cw=[(1,width/4),(2,width/4),(3,width/4)])
mc.setParent('..')
mc.rowLayout(nc=3)
mc.separator(style="none", w=width/3)
shareToAuto = mc.checkBox(value=1, label='Share Ramp')
mc.separator(style="none", w=width/3)
mc.setParent('..')
mc.rowLayout(nc=3)
mc.separator(style="none", w=width/3)
mc.button(c=cellNodesDo, l="Auto from Selection",w=width/3,bgc=(0.5, 0.5, 0.5))
mc.separator(style="none", w=width/3)
mc.setParent('..')
mc.separator(style="out", w=width, height=10)
##AOVs
mc.columnLayout(rs=8)
mc.rowLayout(nc=3)
mc.separator(style="none", w=width/3)
mc.button(c=aovCell, l="Custom Cell AOVs",w=width/3,bgc=(0.5, 0.3, 0.2))
mc.separator(style="none", w=width/3)
mc.setParent('..')
mc.rowLayout(nc=3)
mc.separator(style="none", w=width/3)
mc.button(c=connectCellAovDo, l="Connect",w=width/3,bgc=(0.5, 0.3, 0.2))
mc.separator(style="none", w=width/3)
mc.setParent('..')
mc.separator(style="out", w=width, height=10)
mc.rowLayout(nc=3)
mc.separator(style="none", w=width/3)
mc.button(c=updateCellCC, l="Update Nodes",w=width/3,bgc=(0.2, 0.3, 0.2))
mc.separator(style="none", w=width/3)
mc.setParent('..')
searchUpdate = mc.textFieldGrp(text="colorConstant, file", label="Search")
mc.showWindow(cell)
Hope you find this useful. Thanks

3 comments: