Working with the Scene Graph
The scene graph is a tree of scene nodes. Each node is a hub of information for objects in the scene, and is responsible for:
- applying modifiers to an object
- storing information about the geometry
- storing information about shading materials
- storing position, rotation, and scale information
- storing node tree hierarchical information: the parent and child of the node
Traversing the Scene Graph
The root node of a scene can be accessed by the MAXScript rootNode
global. See, for example, the scene_graph.py sample script:
'''
Creates a simple text representation of the scene graph
'''
from pymxs import runtime as rt
def output_node(node, indent=''):
"""Print the scene graph as text to stdout."""
print(indent, node.Name)
for child in node.Children:
output_node(child, indent + '--')
output_node(rt.rootnode)
Getting Scene Objects
If you do not need the scene hierarchy information (that is, parent/child relationships), you can get a flat list of all objects in the scene with the objects
global variable. For example:
objs = pymxs.runtime.objects
for o in objs:
print(o.name)
You can also obtain individual objects by name:
s = pymxs.runtime.getNodeByName('Sphere002')
Finally, you can also access objects by PathName values, though this is not directly exposed to pymxs and requires a work-around. See Accessing MAXScript PathName Values for more information.
Getting the Selection List
There are times when it is useful to get the list of selected objects in a scene.
There are several reserved system variables of type ObjectSet
that contain various scene object categories. For example, objects
contains all the objects in the scene, lights
all the lights, and selection contains the current selection
.
For example:
>>> pymxs.runtime.selection
<ObjectSet<$selection>>
>>> pymxs.runtime.selection[0]
<Sphere<$Sphere:Sphere004 @ [90.515228,15.940669,0.000000]>>
>>>
Named selection sets are contained in the selectionSets
array, and are accessed by index.
for example:
>>> pymxs.runtime.selectionSets[0]
<SelectionSet<SelectionSet:my_selection>>