Hello!

I’m working on a Maya Python script that takes a plane and divides it into even sections, then separates those sections into distinct objects. I’ve written the script and it seems to work, but it runs very slow on large meshes and I’m scratching my head as to whether there’s a way to optimize it.

The script assumes that the provided plane has length and width resolution in powers of two (ex. 64, 128, 256, etc.). The script is intended to be used on landscape meshes in a game engine to help with optimization using occlusion culling.

Here is the script. Give it a try on a plane with resolution 32 x 32 and it should run ok. If the resolution is much higher than that, you will notice significant performance issues.

```
import maya.cmds as cmds
import math
# 4 divisions will result in 4 x 4 = 16 separate pieces
divisions = 4
# get the selected object
sel = cmds.ls( selection=True, long=True )
obj = sel[0]
# get the verts along the border of the plane
faces = cmds.polyListComponentConversion( obj, toFace=True )
border_edges = cmds.polyListComponentConversion( faces, toEdge=True, bo=True )
verts = cmds.polyListComponentConversion( border_edges, toVertex=True)
verts = cmds.ls( verts, flatten=True, long=True )
# get one of the corner verts
for v in verts:
print( v )
adj_edges = cmds.polyListComponentConversion( v, toEdge=True)
adj_edges = cmds.ls( adj_edges, flatten=True, long=True )
if len( adj_edges ) <= 2:
corner_vert = v
break
# get the corner edges
corner_edges = cmds.polyListComponentConversion( corner_vert, toEdge=True )
corner_edges = cmds.ls( corner_edges, flatten=True, long=True )
detatch_edges = set()
for k in range( 2 ):
# get the edges of one dimension
cmds.select( corner_edges[k] )
cmds.polySelectConstraint( t=0x8000, pp=4 )
edges_u = cmds.ls( selection=True, long=True, flatten=True )
# get the division resolution
resolution = len( edges_u )
div_resolution = int( math.floor( resolution / divisions ) )
# convert the border edges to a set
border_edges = cmds.ls( border_edges, flatten=True, long=True )
border_edges = set( border_edges )
grow_faces = cmds.polyListComponentConversion( edges_u, toFace=True )
grow_faces = cmds.ls( grow_faces, long=True, flatten=True )
for j in range( divisions-1 ):
# loop start
for i in range( div_resolution ):
step_edges = cmds.polyListComponentConversion( grow_faces, toEdge=True, bo=True )
step_edges = cmds.ls( step_edges, flatten=True, long=True )
step_edges = set( step_edges )
leading_edge = step_edges.difference( border_edges )
new_faces = cmds.polyListComponentConversion( leading_edge, toFace=True )
new_faces = cmds.ls( new_faces, long=True, flatten=True )
new_faces = set( new_faces )
grow_faces = set( grow_faces )
grow_faces = grow_faces.union( new_faces )
grow_faces = list( grow_faces )
detatch_edges = detatch_edges.union( leading_edge )
#cmds.select( detatch_edges )
# split edges
cmds.polySplitEdge( detatch_edges, op=1 )
# separate
cmds.polySeparate( obj, ch=0 )
```