68 from builtins
import range
69 import maya.OpenMaya
as OpenMaya
70 import maya.OpenMayaUI
as OpenMayaUI
71 import maya.OpenMayaMPx
as OpenMayaMPx
75 kSlopeShaderBehaviourName =
"slopeShaderBehaviour"
76 kSlopeNodeName =
"spSlopeShader"
77 kSlopeNodeClassify =
"utility/color"
81 class slopeShader(OpenMayaMPx.MPxNode):
92 OpenMayaMPx.MPxNode.__init__(self)
95 def compute(self, plug, dataBlock):
96 if plug == slopeShader.aOutColor
or plug.parent() == slopeShader.aOutColor:
101 dataHandle = dataBlock.inputValue( slopeShader.aColor1 )
103 sys.stderr.write(
"Failed to get inputValue aColor1" )
105 walkable = dataHandle.asFloatVector()
108 dataHandle = dataBlock.inputValue( slopeShader.aColor2 )
110 sys.stderr.write(
"Failed to get inputValue aColor2" )
112 nonWalkable = dataHandle.asFloatVector()
115 dataHandle = dataBlock.inputValue( slopeShader.aTriangleNormalCamera )
117 sys.stderr.write(
"Failed to get inputValue aTriangleNormalCamera" )
119 surfaceNormal = dataHandle.asFloatVector()
122 dataHandle = dataBlock.inputValue( slopeShader.aMatrixEyeToWorld )
124 sys.stderr.write(
"Failed to get inputValue aMatrixEyeToWorld" )
126 viewMatrix = dataHandle.asFloatMatrix()
130 dataHandle = dataBlock.inputValue( slopeShader.aAngle )
132 sys.stderr.write(
"Failed to get inputValue aAngle" )
134 angle = dataHandle.asFloat()
140 surfaceNormal.normalize()
141 WSVector = surfaceNormal * viewMatrix
145 scalarNormal = WSVector * yVector
149 if scalarNormal < 0.0:
152 radianAngle = math.radians(angle)
153 cosOfAngle = math.cos(radianAngle)
154 if cosOfAngle < scalarNormal:
155 resultColor = walkable
157 resultColor = nonWalkable
162 outColorHandle = dataBlock.outputValue( slopeShader.aOutColor )
164 sys.stderr.write(
"Failed to get outputValue aOutColor" )
167 outColorHandle.setMFloatVector(resultColor)
168 outColorHandle.setClean()
171 return OpenMaya.kUnknownParameter
174 def postConstructor(self):
175 print(
"In slopeShader.postConstructor")
179 class slopeShaderBehavior(OpenMayaMPx.MPxDragAndDropBehavior):
181 OpenMayaMPx.MPxDragAndDropBehavior.__init__(self)
184 def shouldBeUsedFor(self, sourceNode, destinationNode, sourcePlug, destinationPlug):
186 Overloaded function from MPxDragAndDropBehavior
187 this method will return True if it is going to handle the connection
188 between the two nodes given.
192 if sourceNode.hasFn(OpenMaya.MFn.kLambert):
200 src.getConnections(connections)
202 for i
in range(connections.length()):
206 connections[i].connectedTo(connectedPlugs,
True,
False)
207 for j
in range(connectedPlugs.length()):
213 if testNode.typeName() == kSlopeNodeName:
214 shaderNode = connectedPlugs[j].node()
220 if shaderNode
is not None:
225 if destinationNode.hasFn(OpenMaya.MFn.kMesh):
230 if node.typeName() == kSlopeNodeName:
234 if destinationNode.hasFn(OpenMaya.MFn.kLambert):
236 elif destinationNode.hasFn(OpenMaya.MFn.kMesh):
242 def connectNodeToNode(self, sourceNode, destinationNode, force):
244 Overloaded function from MPxDragAndDropBehavior
245 this method will handle the connection between the slopeShader and the shader it is
246 assigned to as well as any meshes that it is assigned to.
253 if sourceNode.hasFn(OpenMaya.MFn.kLambert):
263 src.getConnections(connections)
264 for i
in range(connections.length()):
268 connections[i].connectedTo(connectedPlugs,
True,
False)
269 for j
in range(connectedPlugs.length()):
273 currentnode = connectedPlugs[j].node()
275 shaderNodes.append(currentnode)
281 if shaderNodes.length() > 0:
283 if destinationNode.hasFn(OpenMaya.MFn.kMesh):
289 for i
in range(shaderNodes.length()):
290 srcPlug = dest.findPlug(
"worldMesh")
293 if (
not srcPlug.isNull())
and (
not destPlug.isNull()):
294 cmd =
"connectAttr -na %s %s" % (srcPlug.name(), destPlug.name())
295 OpenMaya.MGlobal.executeCommand(cmd)
300 shadingEngine = self._findShadingEngine(sourceNode)
305 if not shadingEngine.isNull():
306 cmd =
"sets -edit -forceElement %s %s" % (
310 OpenMaya.MGlobal.executeCommand(cmd)
312 elif src.typeName() == kSlopeNodeName:
316 if destinationNode.hasFn(OpenMaya.MFn.kMesh):
322 srcPlug = dest.findPlug(
"worldMesh")
323 destPlug = src.findPlug(
"dirtyShaderPlug")
324 if (
not srcPlug.isNull())
and (
not destPlug.isNull()):
325 cmd =
"connectAttr -na %s %s" % (srcPlug.name(), destPlug.name())
326 OpenMaya.MGlobal.executeCommand(cmd)
328 elif destinationNode.hasFn(OpenMaya.MFn.kLambert):
330 srcPlug = src.findPlug(
"outColor")
331 destPlug = dest.findPlug(
"color")
332 if (
not srcPlug.isNull())
and (
not destPlug.isNull()):
334 cmd =
"connectAttr -f %s %s" % (srcPlug.name(), destPlug.name())
336 cmd =
"connectAttr %s %s" % (srcPlug.name(), destPlug.name())
337 OpenMaya.MGlobal.executeCommand(cmd)
340 def connectNodeToAttr(self, sourceNode, destinationPlug, force):
342 Overloaded function from MPxDragAndDropBehavior
343 this method will assign the correct output from the slope shader
344 onto the given attribute.
352 if destinationPlug.node().hasFn(OpenMaya.MFn.kLambert):
353 if src.typeName() == kSlopeNodeName:
354 srcPlug = src.findPlug(
"outColor")
355 if (
not srcPlug.isNull())
and (
not destinationPlug.isNull()):
356 cmd =
"connectAttr %s %s" % (srcPlug.name(), destinationPlug.name())
357 OpenMaya.MGlobal.executeCommand(cmd)
362 destinationNode = destinationPlug.node()
363 self.connectNodeToNode(sourceNode, destinationNode, force)
366 def _findShadingEngine(self, node):
368 Given the material MObject this method will
369 return the shading group that it is assigned to.
370 if there is no shading group associated with
371 the material than a null MObject is passed back.
374 srcPlug = nodeFn.findPlug(
"outColor")
376 srcPlug.connectedTo(nodeConnections,
False,
True)
381 for i
in range(nodeConnections.length()):
382 theNode = nodeConnections[i].node()
383 if theNode.hasFn(OpenMaya.MFn.kShadingEngine):
396 return OpenMayaMPx.asMPxPtr(slopeShader())
399 def behaviourCreator():
400 return OpenMayaMPx.asMPxPtr(slopeShaderBehavior())
403 def nodeInitializer():
409 slopeShader.aAngle = nAttr.create(
"angle",
"ang", OpenMaya.MFnNumericData.kFloat )
410 nAttr.setDefault(30.0)
418 slopeShader.aColor1 = nAttr.createColor(
"walkableColor",
"w")
419 nAttr.setDefault(0.0, 1.0, 0.0)
422 nAttr.setUsedAsColor(1)
426 slopeShader.aColor2 = nAttr.createColor(
"nonWalkableColor",
"nw" )
427 nAttr.setDefault(1.0, 0.0, 0.0)
430 nAttr.setUsedAsColor(1)
436 slopeShader.aTriangleNormalCamera = nAttr.createPoint(
"triangleNormalCamera",
"n" )
444 slopeShader.aMatrixEyeToWorld = nMAttr.create(
"matrixEyeToWorld",
"mew", OpenMaya.MFnMatrixAttribute.kFloat )
446 nMAttr.setWritable(1)
450 slopeShader.aOutColor = nAttr.createColor(
"outColor",
"oc" )
460 slopeShader.aDirtyShaderAttr = nGAttr.create(
"dirtyShaderPlug",
"dsp")
463 nGAttr.setUsesArrayDataBuilder(1)
464 nGAttr.setReadable(0)
465 nGAttr.setStorable(1)
466 nGAttr.setIndexMatters(0)
471 slopeShader.addAttribute(slopeShader.aAngle)
472 slopeShader.addAttribute(slopeShader.aColor1)
473 slopeShader.addAttribute(slopeShader.aColor2)
474 slopeShader.addAttribute(slopeShader.aTriangleNormalCamera)
475 slopeShader.addAttribute(slopeShader.aOutColor)
476 slopeShader.addAttribute(slopeShader.aMatrixEyeToWorld)
477 slopeShader.addAttribute(slopeShader.aDirtyShaderAttr)
479 slopeShader.attributeAffects (slopeShader.aAngle, slopeShader.aOutColor)
480 slopeShader.attributeAffects (slopeShader.aColor1, slopeShader.aOutColor)
481 slopeShader.attributeAffects (slopeShader.aColor2, slopeShader.aOutColor)
482 slopeShader.attributeAffects (slopeShader.aTriangleNormalCamera, slopeShader.aOutColor)
483 slopeShader.attributeAffects (slopeShader.aDirtyShaderAttr, slopeShader.aOutColor)
487 def initializePlugin(mobject):
488 mplugin = OpenMayaMPx.MFnPlugin(mobject)
492 mplugin.registerNode(kSlopeNodeName, kSlopeNodeId, nodeCreator, nodeInitializer, OpenMayaMPx.MPxNode.kDependNode, kSlopeNodeClassify)
494 sys.stderr.write(
"Failed to register node: %s" % kSlopeNodeName )
499 mplugin.registerDragAndDropBehavior(kSlopeShaderBehaviourName, behaviourCreator)
501 sys.stderr.write(
"Failed to register behaviour: %s" % kSlopeShaderBehaviourName)
503 postCmd =
"if( `window -exists createRenderNodeWindow` ) {refreshCreateRenderNodeWindow(\"%s\");}\n" % kSlopeNodeClassify
504 OpenMaya.MGlobal.executeCommand(postCmd)
508 def uninitializePlugin(mobject):
509 mplugin = OpenMayaMPx.MFnPlugin(mobject,
"Autodesk",
"1.0",
"Any")
511 mplugin.deregisterNode(kSlopeNodeId)
513 sys.stderr.write(
"Failed to unregister node: %s" % kSlopeNodeName )
517 plugin.deregisterDragAndDropBehavior(kSlopeShaderBehaviourName)
519 sys.stderr.write(
"Failed to deregister behaviour: %s" % kSlopeShaderBehaviourName)
521 postCmd =
"if( `window -exists createRenderNodeWindow` ) {refreshCreateRenderNodeWindow(\"%s\");}\n" % kSlopeNodeClassify
522 OpenMaya.MGlobal.executeCommand(postCmd)