I'm using this code to remove all spline segments outside a certain object; however, on complex splines, it consumes all available RAM immediately and crashes 3ds Max.
Is there any way to make it significantly faster and less memory-intensive?
Appreciated in advance
(
fn getMeshBoundingBox2D obj =
(
local bbMin = [1e9,1e9,0]
local bbMax = [-1e9,-1e9,0]
local t = obj.transform
local vCount = getNumVerts obj
for i = 1 to vCount do (
local p = getVert obj i * t
bbMin.x = amin bbMin.x p.x
bbMin.y = amin bbMin.y p.y
bbMax.x = amax bbMax.x p.x
bbMax.y = amax bbMax.y p.y
)
return #(bbMin, bbMax)
)
fn isInside2D p minBB maxBB =
(p.x >= minBB.x and p.x <= maxBB.x and p.y >= minBB.y and p.y <= maxBB.y)
fn keepSegmentsInBox shapeObj minBB maxBB =
(
local t = shapeObj.transform
for i = numSplines shapeObj to 1 by -1 do (
local newPts = #()
local n = numKnots shapeObj i
local closed = isClosed shapeObj i
for k = 1 to n do (
local nextK = if (k == n) then 1 else (k + 1)
local p1 = getKnotPoint shapeObj i k * t
local p2 = getKnotPoint shapeObj i nextK * t
if (isInside2D p1 minBB maxBB and isInside2D p2 minBB maxBB) do (
append newPts (getKnotPoint shapeObj i k)
)
)
deleteSpline shapeObj i
if newPts.count > 1 do (
addNewSpline shapeObj
for pt in newPts do (
addKnot shapeObj (numSplines shapeObj) #corner #line pt
)
)
)
updateShape shapeObj
)
local satelliteMesh = getNodeByName "satellite_texture"
if not isValidNode satelliteMesh do
satelliteMesh = getNodeByName "natural_land"
if isValidNode satelliteMesh and classof satelliteMesh == Editable_Mesh do (
local bb = getMeshBoundingBox2D satelliteMesh
local minBB = bb[1]
local maxBB = bb[2]
local splineNames = #("xx_water_gradient", "xx_spline_trees", "xx_spline_planes")
for name in splineNames do (
local s = getNodeByName name
if isValidNode s and (classof s == line or classof s == splineShape or classof s == Editable_Spline) do (
keepSegmentsInBox s minBB maxBB
)
)
for s in objects where classof s == ChaosScatter do (
s.enable = false
forceCompleteRedraw()
s.enable = true
)
)
)
By harumscarum · 2025-05-13

miauu · 2025-05-13