Switching material libraries


Here’s how to switch from one material library to another. This script could be useful when you have a corrupted material library (eg missing render tree connections), but you have a backup copy of the material library. For example, you could export a good version of the material library from a backup copy of the scene, and then import the matlib into the current version of the scene.


// Material Library with bad render trees (missing connections)
var oMatLib = Dictionary.GetObject( "Sources.Materials.DefaultLib" );

// Material Library with good render trees
var oMatLib1 = Dictionary.GetObject( "Sources.Materials.DefaultLib1" );


oEnum = new Enumerator( oMatLib.Items ) ;
for (;!oEnum.atEnd();oEnum.moveNext() )
{
        var oMat = oEnum.item() ;
        if ( oMat.UsedBy.Count > 0 )
        {
                var oMat1 = oMatLib1.Items(oMat.Name);
                if ( oMat1 == null )
                {
                        LogMessage( "Cannot find " + oMat.Name + " in " + oMatLib1.Name );
                }
                else
                {
                        LogMessage( "Repacing " + oMat.FullName + " with " + oMat1.FullName );
                        
                        // Groups
                        var g = oMat.Owners.Filter( "#Group" );
                        SIAssignMaterial( g.GetAsText(), oMat1 );

                        // Objects
                        SIAssignMaterial( oMat.UsedBy.GetAsText(), oMat1 );
                }
        }

}