Give an argument to an script run from the python console of salome

Hello,
how can I give an argument to a script that I want to run in salome (python interface inside salome GUI)?
when I run a script from the gui in the console is runned the following command:

exec(compile(open('/pathToScript/script.py', 'rb').read(), '/pathToScript/script.py', 'exec'))

but lets say I want to give a path to it, how can I achieve this?

form the terminal using salome, it would be something like this

salome -t /pathToScript/script.py arg0

and then in the script itself I would have:

import sys
try:
     path=sys.arg[0]
except:
     path='/home'

thanks in advance

If you want to pass arguments to a script you can do this:
salome myscript.py args:arg0,arg1

Then in the script sys.argv[0] is the name of the script and sys.argv[1:] the rest of the arguments.
See the example attached to create a block with 3 parameters:

salome block.py args:20,50,30

block.py (1010 Octets)

1 Like

Hello fred,
giving arguments to a script from the terminal wasn’t the issue (I excuse myself if I wasn’t clear enough I my original post).
what I am looking to do is be able to pass arguments when in the GUI we do file/load script…

this will run in the salome python console the following function:
exec(compile(open('/pathToScript/script.py', 'rb').read(), '/pathToScript/script.py', 'exec'))
I wanted to be able to modify this line to be able to give arguments to the script.py

I know that I can give them when running in the terminal (thanks to one of your previous posts actually!) but it is not what I am looking for right now. as I have a script that I select an object in the interface, and want to run it so it export some parts of it and want to give as input the path to where to export.

From the Python Console of SALOME you can pass the arguments like this:
exec(open(“C:\SALOME-9.12.0\block.py”, “rb”).read(), args=(“20”,“30”,“40”))

But if you need to retrieve the selection then it is different.
You probably should try to create a plugin instead and you will be able to create a small GUI to get the needed parameters.

1 Like

thanks for your reply,
for retrive the selection is quite easy (in case you might need it):

import salome
obj0 = salome.IDToObject(salome.sg.getSelected(0))

regards

Thanks :slight_smile: to complete this work for GEOM & SMESH