Panda3D Manual: Searching the Scene Graph
<<prev | top | next>> |
It is often useful to get a handle to a particular node deep within the scene graph, especially to get a sub-part of a model that was loaded from a single file. There are a number of methods dedicated to finding entrenched nodes and returning the NodePaths. First, and most useful, is the ls() command:
nodePath.ls()
This simply lists all of the children of the indicated NodePath, along with all of their children, and so on until the entire subgraph is printed out. It also lists the transforms and Render Attributes that are on each node. This is an especially useful command for when you're running interactively with Python; it's a good way to check that the scene graph is what you think it should be. The two methods `find()` and `findAllMatches()` will return will return a `NodePath` and a `NodePathCollection` respectively. These methods require a path string as an argument. Searches can based on name or type. In its simplest form this path consists of a series of node names separated by slashes, like a directory pathname. When creating the string each component may optionally consist of one of the following special names, instead of a node name.
* | Matches exactly one node of any name
** | Matches any sequence of zero or more nodes
+typename | Matches any node that is or derives from the given type
-typename | Matches any node that is the given type exactly
=tag | Matches any node that has the indicated tag
=tag=value | Matches any node whose tag matches the indicated value
Standard filename globbing characters, such as *, ?, and [a-z] are also usable. Also the @@ special character before a node name indicates that this particular node is a stashed node. Normally, stashed nodes are not returned. @@*, by extension, means any stashed node. The argument may also be followed with control flags. To use a control flag, add a semicolon after the argument, followed by at least one of the special flags with no extra spaces or punctuation.
-h | Do not return hidden nodes
+h | Return hidden nodes
-s | Do not return stashed nodes unless explicitly referenced with @@
+s | Return stashed nodes even without any explicit @@ characters
-i | Node name comparisons are not case insensitive: case must match exactly
+i | Node name comparisons are case insensitive: case is not important. This affects matches against the node name only; node type and tag strings are always case sensitive
The default flags are +h-s-i. The `find()` method searches for a single node that matches the path string given. If there are multiple matches, the method returns the shortest match. If it finds no match, it will return an empty NodePath. On the other hand, `findAllMatches()` will return all NodePaths found, shortest first.
nodePath.find("<Path>") nodePath.findAllMatches("<Path>")
Some examples:
nodePath.find("house/door")
This will look for a node named "door", which is a child of a node named "house", which is a child of the starting path.
nodePath.find("**/red*")
This will look for any node anywhere in the tree (below the starting path) with a name that begins with "red". In addition there are also the methods `getParent()` and `getChildren()`. `getParent()` returns the NodePath of the parent node. `getChildren()` returns the children of the current node as a NodePathCollection( use `getChildrenAsList()` if you want them as a List). some examples:
#if you wanted to search up the Scene Graph until you found a certain node while nodePath.getParent()!=someAncestor: nodePath=nodePath.getParent() nodePath=nodePath.getParent()
For more information and a complete list of NodePath functions please see the API reference.
<<prev | top | next>> |