Search code examples
pythonpython-2.7listvpython

Compare items of a list sequentially with another one then use one by one in VPython application


I have grid of objects in Vpython created by this code:

iX = [(x - pointW // 2) * sclFact for x in range(pointW)]
iY = [(x - pointH // 2) * sclFact for x in range(pointH)]
iYr = iY[::-1]
xy = list(itertools.product(iX,iYr,))
ixyz = np.array(list(itertools.product(iX,iYr,[-0.0])))
for element in ixyz:
        cube = box(pos = element,
               size=( .1, .1, .1 ),)

ixyz list print will look like this:

[[-0.5  0.  -0. ]
 [-0.5 -0.5 -0. ]
 [ 0.   0.  -0. ]
 [ 0.  -0.5 -0. ]
 [ 0.5  0.  -0. ]
 [ 0.5 -0.5 -0. ]]

I have other list that the z value changes sometime depand on certain input and its always updated, it wll look like this

[[-0.5        0.        -0.       ]
 [-0.5       -0.5       -0.       ]
 [ 0.         0.        -0.       ]
 [ 0.        -0.5       -0.       ]
 [ 0.5        0.        -2.3570226]
 [ 0.5       -0.5       -0.       ]]

I want to move the objects based on the new list, i tried different veriation but it did not work, it always look at the last item in the second list

while True:
  .... some code here (the one getting the new list)
  ...
  ...
  # then I added this:
      for obj in scene.objects: 
        if isinstance(obj, box):
            for i in xyz: # xyz is the new list
                if obj.pos != i:
                   obj.pos = i

this variation will make all the boxes be one box and move based on the last position in the list

what I am doing wrong or is there another way to do that ? or should I change the whole process of creating the objects and move them? I am really new in VPython and python itself.

Edit I fixed both lists to be better presented like this

[(-0.5,0.0,-0.0),(-0.5,-0.5,-0.0),...(0.5,-0.5,-0.0)]

Solution

  • You are repeatedly setting the position to each element in the updated positions list:

    box.pos = 1
    box.pos = 2
    box.pos = 3 
    

    You need to set the position one time; so compute an index:

    i = 0
    for obj....
        if isinstance  ...
             obj.pos = xyz [i]
             i += 1