yys

Maya插件开发,(多多练习英文吧~)

  博客园 :: 首页 :: 博问 :: 闪存 :: 新随笔 :: 联系 :: 订阅 订阅 :: 管理 ::

(环境:Maya2009 x32, WinXp)

 

mel 里deleteAttr compound attributes时很不方便。

以下是我尝试的方法,但有都一些问题或让人不满意的地方。


W1:

一种可行的方法是,先将$plug重命名,然后再删除之:

renameAttr $_plug "TO_BE_DELETED";
deleteAttr $node+".TO_BE_DELETED";

我在MayaScriptEditor执行W1的语句没有问题,

 

但是当W1的代码写在mel文件里,再执行这个mel文件时,执行“deleteAttr ...”语句,maya就会崩溃。

 

 

 W2:

经过一些尝试,得出下面的方法W2:

renameAttr $_plug "TO_BE_DELETED";
for($i=0; $i<2; $i++){ print("");}
deleteAttr $node+".TO_BE_DELETED";

 详细的代码就是:

 

                string $node = $currentNodeName;
                string $attr = $argName;
                int $loop    = 2;
                {
                    string $_plug = $node+"."+$attr;
                   
                    print("before  renameAttr "+$_plug+" \"TO_BE_DELETED\")");
                    // philippe: this is a fix to deal with a bug with compound attributes
                    if ( catch( `renameAttr $_plug "TO_BE_DELETED"` ) ){
                         print("error: renameAttr "+$_plug+" \"TO_BE_DELETED\")");
                    }
                    //NOTE: without this for-loop, maya will lead a crash.
                    for($i=0; $i<$loop; $i++){ print("");}

                    print("before  deleteAttr \""+$node+".TO_BE_DELETED\"");
                    if ( catch( `deleteAttr ($node+".TO_BE_DELETED")` ) ){
                        print("error: deleteAttr \""+$node+".TO_BE_DELETED\"");
                    }
                }
 

 

 但是,当我把W2的代码写成一个函数myDeleteAttr(...), 又出问题了:

 

W3:

 

 

global proc myDeleteAttr(string $node, string $attr, int $loop)
{
    print("myDeleteAttr("+$node+","+$attr+","+$loop+")");

    string $plug = $node+"."+$attr;

    liqlog("before  renameAttr "+$plug+" \"TO_BE_DELETED\")");
    if ( catch( `renameAttr $plug "TO_BE_DELETED"` ) ){
        print("error: renameAttr "+$plug+" \"TO_BE_DELETED\")");
    }

    //NOTE: without this for-loop, maya may lead to a crash.
    for($i=0; $i<$loop; $i++){ print("+");}print("\n");

    print("before  deleteAttr \""+$node+".TO_BE_DELETED\"");
    if ( catch( `deleteAttr ($node+".TO_BE_DELETED")` ) ){
        print("error: deleteAttr \""+$node+".TO_BE_DELETED\"");
    }
}

 当我在mel文件里调用myDeleteAttr($Node, $attr, 1000)时,

会输出“before  deleteAttr "XXX.TO_BE_DELETED",然后maya就崩溃了。

 

 

 

 综上所述,我目前使用的是W2代码--硬编码。感觉很不爽。

 

 

//2012-2-9

今天写了个test case ,运行时并没有崩溃。

/// myDeleteAttr.mel

global proc myDeleteAttr(string $node, string $attr, int $loop)
{
    //print("myDeleteAttr("+$node+","+$attr+","+$loop+")\n");

    string $plug = $node+"."+$attr;

    //print("before  renameAttr "+$plug+" \"TO_BE_DELETED\")"+"\n");
    if ( catch( `renameAttr $plug "TO_BE_DELETED"` ) ){
        print("error: renameAttr "+$plug+" \"TO_BE_DELETED\")"+"\n");
    }

    //NOTE: without this for-loop, maya may lead to a crash.
    
//for($i=0; $i<$loop; $i++){ print("+");}print("\n");

    
//print("before  deleteAttr \""+$node+".TO_BE_DELETED\""+"\n");
    if ( catch( `deleteAttr ($node+".TO_BE_DELETED")` ) ){
        print("error: deleteAttr \""+$node+".TO_BE_DELETED\""+"\n");
    }
}
global proc myAddAttr(string $node, string $attr)
{
addAttr -k true -longName $attr -at "double3" $node;
addAttr -k true -longName ( $attr + "a" ) -at "double" -parent $attr $node;
addAttr -k true -longName ( $attr + "b" ) -at "double" -parent $attr $node;
addAttr -k true -longName ( $attr + "c" ) -at "double" -parent $attr $node;
setAttr  ( $node + "." + $attr ) -type double3 1 2 3;
}

global proc test0(string $node, string $attr)
{
    //print("test0()\n");
    myAddAttr( $node,  $attr);
    myDeleteAttr( $node,  $attr, 2);
    //print("test0() done\n");
}
// test0($node[1], $attr);

global proc test1(string $node, string $attr, int $loop)
{  
    print("test1()\n");
    int $i = 0;
    for(; $i<$loop; $i++)
    {
        //print($i+"-----\n");
        test0($node,  $attr);
    }
    print("test1() done\n");
}// test1($node[1], $attr, 100);

// test case
// source myDeleteAttr.mel
//string $node[]=`polyCube -ch on -o on -cuv 4 `;//Result: pCube1 polyCube1 
//string $attr="testAttr2";
//test1($node[1], $attr, 1000);
//test0($node[1], $attr);
////myAddAttr( $node[1],  $attr);
////myDeleteAttr( $node[1],  $attr, 2); 

 

posted on 2012-02-01 22:17  yys  阅读(585)  评论(0编辑  收藏  举报