OpenRocketをPythonから呼び出す

ここを参考に。orhelperを使う。
wiki.openrocket.info
自分の環境では以下が必要だった。

import matplotlib
matplotlib.use('Qt5Agg')

以下のサンプルコードsimple_plot.pyでは、OpenRocket-15.03.jarとsimple.orkをカレントディレクトリにおくことを想定している。

cp /Applications/OpenRocket.app/Contents/Java/OpenRocket-15.03.jar .
 cp <Openrocket source dir>/swing/resources/datafiles/examples/A\ simple\ model\ rocket.ork simple.ork
import os
import numpy as np
from matplotlib import pyplot as plt
import matplotlib
matplotlib.use("Qt5Agg")
import orhelper
from orhelper import FlightDataType, FlightEvent

with orhelper.OpenRocketInstance() as instance:
    orh = orhelper.Helper(instance)
    dir_path = os.path.dirname(os.path.realpath(__file__))
    file_path = os.path.join(dir_path,'simple.ork')
    doc = orh.load_doc(file_path)
    sim = doc.getSimulation(0)
    orh.run_simulation(sim)
    data = orh.get_timeseries(sim, [FlightDataType.TYPE_TIME, FlightDataType.TYPE_ALTITUDE, FlightDataType.TYPE_VELOCITY_Z])
    events = orh.get_events(sim)
    events_to_annotate = {
        FlightEvent.BURNOUT: 'Motor burnout',
        FlightEvent.APOGEE: 'Apogee',
        FlightEvent.LAUNCHROD: 'Launch rod clearance'
    }
    fig = plt.figure()
    ax1 = fig.add_subplot(111)
    ax2 = ax1.twinx()
    ax1.plot(data[FlightDataType.TYPE_TIME], data[FlightDataType.TYPE_ALTITUDE], 'b-')
    ax2.plot(data[FlightDataType.TYPE_TIME], data[FlightDataType.TYPE_VELOCITY_Z], 'r-')
    ax1.set_xlabel('Time (s)')
    ax1.set_ylabel('Altitude (m)', color='b')
    ax2.set_ylabel('Vertical Velocity (m/s)', color='r')
    change_color = lambda ax, col: [x.set_color(col) for x in ax.get_yticklabels()]
    change_color(ax1, 'b')
    change_color(ax2, 'r')
    index_at = lambda t: (np.abs(data[FlightDataType.TYPE_TIME] - t)).argmin()
    for event, times in events.items():
        if event not in events_to_annotate:
            continue
        for time in times:
            ax1.annotate(events_to_annotate[event], xy=(time, data[FlightDataType.TYPE_ALTITUDE][index_at(time)]),
                         xycoords='data', xytext=(20, 0), textcoords='offset points',
                         arrowprops=dict(arrowstyle="->", connectionstyle="arc3"))
    ax1.grid(True)
    plt.show()

f:id:seinzumtode:20211010042837p:plain