Skip to content

Commit 97f0e2d

Browse files
author
Mohamed Koubaa
committed
fix exception when plotting a model with any line bodies
1 parent d2689ee commit 97f0e2d

File tree

2 files changed

+34
-7
lines changed

2 files changed

+34
-7
lines changed

src/ansys/mechanical/core/embedding/viz/pyvista_plotter.py

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ def _transform_to_pyvista(transform: "Ansys.ACT.Math.Matrix4D"):
4444
return np_transform
4545

4646

47-
def _get_nodes_and_coords(tri_tessellation: "Ansys.Mechanical.Scenegraph.TriTessellationNode"):
47+
def _get_tri_nodes_and_coords(tri_tessellation: "Ansys.Mechanical.Scenegraph.TriTessellationNode"):
4848
"""Get the nodes and indices of the TriTessellationNode.
4949
5050
pyvista format expects a number of vertices per facet which is always 3
@@ -55,6 +55,19 @@ def _get_nodes_and_coords(tri_tessellation: "Ansys.Mechanical.Scenegraph.TriTess
5555
return np_coordinates, np_indices
5656

5757

58+
def _get_nodes_and_coords(node: "Ansys.Mechanical.Scenegraph.Node"):
59+
"""Get the nodes and indices of the Scenegraph node.
60+
61+
Currently only supported for tri tessellation nodes
62+
"""
63+
if isinstance(node, Ansys.Mechanical.Scenegraph.TriTessellationNode):
64+
return _get_tri_nodes_and_coords(node)
65+
66+
# TODO - support line tessellation node. See issue #809
67+
#if isinstance(node, Ansys.Mechanical.Scenegraph.LineTessellationNode):
68+
return None, None
69+
70+
5871
def to_pyvista_plotter(app: "ansys.mechanical.core.embedding.App"):
5972
"""Convert the app's geometry to a pyvista plotter instance."""
6073
plotter = pv.Plotter()
@@ -63,6 +76,8 @@ def to_pyvista_plotter(app: "ansys.mechanical.core.embedding.App"):
6376
):
6477
scenegraph_node = Ansys.ACT.Mechanical.Tools.ScenegraphHelpers.GetScenegraph(body)
6578
np_coordinates, np_indices = _get_nodes_and_coords(scenegraph_node.Child)
79+
if np_coordinates is None or np_indices is None:
80+
continue
6681
pv_transform = _transform_to_pyvista(scenegraph_node.Transform)
6782
polydata = pv.PolyData(np_coordinates, np_indices).transform(pv_transform)
6883
color = pv.Color(bgr_to_rgb_tuple(body.Color))

src/ansys/mechanical/core/embedding/viz/usd_converter.py

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -73,22 +73,34 @@ def _convert_tri_tessellation_node(
7373
return mesh_prim
7474

7575

76-
def _convert_transform_node(
77-
node: "Ansys.Mechanical.Scenegraph.TransformNode",
76+
def _create_prim_with_transform(
7877
stage: Usd.Stage,
7978
path: str,
80-
rgb: typing.Tuple[int, int, int],
79+
node: "Ansys.Mechanical.Scenegraph.TransformNode"
8180
) -> Usd.Prim:
82-
"""Convert a mechanical transform node into a Usd Xform prim."""
81+
"""Create an empty Usd Xform prim based on a mechanical transform node."""
8382
prim = UsdGeom.Xform.Define(stage, path)
8483
rotation, translation = _transform_to_rotation_translation(node.Transform)
8584
prim.AddOrientOp().Set(rotation)
8685
prim.AddTranslateOp().Set(translation)
86+
return prim
87+
88+
89+
def _convert_transform_node(
90+
node: "Ansys.Mechanical.Scenegraph.TransformNode",
91+
stage: Usd.Stage,
92+
path: str,
93+
rgb: typing.Tuple[int, int, int],
94+
) -> None:
95+
"""Add a Usd prim to the stage based on the given mechanical transform node.
96+
97+
Currently only supports transforms that contain a single tri tessellation node.
98+
"""
8799
child_node = node.Child
88-
child_path = prim.GetPath().AppendPath("TriTessellation")
89100
if isinstance(child_node, Ansys.Mechanical.Scenegraph.TriTessellationNode):
101+
prim = _create_prim_with_transform(stage, path, node)
102+
child_path = prim.GetPath().AppendPath("TriTessellation")
90103
_convert_tri_tessellation_node(child_node, stage, child_path, rgb)
91-
return prim
92104

93105

94106
def to_usd_stage(app: "ansys.mechanical.core.embedding.App", name: str) -> None:

0 commit comments

Comments
 (0)