Python scripting enables extending C++ and .Net applications with Python scripts. It allows seamless data exchange between the native SDK and Python: existing SDK objects can be passed into Python for processing, and the resulting objects or values can be returned back to the C++ or .NET environment. This makes it possible to enrich applications with Python’s flexibility and ecosystem while keeping the performance and structure of a core application.
Unlike many common Python extension mechanisms, we do not rely on an embedded Python runtime shipped with the Common Vision Blox. Instead, it attaches to the Python installation already available on the system. This allows users to work with their preferred Python environment, but requires:
%CVB%/Lib/Python
(Windows) and /opt/cvb/python
.You can run Python scripts from you .NET or C++ application. In order to to do that you must create a Python context first. Be aware that this can be done only one in a process. In order to load a Python script from a custom location add the folder to Python's search path. Finally, you can import your script using it as normal Python module. See also ScriptLab to try Python scripting within an application.
Once everything is setup you can continue calling into the script. On the .Net/C++ side you will only interact with generic Python objects. Even your module itself is a generic Python object. In order to call into an object's method you must first get the type object from the module. Then create an instance of this type. After that you can access the method by name. The method is also just another generic Python object.
In order to call the method with arguments you must first pack all arguments into a tuple. The factory method supports all basic data types including many more complex CVB types. After running the method, you can convert the result into the expected type.
It is possible to break into your python code. This can be done by attaching debugpy to your process. For convenience this can be done during context creation. You can activate and configure how the context waits for a debugger in %CVBDATA%/config//CVPyScriptConf.json
.
Once the context is in the waiting state, you can attach to your process from VS Code using the following launch.json
. Note that debugpy only attaches to the thread the context was created in. To debug other threads you must attach debugpy manually.