| 1 |
# This is a CamiTK python action |
| 2 |
import camitk |
| 3 |
|
| 4 |
# There are four functions to interact with CamiTK |
| 5 |
# - init is called when the application starts and the extension is loaded |
| 6 |
# - process is called when the action is triggered (for action with GUI, click on "Apply") |
| 7 |
# - targetDefined is called when the action targets, ie., its input(s), are modified |
| 8 |
# - parameterChanged is called when a CamiTK parameter value is changed in the GUI |
| 9 |
# |
| 10 |
# Note that only process is mandatory. |
| 11 |
# |
| 12 |
# self can be used like in a Python class to save the state. |
| 13 |
# you can also use CamiTK property to save the state and have a GUI |
| 14 |
|
| 15 |
def init(self:camitk.Action): |
| 16 |
# Add resource initialization if required (e.g. open a component) |
| 17 |
# You may comment or remove this method if your action does not required |
| 18 |
# any initialization |
| 19 |
return |
| 20 |
|
| 21 |
def process(self:camitk.Action): |
| 22 |
# self.getTargets() contains the list of Component currently set as this action target |
| 23 |
# Example |
| 24 |
logMessage = "Action \"" + self.getName() + "\" called" |
| 25 |
@if@ $parameters$ |
| 26 |
logMessage = logMessage + " with parameters\n" |
| 27 |
|
| 28 |
# Example of parameter value retrieval |
| 29 |
@else@ |
| 30 |
logMessage = logMessage + " without any parameters\n" |
| 31 |
@endif@ |
| 32 |
|
| 33 |
@for@ $p$ in $parameters$ |
| 34 |
param$index(p)$ = self.getParameterValue("$title(p.name)$") |
| 35 |
@if@ contains($p.enumValues$) |
| 36 |
enum$index(p)$Values = self.getProperty("$title(p.name)$").getAttribute("enumNames") |
| 37 |
logMessage = logMessage + " - $title(p.name)$: '" + enum$index(p)$Values[param$index(p)$] + "' (value: " + str(param$index(p)$) + ")\n" |
| 38 |
@else@ |
| 39 |
logMessage = logMessage + " - $title(p.name)$: " + str(param$index(p)$) + "\n"; |
| 40 |
@endif@ |
| 41 |
@endfor@ |
| 42 |
|
| 43 |
# Create a warning log message for this demo (although CAMITK_TRACE_ALT |
| 44 |
# and CAMITK_INFO_ALT are usually recommended for log messages) |
| 45 |
camitk.warning(logMessage) |
| 46 |
|
| 47 |
@if@ $componentClass$ |
| 48 |
# Process each targets |
| 49 |
# example of processing |
| 50 |
for selected in self.getTargets(): |
| 51 |
# Here do something here input |
| 52 |
camitk.info("Processing component '" + selected.getName() + "'...") |
| 53 |
@endif@ |
| 54 |
|
| 55 |
# call self.refreshApplication() or camitk.refresh() at the end to refresh the viewers |
| 56 |
return True # or False if something went wrong |
| 57 |
|
| 58 |
def targetDefined(self:camitk.Action): |
| 59 |
# You may comment or remove this method if your action does not required |
| 60 |
# to do something when the action target(s) is/are defined |
| 61 |
logMessage = "Current targets:\n" |
| 62 |
for selected in self.getTargets(): |
| 63 |
logMessage = logMessage + "- Component \"" + selected.getName() + "\" is a target\n" |
| 64 |
camitk.warning(logMessage) |
| 65 |
return |
| 66 |
|
| 67 |
def parameterChanged(self:camitk.Action, name:str): |
| 68 |
# You may comment or remove this method if your action does not required |
| 69 |
# to do something when one of the parameter's value has changed |
| 70 |
camitk.warning("Parameter " + name + " changed, new value: " + str(self.getParameterValue(name))) |
| 71 |
return |