Skip to content

Commit

Permalink
add pygfx as a "full" variant dependency
Browse files Browse the repository at this point in the history
  • Loading branch information
Krande committed Aug 4, 2023
1 parent 41ee6a4 commit c3972c7
Show file tree
Hide file tree
Showing 6 changed files with 37 additions and 9 deletions.
1 change: 1 addition & 0 deletions conda/environment.full.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,4 @@ dependencies:
- jupyterlab
- pythreejs
- pyparsing
- pygfx
2 changes: 1 addition & 1 deletion src/ada/api/spatial/part.py
Original file line number Diff line number Diff line change
Expand Up @@ -737,7 +737,7 @@ def to_fem_obj(
if interactive is True:
gs.open_gui()

fem = gs.get_fem(name=name if name is not None else self.name)
fem = gs.get_fem(name=name if name is not None else f"{self.name}-FEM")

for mass_shape in masses:
cog_absolute = mass_shape.placement.get_absolute_placement().origin + mass_shape.cog
Expand Down
15 changes: 9 additions & 6 deletions src/ada/visit/render_pygfx.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,11 @@

import ada.visit.render_pygfx_helpers as gfx_utils
except ImportError:
raise ImportError("Please install pygfx to use this renderer -> 'pip install pygfx'.")
raise ImportError("Please install pygfx to use this renderer -> 'mamba install pygfx'.")
try:
from wgpu.gui.auto import WgpuCanvas
except ImportError:
raise ImportError("Please install wgpu to use this renderer -> 'pip install wgpu'.")
raise ImportError("Please install wgpu to use this renderer -> 'mamba install wgpu'.")

from ada.visit.render_backend import (
MeshInfo,
Expand All @@ -43,9 +43,8 @@


class RendererPyGFX:
def __init__(self, render_backend: RenderBackend, canvas_title: str = "PyGFX Renderer"):
def __init__(self, render_backend=SqLiteBackend(), canvas_title: str = "PyGFX Renderer", no_gui: bool = False):
self.backend = render_backend

self._mesh_map = {}
self._selected_mat = gfx.MeshPhongMaterial(color=PICKED_COLOR, flat_shading=True)
self.selected_mesh = None
Expand All @@ -58,9 +57,13 @@ def __init__(self, render_backend: RenderBackend, canvas_title: str = "PyGFX Ren
self._scene_objects.receive_shadow = True
self._scene_objects.cast_shadow = True
self.scene.add(self._scene_objects)
if no_gui:
self._canvas = None
self._renderer = None
else:
self._canvas = WgpuCanvas(title=canvas_title, max_fps=60)
self._renderer = gfx.renderers.WgpuRenderer(self._canvas, show_fps=False)

self._canvas = WgpuCanvas(title=canvas_title, max_fps=60)
self._renderer = gfx.renderers.WgpuRenderer(self._canvas, show_fps=False)
self.before_render = None
self.after_render = None
self.on_click_pre: Callable[[gfx.PointerEvent], None] | None = None
Expand Down
14 changes: 12 additions & 2 deletions src/ada/visit/render_pygfx_helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
cone_geometry,
)
from pygfx.utils import Color
from ada.visit.colors import Color as AdaColor

from ada.visit.gltf.meshes import MeshStore

Expand Down Expand Up @@ -220,6 +221,9 @@ def geometry_from_mesh(
def gfx_mesh_from_mesh(
mesh: trimesh.Trimesh | trimesh.path.Path3D | MeshStore, material=None
) -> gfx.Mesh | gfx.Points | gfx.Line:
default_point_color = AdaColor.from_str("black")
default_line_color = AdaColor.from_str("gray")

if isinstance(mesh, MeshStore):
mat = tri_mat_to_gfx_mat(mesh.visual.material)
geom = gfx.Geometry(
Expand All @@ -229,7 +233,10 @@ def gfx_mesh_from_mesh(
mesh = gfx.Mesh(geom, material=mat)
elif isinstance(mesh, trimesh.points.PointCloud):
geom = gfx.Geometry(positions=np.ascontiguousarray(mesh.vertices, dtype="f4"))
mat = gfx.PointsMaterial(size=10, color=mesh.visual.main_color)
# if hasattr(mesh, "visual"):
# mat = gfx.PointsMaterial(size=10, color=mesh.visual.main_color)
# else:
mat = gfx.PointsMaterial(size=10, color=Color(*default_point_color.rgb))
mesh = gfx.Points(geom, material=mat)
elif isinstance(mesh, trimesh.path.Path3D):
indices = np.ascontiguousarray(mesh.vertex_nodes, dtype="i4")
Expand All @@ -240,7 +247,10 @@ def gfx_mesh_from_mesh(
positions[i + 1] = mesh.vertices[p2]
i += 2
geom = gfx.Geometry(positions=positions)
mat = gfx.LineSegmentMaterial(thickness=3, color=mesh.visual.main_color)
if hasattr(mesh, "visual"):
mat = gfx.LineSegmentMaterial(thickness=3, color=mesh.visual.main_color)
else:
mat = gfx.LineSegmentMaterial(thickness=3, color=Color(*default_line_color.rgb))
mesh = gfx.Line(geom, material=mat)
else:
geom = gfx.Geometry(
Expand Down
Empty file.
14 changes: 14 additions & 0 deletions tests/full/rendering_pygfx/test_read_scene.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import ada
from ada.visit.render_pygfx import RendererPyGFX


def test_read_fem_object():
bm = ada.Beam('bm', (0, 0, 0), (1, 0, 0), 'IPE300')
p = ada.Part('part') / bm
p.fem = p.to_fem_obj(0.1, 'line')
a = ada.Assembly() / p
a.to_gltf('beam_wMesh.glb')

renderer = RendererPyGFX(no_gui=True)
renderer.add_trimesh_scene(a.to_trimesh_scene(), 'myFEM')

0 comments on commit c3972c7

Please sign in to comment.