[Closed] A quick and simple slicer
Here’s the core of a script I’ve been refactoring. I thought I’d share it with everyone and hopefully get some feedback on how I could make it faster or better. This is based on GARP’s voroni fracture script, but the only pieces that survived the refactoring was the ‘cleanCopy’ code block. I removed code that used arrays, as well as code that gathered positions from a particle system (I found that this code failed (most of the time) for large objects (>1,000 units)).
But as a result of this approach, there is no way to set how many fragments will be created. Also, there is a misalignment between the plane and the slicePlane which I’ve been unable to fix, although the misalignment is within an acceptable value (most of the time).
Here ya go, please post any of your thoughts/criticisms/suggestions:
(
--acquire clean copy of selected
local origObj = $; resetPivot origObj
theCopy = copy origObj
resetXForm theCopy
convertToMesh theCopy
local theMesh = editable_mesh()
theMesh.mesh = theCopy.mesh
theMesh.transform = theCopy.transform
theMesh.name = "Fragment"
resetXForm theMesh; resetPivot theMesh
convertToPoly theMesh
delete theCopy; delete origObj
for i=1 to 3 do
(
--create a plane at theMesh.pos, randomize rotation
local thePlane = plane width:1000 length:1000 widthSegs:2 lengthSegs:2 isSelected:off
thePlane.pos = theMesh.pos
local myRand = eulerangles (random -1000 1000) (random -1000 1000) (0); rotate thePlane myRand
--slice mesh, align slicePlane
addModifier theMesh (sliceModifier slice_type:1)
theMesh.slice.slice_plane.pos = thePlane.pos
theMesh.slice.slice_plane.transform = thePlane.transform
delete thePlane; addModifier theMesh (cap_holes()); convertToPoly theMesh
)
fn detachSub myMesh =
(
--detach one of the sub elements
myMesh.EditablePoly.SetSelection #Face #{1}; myMesh.SelectElement()
local theFaces = polyop.getFaceSelection myMesh
polyop.detachFaces myMesh theFaces delete:true asNode:true name:"Fragment"
polyop.CollapseDeadStructs myMesh
local maxFaces = polyop.getNumFaces myMesh
--format "maxFaces:%
" maxFaces
if maxFaces > 0 then detachSub myMesh
)
detachSub theMesh; delete theMesh
)
I refined this core into a rollout. This rollout was then integrated in destruktorV2.
Here’s a standalone version of simpleSlicer, perhaps someone will find the detachSub function useful:
global simpleSlicerRL
try destroyDialog simpleSlicerRL catch()
rollout simpleSlicerRL " simpleSlicer"
(
local myIterations = 3 --links the spinner to the variable
fn fractureMesh myMesh =
(
--acquire clean copy of selected
local origObj = myMesh; resetPivot origObj
local theCopy = copy origObj
resetXForm theCopy
convertToMesh theCopy
local theMesh = editable_mesh()
theMesh.mesh = theCopy.mesh
theMesh.transform = theCopy.transform
theMesh.name = "Fragment"
resetXForm theMesh; resetPivot theMesh
convertToPoly theMesh
delete theCopy; delete origObj
for i=1 to myIterations do
(
--create a plane at theMesh.pos, randomize rotation
local thePlane = plane width:100000 length:100000 widthSegs:2 lengthSegs:2 isSelected:off
centerPivot theMesh; thePlane.pos = theMesh.pos; resetPivot theMesh
--local myRand = eulerangles (random -1000 1000) (random -1000 1000) (0); rotate thePlane myRand
rotate thePlane (eulerangles (random -1000 1000) (random -1000 1000) (random -1000 1000))
--slice mesh, align slicePlane
addModifier theMesh (sliceModifier slice_type:1)
theMesh.slice.slice_plane.pos = thePlane.pos
theMesh.slice.slice_plane.transform = thePlane.transform
delete thePlane; addModifier theMesh (cap_holes())
convertToPoly theMesh; select theMesh
)
)
fn detachSub myMesh =
(
--detach one of the sub elements
myMesh.EditablePoly.SetSelection #Face #{1}; myMesh.SelectElement()
local theFaces = polyop.getFaceSelection myMesh
polyop.detachFaces myMesh theFaces delete:true asNode:true name:"Fragment"
polyop.CollapseDeadStructs myMesh
local maxFaces = polyop.getNumFaces myMesh
--format "maxFaces:%
" maxFaces
if maxFaces > 0 then detachSub myMesh else select myMesh
)
button btnFrack "Slice selected" pos:[5,25] width:85 height:25
on btnFrack pressed do (try(fractureMesh $; detachSub $; delete $; clearSelection())catch())
spinner iterationsSpinner "Slices:" pos:[20,5] width:70 height:16 range:[1,15,3] type:#integer
on iterationsSpinner changed val do (myIterations = val)
) -- end rollout destruktorRL
createDialog simpleSlicerRL 95 55 100 100 style:#(#style_toolwindow, #style_sysmenu, #style_resizing)
Here’s a screencap: