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
Classes
Representation of mesh data for OpenGL rendering. |
Functions
|
Create normals from positions. |
Module Contents
- 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.
- draw_info
CPU draw layout (mode, indexed, per-item strides).
- 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.
- property faces: numpy.ndarray | None[source]
Triangle faces derived from
indices.PicoGL stores the element buffer on
indices. Callers that still sayfacesget that buffer, reshaped to(M, 3)when the length is a multiple of 3.- Returns:
Face array, or
Nonewhen there is no index buffer
- classmethod from_pnc(part: PNCPart, *, indices: numpy.ndarray | None = None, draw_info: picogl.renderer.draw_spec.MeshDrawInfo | None = None) MeshData[source]
Build a
MeshDatafrom aPNCPart.- Parameters:
part –
(positions, normals, colors)float arraysindices – 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_infoare preserved.- Parameters:
colors – RGB rows
(N, 3)or a broadcastable colour array- Returns:
New
MeshData
- property normalized_indices: numpy.ndarray[Any, numpy.dtype[Any]] | None[source]
normalized indices
- 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_itemis set,first_itemis a logical item index (atom, bond, …) and is converted to an index range.- Parameters:
item_count – Number of items to draw;
Nonedraws the remainder.first_item – First logical item (or first index/vertex if unstrided).
- Returns:
MeshDrawSpecclamped 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_itemvertices.- 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_colorsalready matches the vertex count it is returned unchanged. Otherwisecolor_source_indices(gather) orexpand_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 anddraw_spec().- Parameters:
first_item – First logical item (atom, bond, …).
item_count – Number of items;
Nonedraws the remainder.count – Override element/vertex count (HETATM temporary EBO).
mode – Override
MeshDrawSpec.mode(e.g. gizmoLINES).
- Raises:
RuntimeError – When no VAO has been attached.
- 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]
- 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]
- 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
- 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
Noneand 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).
- 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;
Nonebecomes zeros.
- static _as_uv_f32(arr: numpy.ndarray | None, n: int) numpy.ndarray[source]
Return (n, 2) float32;
Nonebecomes zeros.
- static _indices_u32_flat(indices: numpy.ndarray | None) numpy.ndarray | None[source]
- append_mesh(mesh: MeshData) None[source]
Concatenate
meshafter this mesh: vertices/normals/colors/uvs are stacked; triangle indices frommeshare 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
MeshDatainstance (same attribute layout).