picogl.renderer.meshdata

Provides functionality for managing OpenGL mesh data, including vertices, normals, texture coordinates, colors, and indices. This class offers a set of utilities to handle OpenGL-related state objects and simplify rendering workflows.

Attributes

PNCPart

Classes

MeshData

Representation of mesh data for OpenGL rendering.

Functions

np_positions_to_normal_array(positions) → numpy.ndarray[Any, numpy.dtype[Any]]

Create normals from positions.

Module Contents

picogl.renderer.meshdata.PNCPart[source]
picogl.renderer.meshdata.np_positions_to_normal_array(positions: numpy.ndarray[Any, numpy.dtype[Any]] | list[Any]) → numpy.ndarray[Any, numpy.dtype[Any]][source]

Create normals from positions.

class picogl.renderer.meshdata.MeshData(vertices: numpy.ndarray = None, normals: numpy.ndarray = None, texcoords: numpy.ndarray = None, colors: numpy.ndarray = None, indices: numpy.ndarray = None, draw_info: picogl.renderer.draw_spec.MeshDrawInfo | None = None, item_keys: numpy.ndarray | None = None, color_source_indices: numpy.ndarray | None = None)[source]

Representation of mesh data for OpenGL rendering.

This class encapsulates mesh data and provides utilities for setting up and managing vertex attributes such as positions, normals, texture coordinates, colors, and indices. It enables interoperability with OpenGL through context management and binding/unbinding functions. Additionally, the class includes methods for raw data conversion and generation of default attributes.

vertices[source]

Optional array of vertex positions as np.ndarray.

normals[source]

Optional array of vertex normals as np.ndarray.

texcoords[source]

Optional array of texture coordinates as np.ndarray.

colors[source]

Optional array of vertex colors as np.ndarray.

indices[source]

Optional array of vertex indices as np.ndarray.

vertex_count[source]

Optional count of vertices, computed from vertices input.

draw_info

CPU draw layout (mode, indexed, per-item strides).

item_keys[source]

Optional (N, K) identity table for logical items.

color_source_indices[source]

Optional per-vertex gather indices into a logical color array.

bind()[source]

Binds vertex attributes to OpenGL client states for rendering.

unbind()[source]

Unbinds vertex attributes from OpenGL client states.

as_canonical_names()[source]

Converts the mesh data into a dictionary with canonical attribute names.

draw()[source]

Draws the mesh with optional OpenGL parameters for color, line width, draw mode, fill mode, and alpha transparency.

from_raw()[source]

Class method for constructing a MeshData object from raw input data.

vertices = None[source]
normals = None[source]
colors = None[source]
texcoords = None[source]
indices = None[source]
item_keys = None[source]
color_source_indices = None[source]
vao = None[source]
vertex_count[source]
property faces: numpy.ndarray | None[source]

Triangle faces derived from indices.

PicoGL stores the element buffer on indices. Callers that still say faces get that buffer, reshaped to (M, 3) when the length is a multiple of 3.

Returns:

Face array, or None when there is no index buffer

log_contents() → None[source]

Log vertex and triangle counts from vertices/indices.

classmethod from_pnc(part: PNCPart, *, indices: numpy.ndarray | None = None, draw_info: picogl.renderer.draw_spec.MeshDrawInfo | None = None) → MeshData[source]

Build a MeshData from a PNCPart.

Parameters:
  • part – (positions, normals, colors) float arrays

  • indices – Optional element buffer

  • draw_info – Optional draw layout; inferred when omitted

Returns:

New MeshData instance

classmethod from_arrays(positions: numpy.ndarray, normals: numpy.ndarray, colors: numpy.ndarray, *, indices: numpy.ndarray | None = None) → MeshData[source]

Build MeshData from separate P/N/C arrays (see also from_pnc()).

as_pnc() → PNCPart[source]

Return (positions, normals, colors) as float32 (N, 3) arrays.

Missing normals/colors are filled with zeros to match the vertex count. An empty mesh returns three empty (0, 3) arrays.

with_pnc_colors(colors: numpy.ndarray) → MeshData[source]

Return a new mesh with the colour channel replaced.

Positions, normals, indices, and draw_info are preserved.

Parameters:

colors – RGB rows (N, 3) or a broadcastable colour array

Returns:

New MeshData

as_vec3_arrays() → PNCPart[source]

Alias for as_pnc() (positions, normals, colors).

property mesh_data: MeshData[source]

This object is the canonical CPU mesh.

property normalized_indices: numpy.ndarray[Any, numpy.dtype[Any]] | None[source]

normalized indices

validate_data()[source]

validate mesh data

as_indexed_arrays() → tuple[numpy.ndarray, numpy.ndarray, numpy.ndarray, numpy.ndarray][source]

Unpack spheres_for_atoms() output for tests and legacy tuple call sites.

as_arrays() → tuple[numpy.ndarray, numpy.ndarray, numpy.ndarray][source]

Unpack make_unit_sphere() output (no per-vertex colors).

as_arrays_with_normals() → tuple[numpy.ndarray, numpy.ndarray, numpy.ndarray][source]

as arrays with normals

resolved_draw_info() → picogl.renderer.draw_spec.MeshDrawInfo[source]

Return the mesh draw layout (always set in __init__).

draw_spec(item_count: int | None = None, first_item: int = 0) → picogl.renderer.draw_spec.MeshDrawSpec[source]

Build a GL-free draw for first_item .. first_item + item_count.

When MeshDrawInfo.elements_per_item is set, first_item is a logical item index (atom, bond, …) and is converted to an index range.

Parameters:
  • item_count – Number of items to draw; None draws the remainder.

  • first_item – First logical item (or first index/vertex if unstrided).

Returns:

MeshDrawSpec clamped to the mesh buffers.

expand_attribute_per_item(values: numpy.ndarray, elements_per_item: int | None = None) → numpy.ndarray[source]

Repeat one row per item across elements_per_item vertices.

Parameters:
  • values – Array of shape (N, 3) (one row per logical item).

  • elements_per_item – Repeat count; defaults to MeshDrawInfo.vertices_per_item.

Returns:

float32 array of shape (N * count, 3).

expand_colors(logical_colors: numpy.ndarray) → numpy.ndarray[source]

Expand per-item or per-source colors to one RGB row per vertex.

If logical_colors already matches the vertex count it is returned unchanged. Otherwise color_source_indices (gather) or expand_attribute_per_item() is applied.

Parameters:

logical_colors – RGB rows (vertices, items, or gather sources).

Returns:

float32 array of shape (V, 3).

apply_logical_colors(logical_colors: numpy.ndarray) → numpy.ndarray[source]

Expand logical colors onto colors (CPU only; no GPU upload).

Parameters:

logical_colors – RGB rows (vertices, items, or gather sources).

Returns:

The expanded per-vertex color array now stored on this mesh.

attach_vao(vao: Any) → Any[source]

Bind vao to this mesh and this mesh to vao (bidirectional).

Parameters:

vao – GPU vertex array / buffer group that will issue glDraw*.

Returns:

vao (for call chaining from setup helpers).

draw(*, first_item: int = 0, item_count: int | None = None, count: int | None = None, mode: picogl.backend.gl.enums.GLDrawMode | None = None) → None[source]

Issue a modern glDraw* via the attached VAO and draw_spec().

Parameters:
  • first_item – First logical item (atom, bond, …).

  • item_count – Number of items; None draws the remainder.

  • count – Override element/vertex count (HETATM temporary EBO).

  • mode – Override MeshDrawSpec.mode (e.g. gizmo LINES).

Raises:

RuntimeError – When no VAO has been attached.

validate()[source]

validate mesh data

setup_atom_vao()[source]

Build a vertex array from atom mesh data (layout included).

setup_ribbon_vao()[source]
setup_calpha_vao()[source]
setup_bond_vao()[source]

Build a vertex array from bond mesh data (layout included).

setup_vertex_attributes()[source]

setup vertex attributes

property normalized_normals: numpy.ndarray[Any, numpy.dtype[numpy.generic]][source]
property normalized_colors: numpy.ndarray[Any, numpy.dtype[numpy.generic]][source]
property normalized_vertices: numpy.ndarray[Any, numpy.dtype[numpy.generic]][source]
static _ensure_xyz(arr, n=None)[source]
extend(vertices=None, normals=None, colors=None, uvs=None)[source]
setup_vbg(draw_mode: int | picogl.backend.gl.enums.GLDrawMode = GLDrawMode.TRIANGLE_STRIP) → picogl.backend.gl.legacy.setup_vbg.VertexBufferGroup[source]
static from_object_data(object_data: picogl.utils.loader.object_data.ObjectData) → MeshData[source]
extend_from_mesh(other: MeshData)[source]
property vbo[source]
property nbo[source]
property uvs[source]
property cbo[source]
property ebo[source]
as_canonical_names() → dict[source]

Convert into canonical names.

__str__()[source]
classmethod _to_float32_flat(arr, name: str, required: bool = False) → numpy.ndarray[source]
classmethod _to_float32_flat_or_none(arr, name: str) → numpy.ndarray[source]
classmethod _to_int32_flat(arr, name: str, required: bool = False) → numpy.ndarray[source]
classmethod _default_colors_for_vertices(vertex_count: int) → numpy.ndarray[source]

Simple default: red colour per vertex

classmethod _default_normals_for_vertices(vertex_count: int) → numpy.ndarray[source]

Simple default: simple normal per vertex

__enter__()[source]
__exit__(exc_type, exc_val, exc_tb)[source]
bind()[source]
unbind()[source]
classmethod from_raw(vertices: numpy.ndarray | list[float], normals: numpy.ndarray | list[float] | None = None, uvs: numpy.ndarray | list[float] | None = None, colors: numpy.ndarray | list[float] | list[tuple | int] | None = None, indices: numpy.ndarray | list[float] | None = None, color_per_vertex: numpy.ndarray | list[float] | None = None)[source]

Build a MeshData from raw/python inputs.

Parameters:
  • vertices – np.ndarray required, list/array of x,y,z triplets

  • normals – np.ndarray optional, list/array of x,y,z triplets

  • uvs – np.ndarray optional, list/array of u,v pairs

  • indices – np.ndarray optional int indices

  • colors – np.ndarray optional per-vertex colors (flat float32 array)

  • color_per_vertex – np.ndarray if provided and colors is None, generate per-vertex colors

draw_legacy(color: tuple | None = None, line_width: float = 1.0, mode: int = GLDrawMode.TRIANGLES, fill: bool = False, alpha: float = 1.0)[source]

Draw with legacy client arrays (gl*Pointer + glDrawElements).

Parameters:
  • color – Optional RGB override. If None and vertex colors exist, uses the colour array.

  • line_width – Line width for wireframe mode.

  • mode – OpenGL draw mode.

  • fill – Whether to fill polygons or use wireframe.

  • alpha – Transparency (0.0 opaque … 1.0 fully transparent).

delete()[source]

Drop CPU references to mesh arrays (no gl objects on this type).

static _xyz_row_count(arr: numpy.ndarray | None) → int[source]
static _as_xyz_f32(arr: numpy.ndarray | None, n: int, name: str) → numpy.ndarray[source]

Return (n, 3) float32; None becomes zeros.

static _as_uv_f32(arr: numpy.ndarray | None, n: int) → numpy.ndarray[source]

Return (n, 2) float32; None becomes zeros.

static _indices_u32_flat(indices: numpy.ndarray | None) → numpy.ndarray | None[source]
append_mesh(mesh: MeshData) → None[source]

Concatenate mesh after this mesh: vertices/normals/colors/uvs are stacked; triangle indices from mesh are offset by the current vertex count.

Invariants:
  • Vertices are (N, 3), float32.

  • Normals and colors, if present on either mesh, are expanded to (N, 3) with zeros for missing rows so lengths match vertices.

  • Indices are uint32 element buffer; merged as concat(self, mesh + base).

  • Optional texcoords (N, 2); if either side has them, both sides are padded.

Parameters:

mesh – Another MeshData instance (same attribute layout).