diff --git a/conda/environment.full.yml b/conda/environment.full.yml index e24f309af..6f67e36ec 100644 --- a/conda/environment.full.yml +++ b/conda/environment.full.yml @@ -4,3 +4,4 @@ dependencies: - jupyterlab - pythreejs - pyparsing + - pygfx diff --git a/src/ada/api/spatial/part.py b/src/ada/api/spatial/part.py index bf6b8d5a1..d9baec7b8 100644 --- a/src/ada/api/spatial/part.py +++ b/src/ada/api/spatial/part.py @@ -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 diff --git a/src/ada/visit/render_pygfx.py b/src/ada/visit/render_pygfx.py index 863377c06..f3f3513b5 100644 --- a/src/ada/visit/render_pygfx.py +++ b/src/ada/visit/render_pygfx.py @@ -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, @@ -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 @@ -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 diff --git a/src/ada/visit/render_pygfx_helpers.py b/src/ada/visit/render_pygfx_helpers.py index c7cf37bb6..5c31dbffa 100644 --- a/src/ada/visit/render_pygfx_helpers.py +++ b/src/ada/visit/render_pygfx_helpers.py @@ -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 @@ -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( @@ -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") @@ -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( diff --git a/tests/full/rendering_pygfx/__init__.py b/tests/full/rendering_pygfx/__init__.py new file mode 100644 index 000000000..e69de29bb diff --git a/tests/full/rendering_pygfx/test_read_scene.py b/tests/full/rendering_pygfx/test_read_scene.py new file mode 100644 index 000000000..d17286a3b --- /dev/null +++ b/tests/full/rendering_pygfx/test_read_scene.py @@ -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') +