Programmatically setting the center of rotation [SOLVED]

Need help, or want to share a macro? Post here!
Forum rules
Be nice to others! Respect the FreeCAD code of conduct!
Post Reply
Cuaxolotl
Posts: 5
Joined: Tue Mar 17, 2015 8:31 pm

Programmatically setting the center of rotation [SOLVED]

Post by Cuaxolotl »

Hi,

how to set the center of rotation (of the view) programmatically. (In 'CAD Navigation'-mode, it can be done using the middle mouse button.) In particular I'd like the view to rotate exactly around the coordinate origion (0,0,0).

Thanks,

Benjamin
Last edited by Cuaxolotl on Sat Mar 21, 2015 8:07 pm, edited 1 time in total.
JMG
Posts: 288
Joined: Wed Dec 25, 2013 9:32 am
Location: Spain
Contact:

Re: Programmatically setting the center of rotation

Post by JMG »

Hello.

Have a look at this

Regards.


Javier.
FreeCAD scripts, animations, experiments and more: http://linuxforanengineer.blogspot.com.es/
Open source CNC hot wire cutter project (NiCr): https://github.com/JMG1/NiCr
Exploded Assembly Workbench: https://github.com/JMG1/ExplodedAssembly
Cuaxolotl
Posts: 5
Joined: Tue Mar 17, 2015 8:31 pm

Re: Programmatically setting the center of rotation

Post by Cuaxolotl »

Hi,

thank you for your reply, but I can't see, how positioning the camera shall help to set the center of rotation. But I may have overlooked something, so feel free to correct me :) .

Have a nice day,

Benjamin
mario52
Veteran
Posts: 4698
Joined: Wed May 16, 2012 2:13 pm

Re: Programmatically setting the center of rotation

Post by mario52 »

Maybe you need a special feature, go into Macros_recipes and Code_snippets, Topological_data_scripting.
My macros on Gist.github here complete macros Wiki and forum.
User avatar
TT-RS
Posts: 70
Joined: Fri Oct 24, 2014 9:19 pm

Re: Programmatically setting the center of rotation

Post by TT-RS »

I think, Cuaxolotl is NOT asking about camera, but model rotation ;)
Cuaxolotl
Posts: 5
Joined: Tue Mar 17, 2015 8:31 pm

Re: Programmatically setting the center of rotation

Post by Cuaxolotl »

Ok, I understood camera handling wrong. I can indeed set the center of rotation by positioning the camera.

If I want to be able to rotate around (x,y,z) (using the mouse), I have to position the camera at a point of distance norm(5,5,5) to (x,y,z) (I found out to choose that distance by trial and error, see my questions below) and then I must point the camera to (x,y,z).

If, for example, i'd like to rotate around (0,10,10) I could issue the following commands:

Code: Select all

from pivy import coin
camera = FreeCADGui.ActiveDocument.ActiveView.getCameraNode()

camera.position.setValue( FreeCAD.Vector(5,15,15) )
camera.pointAt( coin.SbVec3f(0,10,10) )
It makes sense, that the point of rotation lies on the line defined by x +t*y where x is the point the camera is located and y is the direction the camera is pointed at, but why do I have to choose y to be of length norm(5,5,5) ? Does anybody know that? And is that length the same in every system? And if not, can I somehow determine that length?

Another, minor question I have additionally is: What is the meaning of the second, optional parameter to pointAt?

Thank You for your help,

Benjamin
Cuaxolotl
Posts: 5
Joined: Tue Mar 17, 2015 8:31 pm

Re: Programmatically setting the center of rotation

Post by Cuaxolotl »

TT-RS wrote:I think, Cuaxolotl is NOT asking about camera, but model rotation ;)
Well, I thought everyone would understand me, because everyone would know, that per default the camera does not rotate around (0,0,0) (it seems rather to be (5,5,5)); but maybe not everyone noticed that Fact, since it is rather unnoticable, if you have a larger model. Anyways, I hope my other post makes it clearer. And I hope someone can give me the answers to my questions from the other post. Thank you however for noticing the difference ;) .
wmayer
Founder
Posts: 20319
Joined: Thu Feb 19, 2009 10:32 am
Contact:

Re: Programmatically setting the center of rotation

Post by wmayer »

Basically the camera has three parameters that are relevant for this:
direction: this is a rotation (internally stored as quaternion). You have to apply this on the vector (0,0,-1) to get the actual view direction
position: this is the home position of the camera
focalDistance: this is the distance from the camera to the point where it is looking at. This point is also the rotation center you are looking for. It can be computed like this:

Code: Select all

from pivy import coin
camera=Gui.ActiveDocument.ActiveView.getCameraNode()
position=camera.position.getValue()
orient=camera.orientation.getValue()
focalDistance=camera.focalDistance.getValue()

viewdir=coin.SbVec3f(0,0,-1)
viewdir=orient.multVec(viewdir)

center=position+viewdir*focalDistance
Now when rotating the camera around the object two values change: the direction and the position attributes. The focalDistance doesn't change and the above rotation enter doesn't change either.

The only tricky part is to find the new orientation (in case you want to change it all). From there you can calculate back the new position of the camera again.

Let's assume you have a rotation center different to (0,0,0) which you want to move to (0,0,0) without changing the direction.

Code: Select all

center=coin.SbVec3f(0,0,0)

camera=Gui.ActiveDocument.ActiveView.getCameraNode()
orient=camera.orientation.getValue()
viewdir=coin.SbVec3f(0,0,-1)
viewdir=orient.multVec(viewdir)
focalDistance=camera.focalDistance.getValue()

position=center-viewdir*focalDistance
camera.position.setValue(position)
Cuaxolotl
Posts: 5
Joined: Tue Mar 17, 2015 8:31 pm

Re: Programmatically setting the center of rotation

Post by Cuaxolotl »

Hey, thank you, that was a great explanation :) .

To complete it, setting another center of rotation (here (0,0,0)), without changing the camera position:

Code: Select all

camera = FreeCADGui.ActiveDocument.ActiveView.getCameraNode()
camera.pointAt(coin.SbVec3f(0,0,0))
distToZero = camera.position.getValue() - coin.SbVec3f(0,0,0)
camera.focalDistance.setValue(distToZero.length())
Post Reply