Help with turning reapable viewport and editpoly operations into automatic script.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report
Hi, I'm a newbie to max scripting. I can sort of understand code enough to read what an already written script is doing, but writing one from scratch is currently beyond me.
I'm dealing with a model set of 1000s of low poly objects, each with triangulated and detached faces.
I have a series of operations I can do per object in the viewport that cleans the models up quickly and consistantly, but it's still not viable to do it one at a time. I'd like to compile a script to automate the proccess instead.
I've tried using the macro recorder to get a list of scripts for what I'm doing, but things like "invert selection" and "select all" are recorded as "set selection, edges: 0001, 0002, 0003, etc....".
Basically the operation I want to automate is - for an array of selected objects, one object at a time:
(All objects are Editable Poly)
1. Weld all vertices at 0.001 Weld Threshold. (My scene is in meters and units are set to generic.)
2. Run the following script to select edges by angle (credit to Bobo from CGtalk forums):
macroScript SelEdgesByAngle
category:"# Scripts"
buttontext:"Edges By Angle"
tooltip:"Select Edges By Angle"
(
local theThreshold = 5
local include_open_edges = false
local auto_preview_selection = true
fn filter_function =
(
theObj = modPanel.getCurrentObject()
subObjectLevel == 2 and selection.count == 1 and (classof theObj == Editable_Poly or (classof theObj == Edit_Poly and theObj.enabled and theObj.enabledInViews))
)
on isEnabled return filter_function()
on isVisible return filter_function()
fn perform_selection =
(
selEdges = #{}
theObj = $
eCount = polyOp.getNumEdges theObj
for e = 1 to eCount do
(
theFaces = (polyOp.getEdgeFaces theObj e) as array
if theFaces.count == 2 then
(
theAngle = acos(dot (polyOp.getFaceNormal theObj theFaces[1]) (polyOp.getFaceNormal theObj theFaces[2]))
if theAngle >= theThreshold do selEdges[e] = true
)
else
if include_open_edges do selEdges[e] = true
)
case classof (modPanel.getCurrentObject()) of
(
Editable_Poly: polyOp.setEdgeSelection theObj selEdges
Edit_Poly: (modPanel.getCurrentObject()).SetSelection #Edge &selEdges
)
redrawViews()
)
on execute do perform_selection()
on altExecute theType do
(
rollout SelEdgesByAngle_Rollout "Select Edges By Angle"
(
checkbox include_open "Include Open Edges" checked:include_open_edges
spinner threshold_value "Angle Threshold" range:[0,180,theThreshold] type:#float fieldwidth:60
checkbutton preview_selection "Preview" width:85 across:2 align:#left checked:auto_preview_selection
button select_now "Update" width:85 align:#right
on threshold_value changed val do
(
theThreshold = val
if preview_selection.checked and filter_function() do perform_selection()
)
on include_open changed state do
(
include_open_edges = state
if filter_function() do perform_selection()
)
on preview_selection changed state do auto_preview_selection = state
on select_now pressed do if filter_function() do perform_selection()
)
createDialog SelEdgesByAngle_Rollout 200 70
)
)
3. Invert the selected edges.
4. Remove the selected edges (same behaviour as pressing the backspace key in viewport).
5. Repeat for all objects in selection.
This essentially re-attaches the detatched faces and removes the triangulated edges. I doesn't create perfect quads in all cases, but I don't need that anyway.
Any help on this would be very much appreciated!