Skip to content

Instantly share code, notes, and snippets.

@utatsuya
Last active September 19, 2015 08:06
Show Gist options
  • Select an option

  • Save utatsuya/47f387652418537ea542 to your computer and use it in GitHub Desktop.

Select an option

Save utatsuya/47f387652418537ea542 to your computer and use it in GitHub Desktop.

Revisions

  1. utatsuya revised this gist Aug 29, 2015. 1 changed file with 2 additions and 2 deletions.
    4 changes: 2 additions & 2 deletions OpenMayaCS_setWeights_getWeights.cs
    Original file line number Diff line number Diff line change
    @@ -11,8 +11,8 @@ private void n(string name, ref MDagPath dp)
    private void n(string name, ref MObject mo)
    {
    MSelectionList sellist = new MSelectionList();
    MGlobal.getSelectionListByName(name, sellist);
    sellist.getDependNode(0, mo);
    MGlobal.getSelectionListByName( name , sellist );
    sellist.getDependNode( 0 , mo );
    }

    private void TestGetSetWeights()
  2. utatsuya created this gist Aug 29, 2015.
    79 changes: 79 additions & 0 deletions OpenMayaCS_setWeights_getWeights.cs
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,79 @@
    // c# setWeights(),setWeight()
    // http://ueta.hateblo.jp/entry/2015/08/24/102937

    private void n(string name, ref MDagPath dp)
    {
    MSelectionList sellist = new MSelectionList();
    MGlobal.getSelectionListByName( name, sellist );
    sellist.getDagPath( 0 , dp );
    }

    private void n(string name, ref MObject mo)
    {
    MSelectionList sellist = new MSelectionList();
    MGlobal.getSelectionListByName(name, sellist);
    sellist.getDependNode(0, mo);
    }

    private void TestGetSetWeights()
    {
    SpeedCount.TimeCountByPerformanceCounter time_getWeights = new SpeedCount.TimeCountByPerformanceCounter();
    SpeedCount.TimeCountByPerformanceCounter time_setWeights = new SpeedCount.TimeCountByPerformanceCounter();
    SpeedCount.TimeCountByPerformanceCounter time_total = new SpeedCount.TimeCountByPerformanceCounter();

    const string SKINCLUSTER = "skinCluster1";
    const string MESHSHAPE = "TestMeshShape";

    time_total.Start();

    // スキンクラスタ取得
    MObject skinNode = new MObject();
    n( SKINCLUSTER , ref skinNode );
    MFnSkinCluster skinFn = new MFnSkinCluster( skinNode );

    // シェイプの取得
    MDagPath meshPath = new MDagPath();
    n( MESHSHAPE , ref meshPath );
    MObject meshNode = new MObject( meshPath.node );

    // 取得対象の頂点
    // 今回はすべての頂点を取得したいので[0,1,2,,,,,MaxVertex]となっている。
    MItMeshVertex meshVerItFn = new MItMeshVertex( meshNode );
    MIntArray indices = new MIntArray((uint)meshVerItFn.count() , 0 );
    for (int i = 0, len = (int)indices.length; i < len; ++i)
    {
    indices[i] = i;
    }

    // 指定の頂点をコンポーネントとして取得する
    MFnSingleIndexedComponent singleIdComp = new MFnSingleIndexedComponent();
    MObject vertexComp = singleIdComp.create( MFn.Type.kMeshVertComponent );
    singleIdComp.addElements( indices );

    // インフルエンスの数のIntArray
    MDagPathArray infDags = new MDagPathArray();
    skinFn.influenceObjects( infDags );
    MIntArray infIndices = new MIntArray( infDags.length , 0 );
    for (int i = 0, len = (int)infDags.length; i < len; ++i)
    {
    infIndices[i] = (int)skinFn.indexForInfluenceObject(infDags[i]);
    }

    // すべてのウエイトの値を取得
    MDoubleArray weights = new MDoubleArray();
    uint infCount = 0;
    time_getWeights.Start();
    skinFn.getWeights( meshPath, vertexComp, weights, ref infCount );
    double get = time_getWeights.ElapsedMilliSec() * 0.001;
    MGlobal.displayInfo("C# getWeights() " + get + " s");

    // 最初に取得したウエイトを再設定
    time_setWeights.Start();
    skinFn.setWeights( meshPath , vertexComp , infIndices , weights );
    double set = time_setWeights.ElapsedMilliSec() * 0.001;
    MGlobal.displayInfo("C# setWeights() " + set + " s");
    double total = time_total.ElapsedMilliSec() * 0.001;
    MGlobal.displayInfo("C# total time " + total + " s");
    MGlobal.displayInfo("C# etc time " + (total - get - set) + " s");

    }