Python API 2.0 Reference
python/api1/py1YTwistNode.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 the dependency graph node "spyTwistNode".
43 #
44 # This plug-in demonstrates how to create a user-defined deformer.
45 # A deformer is a node which takes any number of input geometries, deforms them,
46 # and places the output into the output geometry attribute. This example plug-in
47 # defines a new deformer node that twists the deformed vertices of the input
48 # around the y-axis.
49 #
50 # To use this node:
51 #
52 # (1) Load the plug-in and create a sphere (or some other object):
53 #
54 # import maya
55 # maya.cmds.loadPlugin("yTwistNode.py")
56 # maya.cmds.sphere()
57 #
58 # (2) Select the sphere, and then enter the command:
59 #
60 # maya.cmds.deformer( type='spyTwistNode' )
61 #
62 # Bring up the channel box and select the spyTwistNode1 input.
63 # You can change the Angle value to deform the geometry.
64 #
65 ########################################################################
66 
67 from builtins import next
68 import math, sys
69 
70 import maya.OpenMaya as OpenMaya
71 import maya.OpenMayaAnim as OpenMayaAnim
72 import maya.OpenMayaMPx as OpenMayaMPx
73 import maya.cmds as cmds
74 
75 kPluginNodeTypeName = "spyTwistNode"
76 
77 yTwistNodeId = OpenMaya.MTypeId(0x0008004a)
78 
79 kApiVersion = cmds.about(apiVersion=True)
80 if kApiVersion < 201600:
81  outputGeom = OpenMayaMPx.cvar.MPxDeformerNode_outputGeom
82  envelope = OpenMayaMPx.cvar.MPxDeformerNode_envelope
83 else:
84  outputGeom = OpenMayaMPx.cvar.MPxGeometryFilter_outputGeom
85  envelope = OpenMayaMPx.cvar.MPxGeometryFilter_envelope
86 
87 # Node definition
88 class yTwistNode(OpenMayaMPx.MPxDeformerNode):
89  # class variables
90  angle = OpenMaya.MObject()
91  # constructor
92  def __init__(self):
93  OpenMayaMPx.MPxDeformerNode.__init__(self)
94  # deform
95  def deform(self,dataBlock,geomIter,matrix,multiIndex):
96  #
97  # get the angle from the datablock
98  angleHandle = dataBlock.inputValue( self.angle )
99  angleValue = angleHandle.asDouble()
100  #
101  # get the envelope
102  envelopeHandle = dataBlock.inputValue( envelope )
103  envelopeValue = envelopeHandle.asFloat()
104  #
105  # iterate over the object and change the angle
106  while geomIter.isDone() == False:
107  point = geomIter.position()
108  ff = angleValue * point.y * envelopeValue
109  if ff != 0.0:
110  cct= math.cos(ff)
111  cst= math.sin(ff)
112  tt= point.x*cct-point.z*cst
113  point.z= point.x*cst + point.z*cct
114  point.x=tt
115  geomIter.setPosition( point )
116  next(geomIter)
117 
118 # creator
119 def nodeCreator():
120  return OpenMayaMPx.asMPxPtr( yTwistNode() )
121 
122 # initializer
123 def nodeInitializer():
124  # angle
126  yTwistNode.angle = nAttr.create( "angle", "fa", OpenMaya.MFnNumericData.kDouble, 0.0 )
127  #nAttr.setDefault(0.0)
128  nAttr.setKeyable(True)
129  # add attribute
130  try:
131  yTwistNode.addAttribute( yTwistNode.angle )
132  yTwistNode.attributeAffects( yTwistNode.angle, outputGeom )
133  except:
134  sys.stderr.write( "Failed to create attributes of %s node\n", kPluginNodeTypeName )
135 
136 # initialize the script plug-in
137 def initializePlugin(mobject):
138  mplugin = OpenMayaMPx.MFnPlugin(mobject)
139  try:
140  mplugin.registerNode( kPluginNodeTypeName, yTwistNodeId, nodeCreator, nodeInitializer, OpenMayaMPx.MPxNode.kDeformerNode )
141  except:
142  sys.stderr.write( "Failed to register node: %s\n" % kPluginNodeTypeName )
143 
144 # uninitialize the script plug-in
145 def uninitializePlugin(mobject):
146  mplugin = OpenMayaMPx.MFnPlugin(mobject)
147  try:
148  mplugin.deregisterNode( yTwistNodeId )
149  except:
150  sys.stderr.write( "Failed to unregister node: %s\n" % kPluginNodeTypeName )
151 
152