-2

How is this translatable to the classical way of writing if and for loops? This is the first time I have encountered this kind of writing.

vertices = [v for v in obj.data.vertices if (ymin <= v.co[2] <= ymax)]
khelwood
  • 55,782
  • 14
  • 81
  • 108

4 Answers4

1

Take a look at list comprehensions: https://docs.python.org/3/tutorial/datastructures.html#list-comprehensions

but the code is basically a for that appends to a list with a condition

vertices = []
for v in obj.data.vertices:
    if (ymin <= v.co[2] <= ymax):
        vertices.append(v)
th30z
  • 1,902
  • 1
  • 19
  • 28
1

This is called "list comprehension". The code you're looking for is:

vertices = []
for v in obj.data.vertices:
    if ymin <= v.co[2] <= ymax:
        vertices.append(v)
GuyPago
  • 173
  • 1
  • 2
  • 14
0

This techique is called list comprehension, you can google about it. The translation in your example:

vertices = []
for v in range(len(obj.data.vertices)):
     if (ymin <= v.co[2] <= ymax):
            vertices.append(v)

Consider this a pseudocode , since you haven't provided us in detailed your problem and there is no knowledge about the type of obj.data.vertices. So this code could have syntax errors. Anyway the logic you were looking for is here.

George
  • 328
  • 1
  • 9
0
vertices = []
for v in obj.data.vertices:
    if ymin <= v.co[2] <= ymax:
        vertices += [v]
Rufat
  • 536
  • 1
  • 8
  • 25