Monday 25 March 2013

Snap to nearest gridpoint

By request, a script to move selected vertices to the nearest gridpoint. I also threw in snapping selected objects to nearest gridpoint.




Usage
  • Select verts of an editable poly or select objects
  • run script
  • verts/objects will be at nearest gridpoints. (note: uses objects pivot)
  
-- MixeScript snippet: Snap to Nearest Grid Point
-- Snaps selected verts or objects to nearest grid point

--USAGE
-- Select verts of an editable poly or select objects
-- run script
-- choose button to snap to
-- verts/objects will be at nearest gridpoints. (note: uses objects pivot)

fn findnearestgridpoint frompoint axis = (
 spacing = getGridSpacing()
 
 nearestx = frompoint.x
 nearesty = frompoint.y
 nearestz = frompoint.z
 
 nearestx = (floor (frompoint.x / spacing + 0.5)) * spacing
 nearesty = (floor (frompoint.y / spacing + 0.5)) * spacing
 nearestz = (floor (frompoint.z / spacing + 0.5)) * spacing
 
 if axis == "x" then ( nearesty = frompoint.y; nearestz = frompoint.z)
 if axis == "y" then ( nearestx = frompoint.x; nearestz = frompoint.z)
 if axis == "z" then ( nearesty = frompoint.y; nearestx = frompoint.x)
  
 return [nearestx,nearesty,nearestz]
)

fn DoSnap axis = 
(
 if subobjectlevel == 1 then 
 ( 
   obj = $
   vertselection = polyop.getVertSelection obj
  max modify mode
  for vert in vertselection do (
   polyop.setVert obj vert (findnearestgridpoint (polyop.getVert obj vert) axis)
  )
 ) else (
  objarray = getcurrentselection()
  for obj in objarray do (
   obj.pos = findnearestgridpoint obj.pos axis
  )
 )
)

rollout rol_SnapToGrid "Snap To Grid Points" width:136 height:48
(
 button snapX "X" pos:[8,24] width:24 height:19
 label lbl1 "Snap to Grid Points" pos:[24,5] width:96 height:16
 button snapY "Y" pos:[40,24] width:24 height:19
 button snapZ "Z" pos:[72,24] width:24 height:19
 button SnapAll "All" pos:[104,24] width:24 height:19
 
 on snapX pressed do (undo on (DoSnap "x"))
 on snapY pressed do (undo on (DoSnap "y"))
 on snapZ pressed do (undo on (DoSnap "z"))
 on SnapAll pressed do (undo on (DoSnap "all"))
)

createDialog rol_SnapToGrid dlg_SnapToGrid
  

No comments:

Post a Comment