demoNotifications.py

demoNotifications.py
1 '''
2  Lists all of the notification codes broadcast by 3ds Max,
3  and registers a callback function for each and every one.
4 '''
5 import MaxPlus
6 import os
7 import sys
8 
9 codelookup = {}
10 
11 
12 def listCodes():
13  count = 0
14  for name in dir(MaxPlus.NotificationCodes):
15  if not name.startswith('_'):
16  val = getattr(MaxPlus.NotificationCodes, name)
17  # we want to avoid registering for
18  # define NOTIFY_INTERNAL_USE_START 0x70000000
19  if ((type(val) == int) and (val <= 0xFFFF)):
20  print "Notification code ", name, " = ", val
21  codelookup[val] = name
22  count += 1
23  print "Number Notifications registered: ", count
24 
25 
26 def handleNotification(code):
27  print "Notification handled: ", codelookup[code]
28 
29 try:
30  listCodes()
31 
32  for code in codelookup.iterkeys():
33  MaxPlus.NotificationManager.Register(code, handleNotification)
34 
35  # Do some things
36  print "Creating sphere"
37  sphere1 = MaxPlus.Factory.CreateGeomObject(MaxPlus.ClassIds.Sphere)
38  print "Setting radius for sphere 1"
39  sphere1.ParameterBlock.Radius.Value = 2.0
40  print "Creating node for the sphere"
41  sphere1node = MaxPlus.Factory.CreateNode(sphere1)
42  print "Creating sphere 2"
43  sphere2 = MaxPlus.Factory.CreateGeomObject(MaxPlus.ClassIds.Sphere)
44  print "Setting radius on sphere 2"
45  sphere2.ParameterBlock.Radius.Value = 2.0
46  print "Creating node for sphere 2"
47  sphere2node = MaxPlus.Factory.CreateNode(sphere2)
48  print "Setting parent of node 2 to node 1"
49  sphere2node.Parent = sphere1node
50 
51  print "Saving file"
52  tmpfile = os.path.join(MaxPlus.PathManager.GetTempDir(), "temp.max")
54  print "Deleting node 1"
55  sphere1node.Delete()
56  print "Deleting node 2"
57  sphere2node.Delete()
58  print "Opening file"
60 
61 except Exception, err:
62  print 'ERROR: %s\n' % str(err)
63 
64 except MaxBaseException as e:
65  print 'MaxBaseException occured'
66 
67 except:
68  print "Unexpected error:", sys.exc_info()[0]
69 
70 finally:
71  print 'unregistering notification handlers'
72  for h in list(MaxPlus.NotificationManager.Handlers):
73  MaxPlus.NotificationManager.Unregister(h)