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 ):
@@ -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