-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhow_to_create_a_python_plugin_for_mitk.txt
259 lines (188 loc) · 7.19 KB
/
how_to_create_a_python_plugin_for_mitk.txt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
Title: How to create a Python plugin for MITK
Date: 2015-08-20
modified: 2015-09-02
Category: MITK, python
Tags: MITK, python
Author: Luis Javier Salvatierra
Email: ljsalvat@gmail.com
Summary: A manual on how to create a simple Python plugin for MITK.
[TOC]
## Plugins examples
- <a target="_blank" href="https://github.com/ljsalvatierra/mitk-plugins">MITK plugins</a>.
- <a target="_blank" href="https://github.com/ljsalvatierra/mitk-projects">MITK projects</a>.
## Creating a MITK plugin
* For more information, go to <a target="_blank" href="http://docs.mitk.org/2015.05/NewPluginPage.html">`MitkPluginGenerator`</a>.
* There are two ways of accomplish the same result.:
- [Creating a MITK plugin](#creating-a-mitk-plugin).:
```bash
$ ./MitkPluginGenerator --plugin-symbolic-name org.mycompany.myplugin --view-name "My View"
```
- [Creating a MITK project](#creating-a-mitk-project) (recommended).:
```bash
$ ./MitkPluginGenerator --plugin-symbolic-name org.mycompany.myplugin --view-name "My View" \
--project-name "MyProject" --project-app-name "MyApp"
```
It is recommended to take the second approach because you only have to build MITK once.
With the first approach you would have to rebuild MITK every time you make a change to
your plugin/s, instead, with the second approach, each time you make a change to your plugin,
you only have to compile your project.
## Creating a MITK plugin
```bash
$ /path/to/MITK-build/bin/MitkPluginGenerator -h
$ /path/to/MITK-build/bin/MitkPluginGenerator --out-dir /output/directory \
--vendor Plugin_vendor_name --view-name "My View" --plugin-symbolic-name org.mycompany.myplugin
$ cd /output/directory && ls
org.mycompany.myplugin
$ cd org.mycompany.myplugin && ls
CMakeLists.txt documentation files.cmake manifest_headers.cmake plugin.xml resources src
```
* Modify MITK to build with the new plugin.:
```bash
$ cp -r ../org.mycompany.myplugin /path/to/MITK/Plugins && cd /path/to/MITK/Plugins
$ vim PluginList.cmake
# Add your plugin with the flag 'ON'.
```
```cmake
set(MITK_PLUGINS
org.mycompany.myplugin:ON
org.blueberry.core.runtime:ON
...
```
* Set a new CTK Plugin in `CMakeLists.txt`.:
```bash
$ cd /path/to/MITK
$ vim CMakeLists.txt
# Search the string 'set(re_ctkplugin'
/set(re_ctkplugin
```
* Modify it to look like this.:
```cmake
# Specify which plug-ins belong to this project
macro(GetMyTargetLibraries all_target_libraries varname)
set(re_ctkplugin_mitk "^org_mitk_[a-zA-Z0-9_]+$")
set(re_ctkplugin_bb "^org_blueberry_[a-zA-Z0-9_]+$")
set(re_ctkplugin_mycompany "^org_mycompany_[a-zA-Z0-9_]+$")
set(_tmp_list)
list(APPEND _tmp_list ${all_target_libraries})
ctkMacroListFilter(_tmp_list re_ctkplugin_mitk re_ctkplugin_bb re_ctkplugin_mycompany OUTPUT_VARIABLE ${varname})
endmacro()
```
`set(re_ctkplugin_`**`mycompany "^org_mycompany_[a-zA-Z0-9_]+$"`**`)`
`ctkMacroListFilter(_tmp_list re_ctkplugin_mitk re_ctkplugin_bb `**`re_ctkplugin_mycompany`**` OUTPUT_VARIABLE ${varname})`
### Modify your plugin
* Add Python module dependency to the plugin `CMakeLists.txt`.
```cmake
mitk_create_plugin(
EXPORT_DIRECTIVE EXAMPLE_EXPORT
EXPORTED_INCLUDE_SUFFIXES src
MODULE_DEPENDS MitkQtWidgetsExt MitkPython
)
```
### Embed Python in the new plugin
#### Interact with <a target="_blank" href="http://docs.mitk.org/2015.05/classmitk_1_1PythonService.html">Mitk Python Service</a>.
When we create a plugin with `MitkPluginGenerator` the default view contains a button `Do something`. Each time we press that button, it calls the function `DoImageProcessing()` that prints a message in the console/terminal.
```cpp
// MyView.cpp
...
// Add the Python Service header
#include <mitkPythonService.h>
...
// If you followed the instructions then you have the default plugin
// with this function
void MyView::DoImageProcessing()
{
QList<mitk::DataNode::Pointer> nodes = this->GetDataManagerSelection();
if (nodes.empty()) return;
mitk::DataNode* node = nodes.front();
if (!node)
{
// Nothing selected. Inform the user and return
QMessageBox::information( NULL, "Template", "Please load and select an image before starting image processing.");
return;
}
...
```
* Add this two line example to the end of the function `DoImageProcessing()`.:
```cpp
...
message << ".";
//MITK_INFO << message.str();
// Each time we press that button will print `Hello World!` in the console/terminal
// First we interact with mitkPythonService and execute a simple Python function.
itk::SmartPointer<mitk::PythonService> _PythonService(new mitk::PythonService());
std::string result = _PythonService->Execute( "print ('Hello World!')", mitk::IPythonService::SINGLE_LINE_COMMAND );
message << "\n";
message << result << "\n";
MITK_INFO << message.str();
...
```
### Build MITK with your new plugin
```bash
$ cd /path/to/MITK-build #Clean directory
$ ccmake ../MITK
# Build with the option MITK_USE_PYTHON enabled.
# Configure and enable the option MITK_USE_SYSTEM_PYTHON
# Configure and toggle the advance view.
# Modify the Python path, library path and debug path, to use Python2.7 instead of Python3.4 or Python3.4m.
# Configure again and generate.
$ make
# The last command will take several hours.
```
## Creating a MITK project
```bash
$ mkdir MITK-projects && cd MITK-projects
$ /path/to/MitkPluginGenerator -h
$ /path/to/MitkPluginGenerator --plugin-symbolic-name org.mycompany.myplugin \
--view-name "My View" --project-name "MyProject" --project-app-name "MyApp"
$ cd MyProject && ls
Apps build CMake CMakeExternals CMakeLists.txt LICENSE.txt Plugins SuperBuild.cmake
```
### Modify your project/plugin
```bash
$ cd Plugins/org.mycompany.myplugin/src/internal && ls
org_mycompany_myplugin_Activator.cpp org_mycompany_myplugin_Activator.h MyViewControls.ui MyView.cpp MyView.h
$ vim MyView.cpp
```
* [Embed Python in the new plugin](#embed-python-in-the-new-plugin). This part is shared between the two approaches.
* Add Python module dependency to the plugin `CMakeLists.txt`.
```bash
$ vim /path/to/MyProject/Plugins/org.mycompany.myplugin/CMakeLists.txt
```
```cmake
project(org_mycompany_myplugin)
mitk_create_plugin(
EXPORT_DIRECTIVE MYPLUGIN_EXPORT
EXPORTED_INCLUDE_SUFFIXES src
MODULE_DEPENDS MitkQtWidgetsExt MitkPython
)
```
### Build your new project
```bash
$ cd /path/to/MyProject
$ mkdir build && cd build
$ ccmake ..
```

`Press 't' to toggle advanced mode and specify the EXTERNAL_MITK_DIR`

`Configure and look if there are any modules left`
* For example.:
- MITK_BUILD_ALL_PLUGINS ON
- MITK_VTK_DIR /path/to/MITK-build/ep/share/vtk-6.2
- MITK_OpenCV_DIR /path/to/MITK-build/ep/src/OpenCV-build
- ...
## Test it!
* Open the `MitkWorkbench`:
```bash
$ /path/to/MITK-build/bin/MitkWorkbench
```
* Or open your Project Launcher:
```bash
$ /path/to/MyProject/build/MyProject-build/bin/MyApp
```
* Open your plugin view:

* Open a new image to be able to press the button `Do something`:

* You should see this when pressing the button `Do something`:
