def _getBBoxFromPoly(sel):
    vertexColl = sel.Subcomponent.ComponentCollection.NeighborVertices()
    xPosArr, yPosArr, zPosArr = list(vertexColl.PositionArray)
    xArr = []; yArr = []; zArr = []
    xFormMtx = sel.Subcomponent.Parent3DObject.Kinematics.Global.Transform.Matrix4
 
    for i in range(len(xPosArr)):
        posVec = XSIMath.CreateVector3(xPosArr[i], yPosArr[i], zPosArr[i])
        posVec.MulByMatrix4InPlace(xFormMtx)
        xArr.append(posVec.X)
        yArr.append(posVec.Y)
        zArr.append(posVec.Z)
      
    xMin = min(xArr)
    xMax = max(xArr)
    yMin = min(yArr)
    yMax = max(yArr)
    zMin = min(zArr)
    zMax = max(zArr)

    return xMin, yMin, zMin, xMax, yMax, zMax


    xMin, yMin, zMin, xMax, yMax, zMax = _getBBoxFromPoly(sel)

    sclX = xMax - xMin
    sclY = yMax - yMin
    sclZ = zMax - zMin
   
    ctrX = xMin + (sclX / 2)
    ctrY = yMin + (sclY / 2)
    ctrZ = zMin + (sclZ / 2)

On 09/07/2012 4:59 PM, Vincent Ullmann wrote:
For the Center i did a little Script some time ago.... 
Sadly it only works when the Object got no Transformations on it.
Also these RotationStuff may be incorrect

To get a BoundingBox, you could try to modify this, so that it keeps the lowest/highest X/Y/Z-Component and Outputs them
(See the CustomICE-Node-Example "Get BoundingBox", thats exactly the thing i would do in jScript to get a BoundingBox)



// Get Selection

var oSel = selection(0);

var oSelCtr = Sel_Center(oSel);
var vVecX = oSelCtr[0];
var vVecY = oSelCtr[1];
var vVecZ = oSelCtr[2];
var vRotX = oSelCtr[3];
var vRotY = oSelCtr[4];
var vRotZ = oSelCtr[5];


// ------------------------------------------------------------------------
// Get Selection Center

function Sel_Center(oSel)
{
var vAllVerts = 0;
var vPosX = 0; var vPosY = 0; var vPosZ = 0;
var vNrmX = 0; var vNrmY = 0; var vNrmZ = 0;

var oSelType = oSel.Type;
switch (oSelType)
{
// If Poly or Edge
case "polySubComponent":
case "edgeSubComponent":
var vSubCompCount = oSel.SubComponent.ComponentCollection.Count;
for (i=0; i<vSubCompCount; i++)
{
var vVertCount = oSel.SubComponent.ComponentCollection.Item(i).Vertices.Count;
for (j=0; j<vVertCount; j++)
{
var vAllVerts = vAllVerts + 1;
var vPosX = vPosX + oSel.SubComponent.ComponentCollection.Item(i).Vertices(j).Position.x;
var vPosY = vPosY + oSel.SubComponent.ComponentCollection.Item(i).Vertices(j).Position.y;
var vPosZ = vPosZ + oSel.SubComponent.ComponentCollection.Item(i).Vertices(j).Position.z;
var vNrmX = vNrmX + oSel.SubComponent.ComponentCollection.Item(i).Vertices(j).Normal.x;
var vNrmY = vNrmY + oSel.SubComponent.ComponentCollection.Item(i).Vertices(j).Normal.y;
var vNrmZ = vNrmZ + oSel.SubComponent.ComponentCollection.Item(i).Vertices(j).Normal.z;
} // for j End
} // for i End
break; // Case End

// If Vertex
case "pntSubComponent":
var vAllVerts = oSel.SubComponent.ComponentCollection.Count;
for (i=0; i<vAllVerts; i++)
{
var vPosX = vPosX + oSel.SubComponent.ComponentCollection.Item(i).Position.x;
var vPosY = vPosY + oSel.SubComponent.ComponentCollection.Item(i).Position.y;
var vPosZ = vPosZ + oSel.SubComponent.ComponentCollection.Item(i).Position.z;
var vNrmX = vNrmX + oSel.SubComponent.ComponentCollection.Item(i).Normal.x;
var vNrmY = vNrmY + oSel.SubComponent.ComponentCollection.Item(i).Normal.y;
var vNrmZ = vNrmZ + oSel.SubComponent.ComponentCollection.Item(i).Normal.z;
} // for i End
break;
//If Selection =/= Poly/Vertex/Edge
default:
var vAllVerts = 1; 
} //Switch End

//Convert Normal-Direction to Rotation
var vecWorldX = XSIMath.CreateVector3(); vecWorldX.Set (1,0,0);
var vecWorldY = XSIMath.CreateVector3(); vecWorldY.Set (0,1,0);
var vecWorldZ = XSIMath.CreateVector3(); vecWorldZ.Set (0,0,1);

var vecNrmX = XSIMath.CreateVector3(); vecNrmX.Set ( vNrmX/vAllVerts, vNrmY/vAllVerts, vNrmZ/vAllVerts);
var vecNrmY = XSIMath.CreateVector3(); vecNrmY.Set ( 0, vNrmY/vAllVerts, vNrmZ/vAllVerts);
var vecNrmZ = XSIMath.CreateVector3(); vecNrmZ.Set ( 0, 0, vNrmZ/vAllVerts);
var vRotX = vecNrmX.Dot (vecWorldX);
var vRotY = vecNrmY.Dot (vecWorldY);
var vRotZ = vecNrmZ.Dot (vecWorldZ);

LogMessage ("Position: X: " + vPosX/vAllVerts + " / Y: " + vPosY/vAllVerts + " / Z: " + vPosZ/vAllVerts);
LogMessage ("Rotation: X: " + vRotX + " / Y: " + vRotY + " / Z: " + vRotZ);
return [vPosX/vAllVerts, vPosY/vAllVerts, vPosZ/vAllVerts, vRotX, vRotY, vRotZ]
}
// ------------------------------------------------------------------------


Reply via email to