Personal tools
You are here: Home Forum Use Dump view within a python script.

Dump view within a python script.

Up to Use

Dump view within a python script.

Posted by JohnNaysmith at October 25. 2018

Hello,

I generate all of my salome models using python scripting. Once a model is generated I save the .hdf file and exit, but before exiting I would like to generate some .PNG images of the model from various angles. In order to achieve this I would have to do the following.

  1. Show and hide the correct objects in the study.
  2. Set the view angle either by specifying angles or axis orientation e.g. "-OX, +OX, -OY"  etc.
  3. Dump the view.
 
At the moment I am doing all of this manually in the GUI and it is very time consuming.
 
I am not sure where I should look for such information in the documentation, so please would you also explain what part of Salome would handle such functionality and which part of the documentation this might be in.
 
I fear that this is not possible using Python scripting and that there is nothing in the docs. It would be great if I am wrong!
 
Thank you
 
John

Re: Dump view within a python script.

Posted by JohnNaysmith at October 25. 2018

I am sorry but I cannot edit my first post.

I would like to make clear the following; I would like my Python script to generate the model, save the .hdf, dump the view and then exit.

What I do at the moment is generate the model, save a .hdf and exit. Then I manually reopen the .hdf file using the GUI, make some volumes visible, change viewing angles and zoom in/out, dump the view to PNG, then exit.

If this process of making images could be added into the python script that would be great. I generate many separate models and I would like the visualization step to be automated too.

Re: Dump view within a python script.

Posted by Saint Michael at October 25. 2018

Hi John,

To get python commands to set up GUI and Viewers, activate "Save GUI state" option in Dump Study dialog.

Commands to dump the View are

import SalomePyQt

sg = SalomePyQt.SalomePyQt()

sg.dumpView(file_name) # dumps contents of  the currently active view into image file, format is automatically deduced from the file name's extension
# or

sg.dumpView(file_name, viewer_id) # dump contents of viewer that has id "viewer_id"; see SalomePyQt API about how to manage views


St.Michael

 

Re: Dump view within a python script.

Posted by JohnNaysmith at October 25. 2018

Thank you, I will try this ASAP.

What is the command to show or hide a specific object in the view?

Re: Dump view within a python script.

Posted by Saint Michael at October 25. 2018

You will find it in the dump script.

St.Michael

Re: Dump view within a python script.

Posted by JohnNaysmith at October 25. 2018

OK I see now.

I tried it. All of the iparameters stuff from the dump file seems to be working for me. However, the sg.dumpView(file_name) part does not write a file and actually returns False. I suspect I have to do sg.dumpView(file_name, viewer_id) instead, so I will need the viewer_id.

Forgive my ignorance, but I have looked through the pages at http://docs.salome-platform.org/latest/ and I cannot find any reference to viewer_id or SalomePyQt. Which part of the docs can it be found in?

Re: Dump view within a python script.

Posted by Saint Michael at October 25. 2018
St p, li { white-space: pre-wrap; }

For me it works:

 >>> sg.dumpView("/tmp/a.png")
True

 See also http://docs.salome-platform.org/latest/tui/GUI/classSalomePyQt.html#a484b2aa670bbb23f75968c7ab55d2cfc

 St.Michael

Re: Dump view within a python script.

Posted by JohnNaysmith at October 26. 2018

Still not working.

I do

>>> a = sg.getActiveView() 
>>> print("Active view: ")
>>> print(a)
-1

Does this mean there is no active view?

Re: Dump view within a python script.

Posted by Saint Michael at October 26. 2018

Does this mean there is no active view?

Yes

Re: Dump view within a python script.

Posted by JohnNaysmith at November 02. 2018

I included everything from the dump file. What do I need to do to make sure there is an active view when running on the command line?

Re: Dump view within a python script.

Posted by Saint Michael at November 02. 2018

I think an acitve view is set by the following command, VTK view for example

ipar.setProperty("AP_ACTIVE_VIEW", "VTKViewer_0_0")

St.Michael

Re: Dump view within a python script.

Posted by JohnNaysmith at November 06. 2018

OK that's interesting. I am learning more about the internals of Salome but I find the available documentation to be quite sparse.

I am in the GEOM module (using OCC 3D viewer), so my dump script contained

>>> ipar.setProperty("AP_ACTIVE_VIEW", "OCCViewer_0_0")

I have added this to my own script file already but, there still seems to be no active view.

Anyway, I decided to create a simple .py script example, which I am attaching to this post.

The output of this script is as follows:

------
RunStudy
Searching /myStudyManager in Naming Service + found in 0.5 seconds
Searching /Containers/maxwell/FactoryServer in Naming Service +Warning, no type found for resource "localhost", using default value "single_machine"
+ found in 1.0 seconds
Start SALOME, elapsed time :   2.7 seconds
createNewStudy
extStudy 1
ACTIVE VIEW: OCCViewer_0_0
Visibility: On
Salome.sg does not have the desktop
Dumping image...
Active view:  -1
Dump view return value: False
Dumped image!
------

Is there some way to fix this? I do not understand enough about the inner working of salome to get any further on my own.

Thank you.

Attachments

Re: Dump view within a python script.

Posted by Saint Michael at November 06. 2018

As far as I can guess the visual params are restored here:

iparameters.getSession().restoreVisualState(1)

St.Michael

 

Re: Dump view within a python script.

Posted by JohnNaysmith at November 06. 2018

 iparameters.getSession().restoreVisualState(1)

I tried that and unfortunately I get the following error:

Traceback (most recent call last):
  File "/home/user/CODE/spring/salome/example_dumpview.py", line 73, in <module>
    iparameters.getSession().restoreVisualState(1)
  File "/home/share/SALOME/salome_8.4.0/BINARIES-CO6/KERNEL/lib/python2.7/site-packages/salome/iparameters.py", line 49, in getSession
    vp_session = vp_session._narrow(SALOME.Session)
AttributeError: 'NoneType' object has no attribute '_narrow'
ERROR:salomeContext:SystemExit 1 in method _runAppli.

Re: Dump view within a python script.

Posted by Saint Michael at November 06. 2018

Do you launch SALOME in GUI mode?

 

Re: Dump view within a python script.

Posted by JohnNaysmith at November 07. 2018

No, I tried to make that clear in my earlier posts. I am calling the above script on the command line using `salome -t -b <scriptname>.py  `
I want to generate images on the fly while building my models. These scripts are running headless on a HPC cluster so I do not want to launch the GUI.

Is it possible?

Thanks for your time!

Re: Dump view within a python script.

Posted by Saint Michael at November 07. 2018

I see from experiments that all I suggested works in GUI mode only :(       salome <scriptname>.py

St.Michael

Re: Dump view within a python script.

Posted by JohnNaysmith at November 08. 2018

OK no problem, thank you for your time and confirming that what I want is not possible.

As a workaround, I will probably do this in GUI mode for specific geometries of interest.

Regards,

John

Powered by Ploneboard
Document Actions