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