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