|
SGO Mistika: Python Extension
|
This document provides comprehensive guidance on creating Python nodes for the Workflows system. Python nodes are the core building blocks that define custom functionality within workflows.
Python nodes in Workflows are implemented as Python files that define specific functions to handle initialization, readiness checking, and processing logic. Each node file must contain at least three required functions: init, isReady, and process.
A minimal Python node looks like this:
The init function is called when the node is initialized. This is where you set up the node's properties, connectors, and other configurations.
Key methods available in init
self.addConnector(name, type, mode) - Add input/output connectorsself.addProperty(name, defaultValue) - Add a property with optional default value. If the property name starts with an underscore (_), it will be created as internal and will not be stored in the .mwf workflow file.self.setAcceptConnectors(accept, pattern) - Set which connectors are acceptedself.setDropToProperty(property_name) - Set which property receives dropped filesself.setDropSupportedTypes(types) - Set supported drop types (1=Directories, 2=Files, 3=both)self.bypassSupported = True - Enable bypass functionalityself.color = QColor(r, g, b) - Set node colorThe isReady function checks if the node is ready to process. This is called before process
Return values:
True - Node is ready to processFalse - Node is not ready (will be marked as error)self.critical("message", "error description") - Return critical errorThe process function contains the main logic of the node. This is where the actual work happens.
Key methods available in process
self.getConnectorsByType(type, specialType) - Get connectors by typeself.getFirstConnectorByName(name) - Get first connector by nameself.evaluate(property_name) - Evaluate a property valueself.isCancelled() - Check if node execution was cancelledself.progressUpdated(value) - Update progressself.setComplexity(value) - Set node complexity for progress trackingself.info("category", "message") - Log informationself.critical("category", "error message") - Log critical errorself.addFailedUP(up, message) - Add failed universal pathIn some cases, you may need to report errors that should persist beyond the normal validation process. This is particularly useful when errors occur outside of the normal validation flow (e.g., in menuAction or processDroppedFile).
The functions self.addPermanentCritical() and self.addPermanentError() are designed for this purpose:
self.addPermanentCritical(id, description, group) - Add a permanent critical errorself.addPermanentError(id, description, group) - Add a permanent errorThese functions work similarly to their regular counterparts but:
self.clearPermanentErrors() - Clear all permanent errorsself.deletePermanentErrorById(id) - Delete error by IDself.deletePermanentErrorsByGroup(group) - Delete errors by groupImportant: For errors in init, isReady, and process functions, use the regular critical and error methods instead. Permanent errors should only be used in functions that are not part of the core validation/execution process.
Example usage in menuAction:
Example usage in processDroppedFile:
Properties are defined using self.addProperty(name, defaultValue) and can be accessed directly as self.property_name.
Connectors are used to connect nodes in a workflow. Types:
Cconnector.CONNECTOR_TYPE_INPUT - Input connectorCconnector.CONNECTOR_TYPE_OUTPUT - Output connectorCconnector.MODE_OPTIONAL - Optional connectorCconnector.MODE_REQUIRED - Mandatory connectorEach node has an associated XSD schema file that defines the properties shown in the node editor. Note: The properties should be created in the @init function. The XSD defines only whitch properties to show and the format to use.
The schema file name matches the node name in lowercase.
Example schema structure:
self.critical() for critical errorsself.isCancelled() regularly to support cancellationself.setComplexity() for accurate progress reportingself.addPermanentCritical() and self.addPermanentError() for persistent errors that should not be cleared automaticallyNodes can be tested by:
Common issues:
When using error reporting functions:
self.critical(), self.error(), self.warning(), self.info() for errors that should be cleared automatically during validationself.addPermanentCritical() and self.addPermanentError() for errors that should persist beyond the normal validation process (e.g., in menuAction or processDroppedFile)Always check the workflow logs for error messages when debugging node issues.