Skip to main content

A module for creating GLTF files with various geometric primitives and 3D text labels.

Project description

plot2gltf

A Claude-created Python module to save different types of simple geometry and annotations to glTF. You can view the result in any GLTF viewer, such as:

Key features:

  1. Supports multiple geometry types:
    • Triangle meshes
    • Line segments and line strips (as hairlines or cylinder tubes)
    • Points (as dots or spheres)
    • Normal vectors (as lines or arrows)
    • Text labels
  2. Automatic color generation using the golden ratio for visually distinct colors
  3. Optional manual color specification
  4. All geometries are combined into a single GLTF file
  5. Proper material setup with metallic-roughness PBR workflow

It is meant to be as easy to use as generating a 3D matplotlib plot.

Example

# Example usage:
from plot2gltf import GLTFGeometryExporter

exporter = GLTFGeometryExporter()

# Add a triangle mesh
vertices = [
    [0, 0, 0],
    [1, 0, 0],
    [0, 1, 0]
]
faces = [[0, 1, 2]]
exporter.add_triangles(vertices, faces, color=(1, 0, 0))  # Red triangles

# Add some lines
line_vertices = [
    [0, 0, 0],
    [1, 1, 1]
]
edges = [[0, 1]]
exporter.add_lines(line_vertices, edges)  # Auto-generated color

# Create a square as a line strip
points = [
    [0,0,0],
    [1,0,0],
    [1,1,0],
    [0,1,0]
]
exporter.add_linestrip(points, color=(1,0,0))  # Creates a line strip
# The same but as cylinders with sphere mitering and endcaps
exporter.add_cylinder_strips(points, color=(1,0,0), radius=0.03, add_spheres=True)

# Add labels for some points
exporter.add_text([0, 0, 0.1], "Origin", size=0.2, color=(1, 1, 1))  # White text
exporter.add_text([1, 1, 1.1], "Point 2", size=0.3)  # Auto-colored text

# Or for a curve
t = np.linspace(0, 2*np.pi, 50)
curve_points = np.column_stack([
    np.cos(t),
    np.sin(t),
    np.zeros_like(t)
])
exporter.add_linestrip(curve_points, color=(0,1,0))  # Creates a smooth curve
# The same but as cylinders with sphere mitering and endcaps
exporter.add_cylinder_strips(curve_points, color=(0,1,0), radius=0.03, add_spheres=True)

# Add points
points = [
    [0, 0, 0],
    [1, 0, 0],
    [0, 1, 0]
]
exporter.add_points(points, color=(0, 1, 0))  # Green points
# The same but as spheres
exporter.add_spheres(points, color=(0, 1, 0))  # Green points

# Add normals
normal_points = [[0, 0, 0]]
normal_directions = [[0, 0, 1]]
exporter.add_normals(normal_points, normal_directions, color=(0, 0, 1))  # Blue normals
# The same but as cylinders with cone caps
exporter.add_normal_arrows(
            normal_points, normal_directions, color=(0, 0, 1),
            shaft_radius=0.02, head_radius=0.04
        )

# Save the file
exporter.save("output.gltf")

Installation

Copy plot2gltf.py and fonts into your python.

Install dependencies via:

pip install -r requirements.txt

Demo

There is a weird, Claude-generated comprehensive demo. Run it via:

python demo.py

The output file is named demo_scene.gltf.

This demo file showcases:

  1. Geometric Primitives:

    • Triangles (cube and house)
    • Lines (coordinate axes)
    • Points (spiral pattern)
    • Normals (orientation vectors)
  2. Text Features:

    • Different sizes
    • Different colors
    • Labels for geometric objects
    • Stand-alone text examples
  3. Color Usage:

    • Manual color specification
    • Automatic color generation
    • Different colors for different object types
  4. Complex Shapes:

    • A cube with multiple faces
    • A spiral point cloud
    • A simple house shape
    • Coordinate axes

The scene includes a variety of objects arranged in a way that makes it easy to see all the features.

All the things the demo file showcases

License

Public Domain (CC0)

The included DejaVu font has its own (free) license.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

plot2gltf-1.0.tar.gz (349.5 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

plot2gltf-1.0-py2.py3-none-any.whl (9.5 kB view details)

Uploaded Python 2Python 3

File details

Details for the file plot2gltf-1.0.tar.gz.

File metadata

  • Download URL: plot2gltf-1.0.tar.gz
  • Upload date:
  • Size: 349.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: python-requests/2.32.3

File hashes

Hashes for plot2gltf-1.0.tar.gz
Algorithm Hash digest
SHA256 66c88b5ee322d8bcae43415aa9463d33ccf63f6b923536a2c41961408f0565f8
MD5 611bc9c47166f91bbc1f129db6708ef2
BLAKE2b-256 dadb295c316ae4f897371f27900293a2e77f3902f9fe1beba35594e49381ec65

See more details on using hashes here.

File details

Details for the file plot2gltf-1.0-py2.py3-none-any.whl.

File metadata

  • Download URL: plot2gltf-1.0-py2.py3-none-any.whl
  • Upload date:
  • Size: 9.5 kB
  • Tags: Python 2, Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: python-requests/2.32.3

File hashes

Hashes for plot2gltf-1.0-py2.py3-none-any.whl
Algorithm Hash digest
SHA256 b4d4a93fb88222358f031be4d0e3008f2d751379114a85d40390693b53faf0d4
MD5 69b3ac8dae9765d3a66fff5b36db640a
BLAKE2b-256 a645cb3cee721f2e2f06135c676edd218ed7e80e24d54e58bfc44e7d486a6f7c

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page