Search code examples
pythonblender

What is a MeshLoop in the Blender python API


I'm currently making a custom render engine for Blender 3.0+. I'm programming in C++(core engine) and Python(Blender API). In the past I have made two other render engines, where the data stored for meshes are polygons, edges and vertices, which is quite usual.

Blender uses a different structure, where a polygon references multiple "MeshLoops". A "MeshLoop" references one edge and one vertex. What I don't quite understand is the reason behind this. Simple referencing a list of vertices would absolutely work (and that's how my render engine works for now).

I'm certain there is a reason for this, but I'm unable to find any more information about this. The API documentation about it simply states that is keeps the index of one vertex and one edge.

So my question is : what exactly is a MeshLoop, and what purpose does it serve ?


Usefull links :


Solution

  • What the MeshLoop is in Blender API

    You're undoubtedly right about official documentation, even in 2024 it sucks. But the answer to your question is obvious. MeshLoop objects are tiny "utilities" in Blender that help you not only describe a 3D geometry but also store a color data for each vertex. There are four main arrays to define mesh geometry in Blender (unlike Maya, for instance, where vertices, edges and polygons are used for a commonly accepted mesh description, as you previously said):

    • Mesh.vertices : (3 points in space)
    • Mesh.edges : (reference 2 vertices)
    • Mesh.loops : (reference a single vertex and edge)
    • Mesh.polygons : (reference a range of loops)

    As stated in the documentation: "Each polygon references a slice in the loop array, this way, polygons do not store vertices or corner data such as UVs directly, only a reference to loops that the polygon uses." Thus, using the simplest example of a plane with 4 different colors assigned, we'll see how the central vertex, thanks to MeshLoop, retrieves the information about each of the four colors (without MeshLoop you can store only one color per vertex).

    import bpy
    
    plane = bpy.context.object
    mesh = plane.data
    
    len(mesh.vertices)
    len(mesh.edges)
    len(mesh.loops)
    len(mesh.polygons)
    len(mesh.vertex_colors)