27
27
def register_procedure (
28
28
procedure : Callable ,
29
29
procedure_type : Type [Gimp .Procedure ] = Gimp .ImageProcedure ,
30
- arguments : Optional [Iterable [List ]] = None ,
31
- return_values : Optional [Iterable [List ]] = None ,
30
+ arguments : Optional [Union [ Iterable [List ], Callable [[], Iterable [ List ]] ]] = None ,
31
+ return_values : Optional [Union [ Iterable [List ], Callable [[], Iterable [ List ]] ]] = None ,
32
32
menu_label : Optional [str ] = None ,
33
33
menu_path : Optional [Union [str , Iterable [str ]]] = None ,
34
34
image_types : Optional [str ] = None ,
35
35
sensitivity_mask : Optional [Gimp .ProcedureSensitivityMask ] = None ,
36
36
documentation : Optional [Union [Tuple [str , str ], Tuple [str , str , str ]]] = None ,
37
37
attribution : Optional [Tuple [str , str , str ]] = None ,
38
- auxiliary_arguments : Optional [Iterable [List ]] = None ,
38
+ auxiliary_arguments : Optional [Union [ Iterable [List ], Callable [[], Iterable [ List ]] ]] = None ,
39
39
run_data : Optional [Iterable ] = None ,
40
40
init_ui : bool = True ,
41
41
pdb_procedure_type : Gimp .PDBProcType = Gimp .PDBProcType .PLUGIN ,
@@ -69,7 +69,8 @@ def register_procedure(
69
69
registered. If ``procedure_type`` is `Gimp.ImageProcedure`, several PDB
70
70
arguments will be pre-filled (see the documentation for
71
71
`Gimp.ImageProcedure` for more information).
72
- arguments: List of arguments (procedure parameters).
72
+ arguments: List of arguments (procedure parameters), or a function returning
73
+ a list of arguments.
73
74
Each argument must be a list containing the following elements in this
74
75
order:
75
76
* argument type. The type corresponds to one of the
@@ -82,9 +83,40 @@ def register_procedure(
82
83
Underscores in argument names (``_``) are automatically replaced with
83
84
hyphens (``-``).
84
85
86
+ If your argument list contains arguments of type e.g. `Gegl.Color`, you
87
+ will have to pass a function taking no parameters, for example:
88
+ >>> def get_arguments():
89
+ >>> _default_foreground_color = Gegl.Color.new('black')
90
+ >>> _default_foreground_color.set_rgba(0.65, 0.6, 0.3, 1.0)
91
+ >>>
92
+ >>> _default_background_color = Gegl.Color.new('black')
93
+ >>> _default_background_color.set_rgba(0.1, 0.4, 0.15, 1.0)
94
+ >>>
95
+ >>> return [
96
+ >>> [
97
+ >>> 'color',
98
+ >>> 'foreground-color',
99
+ >>> 'Foreground color',
100
+ >>> 'Foreground color',
101
+ >>> True,
102
+ >>> _default_foreground_color,
103
+ >>> GObject.ParamFlags.READWRITE,
104
+ >>> ],
105
+ >>> [
106
+ >>> 'color',
107
+ >>> 'background-color',
108
+ >>> 'Background color',
109
+ >>> 'Background color',
110
+ >>> True,
111
+ >>> _default_background_color,
112
+ >>> GObject.ParamFlags.READWRITE,
113
+ >>> ],
114
+ >>> ]
115
+
85
116
For documentation on the ``Gimp.Procedure.add_*_argument`` functions, see:
86
117
https://lazka.github.io/pgi-docs/#Gimp-3.0/classes/Procedure.html
87
- return_values: List of return values.
118
+ return_values: List of return values, or a function returning a list of
119
+ return values.
88
120
See ``arguments`` for more information about the contents and format of
89
121
the list.
90
122
@@ -111,7 +143,8 @@ def register_procedure(
111
143
specify it explicitly.
112
144
attribution: Plug-in authors, copyright notice and date.
113
145
This is a tuple of (authors, copyright notice, date) strings.
114
- auxiliary_arguments: List of auxiliary arguments.
146
+ auxiliary_arguments: List of auxiliary arguments, or a function returning
147
+ a list of auxiliary arguments.
115
148
See ``arguments`` for more information about the contentsn and format of
116
149
the list.
117
150
@@ -204,15 +237,15 @@ def register_procedure(
204
237
proc_dict = _PROCEDURE_NAMES_AND_DATA [proc_name ]
205
238
proc_dict ['procedure' ] = procedure
206
239
proc_dict ['procedure_type' ] = procedure_type
207
- proc_dict ['arguments' ] = _parse_and_check_parameters ( arguments )
208
- proc_dict ['return_values' ] = _parse_and_check_parameters ( return_values )
240
+ proc_dict ['arguments' ] = arguments
241
+ proc_dict ['return_values' ] = return_values
209
242
proc_dict ['menu_label' ] = menu_label
210
243
proc_dict ['menu_path' ] = menu_path
211
244
proc_dict ['image_types' ] = image_types
212
245
proc_dict ['sensitivity_mask' ] = sensitivity_mask
213
246
proc_dict ['documentation' ] = documentation
214
247
proc_dict ['attribution' ] = attribution
215
- proc_dict ['auxiliary_arguments' ] = _parse_and_check_parameters ( auxiliary_arguments )
248
+ proc_dict ['auxiliary_arguments' ] = auxiliary_arguments
216
249
proc_dict ['run_data' ] = run_data
217
250
proc_dict ['init_ui' ] = init_ui
218
251
proc_dict ['pdb_procedure_type' ] = pdb_procedure_type
@@ -227,12 +260,17 @@ def _parse_and_check_parameters(parameters):
227
260
if parameters is None :
228
261
return None
229
262
230
- if not isinstance (parameters , Iterable ):
263
+ if callable (parameters ):
264
+ processed_parameters = parameters ()
265
+ else :
266
+ processed_parameters = parameters
267
+
268
+ if not isinstance (processed_parameters , Iterable ):
231
269
raise TypeError ('Arguments and return values must be specified as a list-like iterable' )
232
270
233
- processed_parameters = {}
271
+ parsed_parameters = {}
234
272
235
- for param in parameters :
273
+ for param in processed_parameters :
236
274
processed_param = list (param )
237
275
238
276
if isinstance (processed_param , list ):
@@ -249,14 +287,14 @@ def _parse_and_check_parameters(parameters):
249
287
250
288
name = processed_param .pop (1 ).replace ('_' , '-' )
251
289
252
- if name in processed_parameters :
290
+ if name in parsed_parameters :
253
291
raise ValueError (f'Argument or return value named "{ name } " was already specified' )
254
292
255
- processed_parameters [name ] = processed_param
293
+ parsed_parameters [name ] = processed_param
256
294
else :
257
295
raise TypeError ('Only lists are allowed when specifying an argument or return value' )
258
296
259
- return processed_parameters
297
+ return parsed_parameters
260
298
261
299
262
300
def set_use_locale (enabled ):
@@ -337,6 +375,8 @@ def _do_query_procedures(_plugin_instance):
337
375
338
376
339
377
def _do_create_procedure (plugin_instance , proc_name ):
378
+ Gegl .init (None )
379
+
340
380
if proc_name in _PROCEDURE_NAMES_AND_DATA :
341
381
proc_dict = _PROCEDURE_NAMES_AND_DATA [proc_name ]
342
382
else :
@@ -386,17 +426,23 @@ def _do_create_procedure(plugin_instance, proc_name):
386
426
)
387
427
388
428
if proc_dict ['arguments' ] is not None :
389
- for name , params in proc_dict ['arguments' ].items ():
429
+ parsed_arguments = _parse_and_check_parameters (proc_dict ['arguments' ])
430
+
431
+ for name , params in parsed_arguments .items ():
390
432
param_type = params .pop (0 )
391
433
_get_add_param_func (procedure , param_type , 'argument' )(name , * params )
392
434
393
435
if proc_dict ['return_values' ] is not None :
394
- for name , params in proc_dict ['return_values' ].items ():
436
+ parsed_return_values = _parse_and_check_parameters (proc_dict ['return_values' ])
437
+
438
+ for name , params in parsed_return_values .items ():
395
439
param_type = params .pop (0 )
396
440
_get_add_param_func (procedure , param_type , 'return_value' )(name , * params )
397
441
398
442
if proc_dict ['auxiliary_arguments' ] is not None :
399
- for name , params in proc_dict ['auxiliary_arguments' ].items ():
443
+ parsed_auxiliary_arguments = _parse_and_check_parameters (proc_dict ['auxiliary_arguments' ])
444
+
445
+ for name , params in parsed_auxiliary_arguments .items ():
400
446
param_type = params .pop (0 )
401
447
_get_add_param_func (procedure , param_type , 'aux_argument' )(name , * params )
402
448
@@ -464,8 +510,6 @@ def func_wrapper(*procedure_and_args):
464
510
if init_ui and run_mode == Gimp .RunMode .INTERACTIVE :
465
511
GimpUi .init (procedure .get_name ())
466
512
467
- Gegl .init ()
468
-
469
513
return_values = func (* procedure_and_args )
470
514
471
515
if return_values is None :
0 commit comments