积木代码 加渲染合并

nanoblock_mel();
//-----------------------------------------------------------------------------------------
//-------   所有的颗粒类型   --------------------------------------------
//-----------------------------------------------------------------------------------------
global string $arrayTypeAC[]={"1x1y","1x1","1x2","1x3","1x4","1x6","1x8","1x10","1x3g","3x1y",
                    "2x2","2x3","2x4","2x6","2x8","2x10",
                    "1x1p","1x2p","1x3p","1x4p","1x6p","1x8p","2x2p","2x3p","2x4p",
                    "1x1z","1x2z","1x3z","1x4z","2x1z","1x3l","1x3t",
                    "1x2y","1x3y","1x4y","2x2y",
                    "1x2ao","1x2tu","2x2ao","2x2tu","1x2ti","2x1ti",
                    "1x2x","2x1x","1x2o","1x3x","2x3x","2x4x","2x2x",
                    "1x1c","1x1h","2x2z","3x3g",
                    "4x4","4x6","4x8","6x8","8x8","10x10","14x14","7x25","20x20","36x36"
                    
                    };
global int $allColor=92;
global string $allSelectA[];
//-----------------------------------------------------------------------------------------
//-------  所有的颗粒颜色   --------------------------------------------------------------
//-----------------------------------------------------------------------------------------



global proc nanoblock_mel(){
    //定义全局变量
    global string $arrayTypeAC[];
    print("arrayTypeAC:"+size($arrayTypeAC));
    global int $allColor;
    //创建工具窗口
    if (`window -q -ex createNanoblockUI`)deleteUI createNanoblockUI;
    window -t "SCBlock 2015年10月新版" -w 200 -h 30 createNanoblockUI;
           string $tabs=`tabLayout`;
           ///////////////////////////////////////////////////////////
       		string $child0=`rowColumnLayout -nc 1 `; 
        		text -l "D:/Work/nanoblock/maya/Mel_1009/" path;
        		text -l "";
        		
        		//添加颜色和摄像机   
                button -c "onImport Color"  -l  "Color";
                //添加线段
                text -l "";
	            button -l "快速选择" -c "onQuickSelectUI";
                button -l "去除小数点"  -c "onDel";
                button -l "打组镜像"  -c "onMirr";
                button -l "快速打组" -c "onGroup";
                text -l "";
                button -c "onImport Line"  -l  "Line";
                text -l "";
                button -l "    红线    " -bgc 1 0 0 -c "onLineR";
                button -l "    蓝线    " -bgc 0 0.5 1 -c "onLineB";
                button -l "    绿线    " -bgc 0 1 0 -c "onLineG" ;
                text -l "";
                button -l "统计颗粒" -c "onKeli";
                text -l "";
                button -l "打开渲染目录" -c "openRenderFile";
        		button -l "渲染前合并模型" -c "preRender";
        		setParent..;
        	///////////////////////////////////////////////////////////
        	string $child1=`rowColumnLayout -nc 10 `; 
            	string $pathFile=`text -q -l path`;
                    string $tt;
                    for($tt in $arrayTypeAC){
                        //print($tt+"   ");    
                        iconTextButton -style "iconAndTextVertical" -image1 ($pathFile+"type/"+$tt+"_0.png") -label ($tt+"") -c ("onImport SC_"+$tt);
                    }
        	setParent..;
            ///////////////////////////////////////////////////////////
            string $child2=`rowColumnLayout -nc 13 `; 
             string $pathFile=`text -q -l path`;
                //根据 全局变量 颗粒颜色 创建颗粒按钮
                for($t=0;$t<$allColor;$t++){
                    iconTextButton -style "iconAndTextVertical" -image1 ($pathFile+"color/"+$t+".png") -label ($t+"") -c ("onColor vray_"+$t+" "+$t);
                    if($t==29){text -l "森宝→" -bgc 0.2 0.2 0.2 ;}
                }
        		setParent..;
            ///////////////////////////////////////////////////////////
            string $child3=`rowColumnLayout -nc 7 -bgc 0 1 1`; 
        		setParent..;
        		
        		
            tabLayout -edit 
            -tabLabel $child0 "准备" -tabLabel $child1 "颗粒" -tabLabel $child2 "颜色"  -tabLabel $child3 "工具"
            $tabs;
    showWindow createNanoblockUI;
}
//打开渲染目录-----------------------------------------
proc openRenderFile(){
    string $pro=`internalVar -userWorkspaceDir`;
    $pro+="default/images/";
    system("load " + $pro);
}
//打组镜像------------------------------------------------------------------------------------------------------
proc onMirr(){
    Group;
    duplicate -rr;
    string $sel[]=`ls -sl`;
    setAttr  ($sel[0]+".scaleX") -1;
}

//导入颗粒------------------------------------------------------------------------------------------------------
proc onImport(string $_name){
	string $pathFile=`text -q -l path`;
	print("导入文件:"+($pathFile+"mc/"+$_name+".mb"));
	file -import -type "mayaBinary" -gr -ra true  -options "v=0"  -pr -loadReferenceDepth "all" ($pathFile+"mc/"+$_name+".mb");
	
}
//给颗粒上颜色-------------------------------------------------------------------------------
proc onColor(string $_name,int $_num){
    
    string $sel[]=`ls -sl`;
    for ($ppName in $sel){
		if(`attributeQuery -node $ppName -ex "Color"`){
			setAttr ($ppName+".Color") $_num;
		}else{
			addAttr -ln "Color"  -at long  $ppName;
		    //setAttr -k on ($ppName+".Color");
		    setAttr -e -channelBox true ($ppName+".Color");
		    setAttr ($ppName+".Color") $_num;
		}
		//defaultNavigation -source ("Color_"+$_name) -destination ($ppName+"Shape.instObjGroups[0]") -connectToExisting;
		hyperShade -assign ("Color_"+$_name);
	}
	
    //hyperShade -assign ("Color_"+$_name);
    //print("Color_"+$_name);
}




//获取场景中所有颗粒的类型  并计算颗粒的数量-----------------------------------------------------------------
proc onKeli(){
    global string $allSelectA[];
    select -all;
    select -hi;
    string $selAll[]=`ls -sl`;
    string $obj;
    select -cl;
    //--创建类型 和颜色 的数值
    //string $mcType[]; 
    string $mc_Type_Color[];
    int $mcTypeNum=0;
    float $mcWeight;
    float $mcPrice;
    for($obj in $selAll){
            if(`nodeType  $obj`=="transform"){
               if(`attributeQuery -node $obj -ex "Type"`){
                   ////print("mcTpyeNum:"+$mcTpyeNum);
                   string $getType=`getAttr -as ($obj+".Type")`;
                   string $getColor=`getAttr -as ($obj+".Color")`;
                   $mc_Type_Color[$mcTypeNum]=$getType+"_"+$getColor;
                   $mcTypeNum++;
                   $mcWeight+=`getAttr -as ($obj+".Weight")`;
               } 
               if(`attributeQuery -node $obj -ex "Price"`){
                   $mcPrice+=`getAttr -as ($obj+".Price")`;
               }
               
          }
    } 
    
    ArrangeB($mc_Type_Color);
    ////print(size($mc_Type_Color));

    //print("--------产品总重量约:"+$mcWeight+"克"+"\n");
    //获取场景中类型与颜色的数组 --------------$mcTypeA-----$mcColorA
    string $mcType_rd[]=stringArrayRemoveDuplicates($mc_Type_Color);
    int $mcType_count[];
    ////print($mcType_rd);
    
    for($i=0;$i<size($mcType_rd);$i++){
        int $count = stringArrayCount($mcType_rd[$i], $mc_Type_Color);
        $mcType_count[$i]=$count;
        //这里可以算出场景中有什么类型的颗粒 每个类型的颗粒有多少个
        ////print($mcType_rd[$i]+":"+$mcType_count[$i]);
       ////print("\n");
    }  
    $allSelectA=$mcType_rd;
    /*
    创建颗粒表窗口
    */
    string $pathFile=`text -q -l path`;
    if (`window -q -ex countUI`)deleteUI countUI;
        window -t " " -w 800 -h 500 countUI;
        
        columnLayout -bgc 1 1 1 ;  
            text -l ("产品重量约:"+$mcWeight+"克"+"      颗粒总数:"+size($mc_Type_Color)+"   成本大约为:"+$mcPrice+"元") allCount;
            
            //这里可以更改下拉菜单的尺寸------------------------------------------------------------------------------
            scrollLayout -w 1600 -h 1000;
            gridLayout -numberOfColumns 10 -cellWidthHeight 150 170;  
            for($a=0;$a<size($mcType_rd);$a++){
                columnLayout -bgc 1 1 1  ;
                iconTextButton -style "iconOnly" -image1 ($pathFile+"images/"+$mcType_rd[$a]+".png") -l $mcType_rd[$a] -c ("onSelect "+$a);
                rowLayout -bgc 1 1 1  -numberOfColumns 3;
                iconTextButton -style "iconOnly" -image1 ($pathFile+"images/XXX.png") -l ($mcType_rd[$a]+"x");
                iconTextButton -style "iconOnly" -image1 ($pathFile+"images/number/"+$mcType_count[$a]+".png") -l ($mcType_rd[$a]+"n");
                setParent..;
                setParent..;
            }
            setParent..;
             

            
        showWindow countUI;
        
}
//快速选择工具
proc onQuickSelectUI(){
    if (`window -q -ex selectUI`)deleteUI selectUI;
        window -t "快速选择" -w 200 -h 100 selectUI;
        rowColumnLayout -numberOfColumns 2;
        text -l "Type:";
        textField qsType;
        text -l "Color:";
        textField qsColor;
        button -l "快速清除" -c "onQuickClean";
        button -l "快速选择" -c "onQuickSelect";
        
        showWindow selectUI;
} 
proc onQuickSelect(){
    string $qsType=`textField -q -tx qsType`;
    string $qsColor=`textField -q -tx qsColor`;
    if($qsType!=""){
        select -all;
        select -hi;
        string $selAll[]=`ls -sl`;
        string $obj;
        select -cl;
        for($obj in $selAll){
                if(`nodeType  $obj`=="transform"){
                   if(`attributeQuery -node $obj -ex "Type"`){
                       string $getType=`getAttr -as ($obj+".Type")`;
                       if($getType==$qsType){
                           select -add $obj;
                       }
                   } 
                   
              }
        }
    }
    if($qsColor!=""){
       select -all;
        select -hi;
        string $selAll[]=`ls -sl`;
        string $obj;
        select -cl;
        for($obj in $selAll){
                if(`nodeType  $obj`=="transform"){
                   if(`attributeQuery -node $obj -ex "Type"`){
                       string $getColor=`getAttr -as ($obj+".Color")`;
                       if($getColor==$qsColor){
                           select -add $obj;
                       }
                   } 
                   
              }
        } 
    }
}
proc onQuickClean(){
    textField -e -tx "" qsType;
    textField -e -tx "" qsColor;
}
//选择------------------------------------------------------------------------ 
proc onSelect(int $type){
    global string $allSelectA[];
    //print($allSelectA[$type]);
    select -all;
    select -hi;
    string $selAll[]=`ls -sl`;
    string $obj;
    select -cl;
    for($obj in $selAll){
            if(`nodeType  $obj`=="transform"){
               if(`attributeQuery -node $obj -ex "Type"`){
                   ////print("mcTpyeNum:"+$mcTpyeNum);
                   string $getType=`getAttr -as ($obj+".Type")`;
                   string $getColor=`getAttr -as ($obj+".Color")`;
                   if(($getType+"_"+$getColor)==$allSelectA[$type]){
                       select -add $obj;
                   }
               } 
               
          }
    } 

}	

//快速打组工具
proc onGroup(){
    Group ;
    string $sl[]=`ls -sl`;
    int $cTime=`currentTime -q`;
    setKeyframe $sl[0];
    currentTime ($cTime-1);
    setKeyframe  -v 500 -at translateZ $sl[0];
    currentTime ($cTime+1);
    setKeyframe  -v 500 -at translateZ $sl[0];
    currentTime ($cTime);
    rename $sl[0] "line";
}
 //给线上颜色
proc onLineR(){
    string $sel[]=`ls -sl`;
    for ($aa in $sel){
		string $thisColor=`hyperShade -smn $aa`;
		setAttr ($thisColor+".color") -type double3 1 0 0 ;
	}
}
proc onLineB(){
    string $sel[]=`ls -sl`;
    for ($aa in $sel){
		string $thisColor=`hyperShade -smn $aa`;
		setAttr ($thisColor+".color") -type double3 0 0.5 1 ;
	}
}
proc onLineG(){
    string $sel[]=`ls -sl`;
    for ($aa in $sel){
		string $thisColor=`hyperShade -smn $aa`;
		setAttr ($thisColor+".color") -type double3 0 1 0 ;
	}
}

//重新排列颜色-------------------------------------------------------------------
proc Arrange(string $array[]){
    global string $arrayTypeAC[];
    global int $allColor;
    string $arrIndex[];
    for($t=0;$t<$allColor;$t++){
        $arrIndex[$t]=$t;
    }
  
    //定义临时数组
    string $arr_0[]; 
    //定义临时数组索引       
    int $int_0=0;      
    string $newArray[];
    //执行前清空数组
    clear $arr_0;
    clear $newArray;
    string $a;
    for($i=size($arrIndex)-1;$i>-1;$i--){
        for($a in $array){
            string $arraySTSA[];
            $arraySTSA = stringToStringArray($a, "_");  
            string $name=$arraySTSA[1];
            if($name==$arrIndex[$i]){
                $arr_0[$int_0]=$a;
                $int_0 ++;
            }
        }
        $newArray=stringArrayCatenate($arr_0,$newArray);
        clear $arr_0;
        $int_0=0;
    }
    
    clear $array;
    $array=$newArray;
    
}    
//重新排列类型-------------------------------------------------------------------
proc ArrangeB(string $array[]){
    global string $arrayTypeAC[];
    global string $arrayColor[];
    string $arrIndex[]=$arrayTypeAC;
    //定义临时数组
    string $arr_0[]; 
    //定义临时数组索引       
    int $int_0=0;      
    string $newArray[];
    //执行前清空数组
    clear $arr_0;
    clear $newArray;
    string $a;
    for($i=size($arrIndex)-1;$i>-1;$i--){
        for($a in $array){
            string $arraySTSA[];
            $arraySTSA = stringToStringArray($a, "_");  
            string $name=$arraySTSA[0];
            ////print("$name:"+$name+"\n");
            if($name==$arrIndex[$i]){
                $arr_0[$int_0]=$a;
                $int_0 ++;
            }
        }
        $newArray=stringArrayCatenate($arr_0,$newArray);
        clear $arr_0;
        $int_0=0;
    } 
    clear $array;
    $array=$newArray;
    Arrange($array);
} 

//自动处理渲染所需要的模型
proc preRender(){
    global string $allSelectA[];
    select -all;
    select -hi;
    string $selAll[]=`ls -sl`;
    string $obj;
    select -cl;
    //--创建类型 和颜色 的数值
    //string $mcType[]; 
    string $mc_Type_Color[];
    int $mcTypeNum=0;
    for($obj in $selAll){
            if(`nodeType  $obj`=="transform"){
               if(`attributeQuery -node $obj -ex "Type"`){
                   ////print("mcTpyeNum:"+$mcTpyeNum);
                   string $getColor=`getAttr -as ($obj+".Color")`;
                   $mc_Type_Color[$mcTypeNum]=$getColor;
                   $mcTypeNum++;
                   
               }                
          }
    } 
    //print("--------产品总重量约:"+$mcWeight+"克"+"\n");
    //获取场景中类型与颜色的数组 --------------$mcTypeA-----$mcColorA
    string $mcType_rd[]=stringArrayRemoveDuplicates($mc_Type_Color);
    int $mcType_count[];

    
    print("_____"+size($mcType_rd));   

        
        //--------------
        
      
      for($i in $mcType_rd)   { 
          select -all;
            select -hi;
            string $selAll[]=`ls -sl`;
            string $obj;
            select -cl;
            for($obj in $selAll){
                if(`nodeType  $obj`=="transform"){
                   if(`attributeQuery -node $obj -ex "Type"`){
                       string $getColor=`getAttr -as ($obj+".Color")`;
                       if($getColor==$i){
                           select -add $obj;
                       }
                   } 
                   
              }
              
            }
            print("$i"+$i);
            string $selAllB[]=`ls -sl`;
            
            for($i in $selAllB){
                polyBevel -com 0 -fraction 1.5 -offsetAsFraction 1 -autoFit 1 -segments 2 -worldSpace 1 
                -uvAssignment 1 -smoothingAngle 30 -fillNgons 1 -mergeVertices 1 -mergeVertexTolerance 0.0001 
                -miteringAngle 180 -angleTolerance 180 -ch 1 $i;    
                
            }
            if(size($selAllB)>1){
                CombinePolygons;
                string $ss[]=`ls -sl`;
                rename $ss[0] ("n_"+$i);
                DeleteHistory;
            }else{
                 parent -w;  
                 string $ss[]=`ls -sl`;
                rename $ss[0] ("n_"+$i);
                DeleteHistory; 
             }
       }
         
}

  

posted @ 2015-10-13 20:46  古刹红罗  阅读(409)  评论(0编辑  收藏  举报