11-05-2024, 10:34 AM
(11-04-2024, 08:13 PM)nchen Wrote: Thank you! That would save me a lot of time copying and pasting. I have been trying to implement this approach but I can't seem to get it to work.
Do the functions in the 'libmodule.py' have to be registered similar to the GIMP plugins, even though GIMP doesn't check them?
Also, are the input and output capabilities of functions within libmodule.py limited to GIMP's type codes (PF_INT, PF_FLOAT, etc)?
I have tried both registering them and not registering them to no avail.
For the example structure:
plug-ins
├── bar
│ ├── bar.py
│ └── libmodule.py
If I have a function within libmodule.py:
Any time I attempt to import libmodule (with and without the register within libmodule.py), bar.py fails to register.Code:
def insert_new_layer_with_alpha(image, width, height, name="New Layer"):
new_layer = pdb.gimp_layer_new(image, width, height, 0, name, 100, 28)
new_layer.add_alpha()
pdb.gimp_image_insert_layer(image, new_layer, None, 0)
return new_layer
register(
"python_fu_insert-new-layer-with-alpha", # Function Name
"Insert new layer with alpha channel", # Description
"Insert a new layer with an alpha channel", # Help
"user", # Author
"user", # Copyright
"11/4/2024", # Date Created
"insert...", # Menu label
"", # Image types
[
(PF_IMAGE, "image", "Image", None),
(PF_INT, "int", "Width", 0),
(PF_INT, "int", "Height", 0),
(PF_STRING, "string", "Layer name", "New Layer")
],
[
(PF_DRAWABLE, "new_layer", "New layer")
],
insert_new_layer_with_alpha, menu="<Image>/Comfy Tools")
I have also tried registering and then calling insert_new_layer_with_alpha directly without importing.
I did test if the function within libmodule.py registers successfully to be sure it's not an error within the function.
The only time I've been able to get bar.py to register is when I don't attempt to import or call the functions within libmodule.py.
EDIT:
A kind of funny workaround I've been using sometimes is just installing a newer python version on my machine and calling it from the plugins:
Code:
import subprocess
input_data = data
# Run the script and pass the input data
process = subprocess.Popen(
["py", "path/to/script/python3script.py"],
stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE
)
stdout, stderr = process.communicate(input=input_data.encode('utf-8'))
result = stdout if process.returncode == 0 else stderr
If you use the Gimp API in the "libmodule" it should also do the required Gimp imports.