建立一個 Python 外掛

這份逐步指南說明如何建立一個簡單的 Python 外掛,讓它能匯出帶有特定匯出預設的材質集通道。

進入插件資料夾

要新增 Python 外掛,可以在 Substance 3D Painter 的外掛資料夾中建立一個簡單的腳本檔案。

要存取 python 資料夾,請前往:

平台
版本
路徑
窗戶
7.2 或更新版本
C:\Users\username\Documents\Adobe\Adobe Substance 3D Painter
遺產
C:\Users\username\Documents\Allegorithmic\Substance Painter
麥克
7.2 或更新版本
/使用者/使用者名稱/文件/Adobe/Adobe Substance 3D 畫家
遺產
/使用者/用戶名/文件/寓言/內容畫家
Linux
7.2 或更新版本
/首頁/用戶名/文件/Adobe/Adobe Substance 3D 畫家
遺產
/首頁/用戶名/文件/寓言/內容畫家
NOTE
「plugins 」這個資料夾​是用來存放 Javascript 插件的。Python 插件應該放在「python/plugins」資料夾裡。

2 - 建立新的插件檔案

在插件資料夾的根節點,建立一個新的文字檔,並命名為 custom_export.py 。

3 - 劇本內容

打開空的腳本檔到文字編輯器,然後貼上以下程式碼片段。 可以查看程式碼註解,了解更多關於其行為的細節。

import os

# Substance 3D Painter modules
import substance_painter.ui
import substance_painter.export
import substance_painter.project
import substance_painter.textureset

# PySide module to build custom UI
from PySide2 import QtWidgets

plugin_widgets = []

def export_textures() :
    # Verify if a project is open before trying to export something
    if not substance_painter.project.is_open() :
        return

    # Get the currently active layer stack (paintable)
    stack = substance_painter.textureset.get_active_stack()

    # Get the parent Texture Set of this layer stack
    material = stack.material()

    # Build Export Preset resource URL
    # - Context: name of the library where the resource is located
    # - Name: name of the resource (filename without extension or Substance graph path)
    export_preset = substance_painter.resource.ResourceID(
        context="starter_assets",
        name="PBR Metallic Roughness" )

    print( "Preset:" )
    print( export_preset.url() )

    # Setup the export settings
    resolution = material.get_resolution()

    # Setup the export path, in this case the textures
    # will be put next to the spp project file on the disk
    Path = substance_painter.project.file_path()
    Path = os.path.dirname(Path) + "/"

    # Build the configuration
    config = {
        "exportShaderParams"  : False,
        "exportPath" : Path,
        "exportList" : [ { "rootPath" : str(stack) } ],
        "exportPresets" : [ { "name" : "default", "maps" : [] } ],
        "defaultExportPreset" : export_preset.url(),
        "exportParameters" : [
            {
                "parameters" : { "paddingAlgorithm": "infinite" }
            }
        ]
    }

    substance_painter.export.export_project_textures( config )

def start_plugin():
    # Create a text widget for a menu
    Action = QtWidgets.QAction("Custom Python Export",
            triggered=export_textures)

    # Add this widget to the existing File menu of the application
    substance_painter.ui.add_action(
    substance_painter.ui.ApplicationMenu.File,
    Action )

    # Store the widget for proper cleanup later when stopping the plugin
    plugin_widgets.append(Action)

def close_plugin():
    # Remove all widgets that have been added to the UI
    for widget in plugin_widgets:
    substance_painter.ui.delete_ui_element(widget)

    plugin_widgets.clear()

if __name__ == "__main__":
    start_plugin()

載入並啟用該插件

啟動 Substance 3D Painter 讓應用程式發現該外掛。

點選 Python 選單,然後點選外掛名稱以啟用:

最後點選 檔案 選單,選擇 自訂 Python 匯出 ,觸發腳本的匯出功能:

recommendation-more-help
substance-3d-dev-help-painter-python-guide