Problems parenting transforms to lodGroups

Hello everyone :slight_smile:

I am having the hardest time parenting meshes to lodGroups in maya via python. This is what I have learned:

-I can create the group pretty easily:
lodGroupNode = cmds.createNode(‘lodGroup’,n=‘lodGroupName’)

-I can parent transforms to the lod group if use a simple string to ID the transform and the lodGroup:
cmds.parent(‘lod1’, ‘lodGroup1’)

but if I use variables:
cmds.parent(selection[0], selection[1])

Maya returns this error:

TypeError: Object lod1 is invalid

One of my co-workers suggested that I parent the lods during the creation process (while selecting a camera) This works, but I’m unsure how-to add new LODs via script after the initial creation :confused:

can you be a little more specific about how you got here? how did you get ‘selection’? I tried a simple test and it worked the way you’d expect it to, so maybe your selection order or selection code or whatever is broken, but there’s not a ton of context here, so i’m not really sure what to tell you. Did the name ‘lod1’ change at some point? Did ‘lod1’ get moved out of the scene?

This isn’t my actual scenario, but this is my selection command (ls)

import pymel.core as pm
import maya.cmds as cmds

def lodParentingTest():
#create objects and lodGroup
lodGroupNode = cmds.createNode(‘lodGroup’,n=‘lodGroup1’)
pm.polyCube(n=‘lod1’)
pm.polyCube(n=‘lod2’)

#make selections and capture as an array using list command
pm.select('lod1', r=True)
pm.select('lod2', add=True)
pm.select('lodGroup1', add=True)
selection2 = pm.ls(sl=True, l=True)

#try parent command
cmds.parent(selection2[0], selection2[2])

Works fine if you replace cmds.parent with pm.parent, or replace pm.ls with cmds.ls.

In your code you use pm.ls, which gives you a list contains PyMEL nodes (you can spot that they start with “nt.”). maya.cmds doesn’t work on PyMEL nodes.
[nt.Transform(u’lod1’), nt.Transform(u’lod2’), nt.LodGroup(u’lodGroup1’)]

Avoid jumping between the two if you can and your life will be easier, or at least remember to get the correct type of nodes for the functions you want to run later in the script.

import pymel.core as pm
import maya.cmds as cmds

#create objects and lodGroup
lodGroupNode = cmds.createNode('lodGroup',n='lodGroup1')
pm.polyCube(n='lod1')
pm.polyCube(n='lod2')

#make selections and capture as an array using list command
pm.select('lod1', r=True)
pm.select('lod2', add=True)
pm.select('lodGroup1', add=True)
selection2 = pm.ls(sl=True, l=True)

#try parent command
[B]pm[/B].parent(selection2[0], selection2[2]) 

Yes be careful as maya cmds works with strings and pymel with pynodes. If you must mix them within functions, just be careful. Never, ever mix them within an API or for any public-facing method (ie, if most of your module takes in pynodes, which it should, have absolutely no functions that take in names for maya cmds).

Hmm…is there a reason for making a selection vs just capturing the nodes on creation?


import pymel.core as pm

def lodParentingTest():
    """
    """

    lodGroupNode = pm.createNode(pm.nt.LodGroup, name='lodGroup1')
    for i in range(2):
        xf, sh = pm.polyCube(name='lod{0}'.format(i))
        xf.setParent(lodGroupNode)
    
    pm.select(lodGroupNode)

Seems like this does something similar…

Thx for the help everyone :slight_smile:

@Seth: My actual scenario has the artists running this script just before they export and there is no creation-time. By the time this is run, the artists have used the Maya UI to create the object and name it whatever they want.

In my quick-and-dirty example I wanted to create a function that you forum-goers could just ‘run’ to see my selection scenario. If this was an actual script I’d use in my pipeline I would capture the selections as you suggest.

-R

In that case it’s even simpler…



def groupStuffUnderLod(selection):
    """
    Select a bunch of stuff and an lod group node or whatever last
    """
    lodGroupNode = selection.pop()
    for i in selection:
        selection.setParent(lodGroupNode)


You may want to do more robust checking, that sorta thing, but you get the idea…