Skip to content

Commit 8f98cce

Browse files
authored
Merge pull request #381 from compas-dev/cache-scene
Add scene cache to 'Visualize Robot" component
2 parents bb6978c + b9a4c97 commit 8f98cce

File tree

2 files changed

+40
-7
lines changed

2 files changed

+40
-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: 39 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):
@@ -64,14 +69,28 @@ def RunScript(
6469
frame = artist.draw()
6570
frames.append(frame)
6671

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

71-
collision_meshes = []
72-
attached_meshes = []
91+
if update_scene and show_cm:
92+
collision_meshes = []
7393

74-
if show_cm:
7594
for co in scene.world.collision_objects:
7695
header = co.header
7796
frame_id = header.frame_id
@@ -86,7 +105,13 @@ def RunScript(
86105

87106
collision_meshes.append(Artist(mesh).draw())
88107

89-
if show_acm:
108+
cached_scene["cm"] = collision_meshes
109+
110+
collision_meshes = cached_scene.get("cm", [])
111+
112+
if update_scene and show_acm:
113+
attached_meshes = []
114+
90115
for aco in scene.robot_state.attached_collision_objects:
91116
for acm in aco.to_attached_collision_meshes():
92117
frame_id = aco.object["header"]["frame_id"]
@@ -101,6 +126,13 @@ def RunScript(
101126

102127
attached_meshes.append(Artist(mesh).draw())
103128

129+
cached_scene["acm"] = attached_meshes
130+
131+
attached_meshes = cached_scene.get("acm", [])
132+
133+
cached_scene["time"] = time.time()
134+
st[cached_scene_key] = cached_scene
135+
104136
return (
105137
visual,
106138
collision,

0 commit comments

Comments
 (0)