Python API 2.0 Reference
python/api1/py1ParentAddedMsgCmd.py
1 #-
2 # ==========================================================================
3 # Copyright (C) 1995 - 2006 Autodesk, Inc. and/or its licensors. All
4 # rights reserved.
5 #
6 # The coded instructions, statements, computer programs, and/or related
7 # material (collectively the "Data") in these files contain unpublished
8 # information proprietary to Autodesk, Inc. ("Autodesk") and/or its
9 # licensors, which is protected by U.S. and Canadian federal copyright
10 # law and by international treaties.
11 #
12 # The Data is provided for use exclusively by You. You have the right
13 # to use, modify, and incorporate this Data into other products for
14 # purposes authorized by the Autodesk software license agreement,
15 # without fee.
16 #
17 # The copyright notices in the Software and this entire statement,
18 # including the above license grant, this restriction and the
19 # following disclaimer, must be included in all copies of the
20 # Software, in whole or in part, and all derivative works of
21 # the Software, unless such copies or derivative works are solely
22 # in the form of machine-executable object code generated by a
23 # source language processor.
24 #
25 # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND.
26 # AUTODESK DOES NOT MAKE AND HEREBY DISCLAIMS ANY EXPRESS OR IMPLIED
27 # WARRANTIES INCLUDING, BUT NOT LIMITED TO, THE WARRANTIES OF
28 # NON-INFRINGEMENT, MERCHANTABILITY OR FITNESS FOR A PARTICULAR
29 # PURPOSE, OR ARISING FROM A COURSE OF DEALING, USAGE, OR
30 # TRADE PRACTICE. IN NO EVENT WILL AUTODESK AND/OR ITS LICENSORS
31 # BE LIABLE FOR ANY LOST REVENUES, DATA, OR PROFITS, OR SPECIAL,
32 # DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES, EVEN IF AUTODESK
33 # AND/OR ITS LICENSORS HAS BEEN ADVISED OF THE POSSIBILITY
34 # OR PROBABILITY OF SUCH DAMAGES.
35 #
36 # ==========================================================================
37 #+
38 
39 ########################################################################
40 # DESCRIPTION:
41 #
42 # Produces the Python command "spParentAddedMsg".
43 #
44 # This plug-in demonstrates how to create and handle messages in Python.
45 # A global "parent added" message along with user defined client data is created.
46 # Invoking an operation in Maya such as creating a polygon plane will invoke the callback.
47 #
48 # To use, you can execute the following:
49 #
50 # import maya.cmds as cmds
51 # cmds.loadPlugin("parentAddedMsgCmd.py")
52 # cmds.spParentAddedMsg()
53 # cmds.polyPlane()
54 #
55 ########################################################################
56 
57 import sys
58 import maya.OpenMaya as OpenMaya
59 import maya.OpenMayaMPx as OpenMayaMPx
60 
61 kPluginCmdName = "spParentAddedMsg"
62 
63 messageId = 0
64 messageIdSet = False
65 
66 def removeCallback(id):
67  try:
69  except:
70  sys.stderr.write( "Failed to remove callback\n" )
71  raise
72 
73 def dagParentAddedCallback( child, parent, clientData ):
74  print("dagParentAddedCallback...")
75  print("\tchild %s" % child.fullPathName())
76  print("\tparent %s" % parent.fullPathName())
77  print("\tclient data %s" % clientData)
78 
79 def createParentAddedCallback(stringData):
80  # global declares module level variables that will be assigned
81  global messageIdSet
82  try:
83  id = OpenMaya.MDagMessage.addParentAddedCallback( dagParentAddedCallback, stringData )
84  except:
85  sys.stderr.write( "Failed to install dag parent added callback\n" )
86  messageIdSet = False
87  else:
88  messageIdSet = True
89  return id
90 
91 # command
92 class scriptedCommand(OpenMayaMPx.MPxCommand):
93  def __init__(self):
94  OpenMayaMPx.MPxCommand.__init__(self)
95  def doIt(self,argList):
96  global messageId
97  if ( messageIdSet ):
98  print("Message callaback already installed")
99  else:
100  print("Installing parent added callback message")
101  messageId = createParentAddedCallback( "_noData_" )
102 
103 # Creator
104 def cmdCreator():
105  return OpenMayaMPx.asMPxPtr( scriptedCommand() )
106 
107 # Initialize the script plug-in
108 def initializePlugin(mobject):
109  mplugin = OpenMayaMPx.MFnPlugin(mobject)
110  try:
111  mplugin.registerCommand( kPluginCmdName, cmdCreator )
112  except:
113  sys.stderr.write( "Failed to register command: %s\n" % name )
114  raise
115 
116 # Uninitialize the script plug-in
117 def uninitializePlugin(mobject):
118  # Remove the callback
119  if ( messageIdSet ):
120  removeCallback( messageId )
121  # Remove the plug-in command
122  mplugin = OpenMayaMPx.MFnPlugin(mobject)
123  try:
124  mplugin.deregisterCommand( kPluginCmdName )
125  except:
126  sys.stderr.write( "Failed to unregister command: %s\n" % kPluginCmdName )
127  raise
128 
129