shadows in the freecad viewport

Here's the place for discussion related to coding in FreeCAD, C++ or Python. Design, interfaces and structures.
User avatar
yorik
Site Admin
Posts: 11565
Joined: Tue Feb 17, 2009 9:16 pm
Location: São Paulo, Brazil
Contact:

Re: shadows in the freecad viewport

Postby yorik » Fri Jul 19, 2019 3:10 pm

Yes it's realtime... you can change the light direction on the fly (last line of code above).

It works like this: to produce shadows, all the objects that must receive and cast shadows in your coin scene must be placed inside a SoShadowGroup, and there must be one ShadowDirectionalLight in that same SoShadowGroup (there re other types of shadow-casting lights available too). You can also add a SoShadowStyle inside, which allows to turn shadows on/off:

https://grey.colorado.edu/coin3d/classS ... Group.html

That's basically what the OfflineRenderingUtils.embedLight does: You give it a coin node/graph, and it embeds it inside a SoShadowGroup.

Only, the FreeCAD viewport doesn't have tools to replace the scenegraph. This would probably cause some problems too, as some tools likely wouldn't find their children anymore. But it would be cool to allow to add a SoShadowGroup to the main view. Need to look at how to do that correctly..
wmayer wrote:@Werner, any idea how we could work towards that?
So what the above script does is 1) create a blank viewer (which has no connection with FreeCAD tools and view providers), 2) build a coin scenegraph from the visible objects (so far I couldn't make it work to simply copy the main scenegraph, not sure why) 3) embed that scenegraph in a SoShadowGroup, 4) set the viewr's scenegraph with that scenegraph.
paullee
Posts: 1753
Joined: Wed May 04, 2016 3:58 pm

Re: shadows in the freecad viewport

Postby paullee » Fri Jul 19, 2019 3:52 pm

Hi, error running the script ?

Code: Select all

import OfflineRenderingUtils
Traceback (most recent call last):
  File "<input>", line 1, in <module>
ModuleNotFoundError: No module named 'OfflineRenderingUtils'
User avatar
bitacovir
Posts: 710
Joined: Sat Apr 19, 2014 6:23 am
Contact:

Re: shadows in the freecad viewport

Postby bitacovir » Fri Jul 19, 2019 7:11 pm

It works fine for me. just copy and paste on the Python console.
But the functions of standard views (top view, front view) in the toolbar are not working, anymore.
FreeCAD_2019-07-19_15-09-47.png
FreeCAD_2019-07-19_15-09-47.png (20.61 KiB) Viewed 381 times
OS: Windows 10
Word size of OS: 64-bit
Word size of FreeCAD: 64-bit
Version: 0.19.17433 (Git)
Build type: Release
Branch: master
Hash: 125380ddd9a1fdeb45ffc736040dad1f6343daa4
Python version: 3.6.6
Qt version: 5.6.2
Coin version: 4.0.0a
OCC version: 7.3.0
Locale: English/Australia (en_AU)
::bitacovir::
===================================
One must be absolutely modern.
Arthur Rimbaud (A Season in Hell -1873)

My Blog
Mini Airflow Tunnel Project
User avatar
bitacovir
Posts: 710
Joined: Sat Apr 19, 2014 6:23 am
Contact:

Re: shadows in the freecad viewport

Postby bitacovir » Fri Jul 19, 2019 7:19 pm

other examples...
FreeCAD_2019-07-19_15-19-40.png
FreeCAD_2019-07-19_15-19-40.png (46.47 KiB) Viewed 380 times
FreeCAD_2019-07-19_15-20-19.png
FreeCAD_2019-07-19_15-20-19.png (164.02 KiB) Viewed 380 times
::bitacovir::
===================================
One must be absolutely modern.
Arthur Rimbaud (A Season in Hell -1873)

My Blog
Mini Airflow Tunnel Project
User avatar
easyw-fc
Posts: 2668
Joined: Thu Jul 09, 2015 9:34 am

Re: shadows in the freecad viewport

Postby easyw-fc » Fri Jul 19, 2019 7:40 pm

paullee wrote:
Fri Jul 19, 2019 3:52 pm
Hi, error running the script ?

Code: Select all

import OfflineRenderingUtils
Traceback (most recent call last):
  File "<input>", line 1, in <module>
ModuleNotFoundError: No module named 'OfflineRenderingUtils'
the same for me on win and on linux

Code: Select all

OS: Linux Mint 19.1 (MATE/mate)
Word size of OS: 64-bit
Word size of FreeCAD: 64-bit
Version: 0.19.
Build type: Release
Python version: 3.6.8
Qt version: 5.9.5
Coin version: 4.0.0a
OCC version: 7.3.0
Locale: English/UnitedStates (en_US)
User avatar
yorik
Site Admin
Posts: 11565
Joined: Tue Feb 17, 2009 9:16 pm
Location: São Paulo, Brazil
Contact:

Re: shadows in the freecad viewport

Postby yorik » Sat Jul 20, 2019 1:02 am

Ah my bad, forgot to add that file (OfflineRenderingUtils) to the PPA recipe... Will do right now
paullee
Posts: 1753
Joined: Wed May 04, 2016 3:58 pm

Re: shadows in the freecad viewport

Postby paullee » Sat Jul 20, 2019 1:15 am

Looks great, black & white? Texture works?

I am on fedora 30, maybe I need to search for the package.
wmayer
Site Admin
Posts: 14887
Joined: Thu Feb 19, 2009 10:32 am

Re: shadows in the freecad viewport

Postby wmayer » Sat Jul 20, 2019 2:55 pm

yorik wrote:
Fri Jul 19, 2019 3:10 pm
Only, the FreeCAD viewport doesn't have tools to replace the scenegraph. This would probably cause some problems too, as some tools likely wouldn't find their children anymore. But it would be cool to allow to add a SoShadowGroup to the main view. Need to look at how to do that correctly..
This actually shouldn't cause problems because view providers usually search underneath their root separator. And if any function needs to search for nodes of other objects (like the body feature) it shouldn't assume any fix order of how the nodes are organized but is supposed to use an SoSearchAction.

Btw the clipping plane or axis cross functions also change the graph structure at runtime and I have never seen a problem.

Code: Select all

from pivy import coin

sotype=coin.SoType.fromName("SoShadowGroup")
shadow=sotype.createInstance()
shadow.precision.setValue(1)
shadow.quality.setValue(1)

sotype=coin.SoType.fromName("ShadowDirectionalLight")
light=sotype.createInstance()
light.intensity.setValue(200)
light.direction.setValue(-1,-1,-1)

view=Gui.ActiveDocument.ActiveView.getViewer()
root=view.getSceneGraph()
# get the third node of the root (or search for SoSeparator)
scene=root.getChild(2)
root.removeChild(scene)

shadow.addChild(light)
shadow.addChild(scene)
root.addChild(shadow)
paullee
Posts: 1753
Joined: Wed May 04, 2016 3:58 pm

Re: shadows in the freecad viewport

Postby paullee » Sun Jul 21, 2019 12:39 am

Finally found it is @yorik's works can't find elsewhere :)

But seem the current AppImage is not latest compare to @bitacovir's... FreeCAD_0.19-17360-Linux-Conda_Py3Qt5_glibc2.12-x86_64.AppImage
  1. Running the Latest script Downlaod from /FreeCAD on 17360 AppImage return errror about <!DOCTYPE html>
  2. Running the 'original script' on 17360 AppImage return error about embed light
Seem need to wait ...

Code: Select all

>>> import OfflineRenderingUtils
Traceback (most recent call last):
  File "<input>", line 1, in <module>
  File "/run/media/paullee/Home_1/paullee/Downloads/FreeCAD_0.19-17360-Linux-Conda_Py3Qt5_glibc2.12-x86_64/squashfs-root/usr/Mod/Arch/OfflineRenderingUtils.py", line 7
    <!DOCTYPE html>
    ^
SyntaxError: invalid syntax
>>> objs = [o for o in FreeCAD.ActiveDocument.Objects if o.ViewObject.Visibility == True] # gather visible objects
>>> cam = FreeCADGui.ActiveDocument.ActiveView.getCamera() # gather the camera position
>>> s = OfflineRenderingUtils.buildScene(objs) # build the coin scene
Traceback (most recent call last):
  File "<input>", line 1, in <module>
NameError: name 'OfflineRenderingUtils' is not defined
>>> rn = OfflineRenderingUtils.embedLight(s,(1,1,-1)) # embed the wholescene in a ShadowGroup, add directional light
Traceback (most recent call last):
  File "<input>", line 1, in <module>
NameError: name 'OfflineRenderingUtils' is not defined
>>> light = rn.getChild(0) # get a ref to the directional light for later
Traceback (most recent call last):
  File "<input>", line 1, in <module>
NameError: name 'rn' is not defined
>>> v = FreeCADGui.createViewer() # create an empty 3D view in FreeCAD
>>> v.setCamera(cam) # set the camera
>>> vv = v.getViewer().setSceneGraph(rn) # set our scene as its scenegraph
Traceback (most recent call last):
  File "<input>", line 1, in <module>
NameError: name 'rn' is not defined
>>> light.direction = (-1,-1,-1) # change light direction

Code: Select all

>>> import OfflineRenderingUtils
>>> objs = [o for o in FreeCAD.ActiveDocument.Objects if o.ViewObject.Visibility == True] # gather visible objects
>>> cam = FreeCADGui.ActiveDocument.ActiveView.getCamera() # gather the camera position
>>> s = OfflineRenderingUtils.buildScene(objs) # build the coin scene
>>> rn = OfflineRenderingUtils.embedLight(s,(1,1,-1)) # embed the wholescene in a ShadowGroup, add directional light
Traceback (most recent call last):
  File "<input>", line 1, in <module>
AttributeError: module 'OfflineRenderingUtils' has no attribute 'embedLight'
>>> light = rn.getChild(0) # get a ref to the directional light for later
Traceback (most recent call last):
  File "<input>", line 1, in <module>
NameError: name 'rn' is not defined
>>> v = FreeCADGui.createViewer() # create an empty 3D view in FreeCAD
>>> v.setCamera(cam) # set the camera
>>> vv = v.getViewer().setSceneGraph(rn) # set our scene as its scenegraph
Traceback (most recent call last):
  File "<input>", line 1, in <module>
NameError: name 'rn' is not defined
>>> light.direction = (-1,-1,-1) # change light direction
User avatar
yorik
Site Admin
Posts: 11565
Joined: Tue Feb 17, 2009 9:16 pm
Location: São Paulo, Brazil
Contact:

Re: shadows in the freecad viewport

Postby yorik » Sun Jul 21, 2019 8:24 pm

wmayer wrote:
Sat Jul 20, 2019 2:55 pm

Code: Select all

root.removeChild(scene) 
shadow.addChild(light) 
shadow.addChild(scene) 
root.addChild(shadow)
Wow that's audacious :D Great! Thanks!!! It works and it's SERIOUSLY COOL:
Screenshot from 2019-07-21 17-21-30.png
Screenshot from 2019-07-21 17-21-30.png (177.29 KiB) Viewed 248 times
This is the working FreeCAD view, everything still selectable and updatable. Only a few issues like new objects not added to the shadow group. But we're getting there...
paullee wrote:
Sun Jul 21, 2019 12:39 am
File "/run/media/paullee/Home_1/paullee/Downloads/FreeCAD_0.19-17360-Linux-Conda_Py3Qt5_glibc2.12-x86_64/squashfs-root/usr/Mod/Arch/OfflineRenderingUtils.py", line 7 <!DOCTYPE html> ^
There is something VERY wrong there... :shock: I guess the packaging went pretty bad on that build. The error about embedLight is probably that the version of OfflineRenderingUtils there is too old...