Python API 2.0 Reference
python/api1/py1SimpleSpring.py
1 
2 #-
3 # ==========================================================================
4 # Copyright (C) 1995 - 2006 Autodesk, Inc. and/or its licensors. All
5 # rights reserved.
6 #
7 # The coded instructions, statements, computer programs, and/or related
8 # material (collectively the "Data") in these files contain unpublished
9 # information proprietary to Autodesk, Inc. ("Autodesk") and/or its
10 # licensors, which is protected by U.S. and Canadian federal copyright
11 # law and by international treaties.
12 #
13 # The Data is provided for use exclusively by You. You have the right
14 # to use, modify, and incorporate this Data into other products for
15 # purposes authorized by the Autodesk software license agreement,
16 # without fee.
17 #
18 # The copyright notices in the Software and this entire statement,
19 # including the above license grant, this restriction and the
20 # following disclaimer, must be included in all copies of the
21 # Software, in whole or in part, and all derivative works of
22 # the Software, unless such copies or derivative works are solely
23 # in the form of machine-executable object code generated by a
24 # source language processor.
25 #
26 # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND.
27 # AUTODESK DOES NOT MAKE AND HEREBY DISCLAIMS ANY EXPRESS OR IMPLIED
28 # WARRANTIES INCLUDING, BUT NOT LIMITED TO, THE WARRANTIES OF
29 # NON-INFRINGEMENT, MERCHANTABILITY OR FITNESS FOR A PARTICULAR
30 # PURPOSE, OR ARISING FROM A COURSE OF DEALING, USAGE, OR
31 # TRADE PRACTICE. IN NO EVENT WILL AUTODESK AND/OR ITS LICENSORS
32 # BE LIABLE FOR ANY LOST REVENUES, DATA, OR PROFITS, OR SPECIAL,
33 # DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES, EVEN IF AUTODESK
34 # AND/OR ITS LICENSORS HAS BEEN ADVISED OF THE POSSIBILITY
35 # OR PROBABILITY OF SUCH DAMAGES.
36 #
37 # ==========================================================================
38 #+
39 
40 ########################################################################
41 # DESCRIPTION:
42 #
43 # Produces the dependency graph node "simpleSpring".
44 #
45 # This node is an example of a spring node that calculates the spring behavior,
46 # which Maya uses in a simulation.
47 #
48 ########################################################################
49 
50 import maya.OpenMaya as OpenMaya
51 import maya.OpenMayaMPx as OpenMayaMPx
52 
53 #
54 # To test out this plugin example:
55 # Run from a Python tab of the script editor.
56 #
57 #import maya.cmds as cmds
58 #
59 #cmds.file(force=True, new=True)
60 #cmds.currentUnit(linear="centimeter", angle="degree", time="film")
61 #
62 ## Load the plug-in.
63 ##
64 #cmds.loadPlugin("simpleSpring.py")
65 #
66 ## Create the spring node.
67 #cmds.createNode("spSimpleSpring", name="simpleSpring")
68 #
69 #cmds.curve(d=3, p=[(-9, 6, 0), (-4, 6, 0), (4, 6, 0), (9, 6, 0)], k=[(0), (0), (0), (1), (1), (1)])
70 #
71 #cmds.particle(n="fourParticles", p=[(11, -2, 0), (4, -2, 0), (-3, -2, 0), (-7, 4, 0)], c=1)
72 #
73 #cmds.gravity(pos=(0, 0, 0), m=9.8, dx=0, dy=-1, dz=0)
74 #cmds.connectDynamic("fourParticles", f="gravityField1")
75 #
76 #cmds.select("fourParticles", "curve1", "simpleSpring", r=True)
77 #
78 #cmds.spring(add=True, noDuplicate=False, minMax=True, mnd=0, mxd=0, useRestLengthPS=True, s=1, d=0.2, sfw=1, efw=1)
79 #
80 #cmds.playbackOptions(e=True, min=0.00, max=600.0)
81 #cmds.currentTime(0, e=True)
82 #cmds.play(wait=True, forward=True)
83 #
84 
85 
86 import sys
87 
88 kPluginNodeTypeName = "spSimpleSpring"
89 spSimpleSpringNodeId = OpenMaya.MTypeId(0x80044)
90 
91 
92 class simpleSpring(OpenMayaMPx.MPxSpringNode):
93  aSpringFactor = OpenMaya.MObject()
94 
95  def __init__(self):
96  OpenMayaMPx.MPxSpringNode.__init__(self)
97  self.__myFactor = 0.0
98 
99 
100  def compute(self, plug, block):
101  """
102  In this simple example, do nothing in this method. But get the
103  spring factor here for "applySpringLaw" to compute output force.
104 
105  Note: always let this method return "kUnknownParameter" so that
106  "applySpringLaw" can be called when Maya needs to compute spring force.
107 
108  It is recommended to only override compute() to get user defined
109  attributes.
110  """
111  # Get spring factor,
112  self.__myFactor = self.springFactor(block)
113 
114  # Note: return "kUnknownParameter" so that Maya spring node can
115  # compute spring force for this plug-in simple spring node.
116  return OpenMaya.kUnknownParameter
117 
118 
119  def applySpringLaw(self, stiffness, damping, restLength, endMass1, endMass2,
120  endP1, endP2, endV1, endV2, forceV1, forceV2):
121  """
122  In this overridden method, the attribute, aSpringFactor, is used
123  to compute output force with a simple spring law.
124 
125  F = - factor * (L - restLength) * Vector of (endP1 - endP2).
126  """
127  distV = endP1 - endP2
128  L = distV.length()
129  distV.normalize()
130 
131  F = self.__myFactor * (L - restLength)
132 
133  v1 = distV * -F
134  forceV1.x = v1.x
135  forceV1.y = v1.y
136  forceV1.z = v1.z
137 
138  v2 = -forceV1
139  forceV2.x = v2.x
140  forceV2.y = v2.y
141  forceV2.z = v2.z
142 
143 
144  def springFactor(self, block):
145  hValue = block.inputValue(simpleSpring.aSpringFactor)
146  value = hValue.asDouble()
147  return value
148 
149 
150  def end1WeightValue(self, block):
151  hValue = block.inputValue(simpleSpring.mEnd1Weight)
152  value = hValue.asDouble()
153  return value
154 
155 
156  def end2WeightValue(self, block):
157  hValue = block.inputValue(simpleSpring.mEnd2Weight)
158  value = hValue.asDouble()
159  return value
160 
161 
162 #####################################################################
163 
164 
165 def creator():
166  return OpenMayaMPx.asMPxPtr(simpleSpring())
167 
168 
169 def initializer():
170  numAttr = OpenMaya.MFnNumericAttribute()
171 
172  simpleSpring.aSpringFactor = numAttr.create("springFactor", "sf", OpenMaya.MFnNumericData.kDouble, 1.0)
173  numAttr.setKeyable(True)
174  simpleSpring.addAttribute(simpleSpring.aSpringFactor)
175 
176 
177 # initialize the script plug-in
178 def initializePlugin(mobject):
179  mplugin = OpenMayaMPx.MFnPlugin(mobject, "Autodesk", "3.0", "Any")
180  try:
181  mplugin.registerNode(kPluginNodeTypeName, spSimpleSpringNodeId,
182  creator, initializer, OpenMayaMPx.MPxNode.kSpringNode)
183  except:
184  sys.stderr.write( "Failed to register node: %s" % kPluginNodeTypeName )
185  raise
186 
187 
188 # uninitialize the script plug-in
189 def uninitializePlugin(mobject):
190  mplugin = OpenMayaMPx.MFnPlugin(mobject)
191  try:
192  mplugin.deregisterNode( spSimpleSpringNodeId )
193  except:
194  sys.stderr.write( "Failed to deregister node: %s" % kPluginNodeTypeName )
195  raise