Getting Started with RS2 Python Scripting
In this tutorial, we will walk you through the setup process and provide an overview of how to run your first Python script for RS2.
Prerequisites
Before you begin, ensure you have the RS2 program installed. You must have at minimum Version 11.023 in order to use this feature.
Python Environment
There are two methods to run a script, which differ in the Python environment used:
- Running on RocScript Editor
- Comes pre-installed with RS2 and includes the RS2 Python API library
- For all-level users
- Running on your own Python environment
- Requires downloading the RS2 Python API library
- For advanced users
Each method is detailed below.
Method 1: Using RocScript
1.1 Opening the editor
- In RS2, select Scripting > Open Scripting Editor from the menu. This can be accessed in either the RS2 Modeler or RS2 Interpret program. The RocScript Editor will open.
For more information about the RocScript Editor, see the Scripting Overview help topic.
1.2 Open a File
To open a file from the RocScript Editor:
- Select File > Open File option from the menu:
- Select the helloScripting.py python file from the tutorial folder directory: C:\Users\Public\Documents\Rocscience\RS2 Examples\Tutorials\Scripting\HelloScripting\helloScripting.py
1.3 (Optional): Install Libraries
- In either the RS2 Modeler or RS2 Interpret program, select Scripting > Manage Python Environment from the menu.
- The RocScript WinPython Command Prompt will open:
- Check the installed libraries with the command:
pip list
Pre-installed libraries include the RS2 Python API Library (i.e., RS2Scripting on the list) and two common python libraries: Pandas and Matplotlib.
- If you wish to install more libraries, use the command to do so:
pip install [library name]
1.4 Run the File
To run the file:
- Click on the play button at the top right of the window:
The helloScripting.py script showcases a simple application of RS2 Scripting. Utilizing functions from the RS2 Python API library, it initiates the RS2 Modeler and a server at port 60054, opens the Getting Started (Initial).fez RS2 model file, modifies a material property value, saves the file and computes it. Then, the RS2 Interpret and a server at port 60055 are launched. The computed file is opened, displaying the specified contour data.
Files are located at: C:\Users\Public\Documents\Rocscience\RS2 Examples\Tutorials\Scripting
Method 2: Using your Own Environment
The python environment is made up of two parts which need to be installed:
- The Python interpreter (Section 2.1 below)
- The Code Editor (Section 2.2 below)
2.1 Install Python
- Navigate to the 3.11.4 release page, scroll down and select 'Windows installer (64-bit)'.
- Run the installer. Keep the defaults and select Install Now. Installation should complete successfully.
Note that any version of python above 3.7 should work, but 3.11.4 was the most thoroughly tested.
2.2 Install a Python Code Editor
Any code editor can be used, but we recommend you use Visual Studio Code.
To download and install Visual Studio Code:
- Navigate to the Download page (https://code.visualstudio.com/download) and select the Windows option:
- Run the installer and complete the installation.
2.3 Configure Editor for Python
Visual Studio Code (VSCode) has extensions which can be installed.
- Navigate to the Extensions pane and search for python.
- Install the python extension.
- Restart VSCode.
2.4 Install the RS2 Library
- In VSCode, select View > Terminal in the menu to open a terminal.
- Install the library using pip, which is a package manager that comes with python:
pip install RS2Scripting
If you are not using the latest release of RS2, you need to specify a library version that matches your RS2 version. For example:
pip install "RS2Scripting==11.23.0"
To find a list of available versions, see Release History.
You can find your version of RS2 by selecting Help > About RS2 in the RS2 program.
The RS2 Python API Library is now installed.
2.5 Open a File
- In VSCode, select File > Open File in the menu:
- Select the helloScripting.py python file from the tutorial folder directory: C:\Users\Public\Documents\Rocscience\RS2 Examples\Tutorials\Scripting\HelloScripting\helloScripting.py
2.6 Run the file
To run the file:
- Click on the play button at the top right of the window:
The helloScripting.py script showcases a simple application of RS2 Scripting. Utilizing functions from the RS2 Python API library, it initiates the RS2 Modeler and a server at port 60054, opens the Getting Started (Initial).fez RS2 model file, modifies a material property value, saves the file and computed. Then, the RS2 Interpret and a server at port 60055 are launched. The computed file is opened, displaying the specified contour data.
Files are located at: C:\Users\Public\Documents\Rocscience\RS2 Examples\Tutorials\Scripting
You’ve successfully set up your Python environment to use RS2 Scripting. You can build your own python scripts for RS2 now!
Resources
For more information on RS2 Scripting, check out the following resources:
- RS2 Python Reference Guide
- Source code on Github, along with more tests and examples using API calls.
- RS2 Scripting Overview