Create abstract objects represented with icons

Here's the place for discussion related to coding in FreeCAD, C++ or Python. Design, interfaces and structures.
usbhub
Posts: 280
Joined: Tue Apr 02, 2019 8:22 pm
Location: Earth

Create abstract objects represented with icons

Postby usbhub » Sun Mar 01, 2020 4:51 pm

Hello,
first I hope, I'm in the right subforum :?

I'm coding on a solution for renderings in FreeCAD and I want to insert objects like cameras and lights. I looked for how to create objects and I understood how to do that with python. But that were all "real", 3D objects, but camera or lights are more abstract. My idea was to represent them in the scene/model via icons. I attached a screenshot were I added a few icons (only quickly with GIMP) to make my idea more descriptive.
Like you can see, I want to add also (dotted) lines to show e.g. the place, where the camera is focused on.

PS: Icon used Designed by https://www.flaticon.com/authors/srip from www.flaticon.com
Icon used Designed by https://www.flaticon.com/authors/those-icons from www.flaticon.com
Attachments
FreeCAD_icons.png
FreeCAD_icons.png (54.28 KiB) Viewed 358 times
vocx
Posts: 4001
Joined: Thu Oct 18, 2018 9:18 pm

Re: Create abstract objects represented with icons

Postby vocx » Mon Mar 02, 2020 5:08 am

usbhub wrote:
Sun Mar 01, 2020 4:51 pm
I'm coding on a solution for renderings in FreeCAD and I want to insert objects like cameras and lights. ...
You have to use Coin. Coin is the library that actually "draws" things in the 3D view. The regular objects that you see in the tree view are complex App_DocumentObjects of FreeCAD. If you just want to show something on the screen, you don't need to create a document object, you just insert the object in the scene through Coin.

In FreeCAD you can dynamically create these objects using the Pivy library, which provides Coin for Python. See Pivy.

Code: Select all

from pivy import coin
All the functions that have So..., like SoNode, SoSeparator, SoText, SoImage, etc. are Coin classes. All those are documented in C++. Pivy, the Pyton version of Coin, doesn't really have documentation, so you need to refer to the C++ documentation.

https://grey.colorado.edu/coin3d/index.html

----

Many functions and classes in the Draft Workbench internally use pivy.coin, so take a look around in Draft.py and DraftTools.py.

However, with all that said, I think you need some way to reposition the light sources and the camera, therefore, I think having a real App_DocumentObject representing those elements would be good.

Also, did you already try the ArchTexture workbench, or the Render Workbench? I think furti precisely added light sources and cameras to one of these workbenches: Lights in FreeCAD.
Always add the important information to your posts if you need help.
To support the documentation effort, and code development, your donation is appreciated: paypal.
usbhub
Posts: 280
Joined: Tue Apr 02, 2019 8:22 pm
Location: Earth

Re: Create abstract objects represented with icons

Postby usbhub » Tue Mar 03, 2020 5:09 pm

vocx wrote:
Mon Mar 02, 2020 5:08 am
...
Cool, thank you very much for your answer!

I'll take a closer look on coin&pivy and get back to you.

My plan is inspired by Kerkythea, where they use a similar system for representing.
usbhub
Posts: 280
Joined: Tue Apr 02, 2019 8:22 pm
Location: Earth

Re: Create abstract objects represented with icons

Postby usbhub » Sat Mar 07, 2020 7:16 pm

vocx wrote:
Mon Mar 02, 2020 5:08 am
ping
Hello,
now I'm trying to insert a image to the scenegraph. So I searched through the linked site and found SoImage. But now I don't know what to do. My problem is, how to tell pivy what picture I want to use. I think, I have to add a node for that, right (like a SoBaseColor for the example cube)? But how? I don't understand the C++ example at all, how it gets the image. It looks like the image is created in the code (looks a little like mandelbrot set, because the name "mandel"), but what if I want to read a file and use that image?

Thank you a lot in advance!
vocx
Posts: 4001
Joined: Thu Oct 18, 2018 9:18 pm

Re: Create abstract objects represented with icons

Postby vocx » Sat Mar 07, 2020 7:41 pm

usbhub wrote:
Sat Mar 07, 2020 7:16 pm
I don't understand the C++ example at all...
Which example?

Hey, man, I give you straight links to the info you need, and you don't give me a link. I can't guess like that. Not cool.

See the source code in FreeCAD, src/Mod/Draft/draftutils/gui_utils.py, in the function load_texture.

https://github.com/FreeCAD/FreeCAD/blob ... #L562-L623

And also src/Mod/Draft/Draft.py, class _ViewProviderDraft, method onChanged.

https://github.com/FreeCAD/FreeCAD/blob ... 3212-L3246
Always add the important information to your posts if you need help.
To support the documentation effort, and code development, your donation is appreciated: paypal.
usbhub
Posts: 280
Joined: Tue Apr 02, 2019 8:22 pm
Location: Earth

Re: Create abstract objects represented with icons

Postby usbhub » Sat Mar 07, 2020 11:44 pm

vocx wrote:
Sat Mar 07, 2020 7:41 pm
usbhub wrote:
Sat Mar 07, 2020 7:16 pm
I don't understand the C++ example at all...
Which example?

Hey, man, I give you straight links to the info you need, and you don't give me a link. I can't guess like that. Not cool.

See the source code in FreeCAD, src/Mod/Draft/draftutils/gui_utils.py, in the function load_texture.

https://github.com/FreeCAD/FreeCAD/blob ... #L562-L623

And also src/Mod/Draft/Draft.py, class _ViewProviderDraft, method onChanged.

https://github.com/FreeCAD/FreeCAD/blob ... 3212-L3246
Sorry I forgot to include the links. I hope I'll remember that in the future :oops:

I uses this tutorial to learn the basics of pivy/coin: https://wiki.freecadweb.org/Pivy
There was this site linked, a list of the classes: https://coin3d.bitbucket.io/Coin/annotated.html
I searched a bit through the classes and found SoImage: https://coin3d.bitbucket.io/Coin/classSoImage.html. On this site is also the C++ example. I was confused, that the image was created pixel by pixel.

I looked through your linked files and now a lot is more clear for me. An image have to be converted into pixels before giving it to pivy. I will take a closer look into the code and will be propably back with new questions ;)

I hope, I corrected the biggest things…
User avatar
Chris_G
Posts: 1307
Joined: Tue Dec 31, 2013 4:10 pm
Location: France
Contact:

Re: Create abstract objects represented with icons

Postby Chris_G » Sun Mar 08, 2020 8:32 am

Code: Select all

from pivy import coin
sg = Gui.ActiveDocument.ActiveView.getSceneGraph()
image = coin.SoImage()
image.filename = "/path/to/image.png"
sg.addChild(image)
SoImage.png
SoImage.png (12.83 KiB) Viewed 154 times
usbhub
Posts: 280
Joined: Tue Apr 02, 2019 8:22 pm
Location: Earth

Re: Create abstract objects represented with icons

Postby usbhub » Tue Mar 10, 2020 6:21 pm

Chris_G wrote:
Sun Mar 08, 2020 8:32 am
Many, many thanks, vocx and Chris_G! You are my heroes :)

Now it works and I also managed to use it for the abstract objects. A small screenshot is attached.
Attachments
FreeCAD_PointLight.png
FreeCAD_PointLight.png (31.06 KiB) Viewed 108 times