33
44COMPAS FAB v0.27.0
55"""
6+ import time
7+
68from compas .artists import Artist
79from compas .geometry import Frame
810from compas .geometry import Transformation
9- from compas_fab .robots import PlanningScene
1011from 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
1318class 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