Python API 2.0 Reference
python/api1/py1HelloWorldCmd.py
1 ########################################################################
2 # DESCRIPTION:
3 #
4 # Produces the Python command "spHelloWorld".
5 #
6 # This is a simple demonstration of how to use a Python plug-in.
7 #
8 # To use, make sure that helloWorldCmd.py is in your MAYA_PLUG_IN_PATH (and the
9 # C++ version is not), and then do the following:
10 #
11 # import maya.cmds
12 # maya.cmds.loadPlugin("helloWorldCmd.py")
13 # maya.cmds.spHelloWorld()
14 #
15 # A "Hello World" text is output to the script editor window.
16 #
17 ########################################################################
18 
19 import sys
20 import maya.OpenMaya as OpenMaya
21 import maya.OpenMayaMPx as OpenMayaMPx
22 
23 # command
24 class HelloWorldCmd(OpenMayaMPx.MPxCommand):
25  kPluginCmdName = "spHelloWorld"
26 
27  def __init__(self):
28  OpenMayaMPx.MPxCommand.__init__(self)
29 
30  @staticmethod
31  def cmdCreator():
32  return OpenMayaMPx.asMPxPtr( HelloWorldCmd() )
33 
34  def doIt(self, argList):
35  print("Hello World!")
36 
37 
38 # Initialize the script plug-in
39 def initializePlugin(plugin):
40  pluginFn = OpenMayaMPx.MFnPlugin(plugin)
41  try:
42  pluginFn.registerCommand(
43  HelloWorldCmd.kPluginCmdName, HelloWorldCmd.cmdCreator
44  )
45  except:
46  sys.stderr.write(
47  "Failed to register command: %s\n" % HelloWorldCmd.kPluginCmdName
48  )
49  raise
50 
51 # Uninitialize the script plug-in
52 def uninitializePlugin(plugin):
53  pluginFn = OpenMayaMPx.MFnPlugin(plugin)
54  try:
55  pluginFn.deregisterCommand(HelloWorldCmd.kPluginCmdName)
56  except:
57  sys.stderr.write(
58  "Failed to unregister command: %s\n" % HelloWorldCmd.kPluginCmdName
59  )
60  raise
61 
62 #-
63 # ==========================================================================
64 # Copyright (C) 2011 Autodesk, Inc. and/or its licensors. All
65 # rights reserved.
66 #
67 # The coded instructions, statements, computer programs, and/or related
68 # material (collectively the "Data") in these files contain unpublished
69 # information proprietary to Autodesk, Inc. ("Autodesk") and/or its
70 # licensors, which is protected by U.S. and Canadian federal copyright
71 # law and by international treaties.
72 #
73 # The Data is provided for use exclusively by You. You have the right
74 # to use, modify, and incorporate this Data into other products for
75 # purposes authorized by the Autodesk software license agreement,
76 # without fee.
77 #
78 # The copyright notices in the Software and this entire statement,
79 # including the above license grant, this restriction and the
80 # following disclaimer, must be included in all copies of the
81 # Software, in whole or in part, and all derivative works of
82 # the Software, unless such copies or derivative works are solely
83 # in the form of machine-executable object code generated by a
84 # source language processor.
85 #
86 # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND.
87 # AUTODESK DOES NOT MAKE AND HEREBY DISCLAIMS ANY EXPRESS OR IMPLIED
88 # WARRANTIES INCLUDING, BUT NOT LIMITED TO, THE WARRANTIES OF
89 # NON-INFRINGEMENT, MERCHANTABILITY OR FITNESS FOR A PARTICULAR
90 # PURPOSE, OR ARISING FROM A COURSE OF DEALING, USAGE, OR
91 # TRADE PRACTICE. IN NO EVENT WILL AUTODESK AND/OR ITS LICENSORS
92 # BE LIABLE FOR ANY LOST REVENUES, DATA, OR PROFITS, OR SPECIAL,
93 # DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES, EVEN IF AUTODESK
94 # AND/OR ITS LICENSORS HAS BEEN ADVISED OF THE POSSIBILITY
95 # OR PROBABILITY OF SUCH DAMAGES.
96 #
97 # ==========================================================================
98 #+