import pymel.core as pm
#Initialize
myCamera = pm.ls(sl = True)
planeScaleSize = 30
planeSubD = 8
myCamTransform = myCamera[0].translate.get()
#Create First Plane
myPlane = pm.polyPlane(n = 'NEAR_plane', w = planeScaleSize, h = planeScaleSize, sx = planeSubD, sy = planeSubD)
pm.parent('NEAR_plane', myCamera, relative = True)
pm.rotate('NEAR_plane', '-90deg', 0)
pm.move(0, 0, -5, ls = True, r = True, wd = True)
pm.setAttr('NEAR_planeShape.castsShadows', 0)
pm.setAttr('NEAR_planeShape.receiveShadows', 0)
pm.setAttr('NEAR_planeShape.motionBlur', 0)
pm.setAttr('NEAR_planeShape.primaryVisibility', 0)
pm.setAttr('NEAR_planeShape.smoothShading', 0)
pm.setAttr('NEAR_planeShape.visibleInReflections', 0)
pm.setAttr('NEAR_planeShape.visibleInRefractions', 0)
#Create Second Plane (Comes in parented)
pm.duplicate('NEAR_plane', n = 'FAR_plane')
pm.rotate('FAR_plane', '-90deg', 0)
pm.move(0, 0, -5, ls = True, r = True, wd = True)
pm.setAttr('FAR_planeShape.castsShadows', 0)
pm.setAttr('FAR_planeShape.receiveShadows', 0)
pm.setAttr('FAR_planeShape.motionBlur', 0)
pm.setAttr('FAR_planeShape.primaryVisibility', 0)
pm.setAttr('FAR_planeShape.smoothShading', 0)
pm.setAttr('FAR_planeShape.visibleInReflections', 0)
pm.setAttr('FAR_planeShape.visibleInRefractions', 0)
#Unparent //W means parented to the world
pm.parent('NEAR_plane', w = True)
pm.parent('FAR_plane', w = True)
#Create Distant Dimensions
pm.distanceDimension(sp = (myCamTransform.x, myCamTransform.y, myCamTransform.z), ep = (0, 0, -5))
pm.distanceDimension(sp = (myCamTransform.x, myCamTransform.y, myCamTransform.z), ep = (0, 0, -10))
#Rename
pm.rename('locator1', 'CAM_locator')
pm.rename('locator2', 'NEAR_locator')
pm.rename('locator3', 'FAR_locator')
pm.rename('distanceDimension1', 'NEAR_distanceDimension')
pm.rename('distanceDimension2', 'FAR_distanceDimension')
#Constrain and Group
pm.parentConstraint(myCamera, 'CAM_locator')
pm.parentConstraint('NEAR_plane', 'NEAR_locator')
pm.parentConstraint('FAR_plane', 'FAR_locator')
pm.group('NEAR_plane', 'FAR_plane', n = 'Planes')
#Parent and Maintain Offset
pm.parentConstraint(myCamera, 'Planes', mo = True)
pm.group('Planes', 'CAM_locator', 'NEAR_locator', 'FAR_locator', 'NEAR_distanceDimension', 'FAR_distanceDimension', n = 'ZDepth_Group')
#Select All
pm.select(all = True)
pm.createRenderLayer(g = True, n = 'ZDepth')
#Create Surface Shader
myShader = pm.shadingNode('surfaceShader', asShader = True, n = 'mySurfaceShader')
pm.sets(r= True, nss = True, em = True, n = 'mySurfaceShaderSG')
print myShader
#Create Multiple/Divide
pm.shadingNode('multiplyDivide', asUtility = True, n = 'myMultiDiv')
pm.setAttr('myMultiDiv.input2.input2X', -1)
#Create Sampler Info
pm.shadingNode('samplerInfo', asUtility = True, n = 'mySamplerInfo')
#Create setRange
pm.shadingNode('setRange', asUtility = True, n = 'myRangeNode')
pm.setAttr('myRangeNode.min.minX', 1)
#Hookup
pm.connectAttr('mySamplerInfo.pointCamera.pointCameraZ', 'myMultiDiv.input1.input1X')
pm.connectAttr('myMultiDiv.output.outputX', 'myRangeNode.value.valueX')
pm.connectAttr('myRangeNode.outValue.outValueX', 'mySurfaceShader.outColor.outColorR')
pm.connectAttr('myRangeNode.outValue.outValueX', 'mySurfaceShader.outColor.outColorG')
pm.connectAttr('myRangeNode.outValue.outValueX', 'mySurfaceShader.outColor.outColorB')
pm.connectAttr('NEAR_distanceDimensionShape.distance', 'myRangeNode.oldMin.oldMinX')
pm.connectAttr('FAR_distanceDimensionShape.distance', 'myRangeNode.oldMax.oldMaxX')
#Apply Shader to everything#ssn = pm.select(ado = True)
ssn = pm.ls(sl = True) #<
pm.hyperShade(myShader, assign = True) #<
#pm.sets('mySurfaceShader', edit = True, forceElement = ssn)
Replies
Hopefully I understood your problem but just to clarify you want to assign the mesh to all meshes in the scene right? Does that include the near and far planes? If not then you can just filter them as I see you explicity named them. Your pm.ls(sl=true) wont work because it should select the myRangeNode as technically its the last thing created in maya which is "selected"
Using hyperShade is the way to go, right?
Did this:
Got this:
I'm guessing it repeats it three times because I have three objects in the scene. At least ls seems to be picking the meshes up, but this damn shader...
Bonus question: Looks like this material application will take place on the master layer. That's bad, as it will replace the textures. How can I switch to the render layer I created?
pm.select(i)
then use hypershade to assign. Don't have maya on this laptop so cant test
Is, "myShader = pm.shadingNode('surfaceShader', asShader = True, n = 'mySurfaceShader')" not enough to create a variable containing my shader? Did I not create my shader correctly? I even tried plugging the name into the hypergraph function directly. So, instead of: pm.hyperShade(myShader, assign = True) I did pm.hyperShade('mySurfaceShader', assign = True).
Thanks for the help so far
EDIT: I think the problem is I don't have a SG for my surfaceShader... looking into that now.
Thanks haiddasalami!