2
$\begingroup$

enter image description herei'm trying to get output from a list, but i get a weird syntax error.

facecountt = []
for polygon in bpy.context.scene.objects['Torus'].data.polygons:    
    facecountt.append(polygon.index)
print(facecountt[2])

i don't get why it won't work. whatever i try - for as long as the list is filled with polygon.index i get this error, even when i tried to convert the values to integers. i can print polygon.index itself, but not within the list.

$\endgroup$
  • $\begingroup$ Please show the [weird syntax error] which is really your friend. Screen capture or cut and paste. $\endgroup$ – atomicbezierslinger Jan 19 '16 at 21:54
  • $\begingroup$ Try running it from a file. It's working fine for me... $\endgroup$ – someonewithpc Jan 19 '16 at 22:10
  • $\begingroup$ I am executing the Python below from a text window. It has a button [Run Script]. I have a mesh of the expected name in my window. Note I printed the whole list ...not the element 2 .... [2]. $\endgroup$ – atomicbezierslinger Jan 19 '16 at 22:13
  • $\begingroup$ it seems to work, when i run it from a file. problem solved, still a mystery, why the console can't handle it $\endgroup$ – e-proxy Jan 19 '16 at 22:19
  • $\begingroup$ use bpy.data.objects['torus'].data.polygons , not context.scene... $\endgroup$ – zeffii Jan 19 '16 at 22:43
4
$\begingroup$

This behavior is intended and is how Python knows you intend to terminate a loop. You have to end the for block, by pressing Enter on an empty line, before writing anything outside it.

Oppose:

>>> f = []
>>> for p in bpy.context.scene.objects['Torus'].data.polygons:
...     f.append(p.index)
... print(f) #Notice how this line starts with an ellipsis

With:

>>> f = []
>>> for p in bpy.context.scene.objects['Torus'].data.polygons:
...     f.append(p.index) #Hit enter _twice_ after this line
...
>>> print(f) #Notice how this line starts with three greater than symbols

Note that this isn't specific to Blender, the same behavior can be found in Python's default console - just by running "python" from the command line.

$\endgroup$
0
$\begingroup$

Execution from the Text Editor Window

import bpy ...... If you have a list to print you may find it useful to interate through the list.

import bpy
facecountt = []
for polygon in bpy.context.scene.objects['Torus'].data.polygons:    
    facecountt.append(polygon.index)

print(facecountt)
for p in facecountt:
    print(p)
$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.