Widgets
Description
Widgets work slightly differently then the other objects in the QLabs Workspaces. They are special actors that cannot be addressed after they have been spawned; therefore they cannot be deleted individually, they do not support parenting, and all actor properties must be set when they are spawned. The advantage of widgets is that they are highly efficient dynamic actors so it is possible to spawn thousands of widgets while maintaining performance.
In addition to the visible properties of widgets, widget actors can also individually contain invisible properties such as mass, a numerical ID tag, and a general purpose properties string. Some actors that are designed to interact with widgets include class methods to read these properties.
If you need the ability to make shapes static or the ability to address, modify, or parent shapes, see Basic Shapes instead.
See the Widget Tutorial to get a better understanding of using people in Quanser Interactive Labs.
Library
Constants
Tip
See Widget Tutorial to see the different flooring options.
- QLabsWidget.CUBE = 0
See configurations
- QLabsWidget.CYLINDER = 1
See configurations
- QLabsWidget.SPHERE = 2
See configurations
- QLabsWidget.PLASTIC_BOTTLE = 4
See configurations
- QLabsWidget.METAL_CAN = 5
See configurations
Methods
- QLabsWidget.__init__(qlabs, verbose=False)[source]
Constructor Method
- Parameters
qlabs (object) – A QuanserInteractiveLabs object
verbose (boolean) – (Optional) Print error information to the console.
- QLabsWidget.spawn(location, rotation, scale, configuration, color=[1, 1, 1], measuredMass=0, IDTag=0, properties='', waitForConfirmation=True)[source]
Spawns a widget in an instance of QLabs at a specific location and rotation using radians.
- Parameters
location (float array[3]) – An array of floats for x, y and z coordinates.
rotation (float array[3]) – An array of floats for the roll, pitch, and yaw in radians.
scale (float array[3]) – An array of floats for the scale in the x, y, and z directions.
configuration (uint32) – See configuration options
color (float array[3]) – Red, Green, Blue components of the RGB color on a 0.0 to 1.0 scale.
measuredMass (float) – A float value for use with mass sensor instrumented actors. This does not alter the dynamic properties.
IDTag (uint8) – An integer value for use with IDTag sensor instrumented actors.
properties (string) – A string for use with properties sensor instrumented actors. This can contain any string that is available for use to parse out user-customized parameters.
waitForConfirmation (boolean) – (Optional) Make this operation blocking until confirmation of the spawn has occurred.
- Returns
status - True if successful, False otherwise
- Return type
boolean
- QLabsWidget.spawn_degrees(location, rotation, scale, configuration, color=[1, 1, 1], measuredMass=0, IDTag=0, properties='', waitForConfirmation=True)[source]
Spawns a widget in an instance of QLabs at a specific location and rotation using degrees.
- Parameters
location (float array[3]) – An array of floats for x, y and z coordinates.
rotation (float array[3]) – An array of floats for the roll, pitch, and yaw in degrees.
scale (float array[3]) – An array of floats for the scale in the x, y, and z directions.
configuration (uint32) – See configuration options.
color (float array[3]) – Red, Green, Blue components of the RGB color on a 0.0 to 1.0 scale.
measuredMass (float) – A float value for use with mass sensor instrumented actors. This does not alter the dynamic properties.
IDTag (uint8) – An integer value for use with IDTag sensor instrumented actors.
properties (string) – A string for use with properties sensor instrumented actors. This can contain any string that is available for use to parse out user-customized parameters.
waitForConfirmation (boolean) – (Optional) Make this operation blocking until confirmation of the spawn has occurred.
- Returns
status - True if successful, False otherwise
- Return type
boolean
- QLabsWidget.destroy_all_spawned_widgets()[source]
Destroys all spawned widgets, but does not destroy actors.
- Returns
True if successful, False otherwise
- Return type
boolean
- QLabsWidget.widget_spawn_shadow(enableShadow=True)[source]
If spawning a large number of widgets causes performance degradation, you can try disabling the widget shadows. This function must be called in advance of widget spawning and all subsequence widgets will be spawned with the specified shadow setting.
- Parameters
enableShadow (boolean) – (Optional) Show (True) or hide (False) widget shadows.
- Returns
True if successful, False otherwise
- Return type
boolean
Configurations
There are 5 different types of widgets that can be spawned in the widgets class.
0 - Cube
1 - Cylinder
2 - Sphere
4 - Plastic Bottle
5 - Metal Can
Connection Points
There are no connection points for this actor class.
Widget Tutorial
Python Tutorial
Raw to download this tutorial: Widgets Tutorial (.py).
1"""
2Widget Library Example
3----------------------
4
5.. note::
6
7 Make sure you have Quanser Interactive Labs open before running this
8 example. This example is designed to best be run in Cityscape or Plane.
9
10"""
11# imports to important libraries
12import sys
13import math
14import time
15
16from qvl.qlabs import QuanserInteractiveLabs
17from qvl.free_camera import QLabsFreeCamera
18from qvl.system import QLabsSystem
19from qvl.widget import QLabsWidget
20
21def create_widgets(qlabs):
22
23 # Create the widget class object in qlabs
24 widget = QLabsWidget(qlabs)
25
26 # Hide the shadowns on our first stack of objects. This is a global state that is preserved
27 # for all new widgets spawned. Shadows are expensive and can be disabled if spawning a
28 # large number of widgets (thousands).
29 widget.widget_spawn_shadow(enableShadow=False)
30
31 # Create 10 cubes of a variety of shades of red
32 for count in range(10):
33 widget.spawn_degrees(location = [-11.000, 30.000+count*0.01, 1+count*0.6], rotation = [90,0,0], scale = [0.5,0.5,0.5], configuration = widget.CUBE, color = [1,0+count*0.03,0+count*0.02], measuredMass=0, IDTag=0, properties='', waitForConfirmation=True)
34
35 # Re-enable shadows for widgets
36 widget.widget_spawn_shadow(enableShadow=True)
37
38 # Create 20 grey metal cans and place them at slightly different spots
39 for count in range(20):
40 widget.spawn(location = [-11.000, 29.000, 1+count*0.2], rotation = [0,0,0], scale = [1,1,1], configuration = widget.METAL_CAN, color = [1,1,1], measuredMass=0, IDTag=0, properties='', waitForConfirmation=True)
41
42 time.sleep(1)
43
44 # create 20 plastic bottles of a variety of shades of blue
45 for count in range(20):
46 widget.spawn_degrees(location = [-11.000, 29.000, 1+count*0.2], rotation = [90,0,0], scale = [1,1,1], configuration = widget.PLASTIC_BOTTLE, color = [count*0.01 ,count*0.02, 1], measuredMass=0, IDTag=0, properties='', waitForConfirmation=True)
47
48 time.sleep(1)
49
50 # create 10 spheres of a ombre of red to yellow
51 for count in range(10):
52 widget.spawn_degrees(location = [-11.000, 31.000+count*0.01, 1+count*0.6], rotation = [90,0,0], scale = [0.5,0.5,0.5], configuration = widget.SPHERE, color = [1,0+ count*0.05,0+ count*0.01], measuredMass=1000, IDTag=0, properties='', waitForConfirmation=True)
53
54
55def main():
56 # Initialize our desired variables
57 # Note that you can use the coordinate helper to pick locations for your camera.
58 loc = [-18.783, 30.023, 2.757]
59 rot = [0, 8.932, -2.312]
60
61 # Creates a server connection with Quanser Interactive Labs and manages the communications
62 qlabs = QuanserInteractiveLabs()
63
64 print("Connecting to QLabs...")
65 if (not qlabs.open("localhost")):
66 print("Unable to connect to QLabs")
67 return
68
69 print("Connected")
70
71 # Destroying any spawned actors in our QLabs that currently exist
72 qlabs.destroy_all_spawned_actors()
73
74 # Create a camera in this qlabs instance
75 camera = QLabsFreeCamera(qlabs)
76
77 # Add a custom camera at a specified location and rotation using degrees
78 camera.spawn_degrees(location=loc, rotation=rot)
79
80 # To switch our view from our current camera to the new camera we just initialized
81 camera.possess()
82
83 # Run the code for using widgets
84 create_widgets(qlabs)
85
86 # Close qlabs
87 qlabs.close()
88 print('Done!')
89
90if __name__ == "__main__":
91 main()
Matlab Tutorial
Raw to download this tutorial: Widgets Tutorial (.m).
1% Widget Library Example
2% -------------------------
3%
4% .. note::
5%
6% Make sure you have Quanser Interactive Labs open before running this
7% example. This example is designed to best be run in QCar Cityscape
8
9close all;
10clear all;
11clc;
12
13% --------------------------------------------------------------
14% Setting MATLAB Path for the libraries
15% Always keep at the start, it will make sure it finds the correct references
16
17newPathEntry = fullfile(getenv('QAL_DIR'), '0_libraries', 'matlab', 'qvl');
18pathCell = regexp(path, pathsep, 'split');
19if ispc % Windows is not case-sensitive
20 onPath = any(strcmpi(newPathEntry, pathCell));
21else
22 onPath = any(strcmp(newPathEntry, pathCell));
23end
24
25if onPath == 0
26 path(path, newPathEntry)
27 savepath
28end
29% --------------------------------------------------------------
30
31% Creates a server connection with Quanser Interactive Labs and manages the communications
32qlabs = QuanserInteractiveLabs();
33connection_established = qlabs.open('localhost');
34
35if connection_established == false
36 disp("Failed to open connection.")
37 return
38end
39
40disp('Connected')
41
42num_destroyed = qlabs.destroy_all_spawned_actors();
43fprintf('%d actors destroyed', num_destroyed);
44
45main(qlabs);
46
47qlabs.close()
48disp('Done!')
49
50% ------------ functions ----------
51
52function widgets(qlabs)
53 % Initialize the widget class in qlabs
54 widget = QLabsWidget(qlabs);
55
56 % Enable shadows for objects
57 widget.widget_spawn_shadow(true);
58
59 % Create 10 cubes of varying shades of red
60 for count = 0:9
61 widget.spawn_degrees([-11.000, 30.000 + count*0.01, 1 + count*0.6], [90, 0, 0], [0.5, 0.5, 0.5], ...
62 widget.CUBE, [1, 0 + count*0.03, 0 + count*0.02], 0, 0, '', 1);
63 end
64
65 pause(1);
66
67 % Create 20 grey metal cans at different locations
68 % the constants can come from both the object or directly from QLabsWidget
69 for count = 0:19
70 widget.spawn([-11.000, 29.000, 1 + count*0.2], [0, 0, 0], [1, 1, 1], ...
71 QLabsWidget.METAL_CAN, [1, 1, 1], 0, 0, '', 1);
72 end
73
74 pause(1);
75
76 % Create 20 plastic bottles of varying shades of blue
77 for count = 0:19
78 widget.spawn_degrees([-11.000, 29.000, 1 + count*0.2], [90, 0, 0], [1, 1, 1], ...
79 widget.PLASTIC_BOTTLE, [count*0.01, count*0.02, 1], 0, 0, '', 1);
80 end
81
82 pause(1);
83
84 % Create 10 spheres of red to yellow gradient
85 for count = 0:9
86 widget.spawn_degrees([-11.000, 31.000 + count*0.01, 1 + count*0.6], [90, 0, 0], [0.5, 0.5, 0.5], ...
87 widget.SPHERE, [1, 0 + count*0.05, 0 + count*0.01], 1000, 0, '', 1);
88 end
89
90 pause(5);
91
92 % Destroy all spawned widgets
93 % widget.destroy_all_spawned_widgets(); % Uncomment this line if destroy function is defined
94end
95
96function main(qlabs)
97
98 % Initialize desired camera location and rotation
99 loc = [-18.783, 30.023, 2.757];
100 rot = [0, 8.932, -2.312];
101
102 % Create a camera in this qlabs instance
103 camera = QLabsFreeCamera(qlabs);
104
105 % Add a custom camera at a specified location and rotation using degrees
106 camera.spawn_degrees(loc, rot);
107
108 % Switch view to the new camera
109 camera.possess();
110
111 % Run the code for using widgets
112 widgets(qlabs);
113
114 % Close qlabs
115 qlabs.close();
116 disp('Done!');
117
118end