A collection is a group of objects. For example, the selection list is stored as a collection.
' Get the selection using the native Softimage commands Set oList = GetValue( "SelectionList" ) ' Get the selection using the object model Set oList = Selection
Whether you get the selection with commands or the object model, the object that is returned in both these examples (oList) is a valid collection, even if it has no members (and the Count property returns 0).
' Print out the number of items in the selection LogMessage oList.Count & " items are selected."
In the code snippet above, oList.Count gives you the number of objects in the selection collection.
Sometimes a collection is empty. For example, you may try to get a collection of all CurveLists in a scene that contains none (maybe on purpose!). In that case, an empty collection is returned.
Empty collections can sometimes cause errors. For example, if you are looking for something that is not there, either the SIFilter command or the Filter method return nothing. As you may suspect, your script will fail if you try to use an invalid object or collection (nothing). For this reason, you need to test the return value before trying to work with it any further. For more information, see What to Watch Out for with Objects and Collections.
Many Softimage commands and objects use collections to store parameters. The following examples iterate over Softimage's selection list and log the name of each selected object.
VBScript offers two ways to iterate over collections. One way uses the For Each statement.
For Each oSel In Selection LogMessage oSel Next
The other way uses the For statement to index the items in the collection.
Set oSelList = Selection For i = 0 To oSelList.Count - 1 LogMessage oSelList(i) Next
JScript offers two ways to iterate over collections. One way uses the For statement to index the items in the collection.
oSelList = Selection; for (iSel = 0; iSel < oSelList.Count; ++iSel) { LogMessage(oSelList(iSel)); }
The other way uses the Enumerator object.
e = new Enumerator(Selection); for (; !e.atEnd(); e.moveNext()) { LogMessage(e.item()); }
Except where otherwise noted, this work is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 3.0 Unported License