/*
This example illustrates how to detect all parameters that support instance values and determine
which parameters are currently in use based on the usage of the Material.Parameters with
instances values include: ImageShader.tspace_id, Material.ImageClipName, Material.UV, Material.CAV.
*/
NewScene( null, false );
// Create an object using the scene root and with a color at vertices (CAV or VertexColor) property
var model = Application.ActiveSceneRoot.AddModel();
model.Name = "MyModel";
model.AddMaterial( "Phong", true, "MyModelMaterial" );
var cube = model.AddGeometry( "Cube", "MeshSurface", "MyCube" );
var cav = cube.ActivePrimitive.Geometry.AddVertexColor( "MyVertexColor" );
var matCube = cube.Material;
// You can set a InstanceValue directly on a paremeter using the Parameter.Value property if
// you accessed the parameter via its object and not via the material library or some other
// object using the material.
matCube.Parameters("CAV").Value = cav.Name;
var matlib = ActiveProject.ActiveScene.ActiveMaterialLibrary;
Application.LogMessage( "Number of materials in library: " + matlib.Items.Count );
for ( var i=0; i<matlib.Items.Count; i++ ) {
var mat = matlib.Items(i);
if ( mat.UsedBy.Count == 0 ) {
Application.LogMessage( mat.FullName + " is unused" );
continue;
}
Application.LogMessage( mat.FullName + " used by "+ mat.UsedBy.GetAsText() );
for ( var j=0; j<mat.Parameters.Count; j++ ) {
var param = mat.Parameters(j);
if ( param.HasInstanceValue ) {
var eObjs = new Enumerator( mat.UsedBy );
// Dump instance values for each object
for ( ; !eObjs.atEnd(); eObjs.moveNext() ) {
var obj = eObjs.item();
var instancevalue = param.GetInstanceValue( obj );
Application.LogMessage( obj.Name + " " + param.ScriptName + " = \"" + instancevalue + "\"");
}
}
}
}
// Expected results:
//INFO : Number of materials in library 2
//INFO : Sources.Materials.DefaultLib.Scene_Material is unused
//INFO : Sources.Materials.DefaultLib.Material used by MyModel.MyCube
//INFO : MyCube CAV = "MyVertexColor"
//INFO : MyCube UV = "" |