Export Vertex Location and its Neighbours

Hello!
I’m building an fps in ogre and I use blender for everything model-related, including the level editor.
I love blender. I’ve been using it for years now, but I know nothing about python.
It didn’t bother me at all, blender is super handy and lots of fun to use without knowledge of any python.
So I didn’t think I’d need to learn any python, but I’ve hit a wall.

In my game I need ai node locations. I can code them manually into my game, but that would take ages and I’d end up making lots of mistakes, I’m sure.

I did a lot of googling but the best I found was this awesome script on this page.
import bpy
for item in bpy.data.objects:
print(item.name)
if item.type == ‘MESH’:
for vertex in item.data.vertices:
print(vertex.co)

That was pretty awesome, but I need a script that will output vertex x,y,z and all of its neighbours listed as vertex index.

for example, here is a list of vertexes with their index#, x ,y ,z , neighbour1 ,2 ,3 ,4 ,5 ,6

[TABLE=“width: 500”]

x
y
z
n1
n2
n3
n4
n5
n6

1
26.3
11.7
-36.2
2
0
0
0
0
0

2
26.0
-24.7
2.5
1
3
0
0
0
0

3
-0.9
-24.6
3.9
2
4
0
0
0
0

4
-15.1
-25.0
3.2
3
0
0
0
0
0

[/TABLE]

The game I’ve built has an AI node system where each node can have up to 6 neighbours. This is a restriction that I’ve coded in for different reasons, but hopefully that makes things easier here anyway :b
(neighbour 0 means no neighbour)

This is the only time I’ve needed to play with the blender console and python.
It would mean a lot for me and development if someone could write me up a script that could export a mesh’s vertex index #, x,y,z, and all of its 6 neighbours (in ini format would be heavenly)
example:
[1]
x=26.3
y=11.7
z=-36.2
n1=2
[2]
x=26.0
y=-24.7
z=2.5
n1=1
n2=3
[3]
etc…

if that’s too pompous of me, please excuse my manners, I’m just really anxious to move along and trying not to come off as a dink in the process. Please teach me how I can achieve this without having to step into learning a whole new language in the middle of my development schedule. :b

Thanks for reading!

picture of ai node mesh:


picture of game in action for those interested : )

I hope I get it right. I’m using edges to determine the connected verts.

And I hope that more experienced ones will correct and improve this
(consider that I did not use all tricks like list comprehension cause of the syntax supported by respective node)

Is this what you need?


Well, I’m doing that with a script, but for my convenience I’m using Animation Nodes,
that have a script node that updates live, so you can change at any point, and no need for all the start end register etc in the usual script file …+ a node to write this to a text (so you can save it) etc

If you wanna use it like this, then:

  • dld anim Nodes here, get the zip, (! but I think you should cut the “-master” from the name before installing)
  • get this blend with vetex connected script (as in the picture)
  • use it by saving the text file for each obj etc
    ! note that the text is permanently overwritten by the write text node, but is Not also saved
    so you probably wanna save it under different name each time for each obj !

    -you can reuse that script in other files by import/append node tree NodeTreeVertConnectedVerts from this file,
    but for script to work you also need to import /append the corresponding text file scriptVertConnectedVerts.txt
  • in the file there is a variant to get connected Polys, Edges And Verts. For those that care (I do, for other goals …) with corresponding script text file …

If the format or info is not quite ok I can change that …


If you prefer other ways, well, here’s the script part as in the file

#object vertex xyz and neighbour verts indexes
#o.g. for AN

vertsCoords = [v.co for v in Object.data.vertices ]
edgeIndices = [e.vertices for e in Object.data.edges ]

ConnectedVerts = []
for i, v in enumerate(vertsCoords):         #each vert

    vcoV = [i, v.x, v.y, v.z]                  #[xyz]
    for edge in edgeIndices:                #each edge
        vV = []
        if i in edge:                       #if edge contains this vert
            for ve in edge:                 #verts in this edge
                if ve != i:                 #the other vert in this edge (n)
                    vV.append(ve)           #[n1, n2, n6 ...]
        
        vcoV.extend(vV)                     #[x, y, z] > [x, y, z, n1, n2 etc...]

    ConnectedVerts.append(vcoV)             #all together [ [xyz12], [xyz36], [..]... ]
    
VertcoAndConnectedVerts = ConnectedVerts    #out socket

#as text lines ...
li = []
for el in ConnectedVerts: li.append(str(el))
asText = "Object: [" + str(Object.name) + "] Vert [i X Y Z n1 n2 n3 ...]
" + "
".join(li)

Text = asText

wow, that is seriously fantastic.
I wasn’t actually expecting such a talented hand to reach all the way down to assist my efforts so quickly! : D
I’m not able to try it yet, but I’ll definitely let you know when I do.

I’ll be honest, I grew a little impatient waiting for my post to get approved or however this forum’s system works, so I went off and tried to think of other ways to do this same thing.
I ended up using the regular .obj export and told the export settings to list edges, which actually tells me which vert is connected to others.

v 11.254761 26.464388 -3.933739
v -24.975323 26.066404 2.855399
v -24.727734 -0.998366 4.062148
v -24.858742 -15.306908 3.512662
l 1 2
l 2 3
l 3 4
l 4 5

my script in-game counts the vertex index # as it goes down the lines, and the indexes are listed in the edges there, so I told it to take both numbers and add the adjacent number to each of their neighbours.
I’m still unsure if it worked properly.
Still haven’t even been able to test if this method works at all. I plugged it into my game and it didn’t crash, so that’s step 1, but getting it to work properly is step 2 through 1000 :b

I was going to post my solution the second I knew my post got through- unfortunately I was too late and you were quick to draw.
But your script is still awesome, I’m absolutely going to use it, and it’ll make my debugging even easier!
With your script I can put my worries to rest, knowing all the nodes and their neighbours get parsed perfectly!

if i could specifically choose an output format, would it be too much to ask for it to be arranged like the following?

[1]//index of vertex 1
x=11.254761
y=26.464388
z=-3.933739
n1=3 //neighbour 1 is vert# 3
n2=5 //neighbour 2 is vert# 5
n3=0 //if a neighbour is listed as vert# "0" it means it has no more neighbours or this line can just not exist ie blank
n4=0
n5=0
n6=0

[2]
x=-24.975323
y=26.066404
z=2.855399
n1=1
n2=3
n3=5
n4=0
n5=0
n6=0

etc..

uhh ini file format if that makes it sound easier~
if not, your original post’s script still makes things 100x easier!

talk about o.g., o.g. :b
Thanks so much, man. This is truly an honour. I’d vigorously shake your hand if I could :cool:

if u want that inf kind of format, replace the lower part that formats text with:


#as text lines ...
li = []
for i, el in enumerate(ConnectedVerts): 
    li.append("
[" + str(el[0]) + "]" + 
    "
x=" + str(el[1]) + 
    "
y=" + str(el[2]) + 
    "
z=" + str(el[3]) )
    N=[]
    for j in range(4, len(el)):
        N.append("n" + str(j-4) + "=" + str(el[j])  )
    li.extend(N)
            
    asText = "
".join(li)

Text = asText

But you could parse this kind of list [ [i, x, y, z, n1, n2][1, 1.5, 1.5, 1.5, 2, 5, 6]… ]
and make a single file for all obj at hand like
[Cube]
v0=[ x, y, z, n1, n2][1.5, 1.5, 1.5, 2, 5, 6] [] …
v1=[ x, y, z, n1, n2][1.5, 1.5, 1.5, 2, 5, 6] [] …
[Cone]
v0 …

however, for that, https://drive.google.com/file/d/0BzzAoBwXO8dsZlpMREhhRk5lR1E/view?usp=sharing


This is awesome.
I think actually, by comparing the two samples of code you’ve conjured for me, I’m starting to understand how it works so I can try and make my own terms l tweaks.

Unfortunately I’m not able to play with it just yet ( some crazy personal stuff suddenly came up)
But I love your speedy replies, and you know exactly what I want here, I’m very thankful.

I will post as-soon as I have my free time back!
Thank you again, so much! : D

(it’s ok to double post in this context, right?)

Wonderful, simply wonderful.
I cannot thank you enough, this is fantastic. It works perfectly! This is everything I could have hoped to get when I signed up on these forums.
Thank you so much, o.g., you’re truly a hero : )
Now I don’t have to worry about python/blender and I can continue my project knowing these ai nodes are going to be parsed into my system perfectly.

Much love, Lime_Soda : )


Now I’ll lurk around on this site, but probably won’t see me in the python section again, haha.
I’ll just admire the art 'n stuff~

ps: is it possible for me to change the thread’s title prefix to [SOLVED]? or do i need to get an admin to do so?
Thanks!