How to add a python application into a loop and access the loop variable within the python script?

Considering the following loop

How could the python script access the variable PID ?

Thanks for the help !

Damien

  • There is no direct way of doing this but there is a simple solution. You can add a PILOT program to print out a Python file (..py) with the variables, and then import this into your Python script.

    PILOT program code

    PRINT LIST='a= 100' FILE="variables.py"
    PRINT LIST='b= "cube"' FILE="variables.py"

    Python code to import variables

    from variables import *
    
    peint(a)
    print(b)

    Hope this helps!

  • As an alternative, you can also pass the token directly as a sys.argv to your Python script.

    For example, in a more general case, let's call the variable "loop_variable" defined as below (just as an example, in a Pilot program or within the Loop circle properties):

    loop_variable = 12

    Your Python command line could look like the below in a Pilot script direclty:

    *"C:\Program Files\Python311\python.exe" "{CATALOG_DIR}\Applications\python_script.py" @loop_variable@


    Then, your Python script will look like the below (just an example):

    import sys
    
    loop_variable = sys.argv[1]
    
    with open("D:/some_path/Base/out_txt.txt", "w") as txt_file:
        print(loop_variable, file=txt_file)

    This will print out 12 in your output text file.

  • Hi Ahmed

    Thanks for the reply, this isn't what I was expecting but your answer provided a good tip.

    I used a pilot to print a data file with the PID value, and added this file as an input in the python script

    And it works!

    Thanks :)

    Damien