Skip to content

Instantly share code, notes, and snippets.

@utatsuya
Created August 29, 2015 14:21
Show Gist options
  • Select an option

  • Save utatsuya/60e39f25606eb8172b72 to your computer and use it in GitHub Desktop.

Select an option

Save utatsuya/60e39f25606eb8172b72 to your computer and use it in GitHub Desktop.
# python api1.0 getWeight(),setWeights()
# http://ueta.hateblo.jp/entry/2015/08/24/102937
import time
import maya.OpenMaya as OpenMaya
import maya.OpenMayaAnim as OpenMayaAnim
#_time = time.time
_time = time.clock
SKINCLUSTER = "skinCluster1"
MESHSHAPE = "TestMeshShape"
msg = "Python API 1.0"
startTime = _time()
def n( name ):
sllist = OpenMaya.MSelectionList()
OpenMaya.MGlobal.getSelectionListByName( name , sllist )
try:
dp = OpenMaya.MDagPath()
sllist.getDagPath( 0 , dp )
return dp
except:
mo = OpenMaya.MObject()
sllist.getDependNode( 0 , mo )
return mo
# スキンクラスタ取得
skinNode = n( SKINCLUSTER )
skinFn = OpenMayaAnim.MFnSkinCluster( skinNode )
# シェイプの取得
meshPath = n( MESHSHAPE )
meshNode = meshPath.node()
# 取得対象の頂点
# 今回はすべての頂点を取得したいので[0,1,2,,,,,MaxVertex]となっている。
meshVerItFn = OpenMaya.MItMeshVertex( meshNode )
indices = OpenMaya.MIntArray( meshVerItFn.count() , 0 )
for i in xrange( indices.length() ):
indices[i]=i
# 指定の頂点をコンポーネントとして取得する
singleIdComp = OpenMaya.MFnSingleIndexedComponent()
vertexComp = singleIdComp.create( OpenMaya.MFn.kMeshVertComponent )
singleIdComp.addElements( indices )
# インフルエンスの数のIntArray
infDags = OpenMaya.MDagPathArray()
skinFn.influenceObjects( infDags )
infIndices = OpenMaya.MIntArray( infDags.length() , 0 )
for x in xrange( infDags.length() ):
infIndices[x] = int( skinFn.indexForInfluenceObject( infDags[x] ) )
# すべてのウエイトの値を取得
weights = OpenMaya.MDoubleArray()
s = _time()
infCountUtil = OpenMaya.MScriptUtil(0)
infCountPtr = infCountUtil.asUintPtr()
skinFn.getWeights( meshPath , vertexComp , weights , infCountPtr )
get = _time() - s
print msg, "getWeights()", get, "s"
#infCount = OpenMaya.MScriptUtil(infCountPtr).asUint()
# 最初に取得したウエイトを再設定
s = _time()
skinFn.setWeights( meshPath , vertexComp , infIndices , weights )
set = _time() - s
print msg, "setWeights()", set, "s"
total = _time() - startTime
print msg, "(Total Time)", total, "s"
print msg, "(etc)", (total - get - set), "s"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment