Mesh

Reference for the Mesh class, which lets you define custom geometry (triangles, lines, points) by specifying vertices, indices, colors, UVs and normals.

Mesh

Mesh(vertices=None, triangles=None, colors=None, uvs=None, normals=None, static=True, mode='triangle', thickness=1, render_points_in_3d=True, vertex_buffer=None, vertex_buffer_length=None, vertex_buffer_format=None)

Located in ursina/mesh.py

Overview

The Mesh class (a p3d.NodePath subclass) lets you build custom geometry by providing:

  • vertices: list of 3‑tuples or flat list of floats
  • triangles (indices): flat list or list of index‑tuples
  • colors, uvs, normals: per‑vertex arrays
  • mode: 'triangle', 'line', 'point', 'ngon', 'tristrip'
  • thickness and render_points_in_3d for line/point modes
  • or supply a raw vertex_buffer

When constructed it auto‑generates the Panda3D Geom under the hood.

Constructor Arguments

ArgumentTypeDefaultDescription
verticeslist or flat listNoneVertex positions (x,y,z) or flat [x0,y0,z0, x1,y1,z1,…].
triangleslist or flat listNoneIndices defining primitives: [0,1,2, 2,3,0] or [(0,1,2),(2,3,0)].
colorslistNonePer‑vertex RGBA floats.
uvslistNonePer‑vertex UV coords (u,v).
normalslistNonePer‑vertex normals (nx,ny,nz).
staticboolTrueUse static or dynamic vertex buffer.
modestr or MeshModes'triangle'Primitive type: triangles, lines, points, fans or strips.
thicknessint1Line or point thickness.
render_points_in_3dboolTrueDraw points in 3D rather than overlay.
vertex_bufferbinary bufferNoneRaw interleaved buffer.
vertex_buffer_lengthintNoneNumber of vertices in raw buffer.
vertex_buffer_formatstrNoneFormat string, e.g. "p3f, c4f" for position+color.

Properties

PropertyDescription
.verticesThe vertex array.
.trianglesThe index array.
.colors, .uvs, .normalsPer‑vertex data arrays.
.modeCurrent primitive mode.
.thicknessLine/point thickness.
.render_points_in_3dDraw points as 3D or overlay.
.indicesFlattened index list.
.generated_verticesInternal expanded vertex list after generate.

Methods

MethodDescription
generate()(Re)build the underlying Geom from current arrays.
generate_normals(smooth=True, regenerate=True)Compute normals per‑vertex.
project_uvs(aspect_ratio=1, direction='forward')Auto‑generate planar UVs.
colorize(left, right, down, up, back, forward, smooth=True, world_space=True, strength=1)Assign vertex colors based on face orientation.
clear(regenerate=True)Clear all arrays (optionally regenerate an empty mesh).
save(name='', folder=..., flip_faces=False, ...)Write to a .bam or .egg in the compressed models folder.
serialize(vertex_decimal_limit=4, color_decimal_limit=4, uv_decimal_limit=4, normal_decimal_limit=4)Return a JSON‑friendly dict of mesh data.

Example Usage

from ursina import *
from ursina.mesh import Mesh

app = Ursina()

# simple triangle
t1 = Entity(
    model=Mesh(vertices=[(-.5,0,0), (.5,0,0), (0,1,0)])
)

# line strip
line = Entity(
    position=(2,0,0),
    model=Mesh(
        vertices=[(0,0,0), (1,1,0), (2,0,0)],
        mode='line',
        thickness=4
    ),
    color=color.cyan
)

# quad with UVs and texture
quad = Entity(
    position=(4,0,0),
    model=Mesh(
        vertices=[(0.5,0.5,0),(-0.5,0.5,0),(-0.5,-0.5,0),(0.5,-0.5,0),(0.5,0.5,0),(-0.5,-0.5,0)],
        uvs=[(1,1),(0,1),(0,0),(1,0),(1,1),(0,0)],
        triangles=[0,1,2,2,3,0]
    ),
    texture='shore'
)

EditorCamera()
app.run()