Python API 2.0 Reference
python/api1/py1GeomShader.py
1 #-
2 # ==========================================================================
3 # Copyright (C) 1995 - 2006 Autodesk, Inc. and/or its licensors. All
4 # rights reserved.
5 #
6 # The coded instructions, statements, computer programs, and/or related
7 # material (collectively the "Data") in these files contain unpublished
8 # information proprietary to Autodesk, Inc. ("Autodesk") and/or its
9 # licensors, which is protected by U.S. and Canadian federal copyright
10 # law and by international treaties.
11 #
12 # The Data is provided for use exclusively by You. You have the right
13 # to use, modify, and incorporate this Data into other products for
14 # purposes authorized by the Autodesk software license agreement,
15 # without fee.
16 #
17 # The copyright notices in the Software and this entire statement,
18 # including the above license grant, this restriction and the
19 # following disclaimer, must be included in all copies of the
20 # Software, in whole or in part, and all derivative works of
21 # the Software, unless such copies or derivative works are solely
22 # in the form of machine-executable object code generated by a
23 # source language processor.
24 #
25 # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND.
26 # AUTODESK DOES NOT MAKE AND HEREBY DISCLAIMS ANY EXPRESS OR IMPLIED
27 # WARRANTIES INCLUDING, BUT NOT LIMITED TO, THE WARRANTIES OF
28 # NON-INFRINGEMENT, MERCHANTABILITY OR FITNESS FOR A PARTICULAR
29 # PURPOSE, OR ARISING FROM A COURSE OF DEALING, USAGE, OR
30 # TRADE PRACTICE. IN NO EVENT WILL AUTODESK AND/OR ITS LICENSORS
31 # BE LIABLE FOR ANY LOST REVENUES, DATA, OR PROFITS, OR SPECIAL,
32 # DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES, EVEN IF AUTODESK
33 # AND/OR ITS LICENSORS HAS BEEN ADVISED OF THE POSSIBILITY
34 # OR PROBABILITY OF SUCH DAMAGES.
35 #
36 # ==========================================================================
37 #+
38 
39 ########################################################################
40 # DESCRIPTION:
41 #
42 # Produces a dependency graph node "spGeomShader".
43 #
44 # This node is an example of evaluating the geometric xyz position on an object.
45 #
46 # The inputs for this node are scale and offsets depicted as sliders in the
47 # Attribute Editor for the node.
48 #
49 # The output attribute of the spGeomShader node is called "outColor". It is a 3 float
50 # value that represents the xyz position on the object. To use this shader, create a
51 # spGeomShader node and connect its output "outColor" to an input of a surface/shader node
52 # like Color.
53 #
54 ########################################################################
55 
56 import sys
57 import maya.OpenMaya as OpenMaya
58 import maya.OpenMayaMPx as OpenMayaMPx
59 
60 kPluginNodeName = "spGeomShader"
61 kPluginNodeClassify = "utility/color"
62 kPluginNodeId = OpenMaya.MTypeId( 0x00080055 )
63 
64 class geomShader(OpenMayaMPx.MPxNode):
65  def __init__(self):
66  OpenMayaMPx.MPxNode.__init__(self)
67  aOutColor = OpenMaya.MObject()
68  aPoint = OpenMaya.MObject()
69  aScale = OpenMaya.MObject()
70  aOffset = OpenMaya.MObject()
71 
72  def compute(self, plug, block):
73  if plug == geomShader.aOutColor or plug.parent() == geomShader.aOutColor:
74  resultColor = OpenMaya.MFloatVector(0.0,0.0,0.0)
75 
76  point = block.inputValue( geomShader.aPoint ).asFloatVector()
77  scale = block.inputValue( geomShader.aScale ).asFloatVector()
78  offset = block.inputValue( geomShader.aOffset ).asFloatVector()
79 
80  resultColor.x = point.x * scale.x + offset.x
81  resultColor.y = point.y * scale.y + offset.y
82  resultColor.x = point.z * scale.z + offset.z
83 
84  outColorHandle = block.outputValue( geomShader.aOutColor )
85  outColorHandle.setMFloatVector(resultColor)
86  outColorHandle.setClean()
87  else:
88  return OpenMaya.kUnknownParameter
89 
90 
91 def nodeCreator():
92  return OpenMayaMPx.asMPxPtr( geomShader() )
93 
94 def nodeInitializer():
96 
97  try:
98  geomShader.aPoint = nAttr.createPoint("pointObj", "p")
99  nAttr.setStorable(0)
100  nAttr.setHidden(1)
101 
102  geomShader.aScale = nAttr.createPoint("scale", "s")
103  nAttr.setKeyable(1)
104  nAttr.setStorable(1)
105  nAttr.setReadable(1)
106  nAttr.setWritable(1)
107  nAttr.setDefault(1.0, 1.0, 1.0)
108 
109  geomShader.aOffset = nAttr.createPoint("offset", "o")
110  nAttr.setKeyable(1)
111  nAttr.setStorable(1)
112  nAttr.setReadable(1)
113  nAttr.setWritable(1)
114 
115  geomShader.aOutColor = nAttr.createColor("outColor", "oc")
116  nAttr.setStorable(0)
117  nAttr.setHidden(0)
118  nAttr.setReadable(1)
119  nAttr.setWritable(0)
120 
121  except:
122  sys.stderr.write("Failed to create attributes\n")
123  raise
124 
125  try:
126  geomShader.addAttribute(geomShader.aPoint)
127  geomShader.addAttribute(geomShader.aScale)
128  geomShader.addAttribute(geomShader.aOffset)
129  geomShader.addAttribute(geomShader.aOutColor)
130  except:
131  sys.stderr.write("Failed to add attributes\n")
132  raise
133 
134  try:
135  geomShader.attributeAffects (geomShader.aPoint, geomShader.aOutColor)
136  geomShader.attributeAffects (geomShader.aScale, geomShader.aOutColor)
137  geomShader.attributeAffects (geomShader.aOffset, geomShader.aOutColor)
138  except:
139  sys.stderr.write("Failed in setting attributeAffects\n")
140  raise
141 
142 
143 # initialize the script plug-in
144 def initializePlugin(mobject):
145  mplugin = OpenMayaMPx.MFnPlugin(mobject)
146  try:
147  mplugin.registerNode( kPluginNodeName, kPluginNodeId, nodeCreator,
148  nodeInitializer, OpenMayaMPx.MPxNode.kDependNode, kPluginNodeClassify )
149  except:
150  sys.stderr.write( "Failed to register node: %s" % kPluginNodeName )
151  raise
152 
153 # uninitialize the script plug-in
154 def uninitializePlugin(mobject):
155  mplugin = OpenMayaMPx.MFnPlugin(mobject)
156  try:
157  mplugin.deregisterNode( kPluginNodeId )
158  except:
159  sys.stderr.write( "Failed to deregister node: %s" % kPluginNodeName )
160  raise
161