If you ever needed a lot of individual chunks instead of a large mesh, you know that it's not so easy to spilt the mesh (in a grid) and detach all the resulting elements into individual meshes. If you only need to separate it in let's say 10,000 individual pieces, this script may be for you:
try destroyDialog sliceInChunks catch()rollout sliceInChunks "Slice in Chunks" width:187(spinner spnStep1 "" range:[1,10,4] type:#integer width:40 across:3 align:#leftspinner spnStep2 "\xd7 " range:[1,10,3] type:#integer width:55 offset:[-14,0]spinner spnStep3 "\xd7 " range:[1,10,3] type:#integer width:55 align:#rightcheckBox chxHidden "Make the sliced chunks hidden" checked:true align:#center offset:[0,3]button btnSlice "" height:27 align:#center offset:[0,5]progressBar pbStep1 "" width:170 height:10 color:orange offset:[-5,5]progressBar pbStep2 "" width:170 height:10 color:[0,100,180] offset:[-5,0]local detachFaces = meshOp.detachFaceslocal getElement = meshOp.getElementsUsingFacelocal meshSlice = meshOp.slicefn updateCount =btnSlice.text = " Slice into " + (spnStep1.value^2 * spnStep2.value^2 * spnStep3.value^2) as string + " pieces "mapped fn renameDetached obj formatStr &objCount =obj.name += formattedPrint (objCount += 1) format:formatStrfn explodeMesh obj intCount &detachedObjs =(detachedObjs[1] = objfor element = 2 to intCount do(local newMesh = detachFaces obj (getElement obj 1) delete:true asMesh:true(detachedObjs[element] = (copy obj name:#detached isHidden:true)).mesh = newMeshdelete newMesh))fn explodeMeshNoCollect obj intCount =for element = 2 to intCount do(local newMesh = detachFaces obj (getElement obj 1) delete:true asMesh:true(copy obj name:#detached isHidden:true).mesh = newMeshdelete newMesh)fn sliceObjXY obj intCount =(local objFaces = obj.faceslocal sliceWidthX = (obj.max - obj.min).x/intCountlocal slicePosX = obj.min.x - obj.pos.xlocal sliceWidthY = (obj.max - obj.min).y/intCountlocal slicePosY = obj.min.y - obj.pos.yfor i = 2 to intCount do(meshSlice obj objFaces [1,0,0] (slicePosX += sliceWidthX) separate:truemeshSlice obj objFaces [0,1,0] (slicePosY += sliceWidthY) separate:true)obj)fn sliceObjChunks obj intStep1 intStep2 intStep3 =(if queryBox ("This operation will reset undo buffer.\nAre you sure you want to continue?") title:"Warning"do with redraw off, undo off(gc()clearSelection()statusPanel.visible = falsesetCommandPanelTaskMode mode:#createif NOT isKindOf obj Editable_mesh doconvertToMesh objobj.name = #detachedhide objlocal intStep1_2 = intStep1^2, intStep2_2 = intStep2^2, intStep3_2 = intStep3^2local progressStep1 = 100/intStep1_2, progressStep2 = 100/intStep2_2local detachedObjs = #() ; detachedObjs[intStep1_2] = undefinedlocal explodedArr = #() ; explodedArr[intStep2_2] = undefinedexplodeMesh (sliceObjXY obj intStep1) intStep1_2 &detachedObjsfor each in detachedObjs do(pbStep2.value = 0explodeMesh (sliceObjXY each intStep2) intStep2_2 &explodedArrfor chunk in explodedArr do(explodeMeshNoCollect (sliceObjXY chunk intStep3) intStep3_2pbStep2.value += progressStep2)each = undefinedif keyboard.escPressed do exitpbStep1.value += progressStep1)detachedObjs = $detached* as arraylocal objCount = 0local formatStr = "." + ((detachedObjs.count as string).count as string) + "i"renameDetached detachedObjs formatStr &objCountif NOT chxHidden.checked do unhide detachedObjsstatusPanel.visible = truepbStep1.value = pbStep2.value = 0completeRedraw()))on sliceInChunks open do updateCount()on spnStep1 changed val do updateCount()on spnStep2 changed val do updateCount()on spnStep3 changed val do updateCount()on btnSlice pressed do(st = timeStamp()sliceObjChunks selection[1] spnStep1.value spnStep2.value spnStep3.valueformat "Time: % sec.\n" ((timeStamp() - st)/1000.)))createDialog sliceInChunks
USAGE: Select an object that you want to slice, enter numbers that when multiplied together give the number of segments in X as well as Y direction and press the button. If you want to stop execution of the script, press and hold ESC key (it needs to be pressed when the blue bar reaches end). If you don't want the resulting meshes to be hidden, uncheck the checkbox.
DISCLAIMER: All scripts and snippets are provided as is under Creative Commons Zero (public domain, no restrictions) license. The author and this blog cannot be held liable for any loss caused as a result of inaccuracy or error within these web pages. Use at your own risk.



This Post needs Your Comment!
Leave a Comment