Python 3 Scripting
This section describes how to extend and add additional functionality by using the built in scripting tool.
- 1 Supported Use Case
- 2 Basic Concepts
- 3 Workflow
- 3.1 Setting the Python Interpreter
- 3.2 Adding a Dynamics Script extension to your content
- 3.3 Adding Fields
- 3.4 Writing a Dynamics Script's Code
- 3.4.1 Available Callbacks
- 3.4.1.1 Code template
- 3.4.1.1.1 Template
- 3.4.1.1 Code template
- 3.4.2 Available helper functions to write scripts
- 3.4.2.1 The Application Context
- 3.4.2.1.1 The Application Context
- 3.4.2.2 VxMath::Transformation
- 3.4.2.2.1 Transformation Helpers
- 3.4.2.3 Accessing Contact Data
- 3.4.2.3.1 Accessing Contacts
- 3.4.2.3.2 Contact Data
- 3.4.2.3.3 Part Data
- 3.4.2.1 The Application Context
- 3.4.3 Getting the full Vortex.py documentation
- 3.4.1 Available Callbacks
- 3.5 Using the Python Output Window
- 3.6 Example: Writing a simple script to automatically change the gears of a car
- 4 Accessing an Extension's Fields via Python
- 4.1 control.py
- 5 Converting Python 2 scripts to Python 3 Dynamics Script
- 6 Reference Information
Supported Use Case
The Dynamics Script Extension (Python 3) allows you to use the Python 3 programming language to modify the behaviour of dynamic elements of your simulation while it is running.
For example, if a mechanism consists of two parts that are constrained by a hinge, adding tension to that mechanism will not cause the hinge to break. However, a simple script that reads the tension and disables the constraint if it reaches a certain threshold will accurately simulate breaking the hinge.
A Dynamics Script is part of your content. The extension can be added to either a scene, mechanism or assembly document.
Basic Concepts
Python Interpreter Support
The Dynamics Script extension makes it easy to work with a Python 3 interpreter installed on your system. Vortex Studio comes bundled with a default interpreter but if you want to use third party packages or a different Python distribution you should point your Vortex application
towards an external interpreter that you previously installed.
The only restriction is that the supported Python version is Python 3.8. The default interpreter currently bundled with Vortex Studio is 3.8.6.
Workflow
(Optional) Setting the Python Interpreter
Adding a Dynamics Script extension to your content
Adding Fields
Writing a Dynamics Script's Code
Setting the Python Interpreter
Refer to the following documentation for additional information on a Vortex application's setup file and how to modify it.
Vortex provides a default Python Interpreter. If you need your own, you can specify it at the root level of a setup document, under the container Python 3.
See Python 3 Scripting - Python Usage and Environment Configuration
Adding a Dynamics Script extension to your content
Prerequisites: If you need to use another Python 3 interpreter than the one bundled with Vortex Studio, follow the steps labelled Setting the Python Interpreter above.
Launch the Vortex Studio Editor.
Open your content file or create a new assembly, mechanism or scene.
In the Toolbox panel, under the Scripting category, add a Dynamics Script extension.
In the Properties panel, set the path to the script you want to use to the parameter Script Path.
Your script will be automatically analyzed and any syntax error will be reported to you as warnings on the extension.
Every time, you edit and save your script in your code editor, it will be reimported into Vortex.
Adding Fields
The most direct way to communicate between Vortex and your Python script is to use fields (inputs, outputs and parameters) on the Dynamics Script extension. Both the Vortex application and the Python script can access those fields.
But before accessing fields you must first add them to the extension.
Using the Vortex Studio Editor
Right click on the dynamics script extension and select "Edit"
In the window that appears you can add inputs, outputs and parameters to your script.
There are 3 field containers on each scripts,
Inputs
Outputs
Parameters
in Python access the fields using the syntax
"extension.inputs.field_name"
"extension.outputs.field_name"
"extension.parameters.field_name"
See code below as an example accessing fields created earlier.
Accessing fields in Python 3
def pre_step(extension):
# The boolean input field in the screenshot above is named "boolean input"
# Notice that spaces are replaced by underscores
my_field = extension.inputs.boolean_input
# my_field is a boolean input, you can read its value like so
bool_value_of_my_field = my_field.value
if bool_value_of_my_field:
extension.outputs.an_output_double.value = 3.1415
else:
parameter_number = extension.parameters.number.value
extension.outputs.an_output_double.value = parameter_numberUsing Python Code only
Fields can be created directly from Python code if you prefer. The following code is functionally the same as the previous example.
Creating fields from a script in python 3
import Vortex
def on_simulation_start(extension):
# Creating an input of type boolean
extension.createInput("boolean input", Vortex.Types.Type_Bool)
# Creating an output of type double
extension.createOutput("an output double", Vortex.Types.Type_Double)
# Creating a parameter of type double
extension.createParameter("number", Vortex.Types.Type_Double)
def pre_step(extension):
# The boolean input field we created on simulation start is named "boolean input"
# Notice that spaces are replaced by underscores
my_field = extension.inputs.boolean_input
# my_field is a boolean input, you can read its value like so
bool_value_of_my_field = my_field.value
if bool_value_of_my_field:
extension.outputs.an_output_double.value = 3.14159265359
else:
parameter_number = extension.parameters.number.value
extension.outputs.an_output_double.value = parameter_numberMore information on creating and getting fields in python
The functions createInput(), createOutput() and createParameter() are utility functions each encapsulating a frequent use case of this API. There is several things to mention about these functions.
The new functions have the following signatures
create field functions signatures
def createInput(name, type, defaultValue = None, description = None, physicalDimension = None) def createParameter(name, type, defaultValue = None, description = None, physicalDimension = None) def createOutput(name, type, defaultValue = None, description = None, physicalDimension = None)The available types for the parameter
typeare inVortex.Typesenumeration. If this parameter is an invalid value, this function will fail.The available types for the parameter
physicalDimensionare inVortex.VxPhysicalDimensionenumeration. By default, Vortex will assign a value corresponding toVxSim.VxPhysicalDimension.kNoneto fields whose physical dimension are not defined. This means the functiongetPhysicalDimension()will never returnNoneeven if explicitly declared in the call tocreateInput(),createOutput()orcreateParameter(). This behavior also is consistent with theaddInput(),addParameter(),addOutput()functions.The functions
createInput()andcreateParameter()ignore the parameterdefaultValueif the field already exists. This allows experimenting with different values in the editor's interface without having to change the script.The function
createOutput()reset the output field's value todefaultValueeven if the field already exists. This behavior is different fromcreateInput()andcreateParameter().