User Tools

Site Tools


Select by Tag

Summary

Below is an example demonstrating how to select materials that have a particular tag, via script. This example also demonstrates how to gain access to static methods on DzMaterial for iterating over all materials in a scene, and how to use element parent information to get the node(s) that a material or shape is associated with.

API Areas of Interest

Example

Material_Select_by_Tag.dsa
// Define an anonymous function;
// serves as our main loop,
// limits the scope of variables
(function(){
 
	/*********************************************************************/
	// Boolean : A function for testing whether or not a QObject instance
	// inherits one of a list of types
	function inheritsType( oObject, aTypeNames )
	{
		// If the object does not define the 'inherits' function
		if( !oObject || typeof( oObject.inherits ) != "function" ){
			// We are done... it is not a QObject
			return false;
		}
 
		// Iterate over the list of type names
		for( var i = 0, nTypes = aTypeNames.length; i < nTypes; i += 1 ){
			// If the object does not inherit the 'current' type
			if( !oObject.inherits( aTypeNames[i] ) ){
				// Next!!
				continue;
			}
 
			// Return the result
			return true;
		}
 
		// Return the result
		return false;
	};
 
	/*********************************************************************/
	// DzMaterial : A function for getting a material to use for statics
	function getMaterialForStatic()
	{
		// Initialize the result
		var oMaterial = undefined;
 
		// Declare working variables
		var oNode, oObject, oShape;
 
		// Get the list of nodes from the scene
		var aNodes = Scene.getNodeList();	
		// Iterate over the nodes
		for( var i = 0; i < aNodes.length; i += 1 ){
			// Get the 'current' node
			oNode = aNodes[ i ];
			// Get the object
			oObject = oNode.getObject();
			// If we do not have an object
			if( !oObject ){
				// Next!!
				continue;
			}
 
			// Get the current shape
			oShape = oObject.getCurrentShape();
			// If we do not have a shape
			if( !oShape ){
				// Next!!
				continue;
			}
 
			// Get the first material
			oMaterial = oShape.getMaterial( 0 );
			// If we have a material
			if( oMaterial ){
				// We have enough to use the static methods
				break;
			}
		}
 
		// Return the result
		return oMaterial;
	};
 
	/*********************************************************************/
	// DzNode : A function for getting a node from an element child
	function getNodeFromElement( oElement )
	{
		// Get the element parent of the element
		var oParent = oElement.getElementParent();
		// While there are more element parents
		while( oParent ){
			// If the parent is a node
			if( inheritsType( oParent, ["DzNode"] ) ){
				// We are done...
				return oParent;
			}
 
			// Get the element parent of the parent
			oParent = oParent.getElementParent();
		}
 
		return undefined;
	};
 
	/*********************************************************************/
	// Array<DzNode> : A function for getting nodes from a material
	function getNodesFromMaterial( oMaterial )
	{
		// Initialize the result
		var aNodes = [];
 
		// Get the shapes this material is on
		var aShapes = oMaterial.getShapeList();
		// Iterate over the shapes
		for( var i = 0; i < aShapes.length; i += 1 ){
			// Get the 'current' shape
			oShape = aShapes[ i ];
			// Get the node from the shape
			oNode = getNodeFromElement( oShape );
			// If we do not have a node
			if( !oNode ){
				// Next!!
				continue;
			}
 
			// Append the node
			aNodes.push( oNode );
		}
 
		// Return the result
		return aNodes;
	};
 
	/*********************************************************************/
	// DzNode : A function for selecting materials with a given tag
	function selectTaggedMaterials( sTag, bEntireScene, bSyncNodes, bSyncMaterials )
	{
		// Get a material in the scene so we can use the static methods
		var oStaticMaterial = getMaterialForStatic();
 
		// If we do not have a material
		if( !oStaticMaterial ){
			// We have nothing else to do...
			return;
		}
 
		// Declare working variables
		var oNode, oShape, oMaterial;
		var aTags, aShapes;
 
		// Initialize
		var aNodes = [];
 
		// Get the number of materials
		var nMaterials = oStaticMaterial.getNumMaterials();
		// Iterate over the materials
		for( var i = 0; i < nMaterials; i += 1 ){
			// Get the 'current' material
			oMaterial = oStaticMaterial.getMaterial( i );
 
			// If we do not have a material
			if( !oMaterial ){
				// Next!!
				continue;
			}
 
			// Get the tags on the material
			aTags = oMaterial.getTags();
 
			// If the one we want is not in the list
			if( aTags.indexOf( sTag ) < 0 ){
				// If we are sync'ing material selection
				if( bSyncMaterials ){
					// Deselect the material
					oMaterial.select( false );	
				}
 
				// Next!!
				continue;
			}
 
			// If we are considering the entire scene
			if( bEntireScene ){
				// Select the material
				oMaterial.select( true );
 
				// If we are sync'ing node selection
				if( bSyncNodes ){
					// Collect the nodes from the material
					aNodes = aNodes.concat( getNodesFromMaterial( oMaterial ) );
				}
			// If we are using node selection
			} else {
				// Get the shapes this material is on
				aShapes = oMaterial.getShapeList();
				// Iterate over the shapes
				for( var j = 0; j < aShapes.length; j += 1 ){
					// Get the 'current' shape
					oShape = aShapes[ j ];
					// Get the node from the shape
					oNode = getNodeFromElement( oShape );
					// If we do not have a node or it is not selected
					if( !oNode || !oNode.isSelected() ){
						// Next!!
						continue;
					}
 
					// Select the material
					oMaterial.select( true );
 
					// If we are sync'ing node selection
					if( bSyncNodes ){
						// Collect the nodes from the material
						aNodes = aNodes.concat( getNodesFromMaterial( oMaterial ) );
					}
 
					// We are done with this material
					break;
				}
			}
		}
 
		// If we are sync'ing node selection
		if( bSyncNodes ){
			// Deselect all nodes
			Scene.selectAllNodes( false );
			// Iterate over the nodes
			for( var i = 0; i < aNodes.length; i += 1 ){
				// Get the 'current' node
				oNode = aNodes[ i ];
				// Select it
				oNode.select( true );
			}
		}
	};
 
	/*********************************************************************/
	// Select only objects with materials that have the "test" tag
	selectTaggedMaterials( "test", true, true, true );
 
// Finalize the function and invoke
})();