I always cringe when I see the SafeArray suggestion as it doesn't tell the 
whole story.

I haven't tried this for ICE attributes, but for all other things JScript 
requiring the use of an array, you can use the GridData object from the 
Softimage SDK.  It's a lot more convenient than dabbling with the esoteric 
SafeArray object and is fully supported by the Softimage SDK avoiding the 
workarounds for certain commands and methods.  The GridData object is smart 
enough to automatically resize itself when ingesting data from a known 
source (for multi-dimensional arrays):

var oObject    = Selection(0);
var oGridData  = XSIFactory.CreateGridData();
oGridData.Data = oObject.ActivePrimitive.Geometry.Vertices.PositionArray;

LogMessage( "Grid[rows,cols]: " + oGridData.RowCount + ", " + 
oGridData.ColumnCount, siComment );


The Grid doesn’t auto-resize for all array types, but it supports most you’ll 
encounter such as cluster data (envelopes, materials, user normals, vertex 
colors, texture UVWs, ...).  to write the data to another property, just 
assign the .Data property in reverse of what is shown above.

oCluster.Elements.Array = oGridData.Data;

Anyway, writing the data to the ICE Attribute does work, but as you found 
out you must freeze the modeling history of the ICE Attribute property 
because technically (I think) it’s an operator.  That implies it has an 
internal update callback which will get triggered after any attempt you make 
to modify the data.  You make a change, then the update callback is 
triggered recomputing and applying the original data you overwrote. 
Freezing the operator converts it to static data which you can then modify 
with script.

As for purely JScript issues with arrays, you only need to convert to a 
safeArray when you need to modify array data and send it back into 
Softimage.  If you’re just reading data, or modifying it locally with no 
intention of sending it back where it came from, you don’t have to convert. 
that’ll save overhead in the runtime of your code.

Another shortcut is the use of the .Array() method to convert the data to 
safeArray to make it more concise and easier to read in code.  For example:

// Get envelope weights and convert from safeArray to JScript array in the 
process
var aEnvelopeWeights = ( oEnvelope.weights.Array ).toArray();

// Get vertex colors on mesh located nearest specified set of position 
coordinates
var oPointLocators = oPolygonMesh.GetClosestLocations( aPositions );
aVertexColors = oPolygonMesh.EvaluateClusterProperty( oPointLocators, 
oParentCluster, oClusterProperty ).toArray();

Just enclose the quantity to be converted in parentheses, then append 
.Array() to the right parenthesis.  the only caveat is to make sure the 
quantity to be converted is not null or else it’ll throw an error.

Matt





Date: Tue, 13 Dec 2016 20:34:02 -0200
From: Fabricio Chamon <xsiml...@gmail.com>
Subject: Setting ICE attribute DataArray in javascript
To: "softimage@listproc.autodesk.com"
<softimage@listproc.autodesk.com>

Hey,

lots of questions these days, sorry.

So I'm searching all day through the list and internet to find a solution
for this... I'm trying to set per polygon ice attribute values on a mesh
via sdk (javascript).
The subject was already discussed, Stephen Blair have some studies on it,
one solution was given but in the end I'm still not able to get it working.

So here's roughly my code:

var ICEAttr = highResGeo.AddICEAttribute("rbdId", siICENodeDataLong,
siICENodeStructureSingle, siICENodeContextComponent2D );
var myDataArray = new Array();
var faces = highResGeo.Facets;
for (i=0;i<faces.count;i++){
var face = faces(i);
myDataArray[i] = parseInt(x); //does not matter, assume any value for x
}

//now here's the tricky part

da1 = getSafeArray(myDataArray);
da2 = getSafeArray([myDataArray]);
ICEAttr.DataArray = da2;


//helper function
function getSafeArray(jsArr) {
    var dict = new ActiveXObject("Scripting.Dictionary");
    for (var i = 0; i < jsArr.length; i++)
    dict.add(i, jsArr[i]);
    return dict.Items();
}




This SafeArray stuff is the solution proposed by Stephen on his blog...but
gives me :

*WARNING : 3392 - Invalid offset specified while extracting data from this
attribute: <Attribute: rbdId>*
*// <Offset: -1>*

Any hints on what I'm doing wrong ??

Thanks! 


------
Softimage Mailing List.
To unsubscribe, send a mail to softimage-requ...@listproc.autodesk.com with 
"unsubscribe" in the subject, and reply to confirm.

Reply via email to