Below is an example demonstrating a deep copy of one material to another.
// Define an anonymous function; // serves as our main loop, // limits the scope of variables (function(){ /*********************************************************************/ // Boolean : A function to [deep] copy a material from source to target function deepCopyMaterial( oSrcNode, sSrcMaterial, oTgtNode, sTgtMaterial ){ // If we don't have source and target information if( !oSrcNode || !oTgtNode || sSrcMaterial.isEmpty() || sTgtMaterial.isEmpty() ){ // We're done... return false; } // Get the objects var oSrcObject = oSrcNode.getObject(); var oTgtObject = oTgtNode.getObject(); // If we don't have both objects if( !oSrcObject || !oTgtObject ){ // We're done... return false; } // Get the current shapes var oSrcShape = oSrcObject.getCurrentShape(); var oTgtShape = oTgtObject.getCurrentShape(); // If we don't have both shapes if( !oSrcShape || !oTgtShape ){ // We're done... return false; } // Get the materials var oSrcMaterial = oSrcShape.findMaterial( sSrcMaterial ); var oTgtMaterial = oTgtShape.findMaterial( sTgtMaterial ); // If we didn't find both materials if( !oSrcMaterial || !oTgtMaterial ){ // We're done... return false; } // Create an element duplication context var oContext = new DzElementDuplicateContext(); // Duplicate the material var oDupeMaterial = oSrcMaterial.doDuplicateElement( oContext ); // Set the name of the duplicate to the name of the target oDupeMaterial.name = oTgtMaterial.name; // Set the label of the duplicate to the label of the target oDupeMaterial.setLabel( oTgtMaterial.getLabel() ); // If the duplicate material classname and the source material // classname do not match and the duplicate material inherits // one of the types we know provide a setMaterialName function if( oDupeMaterial.className() != oSrcMaterial.className() && (oDupeMaterial.inherits( "DzShaderMaterial" ) || oDupeMaterial.inherits( "DzBrickMaterial" )) ){ // Copy the material name oDupeMaterial.setMaterialName( oSrcMaterial.getMaterialName() ); } // Re-create any aliases that exist oContext.createAlaises(); // Re-create any ERC links that exist oContext.createERC(); // Resolve references to the source material oContext.doResolve(); // Finalize the duplication oContext.doFinalize(); // Replace the target material with the duplicate of the source material var oError = oTgtShape.replaceMaterial( oTgtMaterial, oDupeMaterial ); // Return whether there was an error return oError.valueOf() == 0x00000000; } /*********************************************************************/ var sMessage; var sOk = qsTr("&Ok"); // If the version of the application is older than 4.6.3.39 if( App.version64 < 0x0004000600030027 ){ // Construct a message var sMessage = qsTr("This script requires a newer version of the application. " + "Update to the current version and try again."); // Inform the user MessageBox.information( sMessage, qsTr("Version Error"), sOk ); // We're done... return; } // Get the selected nodes var aNodes = Scene.getSelectedNodeList(); // If we do not have more than one if( aNodes.length < 1 ){ // Construct a message sMessage = qsTr("This script requires two (2) nodes to be selected. " + "Select two nodes and try again."); // Inform the user MessageBox.information( sMessage, qsTr("Selection Error"), sOk ); // We're done... return; } // Get the first node var oSrcNode = aNodes[ 0 ]; // If the node is a bone if( oSrcNode.inherits( "DzBone" ) ){ // Get the skeleton oSrcNode = oSrcNode.getSkeleton(); } // Get the second node var oTgtNode = aNodes[ 1 ]; // If the node is a bone if( oTgtNode.inherits( "DzBone" ) ){ // Get the skeleton oTgtNode = oTgtNode.getSkeleton(); } // Define the source material names var aSrcMaterials = [ "Face" ]; // Define the target material names var aTgtMaterials = [ "Default" ]; // If the source and target material name lists are not the same length if( aSrcMaterials.length != aTgtMaterials.length ){ // Construct a message sMessage = qsTr("This script requires pairs of material names to be defined. " + "The source and target material name lists are not the same length."); // Inform the user MessageBox.information( sMessage, qsTr("Resource Error"), sOk ); // We're done... return; } // Declare working variables var sSrcMaterial, sTgtMaterial; var bSuccess; // Iterate over the material names for( var i = 0, nNames = aSrcMaterials.length; i < nNames; i += 1 ){ // Get the 'current' source material name sSrcMaterial = aSrcMaterials[ i ]; // Get the 'current' target material name sTgtMaterial = aTgtMaterials[ i ]; // If performing a deep copy, source to target was not successful if( !deepCopyMaterial( oSrcNode, sSrcMaterial, oTgtNode, sTgtMaterial ) ){ // Next!! continue; } // Provide feedback on successful copying print( "Copied::", oSrcNode.getLabel(), ":", sSrcMaterial, ">>", oTgtNode.getLabel(), ":", sTgtMaterial ); } // Finalize the function and invoke })();