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 # import maya
40 # maya.cmds.loadPlugin("yTwistNode.py")
41 # maya.cmds.sphere()
42 # maya.cmds.deformer( type='spyTwistNode' )
43 
44 import math, sys
45 
46 import maya.OpenMaya as OpenMaya
47 import maya.OpenMayaAnim as OpenMayaAnim
48 import maya.OpenMayaMPx as OpenMayaMPx
49 
50 kPluginNodeTypeName = "spyTwistNode"
51 
52 yTwistNodeId = OpenMaya.MTypeId(0x8702)
53 
54 # Node definition
55 class yTwistNode(OpenMayaMPx.MPxDeformerNode):
56  # class variables
57  angle = OpenMaya.MObject()
58  # constructor
59  def __init__(self):
60  OpenMayaMPx.MPxDeformerNode.__init__(self)
61  # deform
62  def deform(self,dataBlock,geomIter,matrix,multiIndex):
63  #
64  # get the angle from the datablock
65  angleHandle = dataBlock.inputValue( self.angle )
66  angleValue = angleHandle.asDouble()
67  #
68  # get the envelope
69  envelope = OpenMayaMPx.cvar.MPxDeformerNode_envelope
70  envelopeHandle = dataBlock.inputValue( envelope )
71  envelopeValue = envelopeHandle.asFloat()
72  #
73  # iterate over the object and change the angle
74  while geomIter.isDone() == False:
75  point = geomIter.position()
76  ff = angleValue * point.y * envelopeValue
77  if ff != 0.0:
78  cct= math.cos(ff)
79  cst= math.sin(ff)
80  tt= point.x*cct-point.z*cst
81  point.z= point.x*cst + point.z*cct
82  point.x=tt
83  geomIter.setPosition( point )
84  geomIter.next()
85 
86 # creator
87 def nodeCreator():
88  return OpenMayaMPx.asMPxPtr( yTwistNode() )
89 
90 # initializer
91 def nodeInitializer():
92  # angle
93  nAttr = OpenMaya.MFnNumericAttribute()
94  yTwistNode.angle = nAttr.create( "angle", "fa", OpenMaya.MFnNumericData.kDouble, 0.0 )
95  #nAttr.setDefault(0.0)
96  nAttr.setKeyable(True)
97  # add attribute
98  try:
99  yTwistNode.addAttribute( yTwistNode.angle )
100  outputGeom = OpenMayaMPx.cvar.MPxDeformerNode_outputGeom
101  yTwistNode.attributeAffects( yTwistNode.angle, outputGeom )
102  except:
103  sys.stderr.write( "Failed to create attributes of %s node\n", kPluginNodeTypeName )
104 
105 # initialize the script plug-in
106 def initializePlugin(mobject):
107  mplugin = OpenMayaMPx.MFnPlugin(mobject)
108  try:
109  mplugin.registerNode( kPluginNodeTypeName, yTwistNodeId, nodeCreator, nodeInitializer, OpenMayaMPx.MPxNode.kDeformerNode )
110  except:
111  sys.stderr.write( "Failed to register node: %s\n" % kPluginNodeTypeName )
112 
113 # uninitialize the script plug-in
114 def uninitializePlugin(mobject):
115  mplugin = OpenMayaMPx.MFnPlugin(mobject)
116  try:
117  mplugin.deregisterNode( yTwistNodeId )
118  except:
119  sys.stderr.write( "Failed to unregister node: %s\n" % kPluginNodeTypeName )
120 
121