scripted/helixCmd.py

scripted/helixCmd.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 # Creation Date: 2 October 2006
42 #
43 # Description:
44 #
45 # Creates a NURBS curve in the shape of a helix
46 #
47 # Options:
48 #
49 # p=# The pitch of the helix, default to 0.5
50 # r=# The radius of the helix, default to 4.0
51 #
52 # Example:
53 #
54 # From Python:
55 # import maya
56 # maya.cmds.spHelix(p=0.3, r=7)
57 #
58 # From Mel:
59 # spHelix -p 0.3 -r 7
60 #
61 
62 import maya.OpenMaya as OpenMaya
63 import maya.OpenMayaMPx as OpenMayaMPx
64 import sys, math
65 
66 kPluginCmdName="spHelix"
67 
68 kPitchFlag = "-p"
69 kPitchLongFlag = "-pitch"
70 kRadiusFlag = "-r"
71 kRadiusLongFlag = "-radius"
72 
73 # command
74 class scriptedCommand(OpenMayaMPx.MPxCommand):
75  def __init__(self):
76  OpenMayaMPx.MPxCommand.__init__(self)
77 
78  def doIt(self, args):
79  deg = 3
80  ncvs = 20
81  spans = ncvs - deg
82  nknots = spans+2*deg-1
83  radius = 4.0
84  pitch = 0.5
85 
86  # Parse the arguments.
87  argData = OpenMaya.MArgDatabase(self.syntax(), args)
88  if argData.isFlagSet(kPitchFlag):
89  pitch = argData.flagArgumentDouble(kPitchFlag, 0)
90  if argData.isFlagSet(kRadiusFlag):
91  radius = argData.flagArgumentDouble(kRadiusFlag, 0)
92 
93  controlVertices = OpenMaya.MPointArray()
94  knotSequences = OpenMaya.MDoubleArray()
95 
96  # Set up cvs and knots for the helix
97  #
98  for i in range(0, ncvs):
99  controlVertices.append( OpenMaya.MPoint( radius * math.cos(i),
100  pitch * i, radius * math.sin(i) ) )
101 
102  for i in range(0, nknots):
103  knotSequences.append( i )
104 
105  # Now create the curve
106  #
107  curveFn = OpenMaya.MFnNurbsCurve()
108 
109  nullObj = OpenMaya.MObject()
110 
111  try:
112  # This plugin normally creates the curve by passing in the
113  # cv's. A function to create curves by passing in the ep's
114  # has been added. Set this to False to get that behaviour.
115  #
116  if True:
117  curveFn.create( controlVertices,
118  knotSequences, deg,
119  OpenMaya.MFnNurbsCurve.kOpen,
120  0, 0,
121  nullObj )
122  else:
123  curveFn.createWithEditPoints(controlVertices,
124  3, OpenMaya.MFnNurbsCurve.kOpen,
125  False, False, False)
126  except:
127  sys.stderr.write( "Error creating curve.\n" )
128  raise
129 
130 # Creator
131 def cmdCreator():
132  # Create the command
133  return OpenMayaMPx.asMPxPtr( scriptedCommand() )
134 
135 # Syntax creator
136 def syntaxCreator():
137  syntax = OpenMaya.MSyntax()
138  syntax.addFlag(kPitchFlag, kPitchLongFlag, OpenMaya.MSyntax.kDouble)
139  syntax.addFlag(kRadiusFlag, kRadiusLongFlag, OpenMaya.MSyntax.kDouble)
140  return syntax
141 
142 # Initialize the script plug-in
143 def initializePlugin(mobject):
144  mplugin = OpenMayaMPx.MFnPlugin(mobject, "Autodesk", "1.0", "Any")
145  try:
146  mplugin.registerCommand( kPluginCmdName, cmdCreator, syntaxCreator )
147  except:
148  sys.stderr.write( "Failed to register command: %s\n" % kPluginCmdName )
149  raise
150 
151 # Uninitialize the script plug-in
152 def uninitializePlugin(mobject):
153  mplugin = OpenMayaMPx.MFnPlugin(mobject)
154  try:
155  mplugin.deregisterCommand( kPluginCmdName )
156  except:
157  sys.stderr.write( "Failed to unregister command: %s\n" % kPluginCmdName )
158  raise
159 
160