まずは現在編集中のモデルに使われているシェーダを取得します
mari.geo.current().currentShader()
次にこの取得出来たシェーダに対して、新しいモジュールを追加してみます
module = 'Diffuse' newName = 'newShaderModule' currentShader = mari.geo.current().currentShader() currentShader.addModule(module, newName)変数 module に代入している名前は、[Add New Shader Module]ボタン を押したときに
一覧になっているものと同じものを与えてあげれば大丈夫です
(Bump、Specular Colorなど)
こちらも前回の新規チャンネル同様、名前のバッティングでエラー出しますので対策を施してあげます
シェーダモジュールのリストは以下の方法で取得します
mari.geo.current().currentShader().moduleNames()バッティング回避処理です
module = 'Diffuse' newName = 'newShaderModule' currentShader = mari.geo.current().currentShader() moduleList = currentShader.moduleNames() count = 1 for mo in moduleList: if newName in mo: count += 1 if count > 1: newName = '%s_%d' % (newName, count) currentShader.addModule(module, newName)
※シェーダモジュール名はリストを取得した時点で取れているので
チャンネルの時と違い名前を取得するコマンドを呼ぶ必要無い点に注意が必要です(統一して欲しい…)
チャンネルの時と違い名前を取得するコマンドを呼ぶ必要無い点に注意が必要です(統一して欲しい…)
これだけではつまんないので、前回のスクリプト と合体させちゃいましょう
新しく作ったシェーダモジュールに、同じく新しく作ったチャンネルをアサインするようにしてみます
シェーダモジュールのテクスチャの欄のパラメータは何か調べます
currentShader = mari.geo.current().currentShader() currentShader.moduleParameters("モジュールの名前") >>> currentShader = mari.geo.current().currentShader() ... print currentShader.moduleParameters('newShaderModule') (u'BlendAmount', u'Texture.index', u'Texture.invert')今回は Texture.index という名前のようです
シェーダモジュールによって異なるパラメータの場合や、マスク付きでふたつある場合がありますので調べてみてください
テクスチャを割り当てるパラメータが分かったので、以下の方法でセットします
currentShader = mari.geo.current().currentShader() currentShader.setParameter("モジュールの名前", 'Texture.index', "チャンネルの名前")
全部つなげるとこんな感じになります
# ============================================================================ def setChannel(getModule, getChannel): module = getModule param = 'Texture.index' channel = getChannel currentShader = mari.geo.current().currentShader() currentShader.setParameter(module, param, channel) # ============================================================================ def createNewShaderModule(getModule, getName): module = getModule newName = getName currentShader = mari.geo.current().currentShader() moduleList = currentShader.moduleNames() count = 1 for mo in moduleList: if newName in mo: count += 1 if count > 1: newName = '%s_%d' % (newName, count) mari.geo.current().currentShader().addModule(module, newName) return newName # ============================================================================ def createNewChannel(getName): newName = getName channelList = mari.geo.current().channelList() count = 1 for ch in channelList: if newName in ch.name(): count += 1 if count > 1: newName = '%s_%d' % (newName, count) channelInfo = mari.ChannelInfo(newName) mari.geo.current().createChannel(channelInfo) return newName # ============================================================================ newChannelName = 'newChannel' createdChannel = createNewChannel(newChannelName) module = 'Diffuse' newModuleName = 'newShaderModule' createdModule = createNewShaderModule(module, newModuleName) setChannel(createdModule, createdChannel) # ============================================================================
実行すると一連の処理が一括で行われて、手でやると結構手間作業が簡略化されます
なお、この例はブログ用に必要最低限の記述や決め打ちパラメータしかないので
ここからどんどん発展していってもらえたら、と思います。
0 件のコメント:
コメントを投稿