3
$\begingroup$

Given a plane like this:

enter image description here

I want to sort the vertices from left to right, top to bottom (for use in the BGE).

However, vertex.getXYZ() and vertex.XYZ give incorrect values for several vertices. I've checked the plane for doubles, yet a number of verts return the same locations.

from bge import logic

own = logic.getCurrentController().owner
vertlist = []

def sort_verts(k):

    if k['XYZ'][0] < 0:
        return k['XYZ'][1]+.0001
    else:
       return k['XYZ'][1]


for mesh in own.meshes:
    for m_index in range(len(mesh.materials)):
        for v_index in range(mesh.getVertexArrayLength(m_index)):
            vertex = mesh.getVertex(m_index, v_index)

            print(v_index, mesh.getVertex(m_index, v_index).getXYZ())


            vertlist.append({"XYZ": vertex.XYZ.copy(), "index": v_index})

vertlist.sort(key=sort_verts)     

#for v in vertlist:
#    print(v['index'], v['XYZ'])

I've commented out the sorting part, as it doesn't seem to be causing the problem.

Running the above in the BGE outputs the following:
I've commented a few of the duplicates, there might be more.

Blender Game Engine Started
0 <Vector (-1.0000, 1.0000, 0.0000)>
1 <Vector (-1.0000, 0.9310, 0.0000)>
2 <Vector (1.0000, 0.9310, 0.0000)>
3 <Vector (1.0000, 1.0000, 0.0000)>
4 <Vector (1.0000, -1.0000, 0.0000)>
5 <Vector (1.0000, -0.9310, 0.0000)>
6 <Vector (-1.0000, -0.9310, 0.0000)>
7 <Vector (-1.0000, -1.0000, 0.0000)>
8 <Vector (1.0000, -0.8621, 0.0000)>
9 <Vector (-1.0000, -0.8621, 0.0000)>
10 <Vector (1.0000, -0.5172, 0.0000)>
11 <Vector (1.0000, -0.4483, 0.0000)>
12 <Vector (-1.0000, -0.4483, 0.0000)>
13 <Vector (-1.0000, -0.5172, 0.0000)>
14 <Vector (1.0000, -0.3793, 0.0000)>
15 <Vector (-1.0000, -0.3793, 0.0000)>
16 <Vector (1.0000, -0.3103, 0.0000)>
17 <Vector (1.0000, -0.2414, 0.0000)>
18 <Vector (-1.0000, -0.2414, 0.0000)>
19 <Vector (-1.0000, -0.3103, 0.0000)>
20 <Vector (1.0000, -0.7241, 0.0000)>
21 <Vector (1.0000, -0.6552, 0.0000)>
22 <Vector (-1.0000, -0.6552, 0.0000)>
23 <Vector (-1.0000, -0.7241, 0.0000)>
24 <Vector (1.0000, -0.5862, 0.0000)>
25 <Vector (-1.0000, -0.5862, 0.0000)>
26 <Vector (1.0000, -0.7931, 0.0000)>
27 <Vector (-1.0000, -0.7931, 0.0000)>
28 <Vector (1.0000, -0.1724, 0.0000)>
29 <Vector (-1.0000, -0.1724, 0.0000)> # same as 35
30 <Vector (1.0000, -0.1034, 0.0000)>
31 <Vector (1.0000, -0.0345, 0.0000)>
32 <Vector (-1.0000, -0.0345, 0.0000)>
33 <Vector (-1.0000, -0.1034, 0.0000)> # same as 34
34 <Vector (-1.0000, -0.1034, 0.0000)> # same as 33
35 <Vector (-1.0000, -0.1724, 0.0000)> # same as 29
36 <Vector (1.0000, 0.0345, 0.0000)>
37 <Vector (-1.0000, 0.0345, 0.0000)>
38 <Vector (1.0000, 0.1035, 0.0000)>
39 <Vector (-1.0000, 0.1035, 0.0000)>
40 <Vector (1.0000, 0.1724, 0.0000)> # same as 42
41 <Vector (-1.0000, 0.1724, 0.0000)> # same as 45
42 <Vector (1.0000, 0.1724, 0.0000)> # same as 41
43 <Vector (1.0000, 0.2414, 0.0000)>
44 <Vector (-1.0000, 0.2414, 0.0000)>
45 <Vector (-1.0000, 0.1724, 0.0000)> # same as 41
46 <Vector (1.0000, 0.3103, 0.0000)>
47 <Vector (-1.0000, 0.3103, 0.0000)>
48 <Vector (1.0000, 0.3793, 0.0000)>
49 <Vector (-1.0000, 0.3793, 0.0000)>
50 <Vector (1.0000, 0.4483, 0.0000)>
51 <Vector (-1.0000, 0.4483, 0.0000)>
52 <Vector (1.0000, 0.5172, 0.0000)>
53 <Vector (-1.0000, 0.5172, 0.0000)>
54 <Vector (1.0000, 0.5862, 0.0000)>
55 <Vector (-1.0000, 0.5862, 0.0000)>
56 <Vector (1.0000, 0.6552, 0.0000)>
57 <Vector (-1.0000, 0.6552, 0.0000)>
58 <Vector (1.0000, 0.7241, 0.0000)>
59 <Vector (-1.0000, 0.7241, 0.0000)>
60 <Vector (1.0000, 0.7931, 0.0000)>
61 <Vector (-1.0000, 0.7931, 0.0000)>
62 <Vector (1.0000, 0.8621, 0.0000)>
63 <Vector (-1.0000, 0.8621, 0.0000)>
Blender Game Engine Finished

What gives?


I just noticed that the BGE seems to think there are 64 vertices, but my plane only has 60 according to the count in the header.
What could cause this?

$\endgroup$
  • $\begingroup$ How many materials do you expect? 1,2 or more? This relates to the unexpected count of 64. How many faces of each material? $\endgroup$ – atomicbezierslinger Dec 29 '15 at 6:09
  • $\begingroup$ @atomicbezierslinger 1. There should always be only one in my particular case (and there is only one in the example, I checked that). Even stranger, it correctly gives 4 verts if I delete the extra edges in the plane. $\endgroup$ – gandalf3 Dec 29 '15 at 8:10
  • $\begingroup$ Take this with a cellar of salt. 2013. developer.blender.org/T34550 $\endgroup$ – atomicbezierslinger Dec 29 '15 at 8:57
  • $\begingroup$ @atomicbezierslinger Hm.. That may relevant still. The result seems vary based on how I construct the plane. If I do more than 15 loopcuts at once, it returns extra verts. If I loopcut + subdivide (for e.g.) it works fine. Very strange indeed. $\endgroup$ – gandalf3 Dec 29 '15 at 9:10
  • $\begingroup$ getXYZ() returns the position relative to the origin of the mesh. The vertex count and the duplicates are related. Basically it's nesscesary for the GPU in some cases (related to face normals) to duplicate some verticies. BGE does that for you without you noticing, until you count the vertices that's it. Don't worry about it and handle the duplicates as if they where the same vertex. If at some point you want to modify the normals then you'll have a problem. $\endgroup$ – elmeunick Jan 5 '16 at 2:00
1
$\begingroup$

From elmeunick's comment:

getXYZ() returns the position relative to the origin of the mesh. The vertex count and the duplicates are related. Basically it's necessary for the GPU in some cases (related to face normals) to duplicate some vertices. BGE does that for you without you noticing, until you count the vertices that's it.

Don't worry about it and handle the duplicates as if they where the same vertex. If at some point you want to modify the normals then you'll have a problem.

$\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.