~ubuntu-branches/ubuntu/jaunty/gimp/jaunty-security

« back to all changes in this revision

Viewing changes to plug-ins/pygimp/doc/structure-of-plugin.html

  • Committer: Bazaar Package Importer
  • Author(s): Daniel Holbach
  • Date: 2007-05-02 16:33:03 UTC
  • mto: This revision was merged to the branch mainline in revision 12.
  • Revision ID: james.westby@ubuntu.com-20070502163303-6wchheivjxgjtlna
Tags: upstream-2.3.16
ImportĀ upstreamĀ versionĀ 2.3.16

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
<!DOCTYPE HTML PUBLIC "-//Norman Walsh//DTD DocBook HTML 1.0//EN">
2
 
<HTML
3
 
><HEAD
4
 
><TITLE
5
 
>The Structure Of A Plugin</TITLE
6
 
><META
7
 
NAME="GENERATOR"
8
 
CONTENT="Modular DocBook HTML Stylesheet"><LINK
9
 
REL="HOME"
10
 
TITLE="Gimp Python Documentation"
11
 
HREF="pygimp.html"><LINK
12
 
REL="PREVIOUS"
13
 
TITLE="Gimp Python Documentation"
14
 
HREF="pygimp.html"><LINK
15
 
REL="NEXT"
16
 
TITLE="The Procedural Database"
17
 
HREF="procedural-database.html"></HEAD
18
 
><BODY
19
 
><DIV
20
 
CLASS="NAVHEADER"
21
 
><TABLE
22
 
WIDTH="100%"
23
 
BORDER="0"
24
 
CELLPADDING="0"
25
 
CELLSPACING="0"
26
 
><TR
27
 
><TH
28
 
COLSPAN="3"
29
 
ALIGN="center"
30
 
>Gimp Python Documentation</TH
31
 
></TR
32
 
><TR
33
 
><TD
34
 
WIDTH="10%"
35
 
ALIGN="left"
36
 
VALIGN="bottom"
37
 
><A
38
 
HREF="pygimp.html"
39
 
>Prev</A
40
 
></TD
41
 
><TD
42
 
WIDTH="80%"
43
 
ALIGN="center"
44
 
VALIGN="bottom"
45
 
></TD
46
 
><TD
47
 
WIDTH="10%"
48
 
ALIGN="right"
49
 
VALIGN="bottom"
50
 
><A
51
 
HREF="procedural-database.html"
52
 
>Next</A
53
 
></TD
54
 
></TR
55
 
></TABLE
56
 
><HR
57
 
ALIGN="LEFT"
58
 
WIDTH="100%"></DIV
59
 
><DIV
60
 
CLASS="SECT1"
61
 
><H1
62
 
CLASS="SECT1"
63
 
><A
64
 
NAME="STRUCTURE-OF-PLUGIN"
65
 
>The Structure Of A Plugin</A
66
 
></H1
67
 
><P
68
 
>The majority of code in this package resides in
69
 
    <TT
70
 
CLASS="FILENAME"
71
 
>gimpmodule.c</TT
72
 
>, but this provides a poor
73
 
    interface for implementing some portions of a plugin.  For this
74
 
    reason, there is a python module called
75
 
    <TT
76
 
CLASS="FILENAME"
77
 
>plugin.py</TT
78
 
> that sets out a structure for
79
 
    plugins and implements some things that were either too dificult
80
 
    or impossible to do in C.</P
81
 
><P
82
 
>The main purpose of <TT
83
 
CLASS="FILENAME"
84
 
>plugin.py</TT
85
 
> was to
86
 
    implement an object oriented structure for plug-ins.  As well as
87
 
    this, it handles tracebacks, which are otherwise ignored by
88
 
    <TT
89
 
CLASS="FILENAME"
90
 
>libgimp</TT
91
 
>, and gives a method to call
92
 
    other Gimp-Python plug-ins without going through the procedural
93
 
    database.</P
94
 
><DIV
95
 
CLASS="SECT2"
96
 
><H2
97
 
CLASS="SECT2"
98
 
><A
99
 
NAME="EXAMPLE-PLUGIN"
100
 
>An Example Plugin</A
101
 
></H2
102
 
><P
103
 
>As in a lot of manuals, the first thing you examine is an
104
 
      example, so here is an example.  I have included it before
105
 
      explaining what it does to allow more advanced programmers to
106
 
      see the structure up front.  It is a translation of the clothify
107
 
      Script-Fu extension:</P
108
 
><DIV
109
 
CLASS="EXAMPLE"
110
 
><P
111
 
><B
112
 
>Example 1. A sample python plugin</B
113
 
></P
114
 
><TABLE
115
 
BORDER="0"
116
 
BGCOLOR="#E0E0E0"
117
 
WIDTH="100%"
118
 
><TR
119
 
><TD
120
 
><PRE
121
 
CLASS="PROGRAMLISTING"
122
 
>#!/usr/bin/python
123
 
import math
124
 
from gimpfu import *
125
 
 
126
 
have_gimp11 = gimp.major_version &#62; 1 or \
127
 
              gimp.major_version == 1 and gimp.minor_version &#62;= 1
128
 
 
129
 
def python_clothify(timg, tdrawable, bx=9, by=9,
130
 
                    azimuth=135, elevation=45, depth=3):
131
 
        bx = 9 ; by = 9 ; azimuth = 135 ; elevation = 45 ; depth = 3
132
 
        width = tdrawable.width
133
 
        height = tdrawable.height
134
 
        img = gimp.image(width, height, RGB)
135
 
        layer_one = gimp.layer(img, "X Dots", width, height, RGB_IMAGE,
136
 
                               100, NORMAL_MODE)
137
 
        img.disable_undo()
138
 
        if have_gimp11:
139
 
                pdb.gimp_edit_fill(layer_one)
140
 
        else:
141
 
                pdb.gimp_edit_fill(img, layer_one)
142
 
        img.add_layer(layer_one, 0)
143
 
        pdb.plug_in_noisify(img, layer_one, 0, 0.7, 0.7, 0.7, 0.7)
144
 
        layer_two = layer_one.copy()
145
 
        layer_two.mode = MULTIPLY_MODE
146
 
        layer_two.name = "Y Dots"
147
 
        img.add_layer(layer_two, 0)
148
 
        pdb.plug_in_gauss_rle(img, layer_one, bx, 1, 0)
149
 
        pdb.plug_in_gauss_rle(img, layer_two, by, 0, 1)
150
 
        img.flatten()
151
 
        bump_layer = img.active_layer
152
 
        pdb.plug_in_c_astretch(img, bump_layer)
153
 
        pdb.plug_in_noisify(img, bump_layer, 0, 0.2, 0.2, 0.2, 0.2)
154
 
        pdb.plug_in_bump_map(img, tdrawable, bump_layer, azimuth,
155
 
                             elevation, depth, 0, 0, 0, 0, TRUE, FALSE, 0)
156
 
        gimp.delete(img)
157
 
 
158
 
register(
159
 
        "python_fu_clothify",
160
 
        "Make the specified layer look like it is printed on cloth",
161
 
        "Make the specified layer look like it is printed on cloth",
162
 
        "James Henstridge",
163
 
        "James Henstridge",
164
 
        "1997-1999",
165
 
        "&#60;Image&#62;/Python-Fu/Alchemy/Clothify",
166
 
        "RGB*, GRAY*",
167
 
        [
168
 
                (PF_INT, "x_blur", "X Blur", 9),
169
 
                (PF_INT, "y_blur", "Y Blur", 9),
170
 
                (PF_INT, "azimuth", "Azimuth", 135),
171
 
                (PF_INT, "elevation", "elevation", 45),
172
 
                (PF_INT, "depth", "Depth", 3)
173
 
        ],
174
 
        [],
175
 
        python_clothify)
176
 
 
177
 
main()</PRE
178
 
></TD
179
 
></TR
180
 
></TABLE
181
 
></DIV
182
 
></DIV
183
 
><DIV
184
 
CLASS="SECT2"
185
 
><H2
186
 
CLASS="SECT2"
187
 
><A
188
 
NAME="IMPORTANT-MODULES"
189
 
>Import Modules</A
190
 
></H2
191
 
><P
192
 
>In this plugin, a number of modules are imported.  The
193
 
      important ones are:</P
194
 
><P
195
 
></P
196
 
><UL
197
 
><LI
198
 
><P
199
 
><TT
200
 
CLASS="FILENAME"
201
 
>gimpfu</TT
202
 
>: this module provides a
203
 
          simple interface for writing plugins, similar to what
204
 
          script-fu provides.  It provides the GUI for entering in
205
 
          parameters in interactive mode and performs some sanity
206
 
          checks when registering the plugin.</P
207
 
><P
208
 
>By using "from gimpfu import *", this module also
209
 
          provides an easy way to get all the commonly used symbols
210
 
          into the plugin's namespace.</P
211
 
></LI
212
 
><LI
213
 
><P
214
 
><TT
215
 
CLASS="FILENAME"
216
 
>gimp</TT
217
 
>: the main part of the gimp
218
 
          extension.  This is imported with gimpfu.</P
219
 
></LI
220
 
><LI
221
 
><P
222
 
><TT
223
 
CLASS="FILENAME"
224
 
>gimpenums</TT
225
 
>: a number of useful
226
 
          constants.  This is also automatically imported with
227
 
          gimpfu.</P
228
 
></LI
229
 
></UL
230
 
><P
231
 
>The pdb variable is a variable for accessing the
232
 
      procedural database.  It is imported into the plugin's namespace
233
 
      with gimpfu for convenience.</P
234
 
></DIV
235
 
><DIV
236
 
CLASS="SECT2"
237
 
><H2
238
 
CLASS="SECT2"
239
 
><A
240
 
NAME="PLUGIN-FRAMEWORK"
241
 
>Plugin Framework</A
242
 
></H2
243
 
><P
244
 
>With pygimp-0.4, the gimpfu module was introduced.  It
245
 
      simplifies writing plugins a lot.  It handles the run mode
246
 
      (interactive, non interactive or run with last values),
247
 
      providing a GUI for interactive mode and saving the last used
248
 
      settings.</P
249
 
><P
250
 
>Using the gimpfu plugin, all you need to do is write the
251
 
      function that should be run, make a call to
252
 
      <TT
253
 
CLASS="FUNCTION"
254
 
><B
255
 
>register</B
256
 
></TT
257
 
>, and finally a call to
258
 
      <TT
259
 
CLASS="FUNCTION"
260
 
><B
261
 
>main</B
262
 
></TT
263
 
> to get the plugin started.</P
264
 
><P
265
 
>If the plugin is to be run on an image, the first
266
 
      parameter to the plugin function should be the image, and the
267
 
      second should be the current drawable (do not worry about the
268
 
      run_mode parameter).  Plugins that do not act on an existing
269
 
      image (and hence go in the toolbox's menus) do not need these
270
 
      parameters.  Any other parameters are specific to the
271
 
      plugin.</P
272
 
><P
273
 
>After defining the plugin function, you need to call
274
 
      <TT
275
 
CLASS="FUNCTION"
276
 
><B
277
 
>register</B
278
 
></TT
279
 
> to register the plugin with gimp
280
 
      (When the plugin is run to query it, this information is passed
281
 
      to gimp.  When it is run interactively, this information is used
282
 
      to construct the GUI).  The parameters to
283
 
      <TT
284
 
CLASS="FUNCTION"
285
 
><B
286
 
>register</B
287
 
></TT
288
 
> are:</P
289
 
><P
290
 
></P
291
 
><TABLE
292
 
BORDER="0"
293
 
><TR
294
 
><TD
295
 
>name</TD
296
 
></TR
297
 
><TR
298
 
><TD
299
 
>blurb</TD
300
 
></TR
301
 
><TR
302
 
><TD
303
 
>help</TD
304
 
></TR
305
 
><TR
306
 
><TD
307
 
>author</TD
308
 
></TR
309
 
><TR
310
 
><TD
311
 
>copyright</TD
312
 
></TR
313
 
><TR
314
 
><TD
315
 
>date</TD
316
 
></TR
317
 
><TR
318
 
><TD
319
 
>menupath</TD
320
 
></TR
321
 
><TR
322
 
><TD
323
 
>imagetypes</TD
324
 
></TR
325
 
><TR
326
 
><TD
327
 
>params</TD
328
 
></TR
329
 
><TR
330
 
><TD
331
 
>results</TD
332
 
></TR
333
 
><TR
334
 
><TD
335
 
>function</TD
336
 
></TR
337
 
></TABLE
338
 
><P
339
 
></P
340
 
><P
341
 
>Most of these parameters are quite self explanatory.  The
342
 
      menupath option should start with &#60;Image%gt;/ for image
343
 
      plugins and &#60;Toolbox&#62;/ for toolbox plugins.  The remainder
344
 
      of the menupath is a slash separated path to its menu item.</P
345
 
><P
346
 
>The params parameter holds a list parameters for the
347
 
      function.  It is a list of tuples.  Note that you do not have to
348
 
      specify the run_type, image or drawable parameters, as gimpfu
349
 
      will add these automatically for you.  The tuple format is
350
 
      (type, name, description, default [, extra]).  The allowed type
351
 
      codes are:</P
352
 
><P
353
 
></P
354
 
><TABLE
355
 
BORDER="0"
356
 
><TR
357
 
><TD
358
 
>PF_INT8</TD
359
 
></TR
360
 
><TR
361
 
><TD
362
 
>PF_INT16</TD
363
 
></TR
364
 
><TR
365
 
><TD
366
 
>PF_INT32</TD
367
 
></TR
368
 
><TR
369
 
><TD
370
 
>PF_INT</TD
371
 
></TR
372
 
><TR
373
 
><TD
374
 
>PF_FLOAT</TD
375
 
></TR
376
 
><TR
377
 
><TD
378
 
>PF_STRING</TD
379
 
></TR
380
 
><TR
381
 
><TD
382
 
>PF_VALUE</TD
383
 
></TR
384
 
><TR
385
 
><TD
386
 
>PF_INT8ARRAY</TD
387
 
></TR
388
 
><TR
389
 
><TD
390
 
>PF_INT16ARRAY</TD
391
 
></TR
392
 
><TR
393
 
><TD
394
 
>PF_INT32ARRAY</TD
395
 
></TR
396
 
><TR
397
 
><TD
398
 
>PF_INTARRAY</TD
399
 
></TR
400
 
><TR
401
 
><TD
402
 
>PF_FLOATARRAY</TD
403
 
></TR
404
 
><TR
405
 
><TD
406
 
>PF_STRINGARRAY</TD
407
 
></TR
408
 
><TR
409
 
><TD
410
 
>PF_COLOR</TD
411
 
></TR
412
 
><TR
413
 
><TD
414
 
>PF_COLOUR</TD
415
 
></TR
416
 
><TR
417
 
><TD
418
 
>PF_REGION</TD
419
 
></TR
420
 
><TR
421
 
><TD
422
 
>PF_IMAGE</TD
423
 
></TR
424
 
><TR
425
 
><TD
426
 
>PF_LAYER</TD
427
 
></TR
428
 
><TR
429
 
><TD
430
 
>PF_CHANNEL</TD
431
 
></TR
432
 
><TR
433
 
><TD
434
 
>PF_DRAWABLE</TD
435
 
></TR
436
 
><TR
437
 
><TD
438
 
>PF_TOGGLE</TD
439
 
></TR
440
 
><TR
441
 
><TD
442
 
>PF_BOOL</TD
443
 
></TR
444
 
><TR
445
 
><TD
446
 
>PF_SLIDER</TD
447
 
></TR
448
 
><TR
449
 
><TD
450
 
>PF_SPINNER</TD
451
 
></TR
452
 
><TR
453
 
><TD
454
 
>PF_ADJUSTMENT</TD
455
 
></TR
456
 
><TR
457
 
><TD
458
 
>PF_FONT</TD
459
 
></TR
460
 
><TR
461
 
><TD
462
 
>PF_FILE</TD
463
 
></TR
464
 
><TR
465
 
><TD
466
 
>PF_BRUSH</TD
467
 
></TR
468
 
><TR
469
 
><TD
470
 
>PF_PATTERN</TD
471
 
></TR
472
 
><TR
473
 
><TD
474
 
>PF_GRADIENT</TD
475
 
></TR
476
 
></TABLE
477
 
><P
478
 
></P
479
 
><P
480
 
>These values map onto the standard PARAM_* constants.  The
481
 
      reason to use the extra constants is that they give gimpfu more
482
 
      information, so it can produce a better interface (for instance,
483
 
      the PF_FONT type is equivalent to PARAM_STRING, but in the GUI
484
 
      you get a small button that will bring up a font selection
485
 
      dialog).</P
486
 
><P
487
 
>The PF_SLIDER, PF_SPINNER and PF_ADJUSTMENT types require
488
 
      the extra parameter.  It is of the form (min, max, step), and
489
 
      gives the limits for the spin button or slider.</P
490
 
><P
491
 
>The results parameter is a list of 3-tuples of the form
492
 
      (type, name, description).  It defines the return values for the
493
 
      function.  If there is only a single return value, the plugin
494
 
      function should return just that value.  If there is more than
495
 
      one, the plugin function should return a tuple of results.</P
496
 
><P
497
 
>The final parameter to <TT
498
 
CLASS="FUNCTION"
499
 
><B
500
 
>register</B
501
 
></TT
502
 
> is
503
 
      the plugin function itself.</P
504
 
><P
505
 
>After registering one or more plugin functions, you must
506
 
      call the <TT
507
 
CLASS="FUNCTION"
508
 
><B
509
 
>main</B
510
 
></TT
511
 
> function.  This will cause
512
 
      the plugin to start running.  A GUI will be displayed when
513
 
      needed, and your plugin function will be called at the
514
 
      appropriate times.</P
515
 
></DIV
516
 
></DIV
517
 
><DIV
518
 
CLASS="NAVFOOTER"
519
 
><HR
520
 
ALIGN="LEFT"
521
 
WIDTH="100%"><TABLE
522
 
WIDTH="100%"
523
 
BORDER="0"
524
 
CELLPADDING="0"
525
 
CELLSPACING="0"
526
 
><TR
527
 
><TD
528
 
WIDTH="33%"
529
 
ALIGN="left"
530
 
VALIGN="top"
531
 
><A
532
 
HREF="pygimp.html"
533
 
>Prev</A
534
 
></TD
535
 
><TD
536
 
WIDTH="34%"
537
 
ALIGN="center"
538
 
VALIGN="top"
539
 
><A
540
 
HREF="pygimp.html"
541
 
>Home</A
542
 
></TD
543
 
><TD
544
 
WIDTH="33%"
545
 
ALIGN="right"
546
 
VALIGN="top"
547
 
><A
548
 
HREF="procedural-database.html"
549
 
>Next</A
550
 
></TD
551
 
></TR
552
 
><TR
553
 
><TD
554
 
WIDTH="33%"
555
 
ALIGN="left"
556
 
VALIGN="top"
557
 
>Gimp Python Documentation</TD
558
 
><TD
559
 
WIDTH="34%"
560
 
ALIGN="center"
561
 
VALIGN="top"
562
 
>&nbsp;</TD
563
 
><TD
564
 
WIDTH="33%"
565
 
ALIGN="right"
566
 
VALIGN="top"
567
 
>The Procedural Database</TD
568
 
></TR
569
 
></TABLE
570
 
></DIV
571
 
></BODY
572
 
></HTML
573
 
>
 
 
b'\\ No newline at end of file'