Calc Results by Python

You are viewing an old version of the documentation. You can switch to the documentation of the latest version by clicking the top-right corner of the page.

Description

This Step runs a user-defined script through Python and outputs the calculation results to Mech-MSR.

The characteristics of this Step are as follows:

  • Support multi-threading.

  • Load Python script in real time.

  • Support various data type conversions when transferring data between C++ and Python.

  • Allow for the display of the Python’s log in the log panel of Mech-MSR.

Usage Scenario

When a custom calculation is required, this Step can be used to run the user-defined Python script and the measurement solution can be simplified.

Input and Output

  • Input: determined by the data type input in the Input Ports parameter.

  • Output: determined by the data type input in the Output Ports parameter.

You can specify the data type of the input/output ports according to the data type of the previous or subsequent Steps’ input/output ports.

Installation and Usage

Installation

Python 3.9.13 is built into Mech-MSR, and thus this Step will use the built-in Python environment of the software. If there is any missing Python library when you use this Step, you need to install the library in the Mech-MSR’s built-in Python environment. The procedures for installation are as follows:

  1. Open the command prompt window.

  2. Use the cd command to go to the directory where Python is stored in Mech-MSR.

  3. Type python -m pip install Python name of the library on the command line to install the missing Python library.

Two commonly used Python libraries, NumPy and OpenCV, are built into the Mech-MSR.

Usage Instructions

After preparing the Python script, please follow the procedure below to use the Step. For detailed description of the parameters, refer to Parameter Description.

  1. Set the data type for the input and output ports. Enter the data type for Input Ports and Output Ports according to the data type of the previous or subsequent Steps’ input/output ports.

  2. Specify the file path to the Python script. Select the file path to the script to be loaded in the Script File Path parameter.

  3. Set the name of the function to be called. Once the Script File Path has been specified, this Step will retrieve the function names in the script automatically, and you can select the name of the function to be called in the drop-down list of Func Name.

  4. Run the Step.

  • When writing the Python scripts, you can directly write the functions to process data without writing the statements in the scripts to obtain Mech-MSR data.

  • When the Python script is modified, and you want to use the new script to run this Step in real time, click Reload File in the Execution Flags parameter group.

Notes

Please pay attention to the following issues when you write the Python script and run the script in this Step.

Third-Party Library is Recommended

Because running a Python script in Mech-MSR differs from running the script in Python directly, some Python libraries could not be installed successfully or they cannot function properly after being installed, it is recommended to use third-party libraries.

Use the NumPy Library

The NumPy library is used to support some complicated formats of data. If a parameter type is ndarray, but the NumPy has not been imported, an error may occur. Therefore, you should add the import numpy statement at the beginning of the script.

Pay Attention to the Data Dimension when Writing the Script

When writing the Python script, please note the dimension of data corresponding to each port of the Step.

  • 0 dimension by default: Image; Cloud(XYZ); Cloud(XYZ-Normal)

  • 1 dimension by default: NumberList; BoolList; IndexList; StringList; Point2DList

  • 2 dimensions by default: PoseList; Pose2DList; Size3DList

Use “[]” after the data type to add a dimension.

For example, the data dimension of NumberList is 1, while the data dimension of NumberList[] is 2.

Parameter Description

Input Ports

This parameter is used to specify the data type(s) of the input port(s). The input data types will be passed to the called function as parameters in the corresponding order.

Default value: Null.

Output Ports

Description: This parameter is used to specify the data type(s) of the output port(s). Data returned by the function will be returned to the Step in the corresponding order and will be parsed according to the corresponding data type.

Default value: Null.

The currently supported data types are as follows:

Step port type Data type used in Python Input data example

PoseList

List

[[10, 20, 30, 0.951, 0.255, 0.168, 0.045]], [10, 20, 30, 0.951, 0.255, 0.168, 0.045]]
(In each array, the first three values are the coordinates, and the following four values are quaternions.)

Pose2DList

List

[[0, 0, 0]], [2, 0, 120]]
(In each array, the first two values are X and Y values of the coordinates respectively, and the third value is the angle.)

NumberList

List

[1.1, 2, 999.9, -22]

StringList

String

['string_1', 'string_2', 'string_3']

Image

8-bit unsigned integer/64-bit floating point number

Image data

Cloud(XYZ)

Array

Point cloud data

Cloud(XYZ-Normal)

Array

Point cloud data with normals

Cloud(XYZ-RGB)

Array

Data of textured point cloud

Size3DList

64-bit floating point number

[[2.5, 5, 0.001]], [6, 5, 0.02]]
(In each array, the first two values are the width and height, and the third value is the length of each pixel.)

IndexList

Integer

[45, 10, 90]

BoolList

Boolean

[True, False, True]

Point2DList

List

Profile data

Script File Path

This parameter is used to select the file path of the script to be loaded.

Func Name

This parameter is used to set the name of the function to be called.

We Value Your Privacy

We use cookies to provide you with the best possible experience on our website. By continuing to use the site, you acknowledge that you agree to the use of cookies. If you decline, a single cookie will be used to ensure you're not tracked or remembered when you visit this website.