PDA

View Full Version : nextChild



ernpchan
10-09-2014, 06:44 PM
nextChild(LWItemID parent, LWItemID prevChild) → LWItemID¶
Returns the next child item given a parent item and the previous child, or lwsdk.LWITEM_NULL if there are no more children.

How come this code doesn't work? I've tried every combination that seems logical to me for the nextChild args and I only get the first child of my selection.


for item in selected:
child = lwItem.firstChild(item)
while child:
print lwItem.name(child)
child = lwItem.nextChild(item,child)

clintonman
10-09-2014, 09:04 PM
Do you get an error message and can you share more of the code for a quick test?

Sensei
10-09-2014, 09:15 PM
Shouldn't you initialize child to null,
then call nextChild(item,child) in the loop?

LWItemID childid = LWITEM_NULL;
while( ( childid = iteminfo.NextChild( itemid, childid ) ) != LWITEM_NULL )
{
// do stuff
}

ernpchan
10-09-2014, 09:16 PM
I just get the first child. If I print the last child I get "None".

I don't have the code in front of me right now. I'm testing this on a null that has bones with one null parented to one of the bones.

Ideally I'm trying to replicate visitnodes from lscript. Trying to recursively iterate through a hierarchy.

Sensei
10-09-2014, 09:17 PM
Check my code..

It's different than yours.

firstChild() not used at all.

ernpchan
10-09-2014, 09:18 PM
I'll try that Sensei. I'm replicating the logic I see when iterating through channel groups and envelopes and I'm not initializing to a null then.

Sensei
10-09-2014, 09:24 PM
Initializing to null is requirement for C/C++ code. Not sure about scripting.
Because local variables are on stack, and contain old trash that used to be there.

Sensei
10-09-2014, 09:30 PM
You said bones?
This code will (probably) work with just real child items (that you set in Motion Options "parent item").

IIRC to get first bone there is needed objectinfo.boneSource()...

ernpchan
10-09-2014, 11:03 PM
With that code I can select the null or a bone in the object and I get the first child. Just not any other children.

Sensei
10-10-2014, 08:40 AM
What other children?
You mean children of children?
It must be recursive...

ernpchan
10-10-2014, 11:32 AM
Thanks, I figured it out. I had to check if the first child had any children and then recursively call a function that actually iterates.

Getting a good night sleep does wonders for clearing frustration.