Почему я не могу добавить переменную в плагин GIMP?
Хорошо, я пытаюсь разработать небольшой плагин для GIMP, который делает растровый шрифт. По какой-то причине теперь, когда я добавил следующий код, он не будет работать.
current_cell_x = 0
current_cell_y = 0
x_pos = current_cell_x * cwidth
y_pos = current_cell_y * cheight
x_pos += cwidth / 2.0
y_pos += cheight / 2.0
x_pos -= text_layer.width / 2.0
y_pos -= text_layer.height / 2.0
Даже первой строки с current_cell_x = 0 достаточно, чтобы весь плагин не работал.
Вот весь код в рабочем формате:
from gimpfu import *
def create_font(cwidth, cheight, xcells, ycells, font, size, color) :
char_begin = 32
char_end = 127
num_chars = char_end - char_begin
#current_cell_x = 0
#current_cell_y = 0
# Figure out total width & height
twidth = int(cwidth * xcells)
theight = int(cheight * ycells)
# Create Image
img = gimp.Image(twidth, theight, 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
text_layer = pdb.gimp_text_fontname(img, None, 0, 0, string, -1, True, size, PIXELS, font)
#x_pos = current_cell_x * cwidth
#y_pos = current_cell_y * cheight
#x_pos += cwidth / 2.0
#y_pos += cheight / 2.0
#x_pos -= text_layer.width / 2.0
#y_pos -= text_layer.height / 2.0
#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_SPINNER, "xcells", "Cells on X axis", 10, (1, 3000, 1)),
(PF_SPINNER, "ycells", "Cells on Y axis", 10, (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()
Однако, когда я отменяю комментарии в строках 9 - 10 и строках 33 - 40, я получаю следующие ошибки.
Я хотел, чтобы мой окончательный код выглядел примерно так:
def create_font(cwidth, cheight, xcells, ycells, font, size, color) :
char_begin = 32
char_end = 127
num_chars = char_end - char_begin
current_cell_x = 0
current_cell_y = 0
# Figure out total width & height
twidth = int(cwidth * xcells)
theight = int(cheight * ycells)
# Create Image
img = gimp.Image(twidth, theight, 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
text_layer = pdb.gimp_text_fontname(img, None, 0, 0, string, -1, True, size, PIXELS, font)
pdb.plug_in_autocrop_layer(0, img, text_layer)
x_pos = float(current_cell_x * cwidth)
y_pos = float(current_cell_y * cheight)
x_pos += float(cwidth) / 2.0
y_pos += float(cheight) / 2.0
x_pos -= float(text_layer.width) / 2.0
y_pos -= float(text_layer.height) / 2.0
text_layer.translate(x_pos, y_pos)
#Set current cell
current_cell_x += 1
if current_cell_x > xcells:
current_cell_x = 0
current_cell_y += 1
if current_cell_y > ycells:
break
gimp.progress_update(float(offset) / float(num_chars))
#pdb.gimp_image_flatten(img) # Don't do this
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()
Если вы видите какие-либо предложения, я бы не возражал.
Примечание. Получил мой код по https://github.com/sole/snippets/blob/master/gimp/generate_bitmap_font/sole_generate_bitmap_font.py и http://gimpbook.com/scripting/
Запустил 'python -tt plugin.py' и получил следующее:
2 ответа
У меня были противоречивые отступы, мне нужно было перейти в "Настройки"> "Установки"> "Языковое меню / Настройки вкладок"> "Настройки вкладок" и затем выбрать "Заменить на пробел".
Тогда мне нужно было бежать
python -tt yourscript.py
Что позволило мне избавиться от всех ошибок отступов, которые были вызваны моим edior.
Я также использовал reindent.py из папки Python33/Tools/Scripts, но у меня это не сработало. Это, возможно, помогло в некотором роде, я не видел.
Матиас был на полпути прав, но не дал ответа. Я делаю это на его месте.
Ошибка в окнах CMD говорит об этом: вы смешиваете табуляции и пробелы в исходном коде - убедитесь, что строки, добавляемые в код, имеют префикс только с пробелами, а не символы табуляции (\x09).
Вы должны настроить программу редактора, которую вы используете для редактирования кода Python, чтобы не использовать символы табуляции вообще.
(возможно, все наоборот, вы редактируете файл с вкладками, и ваш редактор правильно настроен на использование пробелов - в этом случае вы должны заменить все вкладки)
Обратитесь к руководству по стилю Python: http://www.python.org/dev/peps/pep-0008/