Wrapping Other Objects

You can wrap several other object types using pyvista including:

  • numpy arrays

  • trimesh.Trimesh meshes

  • VTK objects

This allows for the “best of both worlds” programming special to Python due to its modularity. If there’s some limitation of pyvista (or trimesh), then you can adapt your scripts to use the best features of more than one module.

import pyvista as pv

Wrap a point cloud composed of random points from numpy

import numpy as np
points = np.random.random((30, 3))
cloud = pv.wrap(points)
pv.plot(cloud, scalars=points[:, 2], render_points_as_spheres=True, point_size=50,
        opacity=points[:, 0], cpos='xz')
wrap trimesh

Out:

[(0.4947139504535911, -2.535322115944221, 0.5457139871567086),
 (0.4947139504535911, 0.485233387167094, 0.5457139871567086),
 (0.0, 0.0, 1.0)]

Wrap an instance of Trimesh

import trimesh
points = [[0, 0, 0], [0, 0, 1], [0, 1, 0]]
faces = [[0, 1, 2]]
tmesh = trimesh.Trimesh(points, faces=faces, process=False)
mesh = pv.wrap(tmesh)
print(mesh)

Out:

PolyData (0x7f623b4817c0)
  N Cells:      1
  N Points:     3
  X Bounds:     0.000e+00, 0.000e+00
  Y Bounds:     0.000e+00, 1.000e+00
  Z Bounds:     0.000e+00, 1.000e+00
  N Arrays:     0

Wrap an instance of vtk.vtkPolyData

import vtk
points = vtk.vtkPoints()
p = [1.0, 2.0, 3.0]
vertices = vtk.vtkCellArray()
pid = points.InsertNextPoint(p)
vertices.InsertNextCell(1)
vertices.InsertCellPoint(pid)
point = vtk.vtkPolyData()
point.SetPoints(points)
point.SetVerts(vertices)
mesh = pv.wrap(point)
print(mesh)

Out:

PolyData (0x7f623b481980)
  N Cells:      1
  N Points:     1
  X Bounds:     1.000e+00, 1.000e+00
  Y Bounds:     2.000e+00, 2.000e+00
  Z Bounds:     3.000e+00, 3.000e+00
  N Arrays:     0

Total running time of the script: ( 0 minutes 0.731 seconds)

Gallery generated by Sphinx-Gallery