How to make a new group and display layer with selected locators? Maya Python

I'm trying to creat locators on selected vertices and make them a group. And then creat a new display layer with that group. But this doesn't work after form and I don't know why. Can I get some help?

import maya.cmds as cmds

sel =, fl=True)
for i in range(len(sel)):
    pos = cmds.pointPosition(sel[i])
    c = cmds.spaceLocator(n="loc01" , p=(0, 0, 0) )
    d = cmds.xform(c, a=True, t=(pos[0], pos[1], pos[2]) )[0])
    g =, n = 'loc')[0])
    cmds.createDisplayLayer( noRecurse=True, name='LocLayer' )


  • I agree with haggi krey. Furthermore, you should create some naming convention or use long name because it will keep creating loc01 every loop.

    You should avoid and just fill commands with arguments.

    Also if you are a beginner in scripting, you may comment every line describing what you are doing as : # Group the locator 'c' ; # set the locator position ...etc

    import maya.cmds as cmds
    sel =, fl=True)
    for i in sel:
        pos = cmds.pointPosition(i)
        c = cmds.spaceLocator(n="loc{0}".format(x) , p=(0, 0, 0) )
        d = cmds.xform(c[0], a=True, t=(pos[0], pos[1], pos[2]) )
        g =, n = 'loc_grp_{0}'.format(c[0][-2:]))
        dspL = cmds.createDisplayLayer( noRecurse=True, name='LocLayer_{0}'.format(c[0]))
        cmds.editDisplayLayerMembers(dspL, g, noRecurse=True)