Aligning camera to normals

I am working on something that sounds simple enough, but unless there are some tools that will do this easily, it is much harder than you would think to figure it out.

I am trying to get the perspective camera to align itself with the normal to a selected face. I have worked out a way to calculate the face normal which is most likely a poor workaround, but it works.

I can modify the perspective camera's workspace just fine, but I am having a beast of a time keeping my head straight when it comes to rotating the camera's attributes to match the vector.

I get the normal, and then translate it to the origen. Then I force the camera's rotation order to be xyz.

Then I try and calculate the angle to set for the x rotation in the attribute for the camera (which as I see it would depend on the quadrant the vector is in on the yz plane.)

Once I get that, I do a similar thing to calculate a rotation around the y axis that should put me alligned with the vector.

It is dizzying though trying to keep the rotations straight and remember which direction you are rotating and all that crazy stuff. I have looked at aimConstraint, but that doesn't do what I want. Anybody know how to do this?

I thought I was right when I first coded it up, but it doens't work. It translates to a good location right off the selected face, but I cannot get the rotation right or the life of me.

Are you using the "angleBetween" function? It calculates the euler rotation needed to rotate one vector to another. If you know the vector of your face, and the vector of your camera, it should give you what you need.

Thanks so much for that pointer. I was trying to essentially write my own method to find that angle. The only thing that I had noticed in the Mel command reference was angle which gave me the angle between two vectors. It was an unbeleivable headache trying to figure that out.

Thanks again. That made life considerably easier.

Just out of curiosity, do you know of any functions that give you the normal of a face? I had to do that with a dirty workaround. I basically duplicated the face, translated it a bit in the normal direction then got the center of that face if that makes sense. I know that it there is a way to do it mathematically, but I can't remember that right now and the workaround was easier to do I think.

Thanks again.

The only command I know if is:
polyInfo -fn;
Which will return a STRING that has the normal info embedded in it. I actually made a proc that would tokenize that string, and spit out the actual normal info, which is probably what you'll need to do.

have you tried using a normalConstraint, perhaps just temporarily? there is also the pointOnMesh node that comes w the bonus games package, where you plug ina face index and it gives you info like normal and position. mm, and Bryan Ewert has done a alignToFaces plug ...and just looking now (at his fine site, www.ewertb.com), he has the source availible too! what a nice fellow