/*
C# version of the Envelope.Weights examples,
demonstrating how to deform a sphere
and access the weights on each vertex
*/
CXSIApplicationClass app = new CXSIApplicationClass();
// Set up some null objects to be used for passing default values
object dummy = null;
Object[] args = new Object[2];
args[1] = false;
app.ExecuteCommand("NewScene", args);
Model oRoot = app.ActiveSceneRoot;
Object[] aRootPos = new Object[3] { 0.254, 3.998, 0.045 };
Object[] aEffPos1 = new Object[3] { -0.01, -0.056, -2.972 };
Object[] aEffPos2 = new Object[3] { 0.183, -3.999, 0.115 };
// Create the Skeleton
ChainRoot oChain1 = oRoot.Add2DChain(aRootPos, aEffPos1, dummy, si2DChainAlignType.si2DChainTop, "");
ChainBone oChain2 = oChain1.AddBone(aEffPos2, siChainBoneType.siChainBonePin, "");
// Create the Envelope
X3DObject oSphere = oRoot.AddGeometry("Sphere", "MeshSurface", "");
oSphere.Parameters["subdivu"].PutValue2(dummy, 2);
oSphere.Parameters["subdivv"].PutValue2(dummy, 2);
Geometry oGeometry = oSphere.ActivePrimitive.GetGeometry2(dummy, siConstructionMode.siConstructionModeDefault);
// Apply the Envelope to the Skeleton
Envelope oEnvelope = oSphere.ApplyEnvelope(oChain1, siBranchFlag.siBranch, siBranchFlag.siBranch);
// Weights are represented as a 2-dimensional array, which are supported
// by the Array class in C#, just cast the returned object into an Array
// and use the Array class's access members (same idea as in VBScript)
ClusterElementCollection oWeights = oEnvelope.GetWeights2(dummy);
Array aWeightValues = (Array)oWeights.Array;
for (int iElement = aWeightValues.GetLowerBound(1); iElement < aWeightValues.GetLength(1); iElement++)
{
string strElementWeights = "";
for (int iDeformer = aWeightValues.GetLowerBound(0); iDeformer < aWeightValues.GetLength(0); iDeformer++)
{
double dCurrWeight = (double)aWeightValues.GetValue(iDeformer, iElement);
double dRndWeight = Math.Round(dCurrWeight);
strElementWeights = strElementWeights + dRndWeight.ToString() + ",";
}
// Trip the final comma from the string
strElementWeights.Remove(strElementWeights.Length-1, 1);
app.LogMessage("weight (" + iElement.ToString() + ") = " + strElementWeights, siSeverity.siInfo);
}
// Output of the script is this:
//INFO : "weight(0) = 0,0,100,0"
//INFO : "weight(1) = 0,100,0,0"
//INFO : "weight(2) = 0,50,50,0"
//INFO : "weight(3) = 0,50,50,0"
//INFO : "weight(4) = 0,50,50,0"
|