--MeshExtruder 2.2Final by Snow 2000.9.7
--MeshExtrder is a script sim to Greeble which can add details to your mesh.

--History:2.2 add Bald Value and Order Value to let you decide your extruding mesh baldness like.00-10-30
--		 2.0beta final. now you can assign a map as the block extrude (currently I don't have the tech to complete this)
--		 1.2 final. bugs fixed.some codes enhanced.
--		 1.1 beta. support large block extrude
--        1.0 beta. support hole mesh and selection area extrude.
--know problem: large block extrude still not very good because search for blocks is a time resume thing.
--Caution!! you object will turn to mesh automatically. Please backup you object first.
--any comment send to yang.zhang@autodesk.com

--init varibles
--extrude height value sets
extrudeValueLow=1
extrudeValueHigh=2
--extrude scale value sets
extrudeScaleLow=80
extrudeScaleHigh=90
--Large Block Ratio (100%)
largeBlockR=0
--Baring Value sets
baldValue=0
orderOption=true

orgObj=undefined
targetMesh=undefined
extMap=undefined
mapSize=50
--debug=newScript()


--MainFunction
fn meshEx meshObj = 
(   counter=0
	exRange=#()
	meshSource=meshObj
	if meshSource.selectedFaces.count==0 then
		faceSelection=for i in meshSource.Faces collect i.index
	else
		faceSelection=for i in meshSource.selectedFaces collect i.index
	-- extrudeMap=bitmap 	(floor(sqrt(faceSelection.count))
	for i=1 to faceSelection.count-1 do
		(
			if (faceSelection[i] as float)/2==floor((faceSelection[i] as float)/2) as integer do continue
			--collect the faces
			if (random 0 100) < largeBlockR do
				(append exRange faceSelection[i]
				append exRange faceSelection[i+1]
				continue)
			
			counter=counter+1
			if not orderOption then 
				if (random 0 100)< baldValue do
					(   
						exRange=#()
						continue
					)
			else
				if baldValue!=0 do 
					if counter/(floor(100/baldValue))!= floor(counter/(floor(100/baldValue))) do 
						(
							exRange=#()
							continue
						)
			append exRange faceSelection[i]
			append exRange faceSelection[i+1]
			extrudeValue=random extrudeValueLow extrudeValueHigh
			extrudeScale=random extrudeScaleLow extrudeScaleHigh
			extrudeFace meshSource exRange extrudeValue extrudeScale dir:#independent
			--print extrudeScale to:debug
			exRange=#()
         )
	update meshSource
	deselect meshSource.selectedFaces
)

--Create Interface
Utility MeshExtruder "Mesh Extruder" rolledUp:true
( 
	  rollout aboutMeshExtruder "About MeshExtruder" 
       (
               label lab1 "Mesh Extruder 2.2 Final"
			   label lab10 "Written By Snow"
               label lab2 "Home http://zsnow.yeah.net"
               label lab3 "Email: yang.zhang@autodesk.com"
               label lab4 "discreet China ASE. Sep,2000"
        )
	  rollout MeshExtruderR "Mesh Extruder Rollout"
       (
              --group design
			group "Object Picker"
			(
              	label lab5 "Origin Obj--->Meshed Obj"
				label lab6 ""
              	--fn foo_filt obj = classof (Obj)=="Geometry"
				pickbutton chooseMesh "Pick" message:"Please Select Your Mesh" --filter:foo_filt
			)
			group "Extrude Sets"
            (
                spinner extL "Extrude Low" range:[-100,100,1] type:#float
	            spinner extH "Extrude High" range:[-100,100,2] type:#float
				label lab7 "Apply Map:"
				mapbutton choosemap "<<none>>"  tooltip:"Select A Map As Extrude Value" enabled:false
			)
     	
			group "Scale Ratio Sets"
             (  
                  	spinner scaleL "Scale Low" range:[0,100,80] type:#float
	              	spinner scaleH "Scale High" range:[0,100,90] type:#float
     	     )
			 group "Area Feature Ratio Sets"
			 (
                  	spinner lbR "Large Block:" range:[0,100,0] type:#float
                  	spinner bVR "Baldness Value" range:[0,100,0] type:#float
					checkbox orderO "Ordered Mesh Baldness" checked:true
			 )
			 group "Send Command!"
			 (
			 		button extrudeM "Bump The Mesh" toolTip:"Your baby will be hiden" 
			 		button retrieveM "Retrieve" toolTip:"Get my baby back!" 
			  )
               --event part
		     on chooseMesh picked i do 
         		(
				orgObj=i
				if not (classof i == Editable_mesh or classof i==triMesh) then
					(targetMesh=snapshot i 
					 hide i)
				else 
					targetMesh=i				
              	lab6.text=i.name+"--->"+targetMesh.name				
          	 )
			 on extL changed i do 
			 (
			 if i>extrudeValueHigh do (extrudeValueHigh=i
								    extH.value=i)
			 extrudeValueLow=i
			 )
    
               on extH changed i do 
			 (
			 if i<extrudeValueLow do (extrudeValueLow=i
								   extL.value=i)
			 extrudeValueHigh=i
			 )
	          on chooosemap picked texmap do
			 (
			 extMap=texmap
			 )
			  
			 on scaleL changed i do 
			 (
			 if i>extrudeScaleHigh do (extrudeScaleHigh=i
									scaleH.value=i)
			 extrudeScaleLow=i
			 )
    
               on scaleH changed i do 
			 (
			 if i<extrudeScaleLow do (extrudeScaleLow=i
			 						scaleL.value=i)
			 extrudeScaleHigh=i
			 )
			 
	 		 on lbR changed i do largeBlockR=i
			 on bvR changed i do baldValue=i
			 on orderO changed i do orderOption=i 
			 on extrudeM pressed do
             (  	
				if targetMesh!=undefined then 
				    	--judge the mesh mass
					if targetMesh.selectedFaces.count>10000 or( targetMesh.selectedFaces.count==0 and targetMesh.numFaces>10000)then
					   if (queryBox "Too many faces will cause much time. Are You Sure?" title:"Time Caution!" beep:true) do
					   meshEx targetMesh
					else
					   meshEx targetMesh
				else 
				       messageBox "Some elements not set. Check them again!"
			 )
			 on retrieveM pressed do
			 (if targetMesh!=undefined then
				 (if targetMesh!=orgObj then
				 	(
					 lab6.text=""
					 delete targetMesh
					 targetMesh=undefined
					 unhide chooseMesh.object
					 )
				  )
				 else
			 	messageBox "You don't need retrieve anything!"
			 )
	  )
	  --Open Rollout 
      on MeshExtruder open do 
      (  
              addRollout aboutMeshExtruder
              addRollout MeshExtruderR
       ) 

       --close Rollout
       on MeshExtruder close do
       (  
              removeRollout aboutMeshExtruder
              removeRollout MeshExtruderR
        )
	  

)
