This is step 4 in the "Troubleshooting Your STK and Python Integration " FAQ article. Please check out that article first before running through these steps to get your Python working with STK.Note: For STK versions 12.2 and earlier, you need an STK Integration license to connect to STK with Python.
Note: Python 3.7.6 does not work with comtypes, and therefore you cannot connect to STK with comtypes on this version.
The assumption is that you are experiencing an error trying to connect to STK using comtypes.
import comtypes
from comtypes.client import CreateObject
app = CreateObject('STK12.Application') # Possible error
app.Visible = True
root = app.Personality2 # Possible error
If so, there is probably an issue with the Python wrapper files located in the comtypes gen folder.
Run the following code to determine your comtypes gen folder:
import comtypes.client
print(comtypes.client.gen_dir, '\n')
Copy the file path into the address bar of a File Explorer window, which you can access with the folder icon on the task bar. You will see multiple files; these are the files that allow Python to talk to STK.
Figure 1. Python wrapper files, as seen in Windows File Explorer
If you cannot connect to STK, consider those with a filename that contains a GUID (which looks like a random sequence of numbers and letters). These may not have been generated properly and are probably empty. You can verify that this is the problem by opening the files in a text editor and reviewing the contents. Regenerate any empty files. In general, you will always have to regenerate these files between major releases (e.g., STK 11, STK 12, etc.) of STK.
- To begin regenerating any empty files, delete all the contents in this folder.
- Now rerun.
import comtypes
from comtypes.client import CreateObject
app = CreateObject('STK12.Application') # Possible error
app.Visible = True
root = app.Personality2 # Possible error
- When "app = CreateObject('STK12.Application')" and "root=uiApplication.Personality2" are executed, the comtypes library automatically creates the gen folder that contains the Python wrappers for the STK libraries.
- You should see multiple outputs saying "# Generating comtypes.gen. xxxxx", and the contents of the gen folder will be populated.
- If you are trying to access the Astrogator library specifically, then you will also need to run the following line:
comtypes.client.GetModule((comtypes.GUID("{090D317C-31A7-4AF7-89CD-25FE18F4017C}") ,1,0))
- Once the wrapper files have been generated correctly, you can import them directly:
from comtypes.gen import STKObjects
from comtypes.gen import STKUtil
from comtypes.gen import AgSTKVgtLib
from comtypes.gen import AgSTKGraphicsLib
from comtypes.gen import AgStkGatorLib
from comtypes.gen import AgUiApplicationLib
from comtypes.gen import AgUiCoreLib
If you still cannot connect to STK, double-check the permissions of the gen folder and make sure the Properties->Security is Full Control. If trying to connect still doesn't work, you can download the attached zip folder for STK 12, extract the contents, and paste them into your comtypes gen folder.