scripted/simpleSpring.py

scripted/simpleSpring.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 import maya.OpenMaya as OpenMaya
41 import maya.OpenMayaMPx as OpenMayaMPx
42 
43 #
44 # To test out this plugin example:
45 # Run from a Python tab of the script editor.
46 #
47 #import maya.cmds as cmds
48 #
49 #cmds.file(force=True, new=True)
50 #cmds.currentUnit(linear="centimeter", angle="degree", time="film")
51 #
52 ## Load the plug-in.
53 ##
54 #cmds.loadPlugin("simpleSpring.py")
55 #
56 ## Create the spring node.
57 #cmds.createNode("spSimpleSpring", name="simpleSpring")
58 #
59 #cmds.curve(d=3, p=[(-9, 6, 0), (-4, 6, 0), (4, 6, 0), (9, 6, 0)], k=[(0), (0), (0), (1), (1), (1)])
60 #
61 #cmds.particle(n="fourParticles", p=[(11, -2, 0), (4, -2, 0), (-3, -2, 0), (-7, 4, 0)], c=1)
62 #
63 #cmds.gravity(pos=(0, 0, 0), m=9.8, dx=0, dy=-1, dz=0)
64 #cmds.connectDynamic("fourParticles", f="gravityField1")
65 #
66 #cmds.select("fourParticles", "curve1", "simpleSpring", r=True)
67 #
68 #cmds.spring(add=True, noDuplicate=False, minMax=True, mnd=0, mxd=0, useRestLengthPS=True, s=1, d=0.2, sfw=1, efw=1)
69 #
70 #cmds.playbackOptions(e=True, min=0.00, max=600.0)
71 #cmds.currentTime(0, e=True)
72 #cmds.play(wait=True, forward=True)
73 #
74 
75 
76 import sys
77 
78 kPluginNodeTypeName = "spSimpleSpring"
79 spSimpleSpringNodeId = OpenMaya.MTypeId(0x87004)
80 
81 
82 class simpleSpring(OpenMayaMPx.MPxSpringNode):
83  aSpringFactor = OpenMaya.MObject()
84 
85  def __init__(self):
86  OpenMayaMPx.MPxSpringNode.__init__(self)
87  self.__myFactor = 0.0
88 
89 
90  def compute(self, plug, block):
91  """
92  In this simple example, do nothing in this method. But get the
93  spring factor here for "applySpringLaw" to compute output force.
94 
95  Note: always let this method return "kUnknownParameter" so that
96  "applySpringLaw" can be called when Maya needs to compute spring force.
97 
98  It is recommended to only override compute() to get user defined
99  attributes.
100  """
101  # Get spring factor,
102  self.__myFactor = self.springFactor(block)
103 
104  # Note: return "kUnknownParameter" so that Maya spring node can
105  # compute spring force for this plug-in simple spring node.
106  return OpenMaya.kUnknownParameter
107 
108 
109  def applySpringLaw(self, stiffness, damping, restLength, endMass1, endMass2,
110  endP1, endP2, endV1, endV2, forceV1, forceV2):
111  """
112  In this overridden method, the attribute, aSpringFactor, is used
113  to compute output force with a simple spring law.
114 
115  F = - factor * (L - restLength) * Vector of (endP1 - endP2).
116  """
117  distV = endP1 - endP2
118  L = distV.length()
119  distV.normalize()
120 
121  F = self.__myFactor * (L - restLength)
122 
123  v1 = distV * -F
124  forceV1.x = v1.x
125  forceV1.y = v1.y
126  forceV1.z = v1.z
127 
128  v2 = -forceV1
129  forceV2.x = v2.x
130  forceV2.y = v2.y
131  forceV2.z = v2.z
132 
133 
134  def springFactor(self, block):
135  hValue = block.inputValue(simpleSpring.aSpringFactor)
136  value = hValue.asDouble()
137  return value
138 
139 
140  def end1WeightValue(self, block):
141  hValue = block.inputValue(simpleSpring.mEnd1Weight)
142  value = hValue.asDouble()
143  return value
144 
145 
146  def end2WeightValue(self, block):
147  hValue = block.inputValue(simpleSpring.mEnd2Weight)
148  value = hValue.asDouble()
149  return value
150 
151 
152 #####################################################################
153 
154 
155 def creator():
156  return OpenMayaMPx.asMPxPtr(simpleSpring())
157 
158 
159 def initializer():
160  numAttr = OpenMaya.MFnNumericAttribute()
161 
162  simpleSpring.aSpringFactor = numAttr.create("springFactor", "sf", OpenMaya.MFnNumericData.kDouble, 1.0)
163  numAttr.setKeyable(True)
164  simpleSpring.addAttribute(simpleSpring.aSpringFactor)
165 
166 
167 # initialize the script plug-in
168 def initializePlugin(mobject):
169  mplugin = OpenMayaMPx.MFnPlugin(mobject, "Autodesk", "3.0", "Any")
170  try:
171  mplugin.registerNode(kPluginNodeTypeName, spSimpleSpringNodeId,
172  creator, initializer, OpenMayaMPx.MPxNode.kSpringNode)
173  except:
174  sys.stderr.write( "Failed to register node: %s" % kPluginNodeTypeName )
175  raise
176 
177 
178 # uninitialize the script plug-in
179 def uninitializePlugin(mobject):
180  mplugin = OpenMayaMPx.MFnPlugin(mobject)
181  try:
182  mplugin.deregisterNode( spSimpleSpringNodeId )
183  except:
184  sys.stderr.write( "Failed to deregister node: %s" % kPluginNodeTypeName )
185  raise