def recursiveSelectLayers(selectedLayers,parent,all):
for pos,layerOrGroup in enumerate(parent.layers):
if all or layerOrGroup.visible:
if isinstance(layerOrGroup,gimp.GroupLayer):
recursiveSelectLayers(selectedLayers,layerOrGroup,all):
else:
# Add you own criteria here (linked, etc...)
selectedLayer.append(layerOrGroup)
apply_layers = []
recursiveSelectLayers(apply_layer,image,all)
A question I cannot answer for you is whether a layer in a linked group is considered linked or not.
The code below assume we don't care about groups
An efficient way to handle this is to have an array of test functions and to pass the function as a parameter:
# instead of having a cascade of if's, let's define simple yay/nay test functions
# https://www.youtube.com/watch?v=z43bmaMwagI
def testAll(layerOrGroup):
return True;
def testVisible(layerOrGroup):
return layerOrGroup.visible
# we assume the grous are always linked if the test only pertains to the layer
def testLinked(layerOrGroup):
return true if isinstance(layerOrGroup,gimp.GroupLayer) else layerOrGroup.linked
def testNotLinked(layerOrGroup):
return true if isinstance(layerOrGroup,gimp.GroupLayer) else not layerOrGroup.linked
# The list of yay/nay functions. Note that we never deal explicitly with the index
# Functions and labels are implicitly linked here, the array can be reordered without
# having to change the code elsewhere
# First item is the name as show in the dialog, second item is the function to use
testFunctions=[('All',testAll),('Visible',testVisible),('Linked',testLinked),('Not linked',testNotLinked)]
def recursiveSelectLayers(selectedLayers,parent,testFunction):
for pos,layerOrGroup in enumerate(parent.layers):
if testFunction(layerOrGroup):
if isinstance(layerOrGroup,gimp.GroupLayer):
recursiveSelectLayers(selectedLayers,layerOrGroup,testFunction):
else:
selectedLayer.append(layerOrGroup)
def python_run_code_on_visible_layers(image, layer, test, code):
# 'test' is really an index over testFunctions, so e can get the function member of the tuples
testFunction=testFunctions[test][1]
apply_layers = []
recursiveSelectLayers(apply_layers,image,testFunction):
# and in the registration, we use the first item of the tuples as the function label.
(PF_OPTION, "test","Choose on what layers", 1, [tf[0] for tf in testFunctions]),
Warning: untested, syntax errors can happen, this doesn't invalidate the design.