Last active
May 20, 2025 12:28
-
-
Save hnbdr/2c28a02d48a9f5c8127a29b8c551aec9 to your computer and use it in GitHub Desktop.
call_procedure.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # Beware, support for set_core_object_array and set_color_array is poorly tested. | |
| def call_procedure(name, **kwargs): | |
| procedure = Gimp.get_pdb().lookup_procedure(name) | |
| if not procedure: | |
| raise RuntimeError(f"Procedure '{name}' not found") | |
| config = procedure.create_config() | |
| for key, value in kwargs.items(): | |
| argument = procedure.find_argument(key) | |
| if not argument: | |
| raise RuntimeError(f"Argument '{key}' not found in procedure '{name}'") | |
| # TODO: do it better? | |
| type_name = argument.value_type.name | |
| if type_name == 'GimpCoreObjectArray': | |
| config.set_core_object_array(key, value) | |
| elif type_name == 'GimpColorArray': | |
| config.set_color_array(key, value) | |
| else: | |
| config.set_property(key, value) | |
| result = procedure.run(config) | |
| # Check if the first value is success | |
| success = result.index(0) | |
| if not success: | |
| raise RuntimeError(f"Procedure '{name}' failed with error: {result.index(1)}") | |
| # Return the result: single value if only one, or an array if more | |
| if result.length() == 2: | |
| return result.index(1) # Only one value, return it directly | |
| else: | |
| return [result.index(i) for i in range(1, result.length())] # Multiple values, return as array | |
| # Usage | |
| file = Gio.File.new_for_path(fullpath) | |
| args = { | |
| 'image': img, | |
| 'file': file, | |
| 'compression': 0 | |
| } | |
| call_procedure('file-png-export', **args) | |
| if call_procedure('gimp-item-is-group', item=layer): | |
| pass | |
| xres, yres = call_procedure('gimp-get-monitor-resolution') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment