2021年11月3日水曜日

GIMP 2.99.x Python Console を試す例

うちの Blog でも、良く、GIMP の Python Console から引用をしています。
勿論、1文字ずつ、タイプして行っても良いのですが、
コピペ して 楽する事も可能です。
留意すべき点もありますが ... 。


GIMP の Bug Report を見ていて、
試して見ようと思った もの がありました。
将来、機能として盛り込まれるであろう、
Python Script での Font 選択 画面を表示するものです。

https://gitlab.gnome.org/GNOME/gimp/-/issues/6792#note_1300841

此処に、Console での入出力例が載っています。
以下、 Console 引用 から、実際に、試して見る ケース として、ご覧下さい。
さぁ、 やって見ましょう!。
先ずは、 引用で表示された もの を貼って置きますね。

>>> def cb(n, c, u, d):
...   print("Selected: {}¥nClosing: {}¥nudata: {}¥ndestroy: {}".format(n, c, u, d), flush=True)
...
>>> def destroy(u):
...   print("destroy¥nudata: {}".format(n, c, u))
...
>>> name = Gimp.font_select_new('Title', 'DejaVu Sans', cb, 'user data', destroy)
>>> Gimp.fonts_close_popup (name)
False
>>> Gimp.font_select_destroy (name)
>>>

例示 の Code 全体 をコピーします。
メモ帳 notepad.exe 等の テキストエディタ に貼り付けます。
戻り値 出力 の False を削除。  (行頭から文字列が始まるもの ですね。)
">>> " を "" に置換。
"... " を "" に置換。
此れで、素材は 準備完了 です。

  Python は インデント が重要ですので、
  必要以上に 半角スペース を削らない様に留意しましょう。
  此の例ではありませんが、継続行がある場合には 要注意 ですね。


さぁ、行きますよぉ~。

取り敢えず、Font を Ariel Bold 以外に変更して置いて、
Python Console を起動し、訂正した Code を放り込みます。
もう一度、name = の行を入れると、Dialog が立ち上がります。
何か選択すると、Verbose/Debug Console Window に表示が出て、
Dialog を閉じると、もう一度、同じ様に、表示が出ます。
  当然乍ら、Context で Font は変更していないので、最初に変えたままです。
  無論、 一連の作業の後で、指定 Font(変数)を使えば、
  Context で表示されているものでは無く、選んだものが使われるでしょう。
  所謂、 Script 内での動きと同じになります。  きっと。
 

1. From Toolbox, Change Font To Arial
2. Open Python Console
3. Paste the following

def cb(n, c, u, d):
  print("Selected: {}¥nClosing: {}¥nudata: {}¥ndestroy: {}".format(n, c, u, d), flush=True)

def destroy(u):
  print("destroy¥nudata: {}".format(n, c, u))

name = Gimp.font_select_new('Title', 'Arial Bold', cb, 'user data', destroy)
Gimp.fonts_close_popup (name)

Gimp.font_select_destroy (name)

4. Paste the following

name = Gimp.font_select_new('Title', 'Arial Bold', cb, 'user data', destroy)

5. Then, the Font Dialog appears with selected Arial Bold
6. Select Arial Heavy in the Dialog
7. The following results prompt on the Verbose/Debug Console

Selected: Arial Heavy
Closing: False
udata: user data
destroy: <function destroy at 0x000001377f8c1d30>

8. Push Close in the Dialog
9. The following results prompt on the Verbose/Debug Console

Selected: Arial Heavy
Closing: True
udata: user data
destroy: <function destroy at 0x000001377f8c1d30>



Gimp.font_select_new って、こう使えるんだ! と言う驚きと、
私如き には 高度! なレベルの 先行する2行 の def 文!。

いつも感心しているのですが、
先の 返信 を投稿された GIMP Developer の Massimo Valentini さまと、
Report を立ち上げられた GIMP Maintainer/Developer の Jehan さま。
此の おふたり の 技術力の高さ には 憧れ さえ、抱いて仕舞います。
ただただ、 感謝! と添えて置きましょう。



個人的メモ

GIMP 2.99.8 Python Console
Python 3.9.7 (default, Sep 23 2021, 09:13:41)  [GCC 10.3.0 64 bit (AMD64)]
>>>
>>> dir(Gimp.font_select_new)
['__call__', '__class__', '__delattr__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__gt__', '__hash__', '__init__', '__init_subclass__', '__le__', '__lt__', '__module__', '__name__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', 'can_throw_gerror', 'equal', 'get_arguments', 'get_attribute', 'get_caller_owns', 'get_container', 'get_flags', 'get_name', 'get_name_unescaped', 'get_namespace', 'get_property', 'get_return_attribute', 'get_return_type', 'get_symbol', 'get_type', 'get_vfunc', 'invoke', 'is_constructor', 'is_deprecated', 'is_method', 'may_return_null', 'skip_return']
>>>
>>> Gimp.font_select_new.__doc__
'font_select_new(title:str, font_name:str, callback:Gimp.RunFontCallback, data=None) -> str'
>>>
>>> Gimp.font_select_new.get_arguments()
(gi.ArgInfo(title), gi.ArgInfo(font_name), gi.ArgInfo(callback), gi.ArgInfo(data), gi.ArgInfo(data_destroy))
>>> Gimp.font_select_new.get_return_type()
gi.TypeInfo(type_type_instance)

   .__doc__ は古いのか?
   .get_arguments() があれば、正確かも。
   .get_return_type() もあれば、役に立つかも。



0 件のコメント:

コメントを投稿