scripted/yTwistNode.py

scripted/yTwistNode.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 import math, sys
68 
69 import maya.OpenMaya as OpenMaya
70 import maya.OpenMayaAnim as OpenMayaAnim
71 import maya.OpenMayaMPx as OpenMayaMPx
72 import maya.cmds as cmds
73 
74 kPluginNodeTypeName = "spyTwistNode"
75 
76 yTwistNodeId = OpenMaya.MTypeId(0x8702)
77 
78 kApiVersion = cmds.about(apiVersion=True)
79 if kApiVersion < 201600:
80  outputGeom = OpenMayaMPx.cvar.MPxDeformerNode_outputGeom
81  envelope = OpenMayaMPx.cvar.MPxDeformerNode_envelope
82 else:
83  outputGeom = OpenMayaMPx.cvar.MPxGeometryFilter_outputGeom
84  envelope = OpenMayaMPx.cvar.MPxGeometryFilter_envelope
85 
86 # Node definition
87 class yTwistNode(OpenMayaMPx.MPxDeformerNode):
88  # class variables
89  angle = OpenMaya.MObject()
90  # constructor
91  def __init__(self):
92  OpenMayaMPx.MPxDeformerNode.__init__(self)
93  # deform
94  def deform(self,dataBlock,geomIter,matrix,multiIndex):
95  #
96  # get the angle from the datablock
97  angleHandle = dataBlock.inputValue( self.angle )
98  angleValue = angleHandle.asDouble()
99  #
100  # get the envelope
101  envelopeHandle = dataBlock.inputValue( envelope )
102  envelopeValue = envelopeHandle.asFloat()
103  #
104  # iterate over the object and change the angle
105  while geomIter.isDone() == False:
106  point = geomIter.position()
107  ff = angleValue * point.y * envelopeValue
108  if ff != 0.0:
109  cct= math.cos(ff)
110  cst= math.sin(ff)
111  tt= point.x*cct-point.z*cst
112  point.z= point.x*cst + point.z*cct
113  point.x=tt
114  geomIter.setPosition( point )
115  geomIter.next()
116 
117 # creator
118 def nodeCreator():
119  return OpenMayaMPx.asMPxPtr( yTwistNode() )
120 
121 # initializer
122 def nodeInitializer():
123  # angle
125  yTwistNode.angle = nAttr.create( "angle", "fa", OpenMaya.MFnNumericData.kDouble, 0.0 )
126  #nAttr.setDefault(0.0)
127  nAttr.setKeyable(True)
128  # add attribute
129  try:
130  yTwistNode.addAttribute( yTwistNode.angle )
131  yTwistNode.attributeAffects( yTwistNode.angle, outputGeom )
132  except:
133  sys.stderr.write( "Failed to create attributes of %s node\n", kPluginNodeTypeName )
134 
135 # initialize the script plug-in
136 def initializePlugin(mobject):
137  mplugin = OpenMayaMPx.MFnPlugin(mobject)
138  try:
139  mplugin.registerNode( kPluginNodeTypeName, yTwistNodeId, nodeCreator, nodeInitializer, OpenMayaMPx.MPxNode.kDeformerNode )
140  except:
141  sys.stderr.write( "Failed to register node: %s\n" % kPluginNodeTypeName )
142 
143 # uninitialize the script plug-in
144 def uninitializePlugin(mobject):
145  mplugin = OpenMayaMPx.MFnPlugin(mobject)
146  try:
147  mplugin.deregisterNode( yTwistNodeId )
148  except:
149  sys.stderr.write( "Failed to unregister node: %s\n" % kPluginNodeTypeName )
150 
151