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
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?
#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)
@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.
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âŚ