モブ沢工房

プログラミングとかLinux関連(特にOSSのグラフィックツール関連)とかレトロゲームとか3Dプリンタやら日曜大工等、色々。

先日作ったgimp用プラグイン・automate-workflow.pyを改造

以前作りましたgimp用python-fuプラグイン「automate-workflow.py」、自分で不要とか言ってみましたが実際使ってみると至極便利。 そんなわけでさらに改良を加えました。

今度の改良点は実にシンプルなもので、今まで/tmpに吐いていたエラーログを、gtkのMessageDialogに吐いてその場で表示するようにしたというだけのものです。

しかし、これがあるのとないのでは開発効率に歴然とした差が…お絵かきの修正に使ってみましたが、非常に便利な反面、エラーで終了したのか成功したのか分からない瞬間があり(gimpプログレスバーが動くため、一見成功したように見えるけど最後のエクスポート直前で例外出して抜けてたりとか)、結構無意味に手間がかかったような場面も多々あり。

そんなわけで、結局こういう仕組みにしたわけです。よって、今回からpygtkが必須となりました…

#!/usr/bin/env python
# -*- coding: UTF-8 -*-
#[license] GPLv3
#[plugin]
#[name] automate-workflow
#[desc] 
# 画像ファイルのディレクトリにgimp_automate.pyを置くことで、そのファイルのmain関数を呼び出す。
# これにより、その画像ファイルに対する固定的ワークフローを自動化するためのスクリプト。
#
# main関数の引数は (gimpモジュール , Imageオブジェクト,  drawableオブジェクト)
# pdbへのアクセスはgimp.pdbで可能
#[version]
#0.1 初期リリース
#0.2 gimp.pdbがあることに気づいたので、pdbはgimp.pdbを使うように変更
#0.3 例外キャッチ時にダイアログに吐く機能を追加
#[end]

#  このプログラムはGPLライセンスver3で公開します。
# 
#   This program is free software: you can redistribute it and/or modify
#   it under the terms of the GNU General Public License as published by
#   the Free Software Foundation, either version 3 of the License, or
#   (at your option) any later version.
#
#   This program is distributed in the hope that it will be useful,
#   but WITHOUT ANY WARRANTY; without even the implied warranty of
#   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#   GNU General Public License for more details.
#
#   You may have received a copy of the GNU General Public License
#   along with this program.  If not, see <http://www.gnu.org/licenses/>.           
#
#   Copyright (C) 2013 dothiko(http://dothiko.blog.fc2.com/) 


from gimpfu import *
import os
import sys

import pygtk
pygtk.require("2.0")
import gtk

DYNEXE_NAME="gimp_automate"

def python_fu_automate_workflow(a_img,a_drawable,sample_arg=True):

    if a_img.filename==None:
        gimp.message("このスクリプトは、新規ファイルでは使えません")
        return


    basename,ext=os.path.splitext(a_img.filename)
    basedir=os.path.dirname(a_img.filename)
    modname="%s.py" % DYNEXE_NAME 
    modpath="%s/%s" % (basedir,modname)
    if not os.path.exists(modpath):
        gimp.message("画像のディレクトリに%sがありません" % modpath)
        return

    if not basedir in sys.path:
        sys.path.append(basedir)
    else:
        print("already exist in sys.path")


    # start of groping undoable operations
    pdb.gimp_image_undo_group_start(a_img)

    try:

        module=__import__(DYNEXE_NAME,globals(),locals(),[],-1)
        try:
            module.main(gimp,a_img,a_drawable)
        except Exception,e:
            import traceback

            dialog = gtk.MessageDialog(
            buttons=gtk.BUTTONS_CLOSE,
            message_format='gimp_automate.py内でエラー発生')

            dialog.format_secondary_text(traceback.format_exc())
            response = dialog.run()
            dialog.destroy()

            gimp.message("呼び出しに失敗しました")
        finally:
            del sys.modules[DYNEXE_NAME]
            del module

    finally:
        # end of grouping undoable operations
        pdb.gimp_image_undo_group_end(a_img)



register(
        "python_fu_automate_workflow",
        "automate-workflow",
        "自動化用スクリプト",
        "dothiko",
        "kakukaku world",
        "Apr 2015", 
        "<Image>/Python-Fu/others/automate-workflow", 
        "RGB*,GRAY*",
        [
        ],
        [],
        python_fu_automate_workflow)


main()