2

Today I wrote a short python script that should rename a list of layers of a gimp image to add some flags, which are used when the layers are part of an animation. The script is integrated into the Gimp menu and the information provided in the Python-Fu console is correct. But if I execute the script within the Python-Fu console or from the action menu, nothing happens. I was not able to locate a problem. Posting the function content by hand to the console works.

Does someone know a solution to this problem or did face the same problem in Gimp 2.8?

#!/usr/bin/env python
from gimpfu import *
import os

def renameAllLayers(img):
    print 'Starting ...\n'
    
    for layer in img.layers:
        print layer.name + '\n'
        if layer.name[-16:] is '(66ms) (replace)':
            pass
        elif layer.name[-17:] is '(200ms) (replace)':
            pass
        elif layer.name[-10:] is ' (replace)':
            layer.name = layer.name[:-10] + '(66ms) (replace)'
            
        print layer.name + '\n'
    
    print 'Finished ...\n'
            
register(
    proc_name=("rename_all_layers"),
    label=("Rename all layers"),
    blurb=("Rename all layers so they include all missing flags"),
    help=("Help?"),
    author=("Andreas Schickedanz"),
    date=("2013"),
    menu=("<Image>/Scripts/Layer Renamer"), 
    imagetypes=("*"),
    params=[
        (PF_IMAGE, "img", "Select an image", None),
    ],
    results=[],
    function=(renameAllLayers)
)

main()
Phidelux
  • 2,043
  • 1
  • 32
  • 50
  • 2
    In Python, you don't need semi-colons at the end of the line. Also, can you post the output of `/usr/bin/env python --version` on your system? Also, `is` checks for identity, not equality, and you'd want to use `endswith` anyways. – phihag Apr 15 '13 at 16:30
  • just to insist: don't put `;` at the end of lines – Stephane Rolland Apr 15 '13 at 16:39
  • Why shouldn't I? As far as I know it is part of the standard even if it is not needed. – Phidelux Apr 15 '13 at 17:22
  • @Avedo - to avoid being told not to do so everytime you show your code to somebody. – mata Apr 15 '13 at 19:03
  • @Avedo: See [this question](http://stackoverflow.com/questions/8236380/why-is-semicolon-allowed-in-this-python-snippet) as to why not. The main reason: a semicolon in Python is not the same as a semicolon in other languages like C++ or Java. In those languages, a semicolon is a terminator: it says "this statement is finished, the next should begin". In Python, it is a separator: it says "This statement has finished, but there are more statements on this lines". By ending the line with a semicolon, that "other statement" is an empty statement, which while syntactically OK, makes no sense. – Mark Hildreth Apr 15 '13 at 19:08
  • Also, it's pretty standard in the Python developer community to NOT use semicolons to end lines, so it would be better to get out of the habit since if you ever end up programming with a group of other Python programmers, there is a good chance they WON'T use the semicolon at the end, and thus you will have different conventions to programming (which is annoying). – Mark Hildreth Apr 15 '13 at 19:10

1 Answers1

1

In your script you compare strings using is, which checks for object identity, so it will always return False in this cases, therfore your layers aren't renamed.

Use == instead to compare strings. Besides that your script works perfectly.

nneonneo
  • 171,345
  • 36
  • 312
  • 383
mata
  • 67,110
  • 10
  • 163
  • 162