user8972552
user8972552

Reputation: 59

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 = cmds.ls(sl=True, 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]) )
    cmds.select(d[0])
    g = cmds.group(d, n = 'loc')
    cmds.select(g[0])
    cmds.createDisplayLayer( noRecurse=True, name='LocLayer' )

Upvotes: 0

Views: 683

Answers (2)

haggi krey
haggi krey

Reputation: 1978

Your code has some serious problems and cannot work:

d = cmds.xform(c, a=True, t=(pos[0], pos[1], pos[2]) )

The xform() command does not return anything, it only changes objects. So d is None what you try to use in the following code. There is no need to select anything, you can always use the objects name as agrument what you try to do here:

g = cmds.group(d, n = 'loc')

Of course with the None d parameter. And you are trying to create a group for every single locator and every group should have the same name what fails as well because Maya cannot have objects with the exact same name.

Upvotes: 1

DrWeeny
DrWeeny

Reputation: 2512

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 cmds.select 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 = cmds.ls(sl=True, fl=True)
x=1
for i in sel:
    pos = cmds.pointPosition(i)
    c = cmds.spaceLocator(n="loc{0}".format(x) , p=(0, 0, 0) )
    x+=1
    d = cmds.xform(c[0], a=True, t=(pos[0], pos[1], pos[2]) )
    g = cmds.group(c, 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)

Upvotes: 1

Related Questions