Export Functions
Guide to export functions from your python plugin to be used by other language modules within Plugify.
In the Plugify ecosystem, Python plugins can export functions to make them accessible to other plugins. This guide explains how to define and export functions in Python and provides examples to help you integrate your plugins seamlessly.
Basic Type Mapping
The following table lists how types are exposed to the Python API:
| C++ Type | Python Type | Plugify Alias | Ref Support ? |
|---|---|---|---|
| void | None | void | ❌ |
| bool | bool | bool | ✅ |
| char | str | char8 | ✅ |
| char16_t | str | char16 | ✅ |
| int8_t | int | int8 | ✅ |
| int16_t | int | int16 | ✅ |
| int32_t | int | int32 | ✅ |
| int64_t | int | int64 | ✅ |
| uint8_t | int | uint8 | ✅ |
| uint16_t | int | uint16 | ✅ |
| uint32_t | int | uint32 | ✅ |
| uint64_t | int | uint64 | ✅ |
| uintptr_t | int | ptr64 | ✅ |
| uintptr_t | int | ptr32 | ✅ |
| float | float | float | ✅ |
| double | float | double | ✅ |
| void* | Callable | function | ❌ |
| plg::string | str | string | ✅ |
| plg::any | Any | any | ✅ |
| plg::vector<bool> | list[bool] | bool | ✅ |
| plg::vector<char> | list[str] | char8 | ✅ |
| plg::vector<char16_t> | list[str] | char16 | ✅ |
| plg::vector<int8_t> | list[int] | int8 | ✅ |
| plg::vector<int16_t> | list[int] | int16 | ✅ |
| plg::vector<int32_t> | list[int] | int32 | ✅ |
| plg::vector<int64_t> | list[int] | int64 | ✅ |
| plg::vector<uint8_t> | list[int] | uint8 | ✅ |
| plg::vector<uint16_t> | list[int] | uint16 | ✅ |
| plg::vector<uint32_t> | list[int] | uint32 | ✅ |
| plg::vector<uint64_t> | list[int] | uint64 | ✅ |
| plg::vector<uintptr_t> | list[int] | ptr64 | ✅ |
| plg::vector<uintptr_t> | list[int] | ptr32 | ✅ |
| plg::vector<float> | list[float] | float | ✅ |
| plg::vector<double> | list[float] | double | ✅ |
| plg::vector<plg::string> | list[str] | string | ✅ |
| plg::vector<plg::any> | list[Any] | any | ✅ |
| plg::vector<plg::vec2> | list[Vector2] | vec2 | ✅ |
| plg::vector<plg::vec3> | list[Vector3] | vec3 | ✅ |
| plg::vector<plg::vec4> | list[Vectpr4] | vec4 | ✅ |
| plg::vector<plg::mat4x4> | list[Matrix4x4] | mat4x4 | ✅ |
| plg::vec2 | Vector2 | vec2 | ✅ |
| plg::vec3 | Vector3 | vec3 | ✅ |
| plg::vec4 | Vector4 | vec4 | ✅ |
| plg::mat4x4 | Matrix4x4 | mat4x4 | ✅ |
Exporting Functions in Python
Exporting functions in Python is simpler than in C++ because Python is a dynamically-typed language. You only need to define the function and specify it in the plugin manifest. Plugify's Python Language Module handles the rest.
Basic Example
Here’s a simple example of exporting a function in a Python plugin:
Function Definition
Plugin Manifest
To export the function, describe it in the plugin manifest under the methods section:
Advanced Example: Exporting Complex Functions
Here’s an example of exporting a function with complex parameter and return types:
Function Definition
Plugin Manifest
Exporting Functions with References
Plugify supports reference parameters in dynamically-typed languages like Python, but they work differently than in statically-typed languages. In Python, you cannot directly modify parameters passed by reference. Instead, you must return a tuple where:
- First element: The function's return value (use
Noneif the function returnsvoid) - Remaining elements: Modified values for all reference parameters, in order
Function Definition with Reference Parameters
Plugin Manifest with Reference Parameters
In the manifest, mark parameters that should be treated as references using "ref": true:
How It Works
- Return Order: The first return value is the function's actual return value. Subsequent values correspond to reference parameters in order.
- Void Functions: If the function returns
void, the first element of the tuple must beNone. - Mixed Returns: If a function has both a return value and reference parameters, the return value comes first, followed by all reference parameters.
- Type Hints: Use
tuple[ReturnType, RefType1, RefType2, ...]for proper type annotations.
Reference Parameter Support
Reference parameters work with most Plugify types as shown in the "Ref Support" column of the type mapping table. The following types do not support references:
void/None(cannot be passed by reference)function(callback types)
All other types including primitives, strings, and lists support reference parameters via the tuple return pattern.
Handling Callbacks
Plugify allows you to export functions that accept callbacks as parameters. Here’s an example:
Function Definition
Plugin Manifest
Best Practices
- Define Functions Clearly: Ensure your functions are well-documented and easy to understand.
- Follow Type Conventions: Adhere to Plugify's type conventions for parameters and return values.
- Test Thoroughly: Test your exported functions to ensure they work as expected when called by other plugins.
- Update the Manifest: Always describe exported functions in the plugin manifest under the
methodssection.
Conclusion
Exporting functions in Python plugins is simple and straightforward. By defining your functions and describing them in the plugin manifest, you can create robust and interoperable plugins. For more advanced use cases, such as handling callbacks, use the techniques outlined in this guide.