[Python]maintain lists after rename

Become a member of the CGSociety

Connect, Share, and Learn with our Large Growing CG Art Community. It's Free!

THREAD CLOSED
 
Thread Tools Search this Thread Display Modes
  05 May 2013
[Python]maintain lists after rename

I'm having a bit trouble understanding why I can no longer refer to my lists after renaming:

    #get chain from ui
    jgStartChain = cmds.textField(self.jgChainParentTF, q=1, text=1)

    #gather children
    jgFkChain = cmds.listRelatives(jgStartChain, ad=1, type='joint')

    #add jgStartChain to the list
    jgFkChain.insert(0, jgStartChain)

    #rename chain
    for i in range(len(jgFkChain)):
        jgFkChain[i] = cmds.rename(jgFkChain[i], jgFkChain[i]+"_FK")

    #duplicate the chain and rename
    jgIkChain = cmds.duplicate(jgFkChain, n="_IK", rc=1)

    #remove any constraints left in IK chain
    cmds.delete(jgIkChain, cn=1)

    for i in range(len(jgIkChain)):
        jgIkChain[i] = cmds.rename(jgIkChain[i], jgFkChain[i][0:len(jgFkChain[i])-2] + "IK")


In both loops I'm trying to replace the item in the list while renaming it. This code works, but it throws the error:


# Error: IndexError: list index out of range # 


also, when I try to print my lists I get the same error.

Thanks for any help.
__________________

gibby3d.com
 
  05 May 2013
I ran your code snippet on a simple 5 joint skeleton and didn't encounter any errors (only change was hardcoding the first line with the root joint name). It created a second skeleton and named everything correctly. Is it possible that the error is being cause somewhere else?
 
  05 May 2013
Hrm, the above code is all I have written for a function thus far, the only thing passed to this function is my ui instance. I do have several variables declared in the main namespace of the script this function is in. But none of them are being used in this function. I'm kind of stumped.
__________________

gibby3d.com
 
  05 May 2013
It has something to do with passing a chain with constraints on each joint. When I run the code on a joint chain with no constraints I do not get the error.
__________________

gibby3d.com
 
  05 May 2013
A quick fix to my error was to not use type='joint' in my listRelatives command

    #get chain from ui
    jgStartChain = cmds.textField(self.jgChainParentTF, q=1, text=1)

    #gather children
    jgFkChain = cmds.listRelatives(jgStartChain, ad=1)
    #add jgStartChain to the list
    jgFkChain.insert(0, jgStartChain)
    #rename chain
    for i in range(len(jgFkChain)):
        jgFkChain[i] = cmds.rename(jgFkChain[i], jgFkChain[i]+"_FK")

    #duplicate the chain and rename
    jgIkChain = cmds.duplicate(jgFkChain, n="_IK", rc=1)

    for i in range(len(jgIkChain)):
        jgIkChain[i] = cmds.rename(jgIkChain[i], jgFkChain[i][0:len(jgFkChain[i])-2] + "IK")

    cmds.delete(jgIkChain, cn=1)


But the result is still off, my jgIkChain list still contains the constraints as items

I tried making a new list with something like this:

jgNewIkChain=[]

    for i in jgIkChain:
        if i.find("Constraint"):
           print("no")
        else:
             jgNewIkChain.append(i)


but that isn't working, it finds "Constraint" in every item... so nothing is appended to the new list
__________________

gibby3d.com
 
  05 May 2013
Perhaps I'm not using the right string function?

for i, each in enumerate(['some', 'array', 'blah_parentConstraint']):
    if each.find("Constraint"):
       print i ,each



0 some
1 array
2 blah_parentConstraint

__________________

gibby3d.com
 
  05 May 2013
Oh,


noC = "bob"
noC.find("a")
# Result: -1 # 
noC.find("b")
# Result: 0 # 
__________________

gibby3d.com
 
  05 May 2013
Ok, I get it.


 for i, each in enumerate(jgIkChain):
        itemNum = each.find("Constraint")
        if itemNum == -1:

             #jgNewIkChain.append(each)

             #will populate list in correct order
             jgNewIkChain.insert(1,each)
__________________

gibby3d.com

Last edited by jgibz : 05 May 2013 at 08:55 PM.
 
  05 May 2013
I think you could take a more direct approach.
jgStartChain = 'joint1'
 
 #gather children
 jgFkChain = [j for j in reversed(cmds.listRelatives(jgStartChain, ad=1, type='joint'))]
 
 #add jgStartChain to the list
 jgFkChain.insert(0, jgStartChain)
 
 #rename chain
 jgFkChain = [j+"_FK" for j in jgFkChain]
 
 #duplicate the chain and rename
 jgIkChain = []
 for i, j in enumerate(jgFkChain):
 	jgIkChain.extend(cmds.duplicate(j, n=j.replace("_FK","_IK"), po=True))
 	if i > 0:
 		cmds.parent(jgIkChain[i], jgIkChain[i-1])
 


Quick explanation...
listRelatives gives you a list in the wrong order, so I'm reversing it before inserting the root joint. The rename can be done neatly using list comprehension. Rather than duplicate everything (messy), just iterate through your joints and do them one at a time with the parentOnly flag, and reparent them to the one that came before.

By the way, if you have not used "list comprehension" before, I highly recommend looking it up and getting to know how powerful it can be.

David
__________________
http://www.djx.com.au

Last edited by djx : 05 May 2013 at 06:42 AM.
 
  05 May 2013
Originally Posted by djx: By the way, if you have not used "list comprehension" before, I highly recommend looking it up and getting to know how powerful it can be.
David


Thanks for the tip, very helpful
__________________

gibby3d.com
 
  05 May 2013
Okay, I volunteer to ask the question: Why is list comprehension powerful? What's the difference between it and the map or lambda commands? Isn't it just a shortcut to the clearer (c++ view) construct:

list = []
for j in something:
	list.append(j)
 
  05 May 2013
I guess I should point out that the below only renames your list items, not the objects they are used to refer to.

Originally Posted by djx:

 #rename chain
 jgFkChain = [j+"_FK" for j in jgFkChain]
 




Originally Posted by zoharl: Okay, I volunteer to ask the question: Why is list comprehension powerful



#renames chain and replaces list items
myList = cmds.listRelatives('someNode', ad =1)
myNewList = [cmds.rename(i, i +"_FK") for i in myList]
#or
myOtherNewList = [cmds.rename(i,i.replace("_FK","_IK")) for i in myNewList]


It seems pretty useful, that's for sure.
__________________

gibby3d.com
 
  05 May 2013
You could have done it without [] (see my example), and I might have understood what you tried to do.
Sorry to ruin the party, I don't find code obfuscation cool. Also list comprehension is just a language sweetener and no more (albeit a nice one, but again I don't see any usefulness).

Another practical consideration is debugging. Let's say that I didn't want just to replace the suffix, but do a more elaborated string operation using regex, which is bound to fail on the first time I code it. In my example below, I can easily debug, e.g. add print after each step, print the old list, and print each new item and operation. What about your example?

myList = cmds.listRelatives('someNode', ad =1)
  myNewList = []
  for i in myList:
	newName = cmds.rename(i, i +"_FK")
  	myNewList.append(newName)

Last edited by zoharl : 05 May 2013 at 06:15 AM.
 
  05 May 2013
@jgibz: Yes, you are correct about the rename. My bad. Seems like you figured it out though.

@zoharl: List comprehension kind of becomes second nature once you use it for a while. I find it no harder to debug. Easier than map and lambda that's for sure. I'm all for explicit and zen and all that, but I also find the list comprehension fits in well when used appropriately.

David
__________________
http://www.djx.com.au
 
  05 May 2013
Thread automatically closed

This thread has been automatically closed as it remained inactive for 12 months. If you wish to continue the discussion, please create a new thread in the appropriate forum.
__________________
CGTalk Policy/Legalities
Note that as CGTalk Members, you agree to the terms and conditions of using this website.
 
Thread Closed share thread



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
CGSociety
Society of Digital Artists
www.cgsociety.org

Powered by vBulletin
Copyright 2000 - 2006,
Jelsoft Enterprises Ltd.
Minimize Ads
Forum Jump
Miscellaneous

All times are GMT. The time now is 06:19 PM.


Powered by vBulletin
Copyright ©2000 - 2017, Jelsoft Enterprises Ltd.