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 );
                }
        }

}

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s