Programmatically setting the center of rotation [SOLVED]
Forum rules
Be nice to others! Respect the FreeCAD code of conduct!
Be nice to others! Respect the FreeCAD code of conduct!
Programmatically setting the center of rotation [SOLVED]
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
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.
Re: Programmatically setting the center of rotation
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
Open source CNC hot wire cutter project (NiCr): https://github.com/JMG1/NiCr
Exploded Assembly Workbench: https://github.com/JMG1/ExplodedAssembly
Re: Programmatically setting the center of rotation
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
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
Re: Programmatically setting the center of rotation
hi
see here for examples utilisation of camera
Is it possible to rotate camera view by hand?
Macro_FCCamera
Macro_Align_View_to_Face
Camera position - Draft.makeText
mario
see here for examples utilisation of camera
Is it possible to rotate camera view by hand?
Macro_FCCamera
Macro_Align_View_to_Face
Camera position - Draft.makeText
mario
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.
My macros on Gist.github here complete macros Wiki and forum.
Re: Programmatically setting the center of rotation
I think, Cuaxolotl is NOT asking about camera, but model rotation
Re: Programmatically setting the center of rotation
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:
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
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) )
Another, minor question I have additionally is: What is the meaning of the second, optional parameter to pointAt?
Thank You for your help,
Benjamin
Re: Programmatically setting the center of 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 .TT-RS wrote:I think, Cuaxolotl is NOT asking about camera, but model rotation
Re: Programmatically setting the center of rotation
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:
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.
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
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)
Re: Programmatically setting the center of rotation
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:
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())