Working out what materials are used on a canvas can be useful for many things. For example, Quick saving all active materials. That would be useful for storing multiple material settings for a single document instead of saving multiple versions of a 40GB print sized Zbrush document.
<b>Practical explanation:</b>
In the Material palette there is a button called <b>Show Used</b>[color=DimGray]. </span>When you press that button all materials that appear on the canvas are displayed in the Material palette, the rest are hidden from view. It is then a simple matter of checking if each material exists on the interface. The IExists command can take care of that.
Of course there has to be an exception: Whether or not the currently selected material exists on the canvas it is kept in view when Show Used is pressed. So it is neccesary to select the Flat Color material before pressing Show Used. The Flat Color material cannot be replaced or modified so it will exist in all Zbrush documents, and as such it does not need to be saved.
In Zbrush 2 there are 76 materials slots. There can be neither more nor less than 76 slots.
<b>The Source Code:</b>
[[color=PaleGreen]Vardef, MaterialsUsed([color=Blue]75), 0 ] // If material is active 1, otherwise 0.
[Ifreeze, // Hides the following commands from the user. [Iset, Material:Item Info, 0 ] // Select the Flat Color material [Ipress, Material:Show Used] // Hides all unused materials from the UI. [Loop, 75 , // There are 76 materials but we are not interested in material 0, the Flat Color material. [If, [IExists, [Val,10244 + i ] ], // If material is visible in the UI then... [Varset, MaterialsUsed([color=Blue] i )</span>, 1 ] // ...set to 1 for used materials ] // End of If statement , i ] // End of loop ] // End of Ifreeze</span>
As you can see above the [color=PaleGreen]IExists </span>command does not reference the UI interface item by its interface button path, that would be tedious for 75 materials with different names. Luckily each Interface Item has a specific Window ID. The Window ID for the Material slots runs from [color=Wheat]10243</span> to [color=Wheat]10318</span>. You can find the Window ID of any interface item by hovering your cursor over the interface item and examining <b>Preferences : Utilities : View Window ID</b>.
Note, it is necessary to init the [color=SandyBrown]MaterialsUsed</span> variable array to 0 before checking for active materials a second time.
Happy Zscripting
Mark