Need help to simplify script for area cropping

This script do perfect job to crop all mesh object in the scene to the rectangle area
Since last versions of 3dsMax allows to Slice Along both X and Y would it be possible to use only two Slice Modifiers which will be located at top-left and bottom-right of the rectangle shape and be applied to all mesh objects in the scene rather then one-by-one?

(

fn shapeFilt o = (superClassOf o == Shape)
rectObj = pickObject prompt:"Pick Rectangle" filter:shapeFilt

if rectObj != undefined then
(
objs= for obj in objects where superClassOf obj == GeometryClass and obj.isHiddenInVpt == false collect obj
for obj in objs do
(
try(
select obj
addmodifier obj (SliceModifier Slice_Type:3)
objBBox = nodeGetBoundingBox rectObj Obj.transform
obj.modifiers[1].Slice_Plane.transform = (matrix3 [0,0,-1] [0,1,0] [1,0,0] [objBBox[1].x,0,0])
addmodifier obj (SliceModifier Slice_Type:3)
obj.modifiers[1].Slice_Plane.transform = (matrix3 [0,0,1] [0,1,0] [-1,0,0] [objBBox[2].x,0,0])
addmodifier obj (SliceModifier Slice_Type:3)
obj.modifiers[1].Slice_Plane.transform = (matrix3 [0,0,-1] [-1,0,0] [0,1,0] [0,objBBox[1].y,0])
addmodifier obj (SliceModifier Slice_Type:3)
obj.modifiers[1].Slice_Plane.transform = (matrix3 [1,0,0] [0,0,1] [0,-1,0] [0,objBBox[2].y,0])
gc()
convertToMesh obj
meshOps.removeIsolatedVerts obj
if obj.mesh.verts.count == 0 then delete obj
update obj
)catch()
)
)
)

Comments

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
harumscarum's picture

Thank you very much

I appreciate the UI and Undo features, but they aren't necessary. Additionally, there are issues with objects that have transformations. I've attached a screenshot showing a rotated object that has been sliced incorrectly. This issue could be resolved by using 'Reset XForm,' but I'm concerned that this isn't an efficient solution because it could slow down the script

No, there's no need to attach all the objects into one. The Slice modifier should be applied as a single instanced modifier to all visible objects. This approach could potentially resolve the issues related to object transformations. When I perform these steps manually, it works effectively:
- Select all visible objects.
- Assign the Slice Modifier, which unifies the slice planes across all selected objects.

AttachmentSize
slicer.jpeg 86.1 KB
SimonBourgeois's picture

now it's also working with scaled objects

https://www.mediafire.com/file/1bj78smdnzd4wfd/Rectangle_Area_Cropping_V...

i didn't find an other way then to resetxform the obj before applying the slice modifiers, in term of computing time it is still way faster than the original script, and it works for rotated objects i 've tried with 10 000 boxes and it did it in maybe 20sec , in comparaison i've tried with the original script and it is still processing...

SimonBourgeois's picture

new version

i came up with a solution , you can download the new version here: https://www.mediafire.com/file/ps7d4f3zmeujw0r/Rectangle_Area_Cropping_V...

i wasn't able to solve it the way you were thinking.
When all objects are selected when adding the slice it works as you said , but i don't know how to replicate this behavior in maxscript, i tried instancing the slice modifier but they end up at different positions, they were instanced but with their own coordinates, i think that, when you do it manualy it's averaging each object transform and apply the same result to every one, doing the same in maxscript is a bit out of my league, matrices can be a little tricky... but i found an other way, now it works even on rotated objects.
I removed the line " objBBox = nodeGetBoundingBox rectObj Obj.transform", it was getting the bounding box of the rectangle in reference to each object transform, as the modifiers gizmos are in local space it makes sense. but "Obj.transform" is a matrix3 which also contain rotation and scaling, instead i used the actual position of the rectangle and its min and max values and for the rotation i used the rotation value of the rectangle minus the rotation of the object and it worked just fine, i think it's also a little faster :)

edit: there is still a little problem, i didn't think about scaled object, i've just tried and it's not working very well, i will try to find a solution....

SimonBourgeois's picture

Modified Script

Hi,
i 've modified your script, and added a small UI with one button to pick the rectangle shape, you can download it here:

https://www.mediafire.com/file/gjoxt9f2rmit4ix/Rectangle_Area_Cropping.m...

i've also remove some unnecessary stuff and add some basic optimisation ( redraw off, max create mode), i also added the ability to undo which will maybe slow the script a little, but it could be nice to undo if your rectangle wasn't at the right place or at the right size.
if you need, i could add an option to apply the script only on selected objects rather than on entire scene.
Also i don't understand what you mean by " be applied to all mesh objects in the scene rather then one-by-one" , it is currently doing all objects in the scene, the only way i could think of doing it in one operation, would be to attach all objects together then add the slice but you would end up with only one mesh containing the all scene and the attaching process would add extra computing time...

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.