Help! Python loop for the mentally challenged. - Printable Version +- Gimp-Forum.net (https://www.gimp-forum.net) +-- Forum: GIMP (https://www.gimp-forum.net/Forum-GIMP) +--- Forum: Extending the GIMP (https://www.gimp-forum.net/Forum-Extending-the-GIMP) +---- Forum: Scripting questions (https://www.gimp-forum.net/Forum-Scripting-questions) +---- Thread: Help! Python loop for the mentally challenged. (/Thread-Help-Python-loop-for-the-mentally-challenged) |
Help! Python loop for the mentally challenged. - david - 09-11-2020 I have channels, named 0 to 9. To create layers 0 to 9, I can repeat the code, changing "0" to "1", "2", etc. and it works perfectly. I would like to make a loop to carry out this process, but everything I have tried has given me errors and searching the internet hasn't provided any examples for me to follow. An example of how to do this (ideally with a detailed explanation) would be appreciated and perhaps would help others. david. Code: #create layer with 0 as mask RE: Help! Python loop for the mentally challenged. - tmanni - 09-11-2020 If the channels named 0 to 9 are the only channels in the image, you can iterate on a copy of the channels list: Code: for channel in image.channels[:]: image.channels[:] creates a copy of that list (since you delete the channel at the end of the loop, you need to work on a copy of the list) channel.name gives the name of the channel as string RE: Help! Python loop for the mentally challenged. - david - 09-11-2020 tmanni, Many thanks for your reply. I did a cut & paste of your code into my script and it worked perfectly. Now I must study it carefully so that I can do something similar in another part of the same script. It also helps me to understand a little more about python, although I suspect this "dog" is much to old to learn new tricks! david. |