Skip to content

Instantly share code, notes, and snippets.

@redglasses67
Created December 25, 2019 05:14
Show Gist options
  • Save redglasses67/aa637f4556223e30c4a4467a5f985a47 to your computer and use it in GitHub Desktop.
Save redglasses67/aa637f4556223e30c4a4467a5f985a47 to your computer and use it in GitHub Desktop.
頂点カラーを取得、変更する
import maya.api.OpenMaya as om2
selList = om2.MGlobal.getActiveSelectionList()
mDagPath = selList.getDagPath(0)
mesh = om2.MFnMesh(mDagPath)
# ColorSetの名前一覧を取得
colorSetNames = mesh.getColorSetNames()
print(colorSetNames) # stringのTupleで返ってきます
# 指定したColorSetでの頂点カラー情報を取得
vtxColors = mesh.getVertexColors(colorSetNames[0]) # ColorSet名を文字列で指定する事も可
print(vtxColors)
# 別のColorSetに変更する場合
mesh.setCurrentColorSetName(colorSetNames[1])
# 指定したColorSetでの頂点カラー情報を取得
vtxColors = mesh.getVertexColors(colorSetNames[0]) # ColorSet名を文字列で指定する事も可
print(vtxColors) # MColorArrayが返ってきます
print("len(vtxColors) = %s" %len(vtxColors))
import random
# 頂点カラーのRGBをランダム値で変更してみる
for i in xrange(len(vtxColors)):
vtxColors[i].r = random.uniform(0, 1)
vtxColors[i].g = random.uniform(0, 1)
vtxColors[i].b = random.uniform(0, 1)
vtxColors[i].a = 1
# 1頂点ずつセットする setVertexColor(color, vertexId) というメソッドもありますが、
# 複数の変更をまとめて適用する場合はこのサンプルのようにMColorArrayを回して変更した後に
# setVertexColors でまとめて適用した方が処理が早いです
# 第2引数は 頂点のIDのリストですが、下記のように書くことで [0, 1, 2, 3 ...] と len(vtxColors) 個のリストを作れます
mesh.setVertexColors(vtxColors, xrange(len(vtxColors)))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment