User Tools

Site Tools


Remove Prefixed Data

Summary

Below is an example demonstrating how to walk the contents of a scene, looking for data (in the form of nodes, properties, data items, etc) that bears a particular prefix, and remove it.

This functional sample uses the OctaneRender prefix as an example because it represents an actual use-case where data created by the OctaneRender plugin can persist, and even be spread to new scenes, without a user knowing/realizing it is occurring. For users of the OctaneRender plugin, this may be desirable. For those that do not use the OctaneRender plugin, this extra data unnecessarily increases file size, time to load and/or save, and more generally can impact performance.

API Areas of Interest

Example

Remove_Prefixed_Data.dsa
// DAZ Studio version 4.1.0.71 filetype DAZ Script
// Define an anonymous function;
// serves as our main loop,
// limits the scope of variables
(function(){
 
	/*********************************************************************/
	// Array<DzProperty> : A function for getting a list of the properties in a group
	function getGroupProperties( oGroup, bTraverse, bRecurse )
	{
		// Declare an array to hold properties
		var aProperties = [];
 
		// If a group is not passed in
		if( !oGroup ){
			// We are done, return an empty array
			return aProperties;
		}
 
		// Get the number of proeprties in the group
		var nProperties = oGroup.getNumProperties();
		// Pre-size the properties array
		aProperties = new Array( nProperties );
		// Iterate over the properties, setting each element in the array
		for( var i = 0; i < nProperties; i += 1 ){
			// Assign the property to the position in the array
			aProperties[ i ] = oGroup.getProperty( i );
		}
 
		// If we are recursing
		if( bRecurse ){
			// Concatenate the properties array from child groups
			aProperties = aProperties.concat(
				getGroupProperties( oGroup.getFirstChild(), true, bRecurse ) );
		}
 
		// If we are traversing
		if( bTraverse ){
			// Concatenate the properties array from sibling groups
			aProperties = aProperties.concat(
				getGroupProperties( oGroup.getNextSibling(), bTraverse, bRecurse ) );
		}
 
		// Return the array of properties
		return aProperties;
	};
 
	/*********************************************************************/
	// Array<DzProperty> : A function for getting the list properties for an element
	function getElementProperties( oElement, bTraverse, bRecurse )
	{
		// Get the property group tree for the element
		var oPropertyGroupTree = oElement.getPropertyGroups();
 
		// If the application version is 4.9.4.101 or newer and we want all properties
		if( App.version64 >= 0x0004000900040065 && bTraverse && bRecurse ){
			// Return the properties for the element
			return oPropertyGroupTree.getAllProperties();
		}
 
		// Get the first group in the tree
		var oPropertyGroup = oPropertyGroupTree.getFirstChild();
		// Return the properties for the element
		return getGroupProperties( oPropertyGroup, bTraverse, bRecurse );
	};
 
	/*********************************************************************/
	// void : A function for removing element data items
	function removeElementData( oElement, sPrefix )
	{
		// Declare working variables
		var oDataItem;
		var sDataName;
 
		// Iterate over the data items on the element
		for( var i = 0, n = oElement.getNumDataItems(); i < n; i += 1 ){
			// Get the 'current' data item
			oDataItem = oElement.getDataItem( i );
 
			// Get the name of the data item
			sDataName = oDataItem.name
 
			// If the name starts with the prefix we are looking for
			if( sDataName.startsWith( sPrefix ) ){
				// Provide feedback
				print( "Removing Data Item::", oElement.getLabel(), "::", sDataName );
 
				// If the data item cannot be removed from the element
				if( !oElement.removeDataItem( oDataItem ) ){
					// Provide feedback
					print( "\tFailed::", oElement.getLabel(), "::", sDataName );
				}
			}
		}
	};
 
	/*********************************************************************/
	// Define prefix of data to remove
	var sPrefix = "OctaneRender";
 
	// Declare working variables
	var oNode;
	var sNodeName;
	var sNodeLabel;
	var aProperties;
	var oProperty;
	var oPropOwner;
	var sPropName;
	var sPropLabel;
	var oObject;
	var oShape;
	var aMaterials;
	var oMaterial;
 
	// Get the list of nodes in the scene
	var aNodes = Scene.getNodeList();
	// Iterate over the list of nodes
	for( var i = 0, n = aNodes.length; i < n; i += 1 ){
		// Get the 'current' node
		oNode = aNodes[ i ];
 
		// Get the node name
		sNodeName = oNode.name;
		// Get the node label
		sNodeLabel = oNode.getLabel();
 
		// If the node label starts with the prefix we are looking for,
		// or the node name is a GUID
		if( sNodeLabel.startsWith( sPrefix )
		|| App.isValidGuid( sNodeName ) ){
			// Provide feedback
			print( "Removing Node::", sNodeName, sNodeLabel );
 
			// If the node cannot be removed from the scene
			if( !Scene.removeNode( oNode ) ){
				// Provide feedback
				print( "\tFailed::", sNodeName, sNodeLabel );
			}
 
			// Next!!
			continue;
		}
 
		// If the node is not the kind we remove entirely...
 
		// Remove any data items with the prefix from the node
		removeElementData( oNode, sPrefix );
 
		// Get the object for the node
		oObject = oNode.getObject();
 
		// Get the properties presented to the user in the context of the node
		aProperties = getElementProperties( oNode, true, true );
		// Iterate over the list of properties
		for( var j = 0, m = aProperties.length; j < m; j += 1 ){
			// Get the 'current' property
			oProperty = aProperties[ j ];
 
			// Get the property name
			sPropName = oProperty.name;
			// Get the property label
			sPropLabel = oProperty.getLabel();
 
			// If the property name or label starts with the prefix we are looking for
			if( sPropName.startsWith( sPrefix )
			|| sPropLabel.startsWith( sPrefix ) ){
				// Provide feedback
				print( "Removing Property::", sNodeLabel, "::", sPropLabel );
 
				// Get the owner of the property
				oPropOwner = oProperty.getOwner();
 
				// If the property can be removed directly
				if( oProperty.isUserProperty() ){
					// If the property cannot be removed
					if( !oPropOwner.removeProperty( oProperty ) ){
						// Provide feedback
						print( "\tFailed::", sNodeLabel, "::", sPropLabel );
					}
				// If the property is owned by a modifier
				} else if( oPropOwner.inherits( "DzModifier" ) ){
					// If the modifier that owns the property cannot be removed
					if( !oObject.removeModifier( oPropOwner ) ){
						// Provide feedback
						print( "\tFailed::", sNodeLabel, "::", sPropLabel );
					}
				// Otherwise
				} else {
					// Provide feedback
					print( "\tFailed::", sNodeLabel, "::", sPropLabel );
				}
			}
		}
 
		// If we do not have an object
		if( !oObject ){
			// Next!!
			continue;
		}
 
		// Iterate over the shapes of the node
		for( var j = 0, m = oObject.getNumShapes(); j < m; j += 1 ){
			// Get the 'current' shape
			oShape = oObject.getShape( j );
 
			// Get the list of materials on the shape
			aMaterials = oShape.getAllMaterials();
			// Iterate over the materials on the shape
			for( var k = 0, p = aMaterials.length; k < p; k += 1 ){
				// Get the 'current' material
				oMaterial = aMaterials[ k ];
 
				// Remove any data items with the prefix from the material
				removeElementData( oMaterial, sPrefix );
			}
		}
	}
 
// Finalize the function and invoke
})();