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