Using Python for Mouse Tracking
Using Python for Mouse Tracking
This example demonstrates how to connect and send values to CDP automation system using Python in CDP Studio, the independent automation software for open PC-based real-time distributed control systems with NoCode setup within the IDE.
The example launches Python script as a standalone program, that connects to CDP application and sends mouse movements to CDP signals.
Project Overview
The project consists of a very simple CDP application with two input CDPSignals - MouseX and MouseY. The project folder also contains a simple Python script named sendmousemoves.py. This script will connect to CDP via StudioAPI protocol and find the signals MouseX and MouseY. Then it will activate system mouse movement listener (using pynput module) that will send new value to MouseX and MouseY on every mouse move.
The goal of the example is to demonstrate how easy it is to connect and send values to CDP using Python cdp-client module. You just have to initialize cdp-client with the network location of the CDP application (host and port), call find_node to connect to the required signals and create and hook functions that are called when system is connected and signals are found. New values can be sent to found CDP signals using set_value function.
Note: This simple Python script is resistant to CDP application restarts or connection failures. While running, it will try automatically to reconnect and will continue sending mouse movement values once reconnected.
How to Run the Example
To run the example from CDP Studio, open Welcome mode and find it under Examples. Next, in Configure mode right-click on the system project and select Run & Connect. See the Running the Example Project tutorial for more information.
PythonRunner Component
To be able to run this example on your computer without any preparation or manual work the Python script is launched automatically on CDP application startup. To achieve this, a separate component named PythonRunner is added into the application. PythonRunner is actually a CDP ExternalControlBroker component, that uses LocalCommandRun request to launch the example Python script as separate process.
How to Prepare to Run the Example in Your Python Environment
This example uses the Python environment bundled with CDP Studio to run the example.
However, the recommended way to run Python scripts is to run them separately from Studio. For that you have to:
- verify that Python interpreter is installed in your computer and install it, when missing.
- install Python CDP client by entering the following command in your computer command prompt
pip install cdp-client
- install Python mouse and keyboard monitoring module pynput by entering command in your computer command prompt
pip install pynput
- locate the Python script from project folder, for example by typing sendmousemoves.py into the locator box on the lower left area of the CDP Studio window
- copy the file that opened in Code mode editor via copy pasting its text or via menu
File->Save "sendmousemoves.py" As... - start the CDP application again and look in the Application Output pane for a line like
10:40:29.700 StudioAPIServer: listening on 127.0.0.1:7689
- set the IP and port of the StudioAPIServer which the copied script reads from the environment variable StudioAPIURI and run the script, by executing following commands.
Windows:
set StudioAPIURI=127.0.0.1:7689 python sendmousemoves.py
Linux:
export StudioAPIURI=127.0.0.1:7689 python sendmousemoves.py
Troubleshooting
What to do when command "pip install cdp-client
" fails?
Some systems might not include setuptools and wheel packages by default. Run
pip install setuptools wheel
and try to install CDP client again.
Get started with CDP Studio today
Let us help you take your great ideas and turn them into the products your customer will love.