我是 python 的新手,甚至是为 gimp 创建插件的新手,所以我一直在尝试在会话之间保存特定值时遇到问题。更具体地说,我想保存两个文件夹目录,这样用户就不必在每次打开 gimp 时都选择它们。
我尝试了一些方法,例如使用“pdb.gimp_set_data()”或使用“json.dump(config, f)”来制作配置文件。所有这些方法都无济于事,尽管我想那是因为我用错了它们。所以我决定重新开始并寻求帮助。
register(
'ofn-layer-to-tiles-rc',
splitDescRC,splitDescRC+whoiam,author,author,year,splitDescRC+'...',
'*',
[
(PF_IMAGE, 'image', 'Input image', None),
(PF_DRAWABLE, 'layer', 'Layer', None),
(PF_STRING, 'pokeName', 'Pokemon', ""),
(PF_OPTION, 'direction', 'Facing direction', Direction.SOUTH,Direction.labels),
(PF_OPTION, 'shadowsize', 'Size of shadow', ShadowSize.MEDIUMSHADOW,ShadowSize.labels),
(PF_BOOL, 'loop', 'Loop gif', True),
(PF_DIRNAME, 'pngFolder', 'Spritesheet folder', None),
(PF_DIRNAME, 'gifFolder', 'Finished gif folder', None),
],
[],
layerToTilesRC,
menu=menu
)
上面的代码是让用户决定很多事情的注册码。我特别希望跨会话记住“pngFolder”和“gifFolder”值,但是在更改代码中的值时我并没有走得太远。
如果有人有任何建议甚至解决方案,将不胜感激,谢谢!
如果你这样定义一个插件:
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import datetime
from gimpfu import *
### Plugin body
def plugin(*args):
gimp.message('Called with %r' % (args,))
### Registration
register(
"testString","Test plugin parms","Test plugin parms",
"","","",
"Test plugin string arg",
"", # Accepted image type
[
(PF_STRING, "string", "String arg", datetime.datetime.now().isoformat()),
],
[],
plugin,
menu="<Image>/Test",
)
main()
您会发现该参数的默认值是您在启动 Gimp 后首次调用插件的时间(进一步调用将重新使用之前插件执行中使用的值)。这意味着该值是在调用插件执行时由代码在运行时设置的,而不是注册时冻结的值。
所以你确实可以将你的值保存在某个地方,并且每个脚本运行,代码从文件中加载它们,所以你的脚本看起来像:
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import datetime
from gimpfu import *
# load args from file
previousPngFolder,previousGifFolder=loadSavedValues()
### Plugin body
def plugin(...,pngFolder,gifFolder,...):
saveValues(pngFolder,gifFolder)
moreCode
### Registration
register(
[
....
(PF_DIRNAME,'pngFolder','Spritesheet folder',previousPngFolder),
(PF_DIRNAME,'gifFolder','Finished gif folder',previousGifFolder),
....
],
[],
plugin,
menu="<Image>/Test",
)
main()
有很多方法和地方可以存储值。对于您的使用,最好的地方是用户 Gimp 配置文件中的预设目录(通过
os.path.join(gimp.directory,'tool-presets')
获得)。考虑到简单的结构读/写文件可以用普通的 Python I/O 完成,但是使用 JSON 或“.ini”格式(使用ConfigParser
模块)也是可能的(其他脚本中的一些例子你得到了初始代码)。
PS:注册中的第一个参数,又名“atom”,意味着是唯一的,这就是为什么作者使用他们自己的前缀(
ofn-
这里)来避免冲突,所以当你借用代码时你应该做的第一个就是把原子换成自己...