Tips on capturing camera view change events.

Need help, or want to share a macro? Post here!
Forum rules
Be nice to others! Respect the FreeCAD code of conduct!
Post Reply
Mark Szlazak
Posts: 439
Joined: Tue Apr 04, 2017 6:06 pm
Location: SF Bay Area, California

Tips on capturing camera view change events.

Post by Mark Szlazak »

Scripting in Part, I would like to capture camera view change events that happen from the GUI (press scroll wheel down on mouse while moving it to translate the camera AND press both scroll wheel and left button while moving it to change camera orientation). When this happens, I would like to recalculate some "apparent" line intersection points from the camera's POV and redraw some parts accordingly. This maybe "labour" intensive because there will be a lot of movement happening moment to moment. So i was thinking of just doing updates at certain time intervals, the last time interval may happen after all mouse movement stops. Any help would be appreciated.
User avatar
Chris_G
Veteran
Posts: 2579
Joined: Tue Dec 31, 2013 4:10 pm
Location: France
Contact:

Re: Tips on capturing camera view change events.

Post by Chris_G »

Hello,
You can not rely on mouse events, because there are multiple navigation styles, that would be tricky.
You can directly track the camera node with a coin sensor :

Code: Select all

from pivy import coin

def cb(*args):
	print("Cam orientation changed")

cam = Gui.ActiveDocument.ActiveView.getCameraNode()
sensor = coin.SoFieldSensor(cb,cam)
sensor.setPriority(0)
sensor.attach(cam.orientation)
# to stop the sensor
#sensor.detach()
More about camera node here : http://www-evasion.imag.fr/Membres/Fran ... /ch04.html
More about coin sensors here : http://www-evasion.imag.fr/Membres/Fran ... /ch12.html
User avatar
Chris_G
Veteran
Posts: 2579
Joined: Tue Dec 31, 2013 4:10 pm
Location: France
Contact:

Re: Tips on capturing camera view change events.

Post by Chris_G »

You can not rely on mouse events, because there are multiple navigation styles, that would be tricky.
My reply was considering a "reusable by others" approach ( so the navigation style is unknown ).
Of course, if your code is only for your personal use, you know your navigation style, and the mouse events that move the camera, so the mouse events approach is also possible.
Mark Szlazak
Posts: 439
Joined: Tue Apr 04, 2017 6:06 pm
Location: SF Bay Area, California

Re: Tips on capturing camera view change events.

Post by Mark Szlazak »

Thanks Chris. This is only for personal use. There are many structures of the kind I asked about in this post:

https://math.stackexchange.com/question ... nstr?sfb=2

The two apparent intersections mentioned above will really be a portion of the circle in front of the axis line and another portion behind the axis line. If in front then I want to break the axis line with a bit of clear space around that apparent intersection. If behind then I want to break the circle with a bit of clear space. This gives a classic way of illustrating an area where one line is in front of the other.

However, these apparent points of intersection change with changes in point-of-view. I will change POV only with the mouse.

1. There are many camera property functions that give position and orientation. Which should I use?
2. How do I capture the mouse events I mentioned?
3. How should I manage all the GUI image updates while moving the mouse around? Will doing all of them as fast as the system can handle bog it down. Should I only update every 50 or 100 msec?

Thanks for any help.
User avatar
Chris_G
Veteran
Posts: 2579
Joined: Tue Dec 31, 2013 4:10 pm
Location: France
Contact:

Re: Tips on capturing camera view change events.

Post by Chris_G »

To me, what you're trying to achieve would be a job for shaders.
I don't know anything about shaders, but maybe it is worth exploring that path, because shaders are available in coin.
1. There are many camera property functions that give position and orientation. Which should I use?
I don't know, I never use these camera functions.
2. How do I capture the mouse events I mentioned?
http://www-evasion.imag.fr/~Francois.Fa ... /ch10.html
Search also for "freecad SoMouseButtonEvent" and "freecad SoLocation2Event"
But to me, using a SoFieldSensor on the camera would be more logical, since this is what you're really interested in.
Stay away from the SoNodeSensor, it has a bug that crashes FreeCAD.
3. How should I manage all the GUI image updates while moving the mouse around? Will doing all of them as fast as the system can handle bog it down. Should I only update every 50 or 100 msec?
I think using a coin sensor make sense. If you have a callback function that does :

Code: Select all

Get camera position
Get intersection points
cut edges
Then you'll be able to easily switch from the camera sensor to a timer-based sensor (probably without even modifying your callback), if the camera sensor is triggered to often and makes the render laggy.
Mark Szlazak
Posts: 439
Joined: Tue Apr 04, 2017 6:06 pm
Location: SF Bay Area, California

Re: Tips on capturing camera view change events.

Post by Mark Szlazak »

Ok. Thank you Chris.
I will probably skip the dynamic update with mouse movement and go for some key and/or mouse button combination when I want the image changes. That way I can just stay with relatively simple scripting in FreeCAD. I found the direction vector of the camera, Gui.ActiveDocument.ActiveView.getViewDirection(), the rest simply follows from two cross-products as mention in the article. So leaving it as a manual activity of re-rendering the display is simplest and avoids any issues with a fancier approach.
Post Reply