mirror of
https://github.com/allankoechke/ADN-3dsMax-Explode-Geometry.git
synced 2026-11-30 09:22:10 +00:00
[PR #6] feat: feat: experimental multithreading #6
Reference in New Issue
Block a user
No description provided.
Delete Branch "%!s()"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
📋 Pull Request Information
Original PR: https://github.com/allankoechke/ADN-3dsMax-Explode-Geometry/pull/6
Author: @allankoechke
Created: 7/21/2026
Status: 🔄 Open
Base:
master← Head:5-experimental-multithreading📝 Commits (5)
70992dffeat: begin implementation for #5b1c62cbfeat: disable scene redraws during explode loop353ad28feat: suppress per-face undo entries during explode loop5dad8b8feat: throttle processEvents() to every 20 faces during explode loop4f267e6feat: refactor MNMesh path to read-then-create pattern📊 Changes
2 files changed (+154 additions, -110 deletions)
View changed files
📝
Bundle2/Contents/python/explode_geometry.py(+77 -55)📝
Python/explode_geometry.py(+77 -55)📄 Description
Closes #5
Explode Geometry Performance Optimization Implementation Plan
Overview
Research confirmed that
pymxs.runtimecalls cannot be made from background threads — 3ds Max is single-threaded for all scene graph operations. "Multithreading" is therefore implemented as a set of main-thread performance optimizations: disabling scene redraws during the face loop, suppressing per-face undo recording, throttling the Qt event-loop pump, and restructuring the MNMesh path to eliminate an inherent sequential index-shifting dependency. BothPython/explode_geometry.pyandBundle2/Contents/python/explode_geometry.pyare identical and both require the same changes.Current State Analysis
Key Discoveries:
rt.mesh(),rt.addModifier(), andrt.maxOps.collapseNode()triggers a viewport update per face (Python/explode_geometry.py:261-296)QApplication.processEvents()is called once per face via_prog(), adding Qt event-loop overhead on every iteration (Python/explode_geometry.py:257-259)with pymxs.undo(True, "Explode Geometry"):exists but individual per-face calls still record internal undo data, adding overhead (Python/explode_geometry.py:261)polyop.detachFaceswhich mutates the source mesh per call and shifts face indices, forcing a reverse-order sequential loop (Python/explode_geometry.py:360-362)rt.redrawViews()is already called once after the loop (Python/explode_geometry.py:296) — viewport state is recoverable if redraws are suppressed during the loopBundle2/Contents/python/explode_geometry.pyis identical toPython/explode_geometry.pyand needs the same changesDesired End State
The explode operation on large meshes (100+ faces) completes measurably faster. The viewport does not flicker per-face during the operation. A single
Ctrl+Zreverts the entire explode. The progress bar updates at reasonable intervals (~every 20 faces). All existing functionality (Shell modifier, Edit Mesh, collapse stack, center pivot, delete original, TriMesh/MNMesh modes) is preserved and produces identical geometry output.Verification: Run explode on a 500+ face mesh with Shell + Collapse Stack enabled. Record the
elapsedtime printed to the MAXScript Listener. Compare to the pre-patch baseline on the same mesh.What We're NOT Doing
threading.Thread,concurrent.futures.ThreadPoolExecutor, ormultiprocessingforpymxscalls (confirmed impossible per Autodesk documentation)pymxs.mxstoken()usage (deprecated, non-functional)pymxs(deadlock risk)Python/explode_geometry_classic.py(MaxPlus-based, legacy/unsupported since 3ds Max 2022)Implementation Approach
Implement the four ranked optimizations from the research in priority order. Tasks 1 and 2 (redraw suppression + undo suppression) are the highest-impact changes. Task 3 (processEvents throttle) is a quick secondary improvement. Task 4 (MNMesh refactor) eliminates an algorithmic sequential dependency. All changes are applied to both script files.
IMPORTANT: Checkboxes are for implementation steps only. Steps requiring manual human verification are in the numbered "Manual Testing Steps" section at the bottom.
TASK 1: Disable Scene Redraws During the Explode Loop [HIGH PRIORITY]
Status: DONE
Milestone: The 3ds Max viewport no longer updates per-face during explode; a single
rt.redrawViews()fires once at the end.rt.disableSceneRedraw()andrt.enableSceneRedraw()are accessible viapymxs.runtime(test in MAXScript Listener:python.execute "import pymxs; pymxs.runtime.disableSceneRedraw()")rt.execute("disableSceneRedraw()")andrt.execute("enableSceneRedraw()")as fallbackdo_explode()inPython/explode_geometry.py, addrt.disableSceneRedraw()immediately before thewith pymxs.undo(True, "Explode Geometry"):block (before line 261)try/finallythat callsrt.enableSceneRedraw()thenrt.redrawViews()in thefinallyclausert.redrawViews()at line 296 since it is now inside thefinallyblockBundle2/Contents/python/explode_geometry.pyValidation: Run explode on a 200+ face mesh. Viewport must not flicker per-face.
rt.redrawViews()fires once at completion. MAXScript Listener shows lowerelapsedtime vs pre-patch baseline.Requirements from spec:
rt.redrawViews()) after explode completesFiles to Modify:
Python/explode_geometry.py—do_explode()method (~line 261)Bundle2/Contents/python/explode_geometry.py— identical changeImplementation Details:
TASK 2: Suppress Per-Face Undo Entries [HIGH PRIORITY]
Status: DONE
Milestone: The undo stack shows a single "Explode Geometry" entry; per-face undo recording overhead is eliminated.
convert_to_triangle_faces()inPython/explode_geometry.py(lines 330-347), wrap the per-face block (rt.mesh(...)throughapplySettings(...)) inwith pymxs.undo(False):convert_to_mnmesh_faces()inPython/explode_geometry.py(lines 360-372), wrap the per-face block (rt.polyop.detachFaces(...)throughapplySettings(...)) inwith pymxs.undo(False):with pymxs.undo(True, "Explode Geometry"):indo_explode()intact — it provides the single undo group entryBundle2/Contents/python/explode_geometry.pyValidation: Explode a 50+ face mesh. Press
Ctrl+Zonce. The entire explode operation (all created nodes removed, original restored) must be undone in a single step. The Edit menu undo history must show exactly one "Explode Geometry" entry.Requirements from spec:
with pymxs.undo(False):insidewith pymxs.undo(True, ...):must correctly scope undo to the outer block (test this before committing)Files to Modify:
Python/explode_geometry.py—convert_to_triangle_faces()(~lines 330-347) andconvert_to_mnmesh_faces()(~lines 360-372)Bundle2/Contents/python/explode_geometry.py— same changesImplementation Details:
In
convert_to_triangle_faces:TASK 3: Throttle
processEvents()to Every N Faces [MEDIUM PRIORITY]Status: DONE
Milestone: Qt event-loop pump overhead is reduced from O(faces) to O(faces/20) while the progress bar remains responsive.
do_explode()inPython/explode_geometry.py, update the_prog()inner function (lines 257-259) to only callQtWidgets.QApplication.processEvents()whencur % 20 == 0 or cur == totself.progress_bar.setValue(...)updating every call — it is cheap and ensures accurate display whenprocessEvents()does fireQtWidgets.QApplication.processEvents()at line 254 (after showing the progress panel) unchanged — that call is outside the hot pathBundle2/Contents/python/explode_geometry.pyValidation: On a 200-face mesh, the progress bar must advance in visible steps (~10% increments). The UI must remain responsive (window can be moved, not frozen). No single progress jump exceeds ~20% in one tick.
Requirements from spec:
Files to Modify:
Python/explode_geometry.py—_prog()inner function insidedo_explode()(~lines 257-259)Bundle2/Contents/python/explode_geometry.py— same changeImplementation Details:
TASK 4: Refactor MNMesh Path to Read-Then-Create Pattern [MEDIUM PRIORITY]
Status: DONE
Milestone:
convert_to_mnmesh_faces()no longer usespolyop.detachFaces. All vertex data is read in Phase 1 before any node is created in Phase 2, removing the index-shifting sequential dependency._create_poly_node_from_positions(positions)toPython/explode_geometry.py(after_random_wire_color). It must: create a newPolyMeshObjectnode viart.newObjectNode(rt.PolyMeshObject()), callrt.polyop.setNumVerts,rt.polyop.setVertfor each position, thenrt.polyop.createFacewith a 1-based index arrayrt.newObjectNodeorrt.PolyMeshObjectare not accessible in pymxs, usert.execute("newObjectNode (PolyMeshObject())")and retrieve the node viart.objects[-1]convert_to_mnmesh_faces()inPython/explode_geometry.py(lines 351-373): Phase 1 iteratesrange(1, num_faces + 1)(forward order), callsrt.polyop.getFaceVertsandrt.polyop.getVertfor each vertex, stores results as plain Python(x, y, z)tuples — retains no pymxs objects after this phase_create_poly_node_from_positions(positions)per face, setswireColor, callsapplySettingsrange(num_faces, 0, -1)fromconvert_to_mnmesh_faces— forward order is correct since Phase 1 does not mutate the sourcert.delete(node)(thedeleteOriginalpath indo_explode()) is called only after both phases complete, since Phase 1 reads from the source meshBundle2/Contents/python/explode_geometry.pyValidation: Explode a quad-face plane in MNMesh mode. Each resulting node must be a single-face Editable Poly with correct vertex positions (check in modifier panel). Explode a pentagon (5-vert n-gon) to confirm n-gon topology is preserved, not triangulated.
Requirements from spec:
Files to Modify:
Python/explode_geometry.py— add_create_poly_node_from_positions()helper; refactorconvert_to_mnmesh_faces()(~lines 351-373)Bundle2/Contents/python/explode_geometry.py— same changesImplementation Details:
Phase 1 (read, no mutation):
Phase 2 (create from plain Python data):
Helper function:
Testing Strategy
Unit Tests:
Integration Tests:
Manual Testing Steps:
Python/explode_geometry.py, create a GeoSphere with 100+ facesCtrl+Zonce after explode — verify full operation is undone in a single stepPerformance Considerations
detachFacesper-call overhead and removing the reverse-iteration constraint🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.