Python wrapper#

This section describes how to generate and use the Python wrapper (see Python wrapper).

The QuadFlightControl example is used to illustrate the use of the Python wrapper service.

Note

The QuadFlightControl example is not completely supported by the Python wrapper service. The following code presents only how to use the service. A simpler Scade One project should be used to generate a Python wrapper according to service support (see Python proxy).

Generation#

The Python wrapper can be generated using the PythonWrapper class or the PyScadeOne command line.

The code generation job must be executed before generating the Python wrapper. The job execution can be performed via the Scade One IDE or the PyScadeOne job service (see Project jobs).

The Python wrapper produces a Python class that wraps the root operators of the Scade One model. The generated class can be found in the same output directory of the code generation job.

The PythonWrapper class use#

The following script shows how to generate the Python wrapper using the PythonWrapper class:

# Copyright (C) 2024 - 2025 ANSYS, Inc. and/or its affiliates.
# SPDX-License-Identifier: MIT
#
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in all
# copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.

from pathlib import Path
from ansys.scadeone.core import ScadeOne
from ansys.scadeone.core.svc.wrapper import PythonWrapper


# Update according to your installation
s_one_install = Path(r"C:\Scade One")

quad_flight_project = (
    s_one_install / "examples/QuadFlightControl/QuadFlightControl" / "QuadFlightControl.sproj"
)

# Job name
job_name = "CodeGeneration"

# Wrapper name
out_name = "flight_control_wrapper"

# Wrapper target directory
target_dir = Path(__file__).parent

app = ScadeOne(install_dir=s_one_install)
prj = app.load_project(quad_flight_project)

# Load and run the Code Generation job
prj.load_jobs()
cg_job = prj.get_job(job_name)
result = cg_job.run()
assert result.code == 0

# Generate the Python wrapper
gen = PythonWrapper(project=prj, job=job_name, output=out_name, target_dir=target_dir)
gen.generate()

Command line use#

The Python wrapper service can be built using the PyScadeOne command line as follows:

pyscadeone pycodewrap --install_dir "<s_one_install>" --job "CodeGeneration" --out_name "opt_wrapper"
    "<s_one_install>/examples/QuadFlightControl/QuadFlightControl/QuadFlightControl.sproj"

For more details on command line usage, consult Python wrapper command line.

Usage#

Once the Python wrapper is generated, it can be used to interact with the Scade One model in Python. The following example shows how to use the Python wrapper to interact with the QuadFlightControl model:

# Copyright (C) 2024 - 2025 ANSYS, Inc. and/or its affiliates.
# SPDX-License-Identifier: MIT
#
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in all
# copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.

import matplotlib.pyplot as plt
import project.jobs.code_gen.out.code.flight_control_wrapper as flight_control_wrapper
from sources import Ramp

flight_control = flight_control_wrapper.QuadFlightControl()

flight_control.reset()

flight_control.inputs.isReset = False
flight_control.motorStates = (False, False, False, False)
flight_control.inputs.desiredAttitude = (0.0, 0.0, 0.0)
flight_control.inputs.currentAttitude = (0.0, 0.0, 0.0)

cycles = range(20)
ramp = Ramp(0.1, cycles)
frontLeftRotor_sim = []
for cycle in cycles:
    flight_control.inputs.desiredVerticalAccel = ramp[cycle]
    flight_control.cycle()
    frontLeftRotor_sim.append(flight_control.outputs.rotorCmd.frontLeftRotor)

plt.plot(cycles, frontLeftRotor_sim)
plt.show()