33
44COMPAS FAB v0.26.0
55"""
6+ from compas .artists import Artist
7+ from compas .geometry import Frame
68from compas .geometry import Transformation
7- from compas_ghpython .artists import FrameArtist
8- from compas_ghpython .artists import MeshArtist
9+ from compas_fab .robots import PlanningScene
910from ghpythonlib .componentbase import executingcomponent as component
1011
1112
1213class RobotVisualize (component ):
13- def RunScript (self , robot , group , configuration , attached_collision_meshes , show_visual , show_collision ,
14- show_frames , show_base_frame , show_end_effector_frame , show_acm ):
14+ def RunScript (
15+ self ,
16+ robot ,
17+ group ,
18+ configuration ,
19+ show_visual ,
20+ show_collision ,
21+ show_frames ,
22+ show_base_frame ,
23+ show_end_effector_frame ,
24+ show_cm ,
25+ show_acm ,
26+ ):
1527
1628 visual = None
1729 collision = None
30+ collision_meshes = None
1831 attached_meshes = None
1932 frames = None
2033 base_frame = None
2134 ee_frame = None
2235
2336 if robot :
2437 show_visual = True if show_visual is None else show_visual
38+ show_cm = True if show_cm is None else show_cm
39+ show_acm = True if show_acm is None else show_acm
2540 configuration = configuration or robot .zero_configuration ()
2641
2742 robot .update (configuration , visual = show_visual , collision = show_collision )
@@ -35,27 +50,73 @@ def RunScript(self, robot, group, configuration, attached_collision_meshes, show
3550
3651 if show_base_frame :
3752 base_compas_frame = compas_frames [0 ]
38- artist = FrameArtist (base_compas_frame )
53+ artist = Artist (base_compas_frame )
3954 base_frame = artist .draw ()
4055
4156 if show_end_effector_frame :
42- ee_compas_frame = robot .forward_kinematics (configuration , group , options = dict (solver = 'model' ))
43- artist = FrameArtist (ee_compas_frame )
57+ ee_compas_frame = robot .forward_kinematics (
58+ configuration , group , options = dict (solver = "model" )
59+ )
60+ artist = Artist (ee_compas_frame )
4461 ee_frame = artist .draw ()
4562
4663 if show_frames :
4764 frames = []
4865 for compas_frame in compas_frames [1 :]:
49- artist = FrameArtist (compas_frame )
66+ artist = Artist (compas_frame )
5067 frame = artist .draw ()
5168 frames .append (frame )
5269
53- if show_acm :
70+ if show_cm or show_acm :
71+ scene = PlanningScene (robot )
72+ scene = robot .client .get_planning_scene ()
73+
74+ collision_meshes = []
5475 attached_meshes = []
55- for acm in attached_collision_meshes :
56- frame = robot .forward_kinematics (configuration , options = dict (solver = 'model' , link = acm .link_name ))
57- T = Transformation .from_frame_to_frame (acm .collision_mesh .frame , frame )
58- mesh = acm .collision_mesh .mesh .transformed (T )
59- attached_meshes .append (MeshArtist (mesh ).draw ())
6076
61- return (visual , collision , attached_meshes , frames , base_frame , ee_frame )
77+ if show_cm :
78+ for co in scene .world .collision_objects :
79+ header = co .header
80+ frame_id = header .frame_id
81+ cms = co .to_collision_meshes ()
82+
83+ for cm in cms :
84+ if cm .frame != Frame .worldXY ():
85+ t = Transformation .from_frame (cm .frame )
86+ mesh = cm .mesh .transformed (t )
87+ else :
88+ mesh = cm .mesh
89+
90+ collision_meshes .append (Artist (mesh ).draw ())
91+
92+ if show_acm :
93+ for aco in scene .robot_state .attached_collision_objects :
94+ for acm in aco .to_attached_collision_meshes ():
95+ frame_id = aco .object .header .frame_id
96+ frame = robot .forward_kinematics (
97+ configuration , options = dict (link = frame_id )
98+ )
99+ t = Transformation .from_frame (frame )
100+
101+ # Local CM frame
102+ if (
103+ acm .collision_mesh .frame
104+ and acm .collision_mesh .frame != Frame .worldXY ()
105+ ):
106+ t = t * Transformation .from_frame (
107+ acm .collision_mesh .frame
108+ )
109+
110+ mesh = acm .collision_mesh .mesh .transformed (t )
111+
112+ attached_meshes .append (Artist (mesh ).draw ())
113+
114+ return (
115+ visual ,
116+ collision ,
117+ collision_meshes ,
118+ attached_meshes ,
119+ frames ,
120+ base_frame ,
121+ ee_frame ,
122+ )
0 commit comments