Getting Started STK COM integration using Python


QuestionHow do I use Python to drive STK?

AGI recommends using IPython with an IDE such as Spyder or Jupyter Notebook, which can be downloaded for free with the Anaconda Python installer:

Note:  You will need an STK Integration license to integrate Python with STK.
After installation completes, launch the Anaconda navigator, then Launch Jupyter notebook (spyder is also a great IDE, more similar to MATLAB).  Type in the following code to connect to STK:

from win32api import GetSystemMetrics
import comtypes

# Start new instance of STK
from comtypes.client import CreateObject
uiApplication = CreateObject('STK11.Application')
uiApplication.Visible = True

# Get reference to running STK instance
from comtypes.client import GetActiveObject

# Get our IAgStkObjectRoot interface

#Note: When 'root=uiApplication.Personality2' is executed, the comtypes library automatically creates a gen folder that contains STKUtil and STK Objects. After running this at least once on your computer, the following two lines should be moved before the 'uiApplication=CreateObject("STK11.Application")' line for improved performance.  

from comtypes.gen import STKUtil
from comtypes.gen import STKObjects

Alternatively, you can use standard python with the win32com library, which won't have intellisense.  To install the win32com library type "pip install win32com" from a command prompt.

import win32com.client
# Start new instance of STK
uiApplication = win32com.client.Dispatch('STK11.Application')
uiApplication.Visible = True

# Get reference to running STK instance
uiApplication = win32com.client.GetActiveObject('STK11.Application')
uiApplication.Visible = True

# Get our IAgStkObjectRoot interface
root = uiApplication.Personality2

You can now create your scenario and automate STK using the Python code snippets:

For a more comprehensive overview of STK Integration with Python, please visit the following github link:

Resource File 1 
Resource File 2