Skip to content

Commit a3929b3

Browse files
committed
add scene cache to 'Visualize Robot" component
1 parent 8725e91 commit a3929b3

File tree

2 files changed

+41
-7
lines changed

2 files changed

+41
-7
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
1414
**Changed**
1515

1616
* Updated install process of GH components.
17+
* Added caching to the GH component that visualizes scene, to avoid retrieving the whole scene too often.
1718

1819
**Fixed**
1920

src/compas_fab/ghpython/components/Cf_VisualizeRobot/code.py

Lines changed: 40 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,16 @@
33
44
COMPAS FAB v0.27.0
55
"""
6+
import time
7+
68
from compas.artists import Artist
79
from compas.geometry import Frame
810
from compas.geometry import Transformation
9-
from compas_fab.robots import PlanningScene
1011
from ghpythonlib.componentbase import executingcomponent as component
12+
from scriptcontext import sticky as st
13+
14+
from compas_fab.ghpython.components import create_id
15+
from compas_fab.robots import PlanningScene
1116

1217

1318
class RobotVisualize(component):
@@ -24,6 +29,7 @@ def RunScript(
2429
show_cm,
2530
show_acm,
2631
):
32+
2733
visual = None
2834
collision = None
2935
collision_meshes = None
@@ -64,14 +70,28 @@ def RunScript(
6470
frame = artist.draw()
6571
frames.append(frame)
6672

73+
cached_scene_key = create_id(self, "cached_scene")
74+
6775
if show_cm or show_acm:
68-
scene = PlanningScene(robot)
69-
scene = robot.client.get_planning_scene()
76+
cached_scene = st.get(cached_scene_key)
77+
if not cached_scene:
78+
cached_scene = {"time": 0}
79+
80+
# expire cache if the component has not been executed in the last 2 seconds
81+
# this allows to slide through a list of configurations
82+
# without triggering refreshes of the scene in the middle of it
83+
if time.time() - cached_scene["time"] > 2:
84+
update_scene = True
85+
else:
86+
update_scene = False
7087

71-
collision_meshes = []
72-
attached_meshes = []
88+
if update_scene:
89+
scene = PlanningScene(robot)
90+
scene = robot.client.get_planning_scene()
91+
92+
if update_scene and show_cm:
93+
collision_meshes = []
7394

74-
if show_cm:
7595
for co in scene.world.collision_objects:
7696
header = co.header
7797
frame_id = header.frame_id
@@ -86,7 +106,13 @@ def RunScript(
86106

87107
collision_meshes.append(Artist(mesh).draw())
88108

89-
if show_acm:
109+
cached_scene["cm"] = collision_meshes
110+
111+
collision_meshes = cached_scene.get("cm", [])
112+
113+
if update_scene and show_acm:
114+
attached_meshes = []
115+
90116
for aco in scene.robot_state.attached_collision_objects:
91117
for acm in aco.to_attached_collision_meshes():
92118
frame_id = aco.object["header"]["frame_id"]
@@ -101,6 +127,13 @@ def RunScript(
101127

102128
attached_meshes.append(Artist(mesh).draw())
103129

130+
cached_scene["acm"] = attached_meshes
131+
132+
attached_meshes = cached_scene.get("acm", [])
133+
134+
cached_scene["time"] = time.time()
135+
st[cached_scene_key] = cached_scene
136+
104137
return (
105138
visual,
106139
collision,

0 commit comments

Comments
 (0)