Snap Object To Selected Face

Does anyone have a sample script of snapping an object to the center of the selected poly face on another object?

say you have a cone named "polycone" and you want to snap it to the center of the selected face of an object called "polycylinder" so that "polycone" sticks out the normal of the face...

I looked at all the snapping scripts in downloads, and none of them seem to do that..

the Modify/Snap Together tool sort of works ( it's not centered, and I need to script it)..

thanks!!


this what the Snap Together tool does, but it's not much help..

{
float $rp[] = xform -q -rotatePivot "|pCone1";
xform -worldSpace -preserve true
-rotatePivot 0.0563176 -1 9.41199 "|pCone1";
rotate -relative 0 0 0 "|pCone1";
xform -rotatePivot $rp[0] $rp[1] $rp[2] "|pCone1";
};
move -relative -0.0686346 2 -9.39617 "|pCone1";

theres an option at the Snap Together Tool:

Snap to Polygon Face

did you try this? Seems it works on face centers.

Yes that's what I want to do, but in a mel script.

the object named polycone will snap to the selected face, any time I press the button.

Ok I've found part of it in a message, this moves it but doesn't rotate to the selected face...
anyone know how to make it rotate too?

//something must be selected
float $bb[] = exactWorldBoundingBox;
string $newObject[] = polyCube;
setAttr ( $newObject[0] + ".translate" )
( ( $bb[ 3 ] - $bb[ 0 ] ) / 2 + $bb[ 0 ] )
( ( $bb[ 4 ] - $bb[ 1 ] ) / 2 + $bb[ 1 ] )
( ( $bb[ 5 ] - $bb[ 2 ] ) / 2 + $bb[ 2 ] );

http://www.highend3d.com/boards/index.php?...=216035&hl=

The problem is not the positioning. Problem is to get the euler rotaion from the Vector of the selected face.

Good ol polyInfo -faceNormals delivers the face normal in its weirdy manner. So here is a helper for the start:

CODE
global proc float[] getPolyFaceNormals(string $faces[])
{
    // expand face input
    $faces = filterExpand -ex 1 -sm 34 $faces;
    // if nothing given take selection
    if (!size($faces)) $faces = filterExpand -ex 1 -sm 34;
    float $return[];
    int $count = 0;
    for ($face in $faces)
    {
        string $polyInfo[] = polyInfo -faceNormals $face;
        string $buffer[];
        tokenize $polyInfo[0] $buffer;
        $return[$count++] = $buffer[2];
        $return[$count++] = $buffer[3];
        $return[$count++] = $buffer[4];
    }
    return $return;
}
Put in an array of faces and get an array of float normals.
Oh boy you are lucky that I'm interested in this. I wouldn't do a keystroke otherwise ;]

OK. Now how to get the euler rotation from that vector?
I think we have to project the vector on each plane and get the angle via angle command.
CODE
        float $faceNormal[] = getPolyFaceNormals($selFace);
        float $eulerRot[];
        $eulerRot[0] = rad_to_deg(angle <<0, $faceNormal[1], $faceNormal[2]>> <<0,1,0>>);
        $eulerRot[1] = rad_to_deg(angle <<$faceNormal[0], 0, $faceNormal[2]>> <<1,0,0>>);
        $eulerRot[2] = rad_to_deg(angle <<$faceNormal[0], $faceNormal[1], 0>> <<0,1,0>>);

But thats still not it.. I had this problem already. We have to flip some axes here if they are negative or something...

Thanks for the help ewerybody!

How about this for finding the normal of the selected face? I was thinking about having it snap to a single face,, but I guess the option for more would be better. As for figuring out the rotation,, I'm in way over my head..

string $selected[] = ls -sl; // this checks the direction of selected face

string $fNormal[] = polyInfo -fn $selected;

string $tokens[5];

tokenize $fNormal[0] $tokens;

vector $FN = << float($tokens[2]) , float($tokens[3]) , float($tokens[4]) >>;

print "\n\ Face ="; print $FN;