Python API

Python is a widely used high-level programming language for general-purpose programming. Python is a programming language that lets you work faster and integrate your systems more effectively. Python's syntax allows programmers to express concepts in fewer lines of code compared to other languages, making it friendly and easy to learn.

The previous section explains the advantages of using the RoboDK API with a widely used programming language for robot programming such as Python.

Python is automatically installed and integrated with RoboDK by default. Select ToolsOptionsOther to change the default settings (Python location and Python Editor).

RoboDK API - Image 1

The RoboDK API for Python is divided in the following modules:

    The robodk package is the distributed entry point of the Python API. It is the common parent of all sub-packages and modules.

    The robolink sub-module (robolink.py) is the interface between RoboDK and Python. Any object in the RoboDK Station Tree can be retrieved using a Robolink object and it is represented by the Item object. It is possible to perform different operations on that item according to the Robolink.Item class.

    The robomath sub-module (robomath.py) is a robotics toolbox for Python that allow operating with pose transformations and obtain Euler angles for different robot vendors. All post processors depend on this module.

    The robodialogs sub-module (robodialogs.py) is a dialogs toolbox. For instance, open and save file dialogs, message prompts, etc.

    The robofileio sub-module (robofileio.py) is a file operation toolbox. File properties, CSV, FTP, etc.

    The roboapps sub-module (roboapps.py) is a RoboDK Apps toolbox. More infofmation on Apps is available here: AppLoader.

You can find the Python modules in the folder C:/RoboDK/Python/. This folder is automatically included by default (used as PYTHONPATH) when you run Python programs and post processors in RoboDK.

The following sections show how to create a simple program for simulation, offline programming and online programming respectively. More examples are available in the Python API for RoboDK page. Also, the RoboDK library comes with sample Python scripts in the folder Scripts and Macros, available in the default Library (C:/RoboDK/Library/).

Python Simulation

This example shows how to create a new station using a UR robot and simulate a hexagonal movement with the robot.

1.Select FileRoboDK API - Image 2 Open

2.Open the UR10 robot

3.Select FileRoboDK API - Image 3 Open

4.Open the Paint_gun.tool file

5.Open the sample Python program SampleOfflineProgramming.py from C:/RoboDK/Library/Macros/

6.Double click the SampleOfflineProgramming item to run the simulation. The robot should draw a hexagon around the current location of the robot and the trace of the TCP will be displayed in yellow. 
Alternatively, right click the program and select RoboDK API - Image 5Run Python Script.

7.Right click the RoboDK API - Image 6 SampleOfflineProgramming item and select Edit Python Script. We should see the program as shown in the following image.

The SampleOfflineProgramming macro will draw a polygon of side n_sides and radius R at the current location of the robot, with respect to the robot reference frame. If desired, move the robot to a different location changing the polygon dimensions and re-run the program.

This example is similar to the program displayed in the offline programming section of the website (video included).

RoboDK API - Image 7

Python OLP

Python programs can be generated offline using the same Python code used to simulate the robot (as shown in the previous Python Simulation section). Offline Programming allows generating robot programs that can be executed on a specific robot controller:

1.Right click a RoboDK API - Image 8Python program

2.Select Generate robot program (F6)

In this case the program is not simulated but executed quickly to obtain the result: a robot program specific to a robot controller.

RoboDK API - Image 9

RDK = Robolink()

RDK.setRunMode(RUNMODE_MAKE_ROBOTPROG)

Python Online Programming

Python programs can be directly executed on the robot using the same Python code used to simulate the robot (as shown in the Python Simulation section). Online programming allows running a generic program on a specific robot controller using Robot Drivers:

1.Right click a RoboDK API - Image 10Python program

2.Select Run on robot

The program will run on the robot as it the Python program is executed.

RoboDK API - Image 11

RDK = Robolink()

RDK.setRunMode(RUNMODE_RUN_ROBOT)

Troubleshooting Python setup in RoboDK

If you installed RoboDK after installing Python, RoboDK may fail to properly run Python scripts or generate programs. This section shows the steps you should follow to properly set the Python interpreter in RoboDK and run Python scripts without issues.

You should follow these steps if you see an error message such as: “Set the path to the Python interpreter in Tools-Options-Python” or “Failed to run Python script. Set the path to the Python interpreter in: Tools-Options-Python tab”.

Follow these steps to properly setup the Python interpreter path if you already have Python installed:

1.Select the Start Menu on Windows and type python.

2.Right click the Python version you would like to use with RoboDK and select Open file location.

RoboDK API - Image 12

Repeat this procedure on the folder that opens:

3.Right click on Python 3.7 and select Open file location.

RoboDK API - Image 13

4.Select Copy on the path in the path title bar.

RoboDK API - Image 14

Provide the Python path in RoboDK settings:

5.Open RoboDK.

6.Select Tools-Options.

7.Select the Python tab.

8.Paste the path in the Python interpreter box adding /python.exe, as shown in the next image. Using forward slashes or backslashes should both work.

RoboDK API - Image 15