scripted/geomShader.py

scripted/geomShader.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 import sys
39 import maya.OpenMaya as OpenMaya
40 import maya.OpenMayaMPx as OpenMayaMPx
41 
42 kPluginNodeName = "spGeomShader"
43 kPluginNodeClassify = "utility/color"
44 kPluginNodeId = OpenMaya.MTypeId(0x8700E)
45 
46 class geomShader(OpenMayaMPx.MPxNode):
47  def __init__(self):
48  OpenMayaMPx.MPxNode.__init__(self)
49  aOutColor = OpenMaya.MObject()
50  aPoint = OpenMaya.MObject()
51  aScale = OpenMaya.MObject()
52  aOffset = OpenMaya.MObject()
53 
54  def compute(self, plug, block):
55  if plug == geomShader.aOutColor or plug.parent() == geomShader.aOutColor:
56  resultColor = OpenMaya.MFloatVector(0.0,0.0,0.0)
57 
58  point = block.inputValue( geomShader.aPoint ).asFloatVector()
59  scale = block.inputValue( geomShader.aScale ).asFloatVector()
60  offset = block.inputValue( geomShader.aOffset ).asFloatVector()
61 
62  resultColor.x = point.x * scale.x + offset.x
63  resultColor.y = point.y * scale.y + offset.y
64  resultColor.x = point.z * scale.z + offset.z
65 
66  outColorHandle = block.outputValue( geomShader.aOutColor )
67  outColorHandle.setMFloatVector(resultColor)
68  outColorHandle.setClean()
69  else:
70  return OpenMaya.kUnknownParameter
71 
72 
73 def nodeCreator():
74  return OpenMayaMPx.asMPxPtr( geomShader() )
75 
76 def nodeInitializer():
77  nAttr = OpenMaya.MFnNumericAttribute()
78 
79  try:
80  geomShader.aPoint = nAttr.createPoint("pointObj", "p")
81  nAttr.setStorable(0)
82  nAttr.setHidden(1)
83 
84  geomShader.aScale = nAttr.createPoint("scale", "s")
85  nAttr.setKeyable(1)
86  nAttr.setStorable(1)
87  nAttr.setReadable(1)
88  nAttr.setWritable(1)
89  nAttr.setDefault(1.0, 1.0, 1.0)
90 
91  geomShader.aOffset = nAttr.createPoint("offset", "o")
92  nAttr.setKeyable(1)
93  nAttr.setStorable(1)
94  nAttr.setReadable(1)
95  nAttr.setWritable(1)
96 
97  geomShader.aOutColor = nAttr.createColor("outColor", "oc")
98  nAttr.setStorable(0)
99  nAttr.setHidden(0)
100  nAttr.setReadable(1)
101  nAttr.setWritable(0)
102 
103  except:
104  sys.stderr.write("Failed to create attributes\n")
105  raise
106 
107  try:
108  geomShader.addAttribute(geomShader.aPoint)
109  geomShader.addAttribute(geomShader.aScale)
110  geomShader.addAttribute(geomShader.aOffset)
111  geomShader.addAttribute(geomShader.aOutColor)
112  except:
113  sys.stderr.write("Failed to add attributes\n")
114  raise
115 
116  try:
117  geomShader.attributeAffects (geomShader.aPoint, geomShader.aOutColor)
118  geomShader.attributeAffects (geomShader.aScale, geomShader.aOutColor)
119  geomShader.attributeAffects (geomShader.aOffset, geomShader.aOutColor)
120  except:
121  sys.stderr.write("Failed in setting attributeAffects\n")
122  raise
123 
124 
125 # initialize the script plug-in
126 def initializePlugin(mobject):
127  mplugin = OpenMayaMPx.MFnPlugin(mobject)
128  try:
129  mplugin.registerNode( kPluginNodeName, kPluginNodeId, nodeCreator,
130  nodeInitializer, OpenMayaMPx.MPxNode.kDependNode, kPluginNodeClassify )
131  except:
132  sys.stderr.write( "Failed to register node: %s" % kPluginNodeName )
133  raise
134 
135 # uninitialize the script plug-in
136 def uninitializePlugin(mobject):
137  mplugin = OpenMayaMPx.MFnPlugin(mobject)
138  try:
139  mplugin.deregisterNode( kPluginNodeId )
140  except:
141  sys.stderr.write( "Failed to deregister node: %s" % kPluginNodeName )
142  raise
143