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: 11552
Joined: Tue Feb 17, 2009 9:16 pm
Location: São Paulo, Brazil
Contact:

Re: shadows in the freecad viewport

Postby yorik » Wed Jul 31, 2019 12:29 am

Basically the main scenegraph is a SoSeparator with a couple of default nodes, and where each view provider inserts another SoSeparator. Since what interests us is the coin nodes created by the view providers, we grab all the SoSeparators inside the main scenegraph node.

And we pack them into a new SoSeparator for convenience. This is the full script:

Code: Select all

from pivy import coin
root = Gui.ActiveDocument.ActiveView.getSceneGraph()
nodes = [node for node in root.getChildren() if isinstance(node, coin.SoSeparator]
newnode = coin.SoSeparator()
for node in nodes:
    newnode.addChild(node) # pack them all into a separator, for convenience
import OfflineRenderingUtils
cam = FreeCADGui.ActiveDocument.ActiveView.getCamera() # gather the camera position
rn = OfflineRenderingUtils.embedLight(newnode,(1,1,-1)) # embed the wholescene in a ShadowGroup, add directional light
light = rn.getChild(0) # get a ref to the directional light for later
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
light.direction = (-1,-1,-1) # change light direction
pablogil
Posts: 756
Joined: Wed Nov 26, 2014 3:19 pm
Location: Badajoz (Spain)
Contact:

Re: shadows in the freecad viewport

Postby pablogil » Wed Jul 31, 2019 8:19 am

yorik wrote:
Wed Jul 31, 2019 12:29 am
Basically the main scenegraph is a SoSeparator with a couple of default nodes, and where each view provider inserts another SoSeparator. Since what interests us is the coin nodes created by the view providers, we grab all the SoSeparators inside the main scenegraph node.

And we pack them into a new SoSeparator for convenience. This is the full script:

Code: Select all

from pivy import coin
root = Gui.ActiveDocument.ActiveView.getSceneGraph()
nodes = [node for node in root.getChildren() if isinstance(node, coin.SoSeparator]
newnode = coin.SoSeparator()
for node in nodes:
    newnode.addChild(node) # pack them all into a separator, for convenience
import OfflineRenderingUtils
cam = FreeCADGui.ActiveDocument.ActiveView.getCamera() # gather the camera position
rn = OfflineRenderingUtils.embedLight(newnode,(1,1,-1)) # embed the wholescene in a ShadowGroup, add directional light
light = rn.getChild(0) # get a ref to the directional light for later
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
light.direction = (-1,-1,-1) # change light direction
I have fixed a parenthesis in line 3 but I still get this error:

Code: Select all

Traceback (most recent call last):
  File ".../shadows_v02.FCMacro", line 9, in <module>
    rn = OfflineRenderingUtils.embedLight(newnode,(1,1,-1)) # embed the wholescene in a ShadowGroup, add directional light
<class 'AttributeError'>: module 'OfflineRenderingUtils' has no attribute 'embedLight'
Any idea?
Dark and Light stylesheets v2.0 to theme your FreeCAD UI, more information here
User avatar
yorik
Site Admin
Posts: 11552
Joined: Tue Feb 17, 2009 9:16 pm
Location: São Paulo, Brazil
Contact:

Re: shadows in the freecad viewport

Postby yorik » Fri Aug 02, 2019 1:25 pm

pablogil wrote:
Wed Jul 31, 2019 8:19 am
module 'OfflineRenderingUtils' has no attribute 'embedLight'
This is because you don't have the latest version of this module... Either wait until your FreeCAD package gets updated, or grab from github (in src/Mod/Arch) and replace yours manually...
User avatar
bitacovir
Posts: 710
Joined: Sat Apr 19, 2014 6:23 am
Contact:

Re: shadows in the freecad viewport

Postby bitacovir » Mon Aug 05, 2019 12:40 am

I just found this video of a Blender addon with a visual render style for architecture, and it is very similar to what this shadow feature could be. Just wait for the house model in white and with shadows...
phpBB [video]
::bitacovir::
===================================
One must be absolutely modern.
Arthur Rimbaud (A Season in Hell -1873)

My Blog
Mini Airflow Tunnel Project
User avatar
yorik
Site Admin
Posts: 11552
Joined: Tue Feb 17, 2009 9:16 pm
Location: São Paulo, Brazil
Contact:

Re: shadows in the freecad viewport

Postby yorik » Mon Aug 05, 2019 11:48 pm

Great!! I still haven't played enough with Blender 2.80...
pablogil
Posts: 756
Joined: Wed Nov 26, 2014 3:19 pm
Location: Badajoz (Spain)
Contact:

Re: shadows in the freecad viewport

Postby pablogil » Tue Aug 06, 2019 9:01 am

yorik wrote:
Fri Aug 02, 2019 1:25 pm
This is because you don't have the latest version of this module... Either wait until your FreeCAD package gets updated, or grab from github (in src/Mod/Arch) and replace yours manually...
Once updated to the latest version of the module the script works perfectly in macOS:
Captura de pantalla 2019-08-06 a las 10.57.52.png
Captura de pantalla 2019-08-06 a las 10.57.52.png (65.13 KiB) Viewed 445 times
Pretty neat, I wish this can be refined into a workbench or tool so that it can be used as a new mode inside regular FreeCAD :D
Dark and Light stylesheets v2.0 to theme your FreeCAD UI, more information here
wmayer
Site Admin
Posts: 14887
Joined: Thu Feb 19, 2009 10:32 am

Re: shadows in the freecad viewport

Postby wmayer » Wed Aug 07, 2019 8:27 am

In the FreeCAD core system we have the command to render in different draw styles (no shading, wireframe, ...) and we could add it there.

However, at the moment the shadow function is a bit unrealistic because when you move or rotate the model then the shadow follows which means that the light source moves, too. However, the light source should be fix.

I guess this happens because the shadow group appears after the camera node in the scene graph and not before. This must be further checked...
pablogil
Posts: 756
Joined: Wed Nov 26, 2014 3:19 pm
Location: Badajoz (Spain)
Contact:

Re: shadows in the freecad viewport

Postby pablogil » Wed Aug 07, 2019 9:00 am

wmayer wrote:
Wed Aug 07, 2019 8:27 am
In the FreeCAD core system we have the command to render in different draw styles (no shading, wireframe, ...) and we could add it there.

However, at the moment the shadow function is a bit unrealistic because when you move or rotate the model then the shadow follows which means that the light source moves, too. However, the light source should be fix.

I guess this happens because the shadow group appears after the camera node in the scene graph and not before. This must be further checked...
Yes, I do agree it may be placed there.
Anyway, here in macOS the light source stays still when rotating so it works as expected.

Apart from that it would be great to get a GUI for tweaking the light position, casting angle, intensity, color and even ambience light intensity and color too =)
Dark and Light stylesheets v2.0 to theme your FreeCAD UI, more information here
User avatar
yorik
Site Admin
Posts: 11552
Joined: Tue Feb 17, 2009 9:16 pm
Location: São Paulo, Brazil
Contact:

Re: shadows in the freecad viewport

Postby yorik » Sat Aug 10, 2019 1:06 am

wmayer wrote:
Wed Aug 07, 2019 8:27 am
I guess this happens because the shadow group appears after the camera node in the scene graph and not before. This must be further checked...
I have the impression that there is a kind of default light, somewhere above the camera. If you rotate the scene up to a point that this default light would be close to the directional light defined in the shadow group, then we can see that the shadows gets quite consistent, which would prove the existence of that default light (this seems like astronomy proof :) ) I think I saw something somewhere in the code about such default light already... Maybe it needs to be turned off or kept very dim while using a directional light...

Another two things that need to be solved before adding it as a draw style:

- new view providers added after the mode has been turned on will be added outside the shadowgroup. Means they won't emit or receive shadows. Not sure how that can best be handled...

- We need something so the user can control the light direction and intensity (and maybe color), both with GUI and python access. But I'm going to do that anyway, I'll make sure it can be used FreeCAD-wide..
wmayer
Site Admin
Posts: 14887
Joined: Thu Feb 19, 2009 10:32 am

Re: shadows in the freecad viewport

Postby wmayer » Sat Aug 10, 2019 11:28 am