0
$\begingroup$

I have chain of 3 bones which are influenced by IK constraint. IK is added to bone named IK B#3: chain of bones
I'm trying to populate them into array in reverse order, i.e. IK B#1, IK B#2, IK B#3 in that order.
Code snipet:

bone_list = []

def populate_array(self):

    index = self.ik.chain_count - 1
    currentBone = activeBone
    for i in range(-1, self.ik.chain_count - 1):
        print("\nindex is :"+str(index)+"\n\tcurrent bone is: "+str(currentBone.name))
        self.bone_list.insert(index, currentBone)
        index -= 1
        currentBone = currentBone.parent

    #print out array
    for b in self.bone_list:
        print(b.name)

When i execute this code i'm getting this result:enter image description here

Why array is populated in that order, what is wrong with my code?

$\endgroup$
5
  • 2
    $\begingroup$ You should "append" to bone_list instead of "insert" $\endgroup$
    – lemon
    Apr 24, 2018 at 12:38
  • $\begingroup$ Uh, thanks! Write it as an answer so i could accept it. $\endgroup$
    – blablaalb
    Apr 24, 2018 at 12:45
  • $\begingroup$ and can you explain, what has been happening with my version.Why elements was inserted with that order? I don't get it. $\endgroup$
    – blablaalb
    Apr 24, 2018 at 12:50
  • $\begingroup$ I'm voting to close this question as off-topic because this is a general Python problem, not specific to blender, which will be more suited to one of the programming Stack Exchange sites. $\endgroup$ Apr 24, 2018 at 15:47
  • $\begingroup$ Since my question was related to blender scripting i decided to ask it here. Sorry if did something wrong. $\endgroup$
    – blablaalb
    Apr 25, 2018 at 11:32

1 Answer 1

1
$\begingroup$

The question is not a Blender question, but anyway...

So use append(currentBone) instead of insert(index, currentBone), to have [IKB3, IKB2, IKB1].

Or use insert(0, currentBone) instead of insert(index, currentBone) to have [IKB1, IKB2, IKB3].

But concerning "why" your usage of insert does not work:

Step1:

bone_list is empty and index is 2.

So bone_list.insert(2, IKB3) gives bone_list = [IKB3]. Explanation: Python tolerates that even if there is no element at position 2.

And index becomes 1.

Step2:

bone_list.insert(1, IKB2) gives bone_list = [IKB3, IKB2]. Explanation: still no element at position 1 (IKB3 is at position 0), so IKB2 is added at the end (which is the closest position to '1').

And index becomes 0.

Step3:

bone_list.insert(0, IKB1) gives bone_list = [IKB1, IKB3, IKB2]. Explanation: there is now an element at 0, so as expected Python add IKB1 before it.

$\endgroup$
0

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .