MeshBridge - BPY-like mesh wrapper

Hello BlenderArtists!

Recently, I have wished to use the BGE for functions like navmeshes and other such applications. Unfortunately, the way in which the BGE Mesh API is written means that one cannot rely on using vertices as waypoints, for in cases (as documented here in the API) where the data for vertices is non-identical, position aside.

There are many applications whereby using such a system may benefit the end user, but often any user who wishes to interact with a mesh expects there to be shared vertices between polygons, and not the current vertex splitting method.

Thus, i’ve written a form of “wrapper” - MeshBridge

  • MeshBridge will behave in most cases exactly like the previous api.
  • It creates new custom vertex, polygon and mesh instances for a mesh, building its own functions for attribute modification
  • It has certain tradeoffs, and benefits

The current implementation should work in the same way as the existing API. You can move vertices, setXYZ, setUV etc. I’ve added a new feature that permits you to move the location of a polygon, as well as the normal.
New style polygons have child vertices. These are new style vertex instances.
New style vertices have child (base) vertices. These are the original vertices found in the BGE Mesh data for an object. When you find a vertex using mesh.getVertex(matid, vertid), it is no longer a single vertex. It is a list of vertices that share the same location within a distance threshold. These vertices are stored on a list, and when you transform a vertex parameter, it applies the same deformation to all vertices.

This is where one limitation comes into place. As it takes the mean of all the vertices for any list data (e.g normal, position), it may be that things such as UV coordinates don’t function correctly. I’m not sure, please report back with your own findings.

The limitation that I am aware of is vertex normals. In the case of a cube, in the BGE the vertex normals typically face the direction of the face. In this bridge, it takes the mean normal of all the child vertices. In the case of a cube, it would mean that the normal would face away from the center of the object.

I expect that any transformations will run slower than a single vertex transformation (as they are applied to all vertices). The same goes to reading vertex data (as it sums it and then gets the mean).

To use this, create a text file in Blender called “mesh_bridge.py”.
Import “Mesh” from mesh_bridge -

from mesh_bridge import mesh

Now, simply take the mesh of the object, and instantiate the Mesh class with it

Look for mesh bridge.py on BlenderArchive

http://hostilestudios.com/blenderarchive/list.php

A tutorial would be nice showing how it could be used with navmeshes.

I’m not referring to the built - in navigation mesh. I’ve wanted to write various navigation algorithms that permit greater user control than just find path to point. Thus, the aforementioned creation.

Here is some sample code for the cube demo:


import bge

def cube(cont):
    own = cont.owner
    
    try:
        mesh = own['mesh']
     
    except KeyError:
        mesh = own['mesh'] = Mesh(own.meshes[0])
        
    for polygon in mesh.polygons:
        polygon.XYZ += polygon.normal * 0.05

And the wave demo:


import bge
import random

def wave(cont):
    own = cont.owner
    height = 2
    
    try:
        mesh = own['mesh']
     
    except KeyError:
        mesh = own['mesh'] = Mesh(own.meshes[0])
        
    for vertex in mesh.vertices:
        try:
            original_z = vertex.original_z
        
        except AttributeError:
            original_z = vertex.original_z = vertex.z
            
        
        vertex.z = original_z + random.randrange(-height, height)

  • Added area attribute to polygon.
  • Fixed UV setter (lowercase mismatch)

Ye pretty coo navmesh

This is pretty cool. I’m using it to make a waving ocean.

I think I found a bug. getXYZ() throws an error, as there’s no ‘xyz’ property, right?

It should have getXYZ()
However, if not, try .XYZ
I have a faster version using a KDTree, but I need to check if my employer will permit me to release it (and check if it works correctly)

getXYZ() does not work, because it says that the vertex object has no ‘xyz’ property. Vertex.XYZ works fine, but vertex.xyz doesn’t.

That KDTree version sounds useful, indeed.

Then I’ve made a typo!

As a side note, the module’s named “MeshBridge”, but the file’s named “mesh_bridge.py”. “MeshBridge” is easier to type to me, so you could change it to that. Just a thought.

It seems your website domain has expired. Any chance you could upload MeshBridge elsewhere?

Yes, it has expired unfortunately. I have limited control / access to my domain provider. I’ll get a pasteall link in the meantime.