Showing posts with label name. Show all posts
Showing posts with label name. Show all posts

Tuesday, October 7, 2014

Rearrange your geometry group for tidiness!

Haven't got much time to update lately. Been busy with work.
Still, I managed to wrote a little script when my patience was out looking at a messy geo_grp from the model department.

Just select the root geo_grp, the script will reorder all the children under it alphabetically (groups will be ordered last).

import pymel.core as pm
import maya.OpenMaya as om


def rearrangeGeoGrp(geoGrp=None):
    if not geoGrp:
        try:
            geoGrp = pm.selected(type='transform')[0]
        except:
            pass
        if not geoGrp:
            om.MGlobal.displayError('Select a geo group.')
            return

    exit = False
    grps = [geoGrp]
    while not exit:
        nextGrps = []
        for grp in grps:
            children = grp.getChildren(type='transform')

            if not children:
                om.MGlobal.displayWarning('%s : is an empty group.' %grp.longName())

            # seperate grps and plys
            cgrps, plys = [], []
            for c in children:
                checkIfPly = False
                try:
                    shp = c.getShape(ni=True)
                    if isinstance(shp, pm.nt.Mesh):
                        checkIfPly = True
                except:
                    pass

                if checkIfPly == True:
                    plys.append(c)
                else:
                    cgrps.append(c)
                    nextGrps.append(c)

            plys = sorted(plys)
            cgrps = sorted(cgrps)
            plys.extend(cgrps)

            for i, item in enumerate(plys):
                currentIndx = grp.getChildren(type='transform').index(item)
                pm.reorder(item, r=((currentIndx * -1) + i))

        if nextGrps:
            grps = nextGrps
        else:
            exit = True