レンダリングの設定 Python API およびコマンド

レンダリング設定 Python API

レンダリング設定 Python API は Maya インストールの一部として利用でき、..¥Python¥Lib¥site-packages¥maya¥app¥renderSetup フォルダに関連ファイルがあります。

注: レンダリング設定 Python API は現在公式にサポートされていないため、将来変更される可能性があります。

レンダリング設定 API を使用するには、まず次のようにレンダリング設定モジュールを読み込む必要があります。

import maya.app.renderSetup.model.override as override
import maya.app.renderSetup.model.selector as selector
import maya.app.renderSetup.model.collection as collection
import maya.app.renderSetup.model.renderLayer as renderLayer
import maya.app.renderSetup.model.renderSetup as renderSetup

また、ネイティブ Maya コマンドを使用できるように、maya.cmds を読み込む必要があります。

レイヤと 2 つのコレクションを作成するサンプル スクリプトを次に示します。このスクリプトはその後、エクスプレッション mySphere* を使用して最初のコレクションに球を追加し、エクスプレッション myCube* を使用して 2 番目のコレクションに立方体を追加します。

import maya.app.renderSetup.model.override as override
import maya.app.renderSetup.model.selector as selector
import maya.app.renderSetup.model.collection as collection
import maya.app.renderSetup.model.renderLayer as renderLayer
import maya.app.renderSetup.model.renderSetup as renderSetup
import maya.cmds as cmds

rs = renderSetup.instance()    

# Create and append the render layer
rl = rs.createRenderLayer("MyRenderSetupLayer")

# Create and append 2 collections
c1 = rl.createCollection("sphereCollection")
c2 = rl.createCollection("cubeCollection")

# Create a trivial scene with a cube, a sphere and a cylinder
cmds.polySphere(name='mySphere')
cmds.move(2, 'mySphere', y=True)
cmds.polyCube(name='myCube')
cmds.move(5, 'myCube', y=True)
cmds.polyCylinder(name='myCylinder')
cmds.move(10, 'myCylinder', y=True)

# Set up collection 1 to select all spheres using the pattern mySphere*, 
# and collection 2 to select all cubes using the pattern myCube*.
c1.getSelector().setPattern('mySphere*')
c2.getSelector().setPattern('myCube*')

# Set the render layer as visible. 
# Only the sphere and the cube are displayed as they are collection members.
rs.switchToLayer(rl)
注:

rs.switchToLayer() メソッドを呼び出して、レンダリングの設定(Render Setup)がシーンの変更内容と同期するようにします。これは、Maya をインタラクティブに使用しているときに、レイヤの可視性アイコン をクリックしてレイヤのメンバーシップを更新する方法と同等です。

レンダリング設定 Python コマンド

レンダリング設定のメンバーシップを照会できる、レンダリング設定コマンドを使用できます。

次のコマンドの詳細を確認するには:

help() を使用して、ドキュメンテーション文字列を照会します。

たとえば、次の Python コマンドは renderLayerMembers コマンドを照会できます。

import maya.app.renderSetup.model.modelCmds as modelCmds
help(modelCmds.RenderLayerMembersCmd)

同様の方法で他のコマンドのドキュメンテーション文字列を照会できます。

renderSetupSelect コマンドの詳細を確認するには、スクリプト エディタ(Script Editor)Python タブに次のコマンドを入力します。

import maya.app.renderSetup.views.viewCmds as viewCmds
help(viewCmds.RenderSetupSelectCmd)

Python で .json ファイルの読み込みおよび書き出しを行うには

レンダリングの設定(Render Setup)の .json ファイルをシーンに対して読み込みおよび書き出しする関数を次のように定義します。

import maya.app.renderSetup.model.renderSetup as renderSetup

import json

# Cannot name function "import", as this is a reserved Python keyword.
def importFile(filename):
    with open(filename, "r") as file:
        renderSetup.instance().decode(json.load(file), renderSetup.DECODE_AND_OVERWRITE, None)

def exportFile(filename):
    with open(filename, "w+") as file:
        json.dump(renderSetup.instance().encode(None), fp=file, indent=2, sort_keys=True)