Posts: 11
Threads: 4
Joined: Mar 2023
Reputation:
0
Gimp version:
Operating system(s): Windows Vista or 7, 8, 10 (64-bit)
Linux
Yesterday, 08:37 PM
(This post was last modified: Yesterday, 09:31 PM by silenuznowan.)
I had a function in version 2 that inserted layers into a specific layer group.
I'm now trying to use the same procedure in Gimp 3 but have run into a type error.
First here's the doc:
Quote:This procedure adds the specified layer to the image at the given position. If the specified parent is a valid layer group (See 'gimp-item-is-group' and 'gimp-layer-group-new') then the layer is added inside the group. If the parent is 0, the layer is added inside the main stack, outside of any group. The position argument specifies the location of the layer inside the stack (or the group, if a valid parent was supplied), starting from the top (0) and increasing. If the position is specified as -1 and the parent is specified as 0, then the layer is inserted above the active layer, or inside the group if the active layer is a layer group. The layer type must be compatible with the image base type.
However if I pass a valid GroupLayer as the value of parent I get the following error:
Code:
TypeError: could not convert (<PDBStatusType.SUCCESS: 3>, <Gimp.GroupLayer object at 0x7f359387fa40 (GimpGroupLayer at 0x5596f842c040)>)
to type 'GimpLayer' when setting property 'GimpProcedureConfig-gimp-image-insert-layer.parent'
Is there another way to insert layers into a specific LayerGroup ?
Edit I was trying to further figure things out and I notice that passing a LayerGroup to the procedure gimp-item-is-group results in a similar typecast error:
Code:
TypeError: could not convert (<PDBStatusType.SUCCESS: 3>, <Gimp.GroupLayer object at 0x7f70a7ebb180 (GimpGroupLayer at 0x5561a26ba060)>)
to type 'GimpItem' when setting property 'GimpProcedureConfig-gimp-item-is-group.item'
Thanks.
This was human error on my part passed the wrong item. Sorry.
Posts: 6,636
Threads: 288
Joined: Oct 2016
Reputation:
583
Gimp version:
Operating system(s): Linux
Yesterday, 09:36 PM
(This post was last modified: Yesterday, 09:42 PM by Ofnuts.)
It looks like you are using the PDB to make API calls. This is unnecessary, and terribly protracted. This also means that the returned value is a tuple with 1) a status ( PDBStatusType.SUCCESS) and 2) the value you want: ( <Gimp.GroupLayer object at 0x7f70a7ebb180 (GimpGroupLayer at 0x5561a26ba060)>) .
So either:
- you unpack all you calls: success, group = procedure.run(config);
- you forego the PDB (just usable to call plugins) and use the API directly: group=layer.get_parent()
Choose wisely. The "direct" API is described here: Gimp – 3.0. If used properly, your Gimp3 code is shorter and more readable than your Gimp2 code. And if used cleverly, your IDE can catch most errors before you run the code. In Gimp v2, on a simple script, 75% of my runs where just catching plain code errors (like wrong arguments, etc...) vs 25% for logic error and real debugging. Now I am 5%-95%. The number of logic errors is about the same, but the code runs decently on first try in most cases.
Posts: 11
Threads: 4
Joined: Mar 2023
Reputation:
0
Gimp version:
Operating system(s): Windows Vista or 7, 8, 10 (64-bit)
Linux
Yesterday, 09:44 PM
(This post was last modified: Yesterday, 10:10 PM by silenuznowan.)
(Yesterday, 09:36 PM)Ofnuts Wrote: It looks like you are using the PDB to make API calls. This is unnecessary, and terribly protracted. This also means that the returned value is a tuple with 1) a status (PDBStatusType.SUCCESS) and 2) the value you want: (<Gimp.GroupLayer object at 0x7f70a7ebb180 (GimpGroupLayer at 0x5561a26ba060)>) .
So either:
- you unpack all you calls: success, group = procedure.run(config);
- you forego the PDB (just usable to call plugins) and use the API directly: group=layer.get_parent()
Choose wisely. The "direct" API is described here: Gimp – 3.0
I'm following the docs my function looks like this:
Code:
def insert_layer(image: Gimp.Image, layer: Gimp.Layer, parent: Gimp.Layer, position: int = 0) -> bool:
procedure = Gimp.get_pdb().lookup_procedure('gimp-image-insert-layer')
config = procedure.create_config()
config.set_property('image', image)
config.set_property('layer', layer)
config.set_property('parent', parent)
config.set_property('position', position)
result = procedure.run(config)
success = result.index(0)
return success
And Is_group
Code:
def is_group_item (item: Gimp.Item):
procedure = Gimp.get_pdb().lookup_procedure('gimp-item-is-group');
config = procedure.create_config();
config.set_property('item', item);
result = procedure.run(config);
success = result.index(0);
group = result.index(1)
return group
So I'm not sure what your suggesting I do differently.
Also how do I now get a name of the layer? layer.name results in an error that attribute is not found.
Thanks for the pointer to the direct api docs that looks good. Solved my getting the name problem.
Thanks.
Posts: 6,636
Threads: 288
Joined: Oct 2016
Reputation:
583
Gimp version:
Operating system(s): Linux
Yesterday, 10:14 PM
(This post was last modified: Yesterday, 10:15 PM by Ofnuts.)
(Yesterday, 09:44 PM)silenuznowan Wrote: I'm following the docs my function looks like this:
Code:
def insert_layer(image: Gimp.Image, layer: Gimp.Layer, parent: Gimp.Layer, position: int = 0) -> bool:
procedure = Gimp.get_pdb().lookup_procedure('gimp-image-insert-layer')
config = procedure.create_config()
config.set_property('image', image)
config.set_property('layer', layer)
config.set_property('parent', parent)
config.set_property('position', position)
result = procedure.run(config)
success = result.index(0)
return success
And Is_group
Code:
def is_group_item (item: Gimp.Item):
procedure = Gimp.get_pdb().lookup_procedure('gimp-item-is-group');
config = procedure.create_config();
config.set_property('item', item);
result = procedure.run(config);
success = result.index(0);
group = result.index(1)
return group
So I'm not sure what your suggesting I do differently.
Also how do I now get a name of the layer? layer.name results in an error that attribute is not found.
Thanks.
So, you are doing the very hard way, try this instead:
Code:
image.insert_layer(layer,parent,position) # from https://developer.gimp.org/api/3.0/libgimp/method.Image.insert_layer.html
isGroup=iterm.isGroup(). # from https://developer.gimp.org/api/3.0/libgimp/method.Item.is_group.html
# But can also be done with instanceof (item,Gimp.GroupLayer)
Things are much simpler than you think:
Posts: 11
Threads: 4
Joined: Mar 2023
Reputation:
0
Gimp version:
Operating system(s): Windows Vista or 7, 8, 10 (64-bit)
Linux
(Yesterday, 10:14 PM)Ofnuts Wrote: (Yesterday, 09:44 PM)silenuznowan Wrote: I'm following the docs my function looks like this:
Code:
def insert_layer(image: Gimp.Image, layer: Gimp.Layer, parent: Gimp.Layer, position: int = 0) -> bool:
procedure = Gimp.get_pdb().lookup_procedure('gimp-image-insert-layer')
config = procedure.create_config()
config.set_property('image', image)
config.set_property('layer', layer)
config.set_property('parent', parent)
config.set_property('position', position)
result = procedure.run(config)
success = result.index(0)
return success
And Is_group
Code:
def is_group_item (item: Gimp.Item):
procedure = Gimp.get_pdb().lookup_procedure('gimp-item-is-group');
config = procedure.create_config();
config.set_property('item', item);
result = procedure.run(config);
success = result.index(0);
group = result.index(1)
return group
So I'm not sure what your suggesting I do differently.
Also how do I now get a name of the layer? layer.name results in an error that attribute is not found.
Thanks.
So, you are doing the very hard way, try this instead:
Code:
image.insert_layer(layer,parent,position) # from https://developer.gimp.org/api/3.0/libgimp/method.Image.insert_layer.html
isGroup=iterm.isGroup(). # from https://developer.gimp.org/api/3.0/libgimp/method.Item.is_group.html
# But can also be done with instanceof (item,Gimp.GroupLayer)
Things are much simpler than you think:
Yes I already used instanceof to track down my earlier error.
I also moved from using Kate to using an actual IDE and those all come up as suggestions when working with the objects, which is nice.
And even easier yet after I switched from executing the python in a bash script and instead launch the python script from the command line as I get better debug information.
However is there an easier way to launch my script then using the following:
flatpak run --user org.gimp.GIMP -i --batch-interpreter=python-fu-eval -b 'import sys; sys.path=["."]+sys.path;import pytest;pytest.run("./pytest.py")'
Whats the easiest command to start gimp and execute script using the commandline.
Thanks.
Posts: 6,636
Threads: 288
Joined: Oct 2016
Reputation:
583
Gimp version:
Operating system(s): Linux
(Yesterday, 11:28 PM)silenuznowan Wrote: (Yesterday, 10:14 PM)Ofnuts Wrote: (Yesterday, 09:44 PM)silenuznowan Wrote: I'm following the docs my function looks like this:
Code:
def insert_layer(image: Gimp.Image, layer: Gimp.Layer, parent: Gimp.Layer, position: int = 0) -> bool:
procedure = Gimp.get_pdb().lookup_procedure('gimp-image-insert-layer')
config = procedure.create_config()
config.set_property('image', image)
config.set_property('layer', layer)
config.set_property('parent', parent)
config.set_property('position', position)
result = procedure.run(config)
success = result.index(0)
return success
And Is_group
Code:
def is_group_item (item: Gimp.Item):
procedure = Gimp.get_pdb().lookup_procedure('gimp-item-is-group');
config = procedure.create_config();
config.set_property('item', item);
result = procedure.run(config);
success = result.index(0);
group = result.index(1)
return group
So I'm not sure what your suggesting I do differently.
Also how do I now get a name of the layer? layer.name results in an error that attribute is not found.
Thanks.
So, you are doing the very hard way, try this instead:
Code:
image.insert_layer(layer,parent,position) # from https://developer.gimp.org/api/3.0/libgimp/method.Image.insert_layer.html
isGroup=iterm.isGroup(). # from https://developer.gimp.org/api/3.0/libgimp/method.Item.is_group.html
# But can also be done with instanceof (item,Gimp.GroupLayer)
Things are much simpler than you think:
Yes I already used instanceof to track down my earlier error.
I also moved from using Kate to using an actual IDE and those all come up as suggestions when working with the objects, which is nice.
And even easier yet after I switched from executing the python in a bash script and instead launch the python script from the command line as I get better debug information.
However is there an easier way to launch my script then using the following:
flatpak run --user org.gimp.GIMP -i --batch-interpreter=python-fu-eval -b 'import sys; sys.path=["."]+sys.path;import pytest;pytest.run("./pytest.py")'
Whats the easiest command to start gimp and execute script using the commandline.
Thanks.
Beside defining a function/alias/script to run that line, I don't know. I've not yet trying batching things in Gimp3.
Posts: 11
Threads: 4
Joined: Mar 2023
Reputation:
0
Gimp version:
Operating system(s): Windows Vista or 7, 8, 10 (64-bit)
Linux
A quick question about the direct api, from the docs how do you tell when a tuple is expected.
I wanted to get the offsets for a layer so I tried calling 'layer.get_offsets()' like
Code:
x,y = layer.get_offset()
This resulted in an error. I checked the docs for get_offsets and it listed return value as boolean, so I thought maybe wrong function and at the time I was tired so I just used the old way using pdb.
Today I took the time to look at the type hints and see it returns a tuple of success,x,y, so I fixed things. So when using the api docs can I assume that if the description says it returns x and the return value is boolean, that the function will return a tuple of boolean,x ?
Thanks
|