demoAppChunk.py

demoAppChunk.py
1 '''
2  Demonstrates how to manage user specified data for any object derived from Animatable.
3 '''
4 import MaxPlus
5 import pymxs
6 
7 
8 def descendants(node):
9  for c in node.Children:
10  yield c
11  for d in descendants(c):
12  yield d
13 
14 
15 def allNodes():
16  return descendants(MaxPlus.Core.GetRootNode())
17 
18 
19 def CreatSceneWithAppChunk():
21 
22  # Create a teapot, a scene node and a material instance, they are all
23  # objects of Animatable
24  teapot = MaxPlus.Factory.CreateGeomObject(MaxPlus.ClassIds.Teapot)
25  node = MaxPlus.Factory.CreateNode(teapot)
27  node.Material = mtl
28  node.SetName("MyTeapot123")
29 
30  # Now add some user specified strings to these objects
31  teapot.SetAppData(1234, "I'm a teapot!")
32  teapot.SetAppData(2345, u"我是一个茶壶!")
33 
34  node.SetAppData(5678, "Node of teapot")
35  node.SetAppData(7890, "This is to be removed")
36  node.DeleteAppData(7890)
37 
38  mtl.SetAppData(4567, "Material of teapot")
39  pymxs.runtime.saveMaxFile("scene_with_app_chunk.max")
40  print "scene with AppChunk is saved."
41 
42 
43 def LoadAndCheckSceneWithAppChunk():
45  pymxs.runtime.loadMaxFile("scene_with_app_chunk.max")
46  print "scene with AppChunk is loaded."
47  # Find the "MyTeapot123" node
48  nodes = allNodes()
49  teapotNode = None
50  for n in nodes:
51  if n.GetName() == "MyTeapot123":
52  teapotNode = n
53  break
54  if teapotNode is None:
55  print "Error: Incorrect saved scene."
56  else:
57  print teapotNode.GetAppData(5678)
58  obj = teapotNode.GetObject()
59  print obj.GetAppData(1234)
60  print obj.GetAppData(2345)
61  obj.ClearAllAppData()
62 
63  try:
64  obj.GetAppData(9432)
65  except Exception, e:
66  print e
67  print "this is expected."
68  pass
69 
70  try:
71  teapotNode.GetAppData(7890)
72  except Exception, e:
73  print e
74  print "this is expected."
75  pass
76 
77  print teapotNode.Material.GetAppData(4567)
78 
79 CreatSceneWithAppChunk()
80 LoadAndCheckSceneWithAppChunk()