LoginSignup
8
4

More than 3 years have passed since last update.

Substance Painter Python plugin の作り方(入門編)

Last updated at Posted at 2020-07-30

sp_python_1.png

Substance Painter 2020.1 (6.1.0) より、従来の JavaScript API に加え、 Python API が追加されました。

Substance Painter Version 2020.1 (6.1.0)リリースノート – Born Digital サポート

しかし、公式ドキュメントでは Python API の存在はいまだ触れられていません。どこから手を付けていいのかわかりにくかったので、入り口の部分だけメモしておきます。

なお、SP 2020.2 時点で Python API のバージョンは 0.0.2 となっています。この記事の内容はすぐ陳腐化する可能性が高いことに注意してください。

Python API リファレンスの場所

sp_python_2.png

  • アプリ内メニューバーの Help > Documentation > Python API をクリックすると、ローカルにあるHTMLファイルが開かれる
    • Windows の場合、実際のパスは C:\Program Files\Allegorithmic\Substance Painter\resources\python-doc\index.html
  • なぜかオンラインでは公開されていない
    • JavaScript API も同様
    • ググれなくて困る…

スクリプトの配置場所

  • "Python > Plugin Folder" で、プラグイン保存先フォルダーが開く
    • Windows の場合、C:\Users<USER NAME>\Documents\Allegorithmic\Substance Painter\python\plugins
  • .py ファイルは plugins/ 直下に配置する
    • サブフォルダー内に置いても読み込まれない

Python コンソール

  • "Windows > Views > Python Console" でコンソールウィンドウを表示
  • 例えば以下のコマンドでリファレンスを見られる
    • 字がやたら小さいので、HTML リファレンスを見たほうがいい
# モジュールをロード
import substance_painter

# モジュールの説明を表示
help(substance_painter)

# project モジュールの説明を表示
help(substance_painter.project)

簡単なプラグインを作るチュートリアル

plugins/hello_plugin.py を以下の内容で作成する。

"""The hello world of python scripting in Substance Painter
"""

from PySide2 import QtWidgets
import substance_painter.ui

plugin_widgets = []
"""Keep track of added ui elements for cleanup"""

def start_plugin():
    """This method is called when the plugin is started."""
    # Create a simple text widget
    hello_widget = QtWidgets.QTextEdit()
    hello_widget.setText("Hello from python scripting!")
    hello_widget.setReadOnly(True)
    hello_widget.setWindowTitle("Hello Plugin")
    # Add this widget as a dock to the interface
    substance_painter.ui.add_dock_widget(hello_widget)
    # Store added widget for proper cleanup when stopping the plugin
    plugin_widgets.append(hello_widget)

def close_plugin():
    """This method is called when the plugin is stopped."""
    # We need to remove all added widgets from the UI.
    for widget in plugin_widgets:
        substance_painter.ui.delete_ui_element(widget)
    plugin_widgets.clear()

if __name__ == "__main__":
    start_plugin()

"Python > Reload Plugin Folders" で、フォルダーを再スキャン。

"Python > hello_plugin" という項目ができているので、クリック。

画面内に "HELLO PLUGIN" というウィジェット (ペイン) が作成されれば成功です。

sp_python_3.png

その他補足

  • 公式のプラグイン共有サイトでは、2020年7月30日現在、Python プラグインはシェアされていません。
  • プラグインを書こうと思った動機は、メッシュを変更するたびに "Edit > Project Configuration..." からリロードするのが非常に面倒だったからなのですが、再読み込みする API はどうやら存在しないようです。なぜ作らない! :rage:
8
4
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
8
4