Make New Python System - SimDynamX/SpaceTeamsPRO GitHub Wiki
| Simulation Development Workflow → Make a New Python System |
|---|
Up to date for Platform 0.54.2
Written by Connor Jakubik
Prerequisites:
Make a New Python System
- Copy
Compute_Server/packages/Template_Python/Template_Python.py(in the STPro install directory) and paste the copy somewhere. - Rename the file.
- The boilerplate code that is at the top of the template python script needs to be retained. This is what it might look like:
import os, sys, time, datetime, traceback
import spaceteams as st
import numpy as np
# Can import other modules here, but any code that actually executes before st.connect_to_sim()
# will have less helpful logging and error handling. SimGlobals and logger functions are not yet available.
def before_init():
pass # Replace this to run code during the Sync state that needs to finish before Init starts anywhere in the sim.
# Sync state is where Sim Editing is done, so code in here shouldn't change parameters that would
# make saving and reloading the sim problematic.
def before_runtime():
pass # Replace this to run code during the Init state that needs to finish before Runtime starts anywhere in the sim.
#################################################################
# DON'T CHANGE ANY OF THE BELOW; NECESSARY FOR JOINING SIMULATION
def custom_exception_handler(exctype, value, tb):
error_message = "".join(traceback.format_exception(exctype, value, tb))
st.logger_fatal(error_message)
exit(1)
sys.excepthook = custom_exception_handler
st.BeforeInit(before_init)
st.BeforeRuntime(before_runtime)
st.connect_to_sim(sys.argv)
# DON'T CHANGE ANY OF THE ABOVE; NECESSARY FOR JOINING SIMULATION
#################################################################
- Explanation of boilerplate code here:
- It's very important that this code stays intact, and that no non-function lines of python code exist before it in your file. Otherwise, hard-to-debug issues may happen due to code executing before the exception handler has a chance to pass the exception to the log file.
import spaceteams as stimports the Space Teams Python API module. This will only work with the python interpreter we bundle with Space Teams, or exactly matching versions.- The
before_init()code is waited-on before the sim is allowed to continue toInitSimState. This code is ran "during" thest.connect_to_simcall. - The
before_runtime()code is waited-on before the sim is allowed to continue toRuntimeSimState. This code is ran "during" thest.connect_to_simcall. custom_exception_handler- Hooks unhandled exceptions tost.logger_fatal(). When code crashes, the full Python traceback is routed into the active session log in/AppData/Local/SimDynamX/STPro/Logs/as a synchronized log message. Without this, python print and exception outputs are still routed to their respective log files, but may have bad behavior causing them to be interleaved with other output, or other sim backend issues.st.connect_to_simconnects the python process to the STP sim.
- You can modify any lines below this boilerplate.
- Code can call
st.leave_sim()to exit cleanly, orst.stop_sim()to stop all other portions of the sim as well.
Next Steps
- See the Programming STP Sims with Python tutorial.
- Consult the Python API Docs for functionality details.