# Snap Object To Selected Face

**URL:** https://forum.highend3d.com/t/snap-object-to-selected-face/2474
**Category:** Maya
**Tags:** mel
**Created:** [February 10, 2007, 5:52am UTC](https://forum.highend3d.com/t/snap-object-to-selected-face/2474 "2007-02-10T05:52:17Z")
**Posts on this page:** 6
**Page:** 1

<div class="post-metadata">

### Author: ![rifz](https://forum.highend3d.com/letter_avatar_proxy/v4/letter/r/d78d45/32.png) [@rifz](https://forum.highend3d.com/u/rifz)
#### Post date: [February 10, 2007, 5:52am UTC](https://forum.highend3d.com/t/snap-object-to-selected-face/2474/1 "2007-02-10T05:52:17Z")

</div>

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";

---

<div class="post-metadata">

### Author: ![ewerybody](https://forum.highend3d.com/letter_avatar_proxy/v4/letter/e/bcef8e/32.png) [@ewerybody](https://forum.highend3d.com/u/ewerybody)
#### Post date: [February 12, 2007, 9:45am UTC](https://forum.highend3d.com/t/snap-object-to-selected-face/2474/2 "2007-02-12T09:45:11Z")

</div>

theres an option at the Snap Together Tool:

Snap to Polygon Face

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

---

<div class="post-metadata">

### Author: ![rifz](https://forum.highend3d.com/letter_avatar_proxy/v4/letter/r/d78d45/32.png) [@rifz](https://forum.highend3d.com/u/rifz)
#### Post date: [February 12, 2007, 2:52pm UTC](https://forum.highend3d.com/t/snap-object-to-selected-face/2474/3 "2007-02-12T14:52:28Z")

</div>

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.

---

<div class="post-metadata">

### Author: ![rifz](https://forum.highend3d.com/letter_avatar_proxy/v4/letter/r/d78d45/32.png) [@rifz](https://forum.highend3d.com/u/rifz)
#### Post date: [February 13, 2007, 8:00pm UTC](https://forum.highend3d.com/t/snap-object-to-selected-face/2474/4 "2007-02-13T20:00:21Z")

</div>

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=](http://www.highend3d.com/boards/index.php?...=216035&hl=)

---

<div class="post-metadata">

### Author: ![ewerybody](https://forum.highend3d.com/letter_avatar_proxy/v4/letter/e/bcef8e/32.png) [@ewerybody](https://forum.highend3d.com/u/ewerybody)
#### Post date: [February 14, 2007, 1:22pm UTC](https://forum.highend3d.com/t/snap-object-to-selected-face/2474/5 "2007-02-14T13:22:11Z")

</div>

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[])  
{  
&nbsp;&nbsp;&nbsp;&nbsp;// expand face input  
&nbsp;&nbsp;&nbsp;&nbsp;$faces = `filterExpand -ex 1 -sm 34 $faces`;  
&nbsp;&nbsp;&nbsp;&nbsp;// if nothing given take selection  
&nbsp;&nbsp;&nbsp;&nbsp;if (!size($faces)) $faces = `filterExpand -ex 1 -sm 34`;  
&nbsp;&nbsp;&nbsp;&nbsp;float $return[];  
&nbsp;&nbsp;&nbsp;&nbsp;int $count = 0;  
&nbsp;&nbsp;&nbsp;&nbsp;for ($face in $faces)  
&nbsp;&nbsp;&nbsp;&nbsp;{  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;string $polyInfo[] = `polyInfo -faceNormals $face`;  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;string $buffer[];  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;tokenize $polyInfo[0] $buffer;  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;$return[$count++] = $buffer[2];  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;$return[$count++] = $buffer[3];  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;$return[$count++] = $buffer[4];  
&nbsp;&nbsp;&nbsp;&nbsp;}  
&nbsp;&nbsp;&nbsp;&nbsp;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  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;float $faceNormal[] = getPolyFaceNormals($selFace);  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;float $eulerRot[];  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;$eulerRot[0] = rad\_to\_deg(`angle <<0, $faceNormal[1], $faceNormal[2]>> <<0,1,0>>`);  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;$eulerRot[1] = rad\_to\_deg(`angle <<$faceNormal[0], 0, $faceNormal[2]>> <<1,0,0>>`);  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;$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...

---

<div class="post-metadata">

### Author: ![rifz](https://forum.highend3d.com/letter_avatar_proxy/v4/letter/r/d78d45/32.png) [@rifz](https://forum.highend3d.com/u/rifz)
#### Post date: [February 14, 2007, 3:10pm UTC](https://forum.highend3d.com/t/snap-object-to-selected-face/2474/6 "2007-02-14T15:10:33Z")

</div>

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;
