Widgets
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 Widgets Tutorial to get a better understanding of using people in Quanser Interactive Labs.
Description
Library
Constants
- QLabsWidget.CUBE = 0
- QLabsWidget.CYLINDER = 1
- QLabsWidget.SPHERE = 2
- QLabsWidget.PLASTIC_BOTTLE = 4
- QLabsWidget.METAL_CAN = 5
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.
Widgets Tutorial
Example
Raw to download this tutorial: Widgets Tutorial
.
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 QCar Cityscape.
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.widget import QLabsWidget
19
20def widgets(qlabs):
21
22 # initialize the widget class in qlabs
23 widget = QLabsWidget(qlabs)
24
25 # choose to show shadows on our objects since we aren't going to be spawning a lot of widgets
26 widget.widget_spawn_shadow(enableShadow=True)
27
28 # create 10 cubes of a variety of shades of red
29 for count in range(10):
30 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)
31
32 time.sleep(2)
33
34 # create 20 grey metal cans and place them at slightly different spots
35 for count in range(20):
36 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)
37
38 time.sleep(1)
39
40 # create 20 plastic bottles of a variety of shades of blue
41 for count in range(20):
42 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)
43
44 time.sleep(1)
45
46 # create 10 spheres of a ombre of red to yellow
47 for count in range(10):
48 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)
49
50 time.sleep(5)
51
52 # destroy widgets so that they don't continue to consume resources rolling around
53 #widget.destroy_all_spawned_widgets()
54
55def main():
56
57 # creates a server connection with Quanser Interactive Labs and manages
58 # the communications
59 qlabs = QuanserInteractiveLabs()
60
61 # initialize our desired variables
62 # note that you can use the coordinate helper to pick locations for your camera.
63 loc = [-18.783, 30.023, 2.757]
64 rot = [0, 8.932, -2.312]
65
66 # creates a server connection with Quanser Interactive Labs and manages the communications
67 qlabs = QuanserInteractiveLabs()
68
69 print("Connecting to QLabs...")
70 # trying to connect to QLabs and open the instance we have created - program will end if this fails
71 try:
72 qlabs.open("localhost")
73 except:
74 print("Unable to connect to QLabs")
75 return
76
77 # destroying any spawned actors in our QLabs that currently exist
78 qlabs.destroy_all_spawned_actors()
79
80 # create a camera in this qlabs instance
81 camera = QLabsFreeCamera(qlabs)
82
83 # add a custom camera at a specified location and rotation using degrees
84 camera.spawn_degrees(location=loc, rotation=rot)
85
86 # to switch our view from our current camera to the new camera we just initialized
87 camera.possess()
88
89 # run the code for using widgets
90 widgets(qlabs)
91
92 # close our connection to qlabs
93 qlabs.close()
94
95if __name__ == "__main__":
96 main()