In Clara.io, an object is represented as a node, such as scene, mesh, material and so on. To operate an object using ctx scripting, we need to grab the object from the scene graph first using Selector and then apply the related operations.
To perform a selection, we need to use:
var myNodes = ctx(selector);
Here myNodes is an array of nodes that meet the selection criteria, wrapped in a ctx JavaScript object. To select a specific node, we must use the at function. For example, to get the 10th element of the array myNodes we employ:
var myNode = myNodes.at(9)
Selection can be done in several ways. Below we review each of them:
This gets all objects that have been selected by the user in the viewport. The :selection
rule allows a script to select the same nodes in the ctx as those in the viewport.
This can be used for instance to write a custom exporter that only exports those objects that were selected manually by the user on the viewport.
ctx(':selection');
Selects all viewport selected objects.
This is used to select all objects of a particular type, in other words, all objects that belong to a given primitive. This selection is case insensitive. Valid types are the following:
%Scene
- This is a common selection, and will retrieve the root node of the scene graph.
%Objects
- The virtual node containing all objects.
%MaterialLibrary
- The virtual node containing all materials.
%Materials
- Select all materials.
%Renderers
- The virtual node containing all renderers.
%Passes
- The virtual node containing all passes.
%PolyMesh
- Selects all polymesh (geometry) primitives.
%Annotation
- Selects all annotation nodes.
%Light
- Selects all lights.
%Camera
- Selects all cameras.
%Image
- Selects all images.
%Null
- Selects all null objects.
%Bone
- Selects all bone objects.
ctx('%PolyMesh');
Matches any PolyMesh typed objects
ctx('%Objects');
Matches the ‘Objects’ virtual node. For example, in a scene with polygonal objects and lights, the ‘Objects’ node will have all of them as children nodes.
We can select nodes by their name. Since names are not required to be unique in Clara.io, this may retrieve multiple nodes.
ctx('Sphere');
Matches any nodes named Sphere.
Node name selections can use Javascript regular expression (regex) pattern matching to select any node names following a pattern. Currently square brackets are not permitted. Also any escaped characters must be doubly escaped. For more information on regex patterns, visit the W3Schools Reference.
ctx('Sphere.'');
Matches “SphereA” and “Sphere1” but not “Sphere”
ctx('Sphere1?');
Matches “Sphere” and “Sphere1”
ctx('Sphere*');
Matches “Sphere”, “Sphere1”, “SphereA”, “Sphere11”, etc.
ctx('(AB){1,2}');
Matches “AB” and “ABAB” but not “ABABAB”
This selects all plugs of the given type. A plug changes the appearance and behavior of a node and it has associated one primitive. This selection is case sensitive.
Valid plugs are:
#Transform
- Selects all transform plugs.
#Properties
- Selects all properties plugs.
#Material
- Selects all material plugs.
#Texture
- Selects all texture plugs.
ctx('#Transform');
Matches all Transform plugs.
ctx('MySphere#Transform');
Matches all plug nodes corresponding to nodes named MySphere.
This gets operators within a given plug. The operator property must be given as a camelCase name of the label.
Since operators are returned, we can easily extract information from the operators.
To get an attribute value from an operator in a plug, we use:
ctx(#PlugType[Property]).at(n).get('Property');
Here we use ctx to select nodes that match the PlugType and Property, and we use the get function to obtain the actual property value.
ctx('#PolyMesh[depth]');
Matches the depth property of the Box operator within all PolyMesh plugs.
ctx(Mybox#Transform[translation]).at(0).get('translation');
Matches the translation property of the transform node of nodes called ‘MyBox’ and returns the translation of the first match.
This selects operators within a given plug if its the value of one of its attributes or schema objects equals the given value.
ctx('#PolyMesh[name=Box]');
Matches all operators with the name Box within all Polymesh plugs.
As we have seen above, the operator selection depends on having a plug. Other selections can be optionally combined, such as node name and type.
ctx('DLight.*%Light#Light[name=DirectionalLight]');
Matches all operators with the name ‘DirectionalLight’ that belong to a Light plug, that are on a node of type Light, with a node name matching DLight followed by any number of extra characters.