scripted/parentAddedMsgCmd.py

scripted/parentAddedMsgCmd.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 # import maya
40 # maya.cmds.loadPlugin("parentAddedMsgCmd.py")
41 # maya.cmds.spParentAddedMsg()
42 
43 import sys
44 import maya.OpenMaya as OpenMaya
45 import maya.OpenMayaMPx as OpenMayaMPx
46 
47 kPluginCmdName = "spParentAddedMsg"
48 
49 messageId = 0
50 messageIdSet = False
51 
52 def removeCallback(id):
53  try:
54  OpenMaya.MMessage.removeCallback( id )
55  except:
56  sys.stderr.write( "Failed to remove callback\n" )
57  raise
58 
59 def dagParentAddedCallback( child, parent, clientData ):
60  print "dagParentAddedCallback..."
61  print "\tchild %s" % child.fullPathName()
62  print "\tparent %s" % parent.fullPathName()
63  print "\tclient data %s" % clientData
64 
65 def createParentAddedCallback(stringData):
66  # global declares module level variables that will be assigned
67  global messageIdSet
68  try:
69  id = OpenMaya.MDagMessage.addParentAddedCallback( dagParentAddedCallback, stringData )
70  except:
71  sys.stderr.write( "Failed to install dag parent added callback\n" )
72  messageIdSet = False
73  else:
74  messageIdSet = True
75  return id
76 
77 # command
78 class scriptedCommand(OpenMayaMPx.MPxCommand):
79  def __init__(self):
80  OpenMayaMPx.MPxCommand.__init__(self)
81  def doIt(self,argList):
82  global messageId
83  if ( messageIdSet ):
84  print "Message callaback already installed"
85  else:
86  print "Installing parent added callback message"
87  messageId = createParentAddedCallback( "_noData_" )
88 
89 # Creator
90 def cmdCreator():
91  return OpenMayaMPx.asMPxPtr( scriptedCommand() )
92 
93 # Initialize the script plug-in
94 def initializePlugin(mobject):
95  mplugin = OpenMayaMPx.MFnPlugin(mobject)
96  try:
97  mplugin.registerCommand( kPluginCmdName, cmdCreator )
98  except:
99  sys.stderr.write( "Failed to register command: %s\n" % name )
100  raise
101 
102 # Uninitialize the script plug-in
103 def uninitializePlugin(mobject):
104  # Remove the callback
105  if ( messageIdSet ):
106  removeCallback( messageId )
107  # Remove the plug-in command
108  mplugin = OpenMayaMPx.MFnPlugin(mobject)
109  try:
110  mplugin.deregisterCommand( kPluginCmdName )
111  except:
112  sys.stderr.write( "Failed to unregister command: %s\n" % kPluginCmdName )
113  raise
114 
115