Jump to content

How to Call a GOM User Script which Accepts Inputs Using External Python Program


---
 Share

Recommended Posts

Hi, 

I am using GOM Professional 2018 and is able to launch it using an external python program as shown below. How can I pass an input to the user script "test_function"? Can anyone provide a snippet of the code? There is a discussion thread which shows this can be done but I tried and have no luck. 

 

import sys
import subprocess

def main():
    
    subprocess.run(
        [
            "C:/Program Files/GOM/2018/bin/start_gom.exe",
            "gom_inspect",
            "PRO",
            "-eval",
            "gom.script.userscript.test_function()",
        ],
        
    )

if __name__=="__main__":
    main()

Test.py

Edited
Link to comment
Share on other sites

Hi,

I have modified your script Test.py as follows:

import sys
import subprocess

def main(point_name = "Point 2"):
    parameters = dict(point_name=point_name)
    subprocess.run(
        [
            "start_gom.exe",
            "gom_inspect",
            "PRO",
            "-eval",
            "gom.script.userscript.test_function(parameters = " + str(parameters) + ")",
        ],
        
    )

if __name__=="__main__":
    if len(sys.argv) > 1:
        main(sys.argv[1])
    else:
        main()

Lets assume that test_function.py contains the following code which will create a new project and create a simple point with a name which is taken from the global variable point_name:


import gom

if 'point_name' not in globals():
	point_name = "Point 1"

gom.script.sys.create_project ()
gom.script.part.create_new_part (name='Part')
gom.script.primitive.create_point (name=point_name, point=gom.Vec3d (0.0, 1.0, 2.0))

Now you have three possibilities to run this script test_function.py:

1. View the file test_function.py in the GOM script editor and press the play button. A new project will be created and a new point with the name "Point 1".

2. The new point will be named "Point 2" if you issue the following command line in your terminal:

python Test.py

3. You can specify the point name as "Point 3" if you add extend your command line invocation as follows:

python Test.py "Point 3"

 

I hope this works for you.

Link to comment
Share on other sites

  • 9 months later...

Hello Michael,

I encountered  a similar problem when launching user gom script through external python script. I can launch it with using your code structure but could not be able to access the passed variable in user script? Checking in globals() doesn't work for me and also tried to access using sys.argv but no luck.

Do you have any other suggestions?

Thanks,

Canset

Link to comment
Share on other sites

 Share

×
×
  • Create New...