本文主要是介绍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!";}
}
南无阿弥陀佛
这篇关于maya 粒子替代物体转为实体物体的方法 Mel脚本代码的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!