maya 粒子替代物体转为实体物体的方法 Mel脚本代码

一个学生问如何把maya 粒子替代物体转为实体物体的方法脚本代码。我把以前用于一个广告项目的插件代码提取了一下。得到下面两个程序。可以实现以上功能。但需要注意的是,前提要提供粒子镁粒子物体的旋转和缩放属性名,以及粒子的种子物id属相名。此代码源于以前开发的 垃圾清道夫插件中的一个功能。也可以将物体转为粒子。便于加速动力学效果。
在这里插入图片描述

global proc string[] tjh_rubbish_dumper__select_seeds_from_particle(string $nParticle) 
{
string $all_tjh_tree_factory__objects[];
clear $all_tjh_tree_factory__objects;


//$all_tjh_tree_factory__objects = `ls -dag  -type "particle" -type "nParticle" -sl`;

if(size($nParticle)>0)
{
$all_tjh_tree_factory__objects = `ls  -dag -type "particle" -type "nParticle" $nParticle `;
}else{
$all_tjh_tree_factory__objects = `ls  -dag -type "particle" -type "nParticle" -sl`;
}

string $particleShape = "";
string $seeds[];

int $k = 0;
int $h = 0;
for($particleShape in $all_tjh_tree_factory__objects)
{
string $instancers[]=` listConnections -s 0 -d 1 ($particleShape ".instanceData")`;
    string $instancerObjs[]=` instancer -q -object $instancers[0]`;
   
for($k=0;$k
    {
   $seeds[$h]=$instancerObjs[$k];
    $h ;
    }
};
//select -r $all_valid_tjh_objects;//particleShapes
select -r $seeds;//instancer seed objects
//select -add $instancerNodes;//instancer nodes
return $seeds;
}



//粒子转换为实例物体 
global proc tjh_rubbish_dumper_replaceParticles_withObjects(string $nParticle, string $instancerPP,string $rotationPP,string $scalePP)
{
string $particleNode[];
//default define for none selected
if($instancerPP == "")  $instancerPP = "instancerPP";
if($rotationPP == "")  $rotationPP = "rotationPP";
if($scalePP == "")  $scalePP = "scalePP";


if(size($nParticle)>0)
{
$particleNode = `ls  -dag -type "particle" -type "nParticle" $nParticle `;
}else{
$particleNode = `ls  -dag -type "particle" -type "nParticle" -sl`;
}


if(size($particleNode) > 0)//判断选择物体有粒子物体
{
//得到种子物体
string $tjh_replaceWithInstancer_selectSeeds[];
$tjh_replaceWithInstancer_selectSeeds=`tjh_rubbish_dumper__select_seeds_from_particle("")`;


//保存原有种子物的原始位置及缩放旋转值
float $old_posX[],$old_posY[],$old_posZ[];
//float $old_rotateX[],$old_rotateY[],$old_rotateZ[];
//float $old_scaleX[],$old_scaleY[],$old_scaleZ[];
for($i = 0; $i
{
$old_posX[$i] = ` getAttr  ($tjh_replaceWithInstancer_selectSeeds[$i] "." "tx") `;
$old_posY[$i] = ` getAttr  ($tjh_replaceWithInstancer_selectSeeds[$i] "." "ty") `;
$old_posZ[$i] = ` getAttr  ($tjh_replaceWithInstancer_selectSeeds[$i] "." "tz") `;

}


for($i = 0; $i
{
setAttr  ($tjh_replaceWithInstancer_selectSeeds[$i] "." "tx") 0 ;
setAttr  ($tjh_replaceWithInstancer_selectSeeds[$i] "." "ty") 0 ;
setAttr  ($tjh_replaceWithInstancer_selectSeeds[$i] "." "tz") 0 ;

}




int $seedsSize = size($tjh_replaceWithInstancer_selectSeeds) ;

int $particlePointsSize = `getAttr ($particleNode[0] ".count")` ;

//float $instancerPP[] =`getParticleAttr -at "instancerPP" -array true ($particleNode[0] ".pt[0:" ($seedsSize-1) "]")`;
//float $rotatePP[] =`getParticleAttr -at "rotationPP" -array true ($particleNode[0] ".pt[0:" ($seedsSize-1) "]")`;
//float $scalePP[] =`getParticleAttr -at "scalePP" -array true ($particleNode[0] ".pt[0:" ($seedsSize-1) "]")`;

float $instancerPP_Array[];
float $rotatePP_Array[];
float $scalePP_Array[];

if(` objExists  ($particleNode[0] "." $instancerPP)`)$instancerPP_Array =`getParticleAttr -at $instancerPP -array true $particleNode[0]`;
if(` objExists  ($particleNode[0] "." $rotationPP)`)$rotatePP_Array =`getParticleAttr -at $rotationPP -array true $particleNode[0]`;
if(` objExists  ($particleNode[0] "." $scalePP)`)$scalePP_Array =`getParticleAttr -at $scalePP -array true $particleNode[0]`;

string $newObject_instancer[];
//replace process
for($i=0;$i<$particlePointsSize;$i )
{
//int $id = floor( rand($seedsSize) );
int $id =0;


if(` objExists  ($particleNode[0] "." $instancerPP)`)$id= $instancerPP_Array[$i];


string $newObject[]; 
//string $copyType =`radioCollection -q -select  tjh_replaceWithInstancer__radioCollection_copyType`;
//if($copyType == "tjh_replaceWithInstancer__radioButton_copyType_copy")
// $newObject= ` duplicate $tjh_replaceWithInstancer_selectSeeds[$id]`;
//if($copyType == "tjh_replaceWithInstancer__radioButton_copyType_instance")
$newObject= ` instance $tjh_replaceWithInstancer_selectSeeds[$id]`;
$newObject_instancer[$i] = $newObject[0];
float $positionPP_Array[] =`pointPosition -world ($particleNode[0] ".pt[" $i "]")`;


string $cmd = "xform -r -t " ($positionPP_Array[0]) " " ($positionPP_Array[1]) " " ($positionPP_Array[2]) " ";
if(` objExists  ($particleNode[0] "." $rotationPP)`)  $cmd =" -ro " ($rotatePP_Array[$i*3]) " " ($rotatePP_Array[$i*3 1]) " " ($rotatePP_Array[$i*3 2]) " ";
if(` objExists  ($particleNode[0] "." $scalePP)`) $cmd =" -s " ($scalePP_Array[$i*3]) " " ($scalePP_Array[$i*3 1]) " " ($scalePP_Array[$i*3 2]) " ";


$cmd =  $newObject[0];
eval_r($cmd);

//xform -r -t ($positionPP[0]) ($positionPP[1]) ($positionPP[2]) -ro ($rotatePP[$i*3]) ($rotatePP[$i*3 1]) ($rotatePP[$i*3 2]) -s ($scalePP[$i*3]) ($scalePP[$i*3 1]) ($scalePP[$i*3 2]) $newObject[0];

}

//create new group for instancer-objs.
group -world -n ("tjh_rubbish_dumper_" $particleNode[0]) $newObject_instancer;
//delete $particle;

//恢复原有位置
for($i = 0; $i
{
setAttr  ($tjh_replaceWithInstancer_selectSeeds[$i] "." "tx") $old_posX[$i] ;
setAttr  ($tjh_replaceWithInstancer_selectSeeds[$i] "." "ty") $old_posY[$i] ;
setAttr  ($tjh_replaceWithInstancer_selectSeeds[$i] "." "tz") $old_posZ[$i] ;

}

}else{warning "no particle selected!";}
}


南无阿弥陀佛

  • 1
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Maya动画技术

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值