插件调试技巧

99 阅读2分钟

用户在编写一个创建位图字体的插件时遇到了问题,插件无法正常工作。尽管他尝试了从不同来源复制代码,但问题仍然存在。他尝试使用 GIMP Python-Fu 控制台进行调试,但没有得到有用的信息。他也尝试在 Python 脚本中运行代码片段,但它们可以正常工作。因此,他想知道如何调试插件。

huake_00152_.jpg 2、解决方案 首先,建议用户从代码中删除第一行的 Shebang。另外,用户在注释代码时使用了一种不合适的方式。他应该使用适当的注释方法,例如使用多行注释或单行注释。

# Create Image
img = gimp.Image(cwidth * 10, cheight * 10, RGB)
img.disable_undo()

# Save the current foreground color:
pdb.gimp_context_push()

# Set the text color & background color
gimp.set_foreground(color)
gimp.set_background(0, 0, 0)

# Create All Layers & Position Accordingly
for i in range(char_begin, char_end):
    string = '%c' % i
    offset = i - char_begin

    x_pos = offset * cwidth
    y_pos = offset * cheight

    text_layer = pdb.gimp_text_fontname(img, None, x_pos, y_pos, string, -1, False, size, PIXELS, font)

    gimp.progress_update(float(offset) / float(num_chars))

pdb.gimp_image_flatten(img)

img.enable_undo()

# Create a new image window
gimp.Display(img)
# Show the new image window
gimp.displays_flush()

# Restore the old foreground color:
pdb.gimp_context_pop()

其次,提供了另外一种调试插件的方法。用户可以在 Python-Fu 控制台中使用 pdb 模块来设置断点并逐步执行代码。这样,他就可以检查变量的值并在代码中找到问题所在。

import pdb

def create_font(cwidth, cheight, font, size, color):
    # Set GLOBAL
    char_begin = 32
    char_end = 127
    num_chars = char_end - char_begin

    # Figure out total width & height
    twidth = cwidth * 10
    theight = cheight * 10

    # Create Image
    pdb.set_trace()
    img = gimp.Image(cwidth * 10, cheight * 10, RGB)
    img.disable_undo()

    # Save the current foreground color:
    pdb.gimp_context_push()

    # Set the text color & background color
    gimp.set_foreground(color)
    gimp.set_background(0, 0, 0)

    # Create All Layers & Position Accordingly
    for i in range(char_begin, char_end):
        pdb.set_trace()
        string = '%c' % i
        offset = i - char_begin

        x_pos = offset * cwidth
        y_pos = offset * cheight

        text_layer = pdb.gimp_text_fontname(img, None, x_pos, y_pos, string, -1, False, size, PIXELS, font)

        gimp.progress_update(float(offset) / float(num_chars))

    pdb.gimp_image_flatten(img)

    img.enable_undo()

    # Create a new image window
    gimp.Display(img)
    # Show the new image window
    gimp.displays_flush()

    # Restore the old foreground color:
    pdb.gimp_context_pop()

register(
    "python_fu_bitmap_font",
    "Bitmap Font",
    "Create a new bitmap font",
    "*****",
    "*****",
    "2013",
    "Bitmap Font (Py)...",
    "",      # Create a new image, don't work on an existing one
    [
        (PF_SPINNER, "cwidth", "Cell Width", 24, (1, 3000, 1)),
        (PF_SPINNER, "cheight", "Cell Height", 51, (1, 3000, 1)),
        (PF_FONT, "font", "Font face", "Consolas"),
        (PF_SPINNER, "size", "Font size", 50, (1, 3000, 1)),
        (PF_COLOR, "color", "Text color", (1.0, 0.0, 0.0))
    ],
    [],
    create_font, menu="<Image>/File/Create")

main()

通过在代码中的关键位置设置断点,用户可以在 Python-Fu 控制台中逐步执行代码,并检查变量的值。这样,他就可以更容易地找到问题并进行修复。