Python API 2.0 Reference
cameraMessageCmd/cameraMessageTest.py
1 """
2 This is example code demonstrating how to add and remove MCameraMessages using the
3 Maya Python API 1.0 .
4 
5 """
6 
7 import maya.OpenMaya as OpenMaya
8 import maya.cmds
9 
10 def beginManipCB(node, clientData):
11  print("Inside beginManipCB, clientData is %s"%clientData)
12 def endManipCB(node, clientData):
13  print("Inside endManipCB, clientData is %s"%clientData)
14 
15 callbackIDs = []
16 
17 def test():
18  addCallbacksToPerspCamera()
19 
20 def addCallbacksToPerspCamera():
21  global callbackIDs
22  dependNode = OpenMaya.MObject()
23  dagPath = OpenMaya.MDagPath()
24  camFn = OpenMaya.MFnCamera()
25 
26  # For simplicity, select the persp camera's shape
27  #
28  maya.cmds.select('perspShape')
29 
30  # Create a selection list iterator
31  #
32  slist = OpenMaya.MSelectionList()
34  iter = OpenMaya.MItSelectionList(slist)
35 
36  print("List length is %d"%slist.length())
37 
38  # Iterate over all selected dependency nodes
39  #
40  while not iter.isDone():
41  # Get the selected dependency node
42  #
43  iter.getDependNode(dependNode)
44  iter.getDagPath(dagPath)
45  print("Node: %s"%dagPath.partialPathName())
46  if (dependNode.hasFn(OpenMaya.MFn.kCamera)):
47  print("This is a camera, adding manipulation callbacks with payloads")
48  payloadBegin = "12345.5"
49  payloadEnd = "54321.5"
50 
51  # Add the begin manipulation and end manipulation callbacks to the camera shape
52  #
53  callbackIDs.append(
54  OpenMaya.MCameraMessage.addBeginManipulationCallback(dependNode, beginManipCB, payloadBegin)
55  )
56  callbackIDs.append(
57  OpenMaya.MCameraMessage.addEndManipulationCallback(dependNode, endManipCB, payloadEnd)
58  )
59  else:
60  print("This node is not a camera...")
61 
62  next(iter)
63 
64 def removeCallbacks():
65  global callbackIDs
66  for id in callbackIDs:
68  callbackIDs = []
69 
70 """
71 import cameraMessageTest as cmt
72 
73 maya.cmds.file(f=1,new=1)
74 reload(cmt)
75 
76 cmt.test()
77 cmt.removeCallbacks()
78 """