Using Collections

 
 
 

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.

Important

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.

To get information about a collection using VBScript

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

To get information about a collection using JScript

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

To get information about a collection using PerlScript

PerlScript offers two ways to iterate over collections. One way uses the For Each statement.

$oSelList = $Application->Selection;
foreach $oSel (in $oSelList) {
   $Application->LogMessage($oSel->FullName);
}

The other way uses the For statement to index the items in the collection.

$oSelList = $Application->Selection;
$iCount = $oSelList->Count;
for ($iSel = 0; $iSel < $iCount; ++$iSel) {
   $Application->LogMessage(
      $oSelList->Item($iSel)->FullName);
}

To get information about a collection using Python

Python supports iteration over a collection using the For statement to index the items in the collection.

oSelList = Application.Selection
for iSel in range(oSelList.Count):
   Application.LogMessage(oSelList.Item(iSel).FullName)