polySelectConstraint hard edges automagically - how?

I have files that have been tagged with a hard edge from another program, but the value is not correct. I want to select those tagged edges and do a polyCrease with a val of 2. However, i dont want to select them manually because i have around 500 sub objects all with the same problem.



sel = cmds.ls(sl=1)
cmds.polySelectConstraint(m=3, border=0, shell=0, crease=1)
sel_edges = cmds.polyListComponentConversion(toEdge=1)
cmds.select (sel_edges, r=1)

cmds.polyCrease(hardList, value=2, ch=1)


Here’s what I’d try:

Walk a list of all the verts and convert each one to vert normals. Anything that converts to more than one normal is on a hard edge and can be re-smoothed. You can convert to edges if you want to with polyListComponentConversion.

You have a couple of options. You can use Select > Select Using Constraints to restrict the current (or future) selection to hard edges.
The command equivalent of that is cmds.polySelectConstraint. This still requires a selection to operate on, so you’ll have to do something like this:

cmds.select(objects)
cmds.polySelectConstraint( m=2, t=0x8000, sm=1)
hardEdges = {}
for comp in cmds.ls(sl=True, fl=True):
    obj, idx = comp.split('.')
    hardEdges.setdefault(obj, []).append(idx)
    
cmds.polySelectConstraint( m=0)

Or use the API:

import maya.OpenMaya as om
sel = om.MSelectionList()
om.MGlobal.getActiveSelectionList(sel)
selIt = om.MItSelectionList(sel)
dagPath = om.MDagPath()
# Iterate over each selected object
while not selIt.isDone():
    selIt.getDagPath(dagPath)
    edgeIt = om.MItMeshEdge(dagPath)
    hardEdges = set()
    # Iterate over all the edges
    while not edgeIt.isDone():
        # Tests whether the edge is hard or smooth
        if not edgeIt.isSmooth():
            hardEdges.add(edgeIt.index())
        edgeIt.next()
    
    # Do stuff with hardEdge
    
    selIt.next()

Thanks capper :slight_smile:

If you go with polySelectConstraint, remember to reset afterwards or you’ll be suprised later.


import maya.mel as mel
import maya.cmds as cmds


class SelectionConstraintContext( object ):
    '''
    Provides automatic restoration of PolySelectConstraint status
    
    usage: 
    with SelectionConstraintContext() as sc:
        cmds.polySelectConstraint( q=True, sts=True )

    '''

    def __init__( self, enabled=True ):
        self.Enabled = enabled
        self.RestoreString = False;

    def __enter__ ( self ):
        if self.Enabled:

            rs = cmds.polySelectConstraint( q=True, sts=True ).split( ";" )
            rs[3] = "polySelectConstraint -type 16 -dis"

            self.RestoreString = ";".join( rs ) 
            self._was_entered = True
            cmds.polySelectConstraint( dis=True ) 
            return self
        else:
            self._was_entered = False

    def __exit__( self, type, value, tb ):
        if self._was_entered and self.RestoreString:
            mel.eval( self.RestoreString ) 
        if value:
            return False


And capper’s example:


def hard_edges(*obj):
    with SelectionConstraintContext() as dummy:
        cmds.select( *obj ) #@UndefinedVariable
        cmds.polySelectConstraint ( m=3, type=0x8000, smoothness=1, dis=True )
        return cmds.ls( sl=True ) or []

1 Like