Wednesday, 4 March 2009

Traversing Contexts with Get Element ID and Sort Array With Key

In the previous post I looked at how you can create arrays of data in object context and then access that data in an element context. The trick to doing that is the Get Element ID node. Using Get Element ID you can a) sort the array in object context when using Sort Array with Key and b) locate the position of your element in the array when you're in element context. There are, however, a few tricks.

As an example, I will construct an array of all point positions in object context and then sort that array by Index. Using Raf's trick we know that Get Closest Points without any Cutoff distance will give us a list of all the point locations on the geometry (unordered).

It would be great if you could simply pump that array of locations into the Get Element Index node but you can't. XSI returns an array with a single value. Grahame Fuller at Softimage pointed out on the Mailing List that Get Element Index returns the Index associated with the element's context. Since, in this example, the element is in object context it's only going to return a single value.

You need to 'force' Get Element ID into per point context by feeding in a per point attribute (e.g. self.PointPositions) and then store that ID on the point as a custom attribute. Once you've done that you can query that custom attribute back in object context (without using Get Element ID and thus bypassing the problem of contexts with Get Element ID). Here's the tree:

Using Sort Array with key, it's then quite straightforward to pump the index array into the sort and have the Point Positions sorted in Index order. (In the example below the 'extra' sort array is there simply to display the sorted indices).

No comments:

Post a comment