Launching tcl scripts with arguments from python
I am coding an automation process using python as a main script. I would like to call up a .tcl script from python. I know I can use hw.evalTcl() to launch tcl scripts, but I need to give my tcl script input arguments, so I am running it using
subprocess.run( [tclshell_exe, my_tcl_script.tcl, py_arg1, py_arg2], capture_output=True, text=True )
tclshell_exe points to the tclsh85.exe located in the Altair Hyperworks directory.
Standard tcl commands such as "puts" work fine, while HM sepcific functions return errors. Note that no command output is shown in the Hypermesh Tcl command window.
Is using subprocess the optimal way for the tcl script call up?
I imagine a workaround would be to start with a .tcl script as the main script, where I call up python scripts and other tcl scripts, avoiding the argument assignment issue. The idea is to eventually have the whole script in python, and currently some tcl commands are not optimal to run in python.
Find more posts tagged with
Thanks for your reply Michal, this is a cleaner way than the workaround I eventually used. I used evalTcl to set my tcl variables then called the script through evaltcl again.
for name, value in zip(data_tcl_string, data_py_values):
hw.evalTcl(f'set {name} "{value}"')
hw.evaltcl(f"source {my_script.tcl})
Best regards,
Alaa
Hi @Alaam
Subprocess will spin-off a completely independent instance of the Tcl interpreter which will not be aware of the HyperMesh session and its APIs.
Since Tcl's source does not support passing arguments to the script, probably the easiest way would be to restructure your code in a way that you can pass the arguments to a top-level function which then calls the rest of our code (see screenshot below).

Hope this helps.
Thanks,
Michal Stefuca
Automation & Customization
Hi @Alaam
Subprocess will spin-off a completely independent instance of the Tcl interpreter which will not be aware of the HyperMesh session and its APIs.
Since Tcl's source does not support passing arguments to the script, probably the easiest way would be to restructure your code in a way that you can pass the arguments to a top-level function which then calls the rest of our code (see screenshot below).
Hope this helps.
Thanks,
Michal Stefuca
Automation & Customization