Skip to main content

WIP: Now workin' on lighting pipeline scene asset

Here the first code to create a simple production lighting pipeline script.
Stay tuned!


import maya.cmds as cmds
import maya.mel as mel
import pymel.core as pm


ambientName = 'ambient'
flatDiffuseName = 'diffuseflat'
ambientOcclusionName = 'occlusion'
sssFront = 'sssfront'
sssBack = 'sssback'
colorVector = ['red','green','blue']
colors = [[1,0,0],[0,1,0],[0,0,1]]
lights = ['key','fill','rim','extraR','extraG','extraB']
overrideMTLName = 'SHD_overrideMTL'

def createOverrideMTL(name):
overrideMTL = mel.eval('shadingNode -asShader lambert')
cmds.sets(renderable=True, noSurfaceShader = True, empty = True, name = overrideMTL + 'SG')
cmds.connectAttr(overrideMTL + '.outColor', overrideMTL + 'SG.surfaceShader')
cmds.setAttr(overrideMTL + '.color',1,1,1)
cmds.rename(overrideMTL,name)
cmds.rename(overrideMTL + 'SG',name + 'SG')


def createCustomColorRenderPass(contmapMin):
    mayaDir=mel.eval("getenv MAYA_LOCATION") + '/presets/attrPresets/renderPass/customColor.mel'
    pm.shadingNode('renderPass',n=contmapMin, asRendering=1) #Create custom color pass
    mel.eval('applyAttrPreset "'+contmapMin +'" "'+mayaDir + '" 1;')
    cmds.setAttr(contmapMin+'.useTransparency',1)
    cmds.setAttr(contmapMin+'.transparentAttenuation',1)
    cmds.setAttr(contmapMin+'.numChannels',3)
    cmds.setAttr(contmapMin+'.frameBufferType',2)
    cmds.setAttr(contmapMin+'.colorProfile',3)


def createPasses():
    numberOfPass = numberOfIdPasses()
    createCustomColorRenderPass(ambientName)
    createCustomColorRenderPass(flatDiffuseName)
    createCustomColorRenderPass(ambientOcclusionName)
    createCustomColorRenderPass(sssFront)
    createCustomColorRenderPass(sssBack)
    for j in range(0,int(numberOfPass)):
        createCustomColorRenderPass('id0'+str(j+1))
        
def numberOfIdPasses():
    result = cmds.promptDialog(
                title='idPasses',
                message='How many id passes? :',
                button=['OK', 'Cancel'],
                defaultButton='OK',
                cancelButton='Cancel',
                dismissString='Cancel')
    if result == 'OK':
        idNum = cmds.promptDialog(query=True, text=True)
    return idNum
    
def fillBuffer(wtc):
wtcName=wtc
passName = wtc
nodes = cmd.ls(typ='writeToColorBuffer')
if nodes != None:
for node in nodes:
if node.count(wtcName):
try:
cmd.connectAttr(passName + '.message', node + '.renderPass')
except:
pass


def defineGlobals():
cmds.setAttr('defaultRenderGlobals.currentRenderer', 'mentalRay', type='string')
createPasses()
createCustomRenderPass('motionvector','2DMotionVector')
createCustomRenderPass('beauty','beauty')
cmds.setAttr('beauty.frameBufferType',2)
cmds.setAttr('beauty.numChannels',3)
createCustomRenderPass('matte','matte')
cmds.setAttr('matte.frameBufferType',2)
createOverrideMTL(overrideMTLName)
#globals adjustment
cmds.setAttr('defaultRenderGlobals.imageFormat', 3) #TIF
cmds.setAttr('defaultResolution.width',1024) 
cmds.setAttr('defaultResolution.height',436)
cmds.setAttr('defaultRenderGlobals.colorProfileEnabled',1)
cmds.setAttr('defaultRenderGlobals.inputColorProfile',3) #sRGB
cmds.setAttr('defaultRenderGlobals.outputColorProfile',2) #Linear sRGB
# Adjust the frame extension name
cmds.setAttr('defaultRenderGlobals.outFormatControl',0)
cmds.setAttr('defaultRenderGlobals.animation',1)
cmds.setAttr('defaultRenderGlobals.putFrameBeforeExt',1)
cmds.setAttr('defaultRenderGlobals.extensionPadding',4)
# RGB 3x16 frame buffer
cmds.setAttr('miDefaultFramebuffer.datatype',17)
cmds.setAttr('mentalrayGlobals.exportMotionOffset',0)
cmds.setAttr('mentalrayGlobals.exportMotionOutput',0)

def deleteAll():
selectionType = ['renderLayer','renderPass']
cmds.editRenderLayerGlobals( currentRenderLayer = 'defaultRenderLayer')
for s in selectionType:
sel = cmds.ls(type = s)
cmds.delete(sel)
for l in lights:
cmds.select(l,add=True)
cmds.delete()


def getSelection():
sel = cmds.ls(sl = True)
return sel


def createCustomRenderPass(contmapMin,passType):
    mayaDir=mel.eval("getenv MAYA_LOCATION") + '/presets/attrPresets/renderPass/'+ passType + '.mel'
    pm.shadingNode('renderPass',n=contmapMin, asRendering=1) #Create custom pass
    mel.eval('applyAttrPreset "'+contmapMin +'" "'+mayaDir + '" 1;')


def createLightRig():
for l in lights:
cmds.spotLight(n = l, ca = 80, rs = True)
cmds.sets(lights[0],lights[1],lights[2],n = 'basicLights')
cmds.sets(lights[3],lights[4],lights[5],n = 'extraLights')
cmds.select(cl = True)

def createObjectAttributeOverride(layerName, object, attribute, value):
cmds.editRenderLayerAdjustment(object + '.' + attribute, layer = layerName)
if attribute == 'color':
conn = cmds.listConnections(object + '.' + attribute)
cmds.disconnectAttr(str(conn[0]) + '.outColor',object + '.' + attribute)
cmds.setAttr(object + '.' + attribute,value[0],value[1],value[2])
else:
cmds.setAttr(object + '.' + attribute,value)

def createLightsAttributeOverride(layerName, object, attribute, value):
cmds.editRenderLayerAdjustment(object + '.' + attribute)
cmds.setAttr(object + '.' + attribute,value[0],value[1],value[2])

def addSelectionToLayer(layerName, selection):
cmds.editRenderLayerMembers(layerName,selection)

def addObjectToLayer():
selection = getSelection()
for l in layers:
layerName = layerN + l 
addSelectionToLayer(layerName, selection)

#load from interface
name = 'lambert2'
sq = 'sq_001'
sh = 'sh_007'
type = 'chr'
layerN = sq + '_' + sh + '_' + type + '_'
layers = ['basic','lights','extraLights','shadows','extraShadows','ctcocclusion','ctcshadows','motion']


defineGlobals()
createLightRig()


#create all renderLayer
for i in layers:
layerName = layerN + i 
cmds.createRenderLayer(makeCurrent=True, number = 1, noRecurse = True, name = layerName)


#putObject into renderLayer
sel = cmds.ls('basicLights')
layerName = layerN + layers[0]
addSelectionToLayer(layerName,sel)
layerName = layerN + layers[1]
addSelectionToLayer(layerName,sel)
layerName = layerN + layers[3]
addSelectionToLayer(layerName,sel)
sel = cmds.ls('extraLights')
layerName = layerN + layers[0]
addSelectionToLayer(layerName,sel)
layerName = layerN + layers[2]
addSelectionToLayer(layerName,sel)
layerName = layerN + layers[4]
addSelectionToLayer(layerName,sel)


#basic layer
layerName = layerN + layers[0]
cmds.editRenderLayerGlobals( currentRenderLayer = layerName)
cmds.editRenderLayerAdjustment('defaultRenderGlobals.imageFilePrefix')
cmds.setAttr('defaultRenderGlobals.imageFilePrefix','<RenderLayer>/<RenderPass>/<RenderPass>', type = 'string')
sel = cmds.ls(type = 'renderPass')
for s in sel:
if s != 'motionvector':
cmds.connectAttr(layerName + '.renderPass',s + '.owner')


#override light layer
layerName = layerN + layers[1]
cmds.editRenderLayerGlobals( currentRenderLayer = layerName)
cmds.editRenderLayerAdjustment('defaultRenderGlobals.imageFilePrefix')
cmds.setAttr('defaultRenderGlobals.imageFilePrefix','<RenderLayer>/' + layers[1], type = 'string')
for i in range(0,3):
createLightsAttributeOverride(layerName,lights[i],'color',colors[i])
createLightsAttributeOverride(layerName,lights[i],'shadowColor',(0,0,0))
cmds.connectAttr(overrideMTLName + 'SG.message', layerName + '.shadingGroupOverride')

#override extralight layer
layerName = layerN + layers[2]
cmds.editRenderLayerGlobals( currentRenderLayer = layerName)
cmds.editRenderLayerAdjustment('defaultRenderGlobals.imageFilePrefix')
cmds.setAttr('defaultRenderGlobals.imageFilePrefix','<RenderLayer>/' + layers[2], type = 'string')
for i in range(0,3):
createLightsAttributeOverride(layerName,lights[i+3],'color',colors[i])
createLightsAttributeOverride(layerName,lights[i+3],'shadowColor',(0,0,0))
cmds.connectAttr(overrideMTLName + 'SG.message', layerName + '.shadingGroupOverride')

#override shadow layer
layerName = layerN + layers[3]
cmds.editRenderLayerGlobals( currentRenderLayer = layerName)
cmds.editRenderLayerAdjustment('defaultRenderGlobals.imageFilePrefix')
cmds.setAttr('defaultRenderGlobals.imageFilePrefix','<RenderLayer>/' + layers[3], type = 'string')
for i in range(0,3):
createLightsAttributeOverride(layerName,lights[i],'shadowColor',colors[i])
createLightsAttributeOverride(layerName,lights[i],'color',(0,0,0))
cmds.connectAttr(overrideMTLName + 'SG.message', layerName + '.shadingGroupOverride')

#override extrashadow layer
layerName = layerN + layers[4]
cmds.editRenderLayerGlobals( currentRenderLayer = layerName)
cmds.editRenderLayerAdjustment('defaultRenderGlobals.imageFilePrefix')
cmds.setAttr('defaultRenderGlobals.imageFilePrefix','<RenderLayer>/' + layers[4], type = 'string')
for i in range(0,3):
createLightsAttributeOverride(layerName,lights[i+3],'shadowColor',colors[i])
createLightsAttributeOverride(layerName,lights[i+3],'color',(0,0,0))
cmds.connectAttr(overrideMTLName + 'SG.message', layerName + '.shadingGroupOverride')

#override ctcocclusion layer
layerName = layerN + layers[5]
cmds.editRenderLayerGlobals( currentRenderLayer = layerName)
cmds.editRenderLayerAdjustment('defaultRenderGlobals.imageFilePrefix')
cmds.setAttr('defaultRenderGlobals.imageFilePrefix','<RenderLayer>/' + layers[5], type = 'string')


#override ctcshadow layer
layerName = layerN + layers[6]
cmds.editRenderLayerGlobals( currentRenderLayer = layerName)
cmds.editRenderLayerAdjustment('defaultRenderGlobals.imageFilePrefix')
cmds.setAttr('defaultRenderGlobals.imageFilePrefix','<RenderLayer>/' + layers[6], type = 'string')
cmds.connectAttr(overrideMTLName + 'SG.message', layerName + '.shadingGroupOverride')


#override motion layer
layerName = layerN + layers[7]
cmds.editRenderLayerGlobals( currentRenderLayer = layerName)
cmds.editRenderLayerAdjustment('defaultRenderGlobals.imageFilePrefix','miDefaultFramebuffer.datatype','defaultRenderGlobals.imageFormat')
cmds.setAttr('defaultRenderGlobals.imageFormat', 51) #EXR
cmds.setAttr('defaultRenderGlobals.imageFilePrefix','<RenderLayer>/<RenderPass>/motion', type = 'string')
# RGB 4x32 frame buffer
cmds.setAttr('miDefaultFramebuffer.datatype',5)
cmds.connectAttr(layerName + '.renderPass','motionvector.owner')
cmds.connectAttr(overrideMTLName + 'SG.message', layerName + '.shadingGroupOverride')

Comments

Popular posts from this blog

Tunnel modeling with Dynamo

Introduction Last week I had a nice conversation with one of my Italian friends. The focus of the conversation was how to model a tunnel in Revit starting from an Excel list of points coming out from Civil 3D. So the answer was: Dynamo of course! First input, the section of the tunnel with his diameter; I can't share the details but lets say that we have: Theoretical excavation profile: 200 mm  Precast segment thickness: 500 mm Whit this two elements and the excel of the theoretical cent alignment, we can do everything! Revit profile family The first family we're going to create is a simple circle profile with a diameter parameter. This family will be as a mass category to be able to nest it later in an adaptive component family for the tunnel.  Just save this family...easy! Tunnel model Now we can create a new family as generic adaptive component and load the profile just created. It's important that the family is able to host rebars for future st...

Multi-pipe support configurator with Dynamo

As always everything is coming from my childhood…ops no, that's another story. As always everything starts on a white recycled paper just in front of my keyboard; a simple idea becomes immediately a sketch with Revit parameters. I tried many times different kind of plugins for automatic supports placement, but no one of these is able to create a multi-pipes supports (if I'm wrong, please let me know the name of the plugin). The idea for this script in Dynamo, is to create a support for a bounce of pipes going parallel for a while, and create a group that is possible to copy along the path. I'll not focus on the creation of the families that you need for the support but just to explain the main idea, I have 3 different pieces (steel bar, u-bolt and threaded rod) that I'll compose together to create the final support. The first challenge is to align the pipes to the same BOP (bottom of pipe) because they have to stand on the steel bar and locked with the ...

Dynamo, Revit, Excel, UI...what a wonderful day!

Introduction Today I'll show you two examples of how to use Dynamo to export existing Revit schedules and how to compose a custom schedule on the fly, to be exported to Excel. Both of them with a very nice graphical user interface thanks to Data-Shapes (https://data-shapes.net/) package. Export Schedules Let's start from the easier. The idea behind this script is to export a list of existing schedules to an Excel file, creating for each schedule a separate tab. To be able to complete this script you'll need two packages: Data-Shapes for the graphical interfaces BimorphNodes to extract schedules data The script is very easy and you have to focus only on how to create an interface Then you can use your input to feed the next nodes. Finished! So just push the run button (or use Dynamo Player is you are in Revit 2017.1 or 2017.2) and enjoy the interface. Export parameters to Excel In the second script I'll focus on something more int...