Skip to content

Instantly share code, notes, and snippets.

@Eterea
Last active August 29, 2015 14:05
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Eterea/98fb6bedf12cdc5931b9 to your computer and use it in GitHub Desktop.
Save Eterea/98fb6bedf12cdc5931b9 to your computer and use it in GitHub Desktop.
This Modo script will divide an edge on a mesh slicing it evenly to arrive as closer as possible to a "balanced" uniform mesh
#python
#--------------------------------------------------------------------------------------
# NAME: etr_sliceToSquares.py
# VERS: 1.1
# DATE: August 11, 2013
#
# MADE: Cristobal Vila, etereaestudios.com
#
# USES: This script will divide an edge on a mesh (like a vertical edge in a cylinder)
# slicing it evenly to arrive as closer as possible to a "balanced" uniform mesh,
# like if it would be created using near to squared polygons in both axes.
#--------------------------------------------------------------------------------------
# Calculate Length for Selected Edge and Store it in a Set
myEdge_ID = lx.evalN('query layerservice edges ? selected')
myEdge_LEN = lx.eval('query layerservice edge.length ? %s' % myEdge_ID)
lx.eval('select.editSet tempSetA add')
# Define Function to Slice
def sliceFunction(slice_N):
lx.eval('tool.set poly.loopSlice on')
lx.eval('tool.attr poly.loopSlice count %s' % slice_N)
lx.eval('tool.attr poly.loopSlice mode uniform')
lx.eval('tool.attr poly.loopSlice select 0')
lx.eval('tool.attr poly.loopSlice depth 0.0')
lx.eval('tool.setAttr poly.loopSlice edit move')
lx.eval('tool.doApply')
lx.eval('tool.attr poly.loopSlice select false')
lx.eval('tool.set poly.loopSlice off 0')
# Create an individual Loop Slice transversal to our selected Edge
sliceFunction(1)
# Calculate and Store Total Lenght for that Transversal Loop of Edges
transEdges_N = lx.eval('query layerservice edge.N ? selected')
transTotal_LEN = sum(lx.eval('query layerservice edge.length ? %s' % edge)
for edge in lx.evalN('query layerservice edges ? selected'))
# Remove the Transversal Loop of Edges and recover original Edge Selection. Delete Set.
lx.eval('remove')
lx.eval('select.useSet tempSetA select')
lx.eval('!!select.deleteSet tempSetA')
# Calculate Average size for each edge on Transversal Loop
transAvge_LEN = transTotal_LEN / transEdges_N
# Calculate needed Slice Divisions: we divide our Original Edge Length
# by Average Length for each edge on Transversal Loop
sliceDivisions = myEdge_LEN / transAvge_LEN
# Stop script execution if value for sliceDivisions is too small
if sliceDivisions < 1.5:
sys.exit()
# Calculate final number for Divisions, considering decimal part
decimalPart = sliceDivisions - int(sliceDivisions)
if decimalPart < 0.5:
sliceDivisions = sliceDivisions - 1
# Execute final Slice Divisions
sliceFunction(sliceDivisions)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment