faces verts access ?

for i in faces:
n1 = i[0]
print (‘isinstance =’, isinstance(i[0], int ))
print (‘i[0] =’,i[0],’ n1 =’,n1,’ n1 +2 =’, n1+2)
print (‘ver loc =’, verts_loc[3])

print (‘i[0] =’,i[0],’ ver loc =’, verts_loc[n1] )

I get an int from ~ i[0] which happen to be = 3

now if can access verts loc 3 with
print (‘ver loc =’, verts_loc[3])

but if I use the I[0] then it is refusing to get the verts loc
print (‘i[0] =’,i[0],’ ver loc =’, verts_loc[n1] )

why this is not accepting the int as an index ?
anyway to make it work ?

thanks
happy bl

No clue what you’re trying there, but it looks horrible. And you didn’t tell what “faces” contains.

In case of a bmesh object, you would just do:

for f in bm.faces:
    for v in f.verts:
        print(v.index)

Doing something like this in Python:

for i in range(length(bm.faces)):
    bm.faces<i>

is an anti-pattern, don’t do it. There no reason to use an integer to access elements by index. Almost everything is iterable (see 1st example). No need to introduce a counter variable if you can have a reference to the actual object.

Also note, that for [I]f in bm.faces etc. will work without prior call to ensure_lookup_table(), whereas the counter variable approach does require it, because you do bm.faces[…]

still not working



import bpy







verts = [( -12.774209, -12.774209, -12.774209),
   ( -12.774209, +12.774209, -12.774209),
   ( +0.000000, -17.121244, -17.121244),
   ( +0.000000, +17.121244, -17.121244),
   ( +12.774209, -12.774209, -12.774209),
   ( +12.774209, +12.774209, -12.774209),
   ( -17.121244, -17.121244, +0.000000),
   ( -17.121244, +17.121244, +0.000000),
   ( -0.000000, -24.685204, -0.000000),
   ( -0.000000, +24.685204, -0.000000),
   ( +17.121244, -17.121244, +0.000000),
   ( +17.121244, +17.121244, +0.000000),
   ( -12.774209, -12.774209, +12.774209),
   ( -12.774209, +12.774209, +12.774209),
   ( -0.000000, -17.121244, +17.121244),
   ( +0.000000, +17.121244, +17.121244),
   ( +12.774209, -12.774209, +12.774209),
   ( +12.774209, +12.774209, +12.774209),
   ( -17.121244, +0.000000, -17.121244),
   ( +17.121244, +0.000000, -17.121244),
   ( -24.685204, -0.000000, -0.000000),
   ( +24.685204, -0.000000, -0.000000),
   ( -17.121244, +0.000000, +17.121244),
   ( +17.121244, +0.000000, +17.121244),
   ( +0.000000, -0.000000, -24.685204),
   ( -0.000000, -0.000000, +24.685204),
   ]


faces = [(3, 1, 19, 25),
   (1, 3, 9, 7),
   (1, 7, 21, 19),
   (7, 9, 15, 13),
   (7, 13, 23, 21),
   (13, 15, 26, 23),
   (25, 19, 2, 4),
   (19, 21, 8, 2),
   (21, 23, 14, 8),
   (23, 26, 16, 14),
   (8, 10, 4, 2),
   (14, 16, 10, 8),
   (5, 3, 25, 20),
   (3, 5, 11, 9),
   (9, 11, 17, 15),
   (15, 17, 24, 26),
   (20, 25, 4, 6),
   (26, 24, 18, 16),
   (10, 12, 6, 4),
   (16, 18, 12, 10),
   (11, 5, 20, 22),
   (17, 11, 22, 24),
   (22, 20, 6, 12),
   (24, 22, 12, 18),
   ]


   
   
for i in faces:


 n1 = i[0]
 print ('isinstance =', isinstance(i[0], int ))
 print ('i[0] =',i[0],' n1 =',n1,' n1 +2 =', n1+2)
 print ('ver loc =', verts[3])
 print ('i[0] =',i[0],' ver  =', verts[n1] )




why is the last line not working
it has the proper index for the vert ?

thanks
happy bl

Closing in on 20K Ricky.

Remember to index from 0.
26 verts from v[0] to v[25]
You have no vert 0 in your faces, vert 26 is causing the error.

If you are getting data that indexes from 1

n1 = i[0] - 1

import bpy
import bmesh

verts = (
    ( -12.774209, -12.774209, -12.774209),
    ( -12.774209, +12.774209, -12.774209),
    ( +0.000000, -17.121244, -17.121244),
    ( +0.000000, +17.121244, -17.121244),
    ( +12.774209, -12.774209, -12.774209),
    ( +12.774209, +12.774209, -12.774209),
    ( -17.121244, -17.121244, +0.000000),
    ( -17.121244, +17.121244, +0.000000),
    ( -0.000000, -24.685204, -0.000000),
    ( -0.000000, +24.685204, -0.000000),
    ( +17.121244, -17.121244, +0.000000),
    ( +17.121244, +17.121244, +0.000000),
    ( -12.774209, -12.774209, +12.774209),
    ( -12.774209, +12.774209, +12.774209),
    ( -0.000000, -17.121244, +17.121244),
    ( +0.000000, +17.121244, +17.121244),
    ( +12.774209, -12.774209, +12.774209),
    ( +12.774209, +12.774209, +12.774209),
    ( -17.121244, +0.000000, -17.121244),
    ( +17.121244, +0.000000, -17.121244),
    ( -24.685204, -0.000000, -0.000000),
    ( +24.685204, -0.000000, -0.000000),
    ( -17.121244, +0.000000, +17.121244),
    ( +17.121244, +0.000000, +17.121244),
    ( +0.000000, -0.000000, -24.685204),
    ( -0.000000, -0.000000, +24.685204),
)

faces = (
    (3, 1, 19, 25),
    (1, 3, 9, 7),
    (1, 7, 21, 19),
    (7, 9, 15, 13),
    (7, 13, 23, 21),
    (13, 15, 26, 23),
    (25, 19, 2, 4),
    (19, 21, 8, 2),
    (21, 23, 14, 8),
    (23, 26, 16, 14),
    (8, 10, 4, 2),
    (14, 16, 10, 8),
    (5, 3, 25, 20),
    (3, 5, 11, 9),
    (9, 11, 17, 15),
    (15, 17, 24, 26),
    (20, 25, 4, 6),
    (26, 24, 18, 16),
    (10, 12, 6, 4),
    (16, 18, 12, 10),
    (11, 5, 20, 22),
    (17, 11, 22, 24),
    (22, 20, 6, 12),
    (24, 22, 12, 18),
)

bm = bmesh.new()

for v in verts:
    bm.verts.new(v)
bm.verts.ensure_lookup_table()
    
for f in faces:
    bm.faces.new(bm.verts[vi-1] for vi in f)
    
me = bpy.data.meshes.new("")
bm.to_mesh(me)

ob = bpy.data.objects.new("", me)
scene = bpy.context.scene
scene.objects.link(ob)
scene.update()

But standard API is probably more efficient:

import bpy

def accumulate(iterable):
    it = iter(iterable)
    total = 0
    for element in it:
        yield total
        total += element

verts = (
    ( -12.774209, -12.774209, -12.774209),
    ( -12.774209, +12.774209, -12.774209),
    ( +0.000000, -17.121244, -17.121244),
    ( +0.000000, +17.121244, -17.121244),
    ( +12.774209, -12.774209, -12.774209),
    ( +12.774209, +12.774209, -12.774209),
    ( -17.121244, -17.121244, +0.000000),
    ( -17.121244, +17.121244, +0.000000),
    ( -0.000000, -24.685204, -0.000000),
    ( -0.000000, +24.685204, -0.000000),
    ( +17.121244, -17.121244, +0.000000),
    ( +17.121244, +17.121244, +0.000000),
    ( -12.774209, -12.774209, +12.774209),
    ( -12.774209, +12.774209, +12.774209),
    ( -0.000000, -17.121244, +17.121244),
    ( +0.000000, +17.121244, +17.121244),
    ( +12.774209, -12.774209, +12.774209),
    ( +12.774209, +12.774209, +12.774209),
    ( -17.121244, +0.000000, -17.121244),
    ( +17.121244, +0.000000, -17.121244),
    ( -24.685204, -0.000000, -0.000000),
    ( +24.685204, -0.000000, -0.000000),
    ( -17.121244, +0.000000, +17.121244),
    ( +17.121244, +0.000000, +17.121244),
    ( +0.000000, -0.000000, -24.685204),
    ( -0.000000, -0.000000, +24.685204),
)

faces = (
    (3, 1, 19, 25),
    (1, 3, 9, 7),
    (1, 7, 21, 19),
    (7, 9, 15, 13),
    (7, 13, 23, 21),
    (13, 15, 26, 23),
    (25, 19, 2, 4),
    (19, 21, 8, 2),
    (21, 23, 14, 8),
    (23, 26, 16, 14),
    (8, 10, 4, 2),
    (14, 16, 10, 8),
    (5, 3, 25, 20),
    (3, 5, 11, 9),
    (9, 11, 17, 15),
    (15, 17, 24, 26),
    (20, 25, 4, 6),
    (26, 24, 18, 16),
    (10, 12, 6, 4),
    (16, 18, 12, 10),
    (11, 5, 20, 22),
    (17, 11, 22, 24),
    (22, 20, 6, 12),
    (24, 22, 12, 18),
)


me = bpy.data.meshes.new("")
me.vertices.add(len(verts))
me.vertices.foreach_set("co", [vv for v in verts for vv in v])

me.loops.add(sum(len(f) for f in faces))
me.loops.foreach_set("vertex_index", [ff-1 for f in faces for ff in f])

me.polygons.add(len(faces))
me.polygons.foreach_set("loop_start", list(accumulate(len(f) for f in faces)))
me.polygons.foreach_set("loop_total", [len(f) for f in faces])

#me.validate(True)
me.update(calc_edges=True)

ob = bpy.data.objects.new("", me)
scene = bpy.context.scene
scene.objects.link(ob)
scene.update()

Or if verts and faces is the only thing you wanna create (no uv mapping etc.), just do:

import bpy

verts = (
    ( -12.774209, -12.774209, -12.774209),
    ( -12.774209, +12.774209, -12.774209),
    ( +0.000000, -17.121244, -17.121244),
    ( +0.000000, +17.121244, -17.121244),
    ( +12.774209, -12.774209, -12.774209),
    ( +12.774209, +12.774209, -12.774209),
    ( -17.121244, -17.121244, +0.000000),
    ( -17.121244, +17.121244, +0.000000),
    ( -0.000000, -24.685204, -0.000000),
    ( -0.000000, +24.685204, -0.000000),
    ( +17.121244, -17.121244, +0.000000),
    ( +17.121244, +17.121244, +0.000000),
    ( -12.774209, -12.774209, +12.774209),
    ( -12.774209, +12.774209, +12.774209),
    ( -0.000000, -17.121244, +17.121244),
    ( +0.000000, +17.121244, +17.121244),
    ( +12.774209, -12.774209, +12.774209),
    ( +12.774209, +12.774209, +12.774209),
    ( -17.121244, +0.000000, -17.121244),
    ( +17.121244, +0.000000, -17.121244),
    ( -24.685204, -0.000000, -0.000000),
    ( +24.685204, -0.000000, -0.000000),
    ( -17.121244, +0.000000, +17.121244),
    ( +17.121244, +0.000000, +17.121244),
    ( +0.000000, -0.000000, -24.685204),
    ( -0.000000, -0.000000, +24.685204),
)

faces = (
    (3, 1, 19, 25),
    (1, 3, 9, 7),
    (1, 7, 21, 19),
    (7, 9, 15, 13),
    (7, 13, 23, 21),
    (13, 15, 26, 23),
    (25, 19, 2, 4),
    (19, 21, 8, 2),
    (21, 23, 14, 8),
    (23, 26, 16, 14),
    (8, 10, 4, 2),
    (14, 16, 10, 8),
    (5, 3, 25, 20),
    (3, 5, 11, 9),
    (9, 11, 17, 15),
    (15, 17, 24, 26),
    (20, 25, 4, 6),
    (26, 24, 18, 16),
    (10, 12, 6, 4),
    (16, 18, 12, 10),
    (11, 5, 20, 22),
    (17, 11, 22, 24),
    (22, 20, 6, 12),
    (24, 22, 12, 18),
)


me = bpy.data.meshes.new("")
me.from_pydata(verts, [], 
[list(map(lambda x: x-1, f)) for f in faces])
#me.validate(True)
me.update(calc_edges=True)

ob = bpy.data.objects.new("", me)
scene = bpy.context.scene
scene.objects.link(ob)
scene.update()

can you elaborate on this
You have no vert 0 in your faces, vert 26 is causing the error.

I was not trying to either !
I was using I[0] = 3 which was the firrt vert on first face
which is the 4 verts in list of verts

so why is it error while index = 26
I mean almost like it is being calculated in advance but does not give why the error

is hard to guess and debug it is because of this error

I know the index start at 0
I’m trying to do some Macro in BASIC now
and have to test equivalent code in python
what a nightmare this is !

thanks
happy bl

for Bmesh here need to do
bm.verts.ensure_lookup_table()
only after adding verts

I thought this was needed after each added vert!

and no need to do it for faces or edges!

is there other doc explaining more about this ?

thanks
happy bl

problem in summary was this

print (‘ver loc =’, verts[3])
print (‘i[0] =’,i[0],’ ver =’, verts[n1] )

these 2 lines are equivalent as I know of

but as soon as I tried to do the last line it crashes but not the first line

what is the difference here to make it crahes?

thanks
happy bl

so why is it error while index = 26

Because “verts” contains 26 tuples, but their indexes are 0…25, not 1…26. Indexes in Python are zero-based, the first element has index 0, the last length-1. “faces” however references vertices as if they were one-based, while they are actually zero-based. The minimum vertex index in “faces” is 1, the highest 26. They should had been ranging from 0 to 25. I correct that in my example codes. But if you created the data of “verts” and “faces”, then this correction becomes redundant because you can easily fix this in your export code.

I thought this was needed after each added vert!
and no need to do it for faces or edges!

You are still no getting it, even after several threads asking about ensure_lookup_table(), dude…
Whenever you add or remove vertices, the lookup table becomes invalid. But that doesn’t mean you need to call ensure_lookup_table() right after adding or removing a single vertex. That would be in fact the most stupid thing you could do performance-wise. It is supposed to be called once after adding or removing verts BEFORE you access any vertex via index like so: bm.verts[…]
If you don’t access any vert like that, no call is needed AT ALL. Iterations like for v in bm.verts don’t need ensure_lookup_table(), bm.verts[0] does. That’s why bm.faces.ensure_lookup_table() isn’t required, there’s no access by index to bm.faces

but as soon as I tried to do the last line it crashes but not the first line

And again, the faces sequence contains “invalid” vertex indices. They should had been -1 to range from 0 to 25:

faces = (
    (2, 0, 18, 24),
    (0, 2, 8, 6),
    (0, 6, 20, 18),
    (6, 8, 14, 12),
    (6, 12, 22, 20),
    (12, 14, 25, 22),
    (24, 18, 1, 3),
    (18, 20, 7, 1),
    (20, 22, 13, 7),
    (22, 25, 15, 13),
    (7, 9, 3, 1),
    (13, 15, 9, 7),
    (4, 2, 24, 19),
    (2, 4, 10, 8),
    (8, 10, 16, 14),
    (14, 16, 23, 25),
    (19, 24, 3, 5),
    (25, 23, 17, 15),
    (9, 11, 5, 3),
    (15, 17, 11, 9),
    (10, 4, 19, 21),
    (16, 10, 21, 23),
    (21, 19, 5, 11),
    (23, 21, 11, 17),
)

With these correct vertex indices, the code become as simple as:

me = bpy.data.meshes.new("")
me.from_pydata(verts, [], faces)
me.update(calc_edges=True)

ob = bpy.data.objects.new("", me)
scene = bpy.context.scene
scene.objects.link(ob)
scene.update()

I did not build this verts face list
possible there was some errors made !

will talk with original owner for this

For Bmesh I’v seen a few examples with completely different way to add verts edge and faces
and I’m still wondering when it is necessary to use the table lookup thing

by the way your right I did ask questions in a thread but never really got a clear logical answer !
at least not clear for my understanding sorry I’m still not a programmer and sometimes I get loss with programmer’s logic!

anyway will do some other testing for my primitive building and if possible to modify and simplify the loop

thanks
happy bl