How to use vpython - 10 common examples

To help you get started, we’ve selected a few vpython examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github petercorke / robotics-toolbox-python / tests / test_graphics.py View on Github external
def test_graphics_canvas_init(self):
        # Create a canvas with all options being used (different to defaults)
        scene = canvas.GraphicsCanvas3D(
            height=360,
            width=480,
            title="Test Graphics Canvas Creation",
            caption="Caption text here",
            grid=False
        )
        try:
            # Put a box in the created scene
            box(canvas=scene.scene)
        except:
            # Something went wrong
            self.assertEqual(False, True)
github petercorke / robotics-toolbox-python / tests / test_graphics.py View on Github external
def test_import_object(self):
        # Update Scene
        scene = canvas.GraphicsCanvas3D(title="Test Import Object")
        scene.grid_visibility(False)

        # Check num objects
        num_obj = len(scene.scene.objects)

        # Import an object
        graphic_obj = stl.import_object_from_numpy_stl(
            './roboticstoolbox/models/meshes/UNIMATE/puma560/link0.stl',
            scene.scene
        )

        # Verify object was added
        self.assertEqual(graphic_obj.pos, vector(0, 0, 0))  # Object is at origin
        # Can't check how many objects, as each triangle counts as one. No way to know correct amount
github petercorke / robotics-toolbox-python / tests / test_graphics.py View on Github external
def test_vpython_to_se3(self):
        # Create a scene
        scene = canvas.GraphicsCanvas3D(title="TEST VPYTHON TO SE3")

        # Create a basic entity
        # pos = 1, 2, 3
        # X = 0, 0, -1
        # Y = -1, 0, 0
        # Z = 0, 1, 0
        entity = box(
            pos=vector(1, 2, 3),
            axis=vector(0, 0, -1),
            up=vector(-1, 0, 0)
        )
        scene.scene.waitfor("draw_complete")

        # Check resulting SE3
        arr = array([
            [0, -1, 0, 1],
            [0, 0, 1, 2],
            [-1, 0, 0, 3],
            [0, 0, 0, 1]
        ])
        expected = SE3(arr)
        self.assertEqual(common.vpython_to_se3(entity), expected)
github petercorke / robotics-toolbox-python / graphics / graphics_stl.py View on Github external
stl_text = stl_file.readlines()

    # Initial Conditions
    triangles = []
    vertices = []
    normal = None

    # For every line in the file
    for line in stl_text:
        file_line = line.split()
        # If blank line (skip)
        if not file_line:
            pass
        # If a face
        elif file_line[0] == b'facet':
            normal = vec(
                float(file_line[2]),
                float(file_line[3]),
                float(file_line[4])
            )
        # If a vertex
        elif file_line[0] == b'vertex':
            vertices.append(
                vertex(
                    pos=vec(
                        float(file_line[1]),
                        float(file_line[2]),
                        float(file_line[3])
                    ),
                    normal=normal,
                    color=color.white
                )
github ethz-asl / reinmav-gym / gym_reinmav / envs / native / quadrotor3d_slungload.py View on Github external
ref_vel = self.ref_vel

		pos = np.array([state[0], state[1], state[2]]).flatten()
		att = np.array([state[3], state[4], state[5], state[6]]).flatten()
		vel = np.array([state[7], state[8], state[9]]).flatten()
		load_pos = np.array([state[10], state[11], state[12]]).flatten()
		load_vel = np.array([state[13], state[14], state[15]]).flatten()

		current_quat = Quaternion(att)
		x_axis = current_quat.rotation_matrix.dot(np.array([1.0, 0.0, 0.0]))
		y_axis = current_quat.rotation_matrix.dot(np.array([0.0, 1.0, 0.0]))
		z_axis = current_quat.rotation_matrix.dot(np.array([0.0, 0.0, 1.0]))
		tether_vec = load_pos - pos

		if self.viewer is None:
			self.viewer = canvas(title='Quadrotor 3D Slungload', width=640, height=480, center=vector(0, 0, 0), forward=vector(1, 1, -1), up=vector(0, 0, 1), background=color.white)
			self.render_quad1 = box(canvas = self.viewer, pos=vector(pos[0],pos[1],0), axis=vector(x_axis[0],x_axis[1],x_axis[2]), length=0.2, height=0.05, width=0.05)
			self.render_quad2 = box(canvas = self.viewer, pos=vector(pos[0],pos[1],0), axis=vector(y_axis[0],y_axis[1],y_axis[2]), length=0.2, height=0.05, width=0.05)
			self.render_rotor1 = cylinder(canvas = self.viewer, pos=vector(pos[0],pos[1],0), axis=vector(0.01*z_axis[0],0.01*z_axis[1],0.01*z_axis[2]), radius=0.2, color=color.cyan, opacity=0.5)
			self.render_rotor2 = cylinder(canvas = self.viewer, pos=vector(pos[0],pos[1],0), axis=vector(0.01*z_axis[0],0.01*z_axis[1],0.01*z_axis[2]), radius=0.2, color=color.cyan, opacity=0.5)
			self.render_rotor3 = cylinder(canvas = self.viewer, pos=vector(pos[0],pos[1],0), axis=vector(0.01*z_axis[0],0.01*z_axis[1],0.01*z_axis[2]), radius=0.2, color=color.cyan, opacity=0.5)
			self.render_rotor4 = cylinder(canvas = self.viewer, pos=vector(pos[0],pos[1],0), axis=vector(0.01*z_axis[0],0.01*z_axis[1],0.01*z_axis[2]), radius=0.2, color=color.cyan, opacity=0.5)
			self.render_velocity = pointer = arrow(pos=vector(pos[0],pos[1],0), axis=vector(vel[0],vel[1],vel[2]), shaftwidth=0.05, color=color.green)
			self.render_ref = sphere(canvas = self.viewer, pos=vector(ref_pos[0], ref_pos[1], ref_pos[2]), radius=0.02, color=color.blue, make_trail = True)
			self.render_tether = cylinder(canvas = self.viewer, pos=vector(pos[0],pos[1],0), axis=vector(tether_vec[0],tether_vec[1],tether_vec[2]), radius=0.01, color=color.black)
			self.render_load = sphere(canvas = self.viewer, pos=vector(load_pos[0], load_pos[1], load_pos[2]), radius=0.1, color=color.red, make_trail = True)
		if self.state is None: return None

		self.render_quad1.pos.x = pos[0]
		self.render_quad1.pos.y = pos[1]
		self.render_quad1.pos.z = pos[2]
		self.render_quad2.pos.x = pos[0]
github petercorke / robotics-toolbox-python / graphics / graphics_grid.py View on Github external
min_z_coord = z_origin + int(-(self.__num_squares / 2) + (sign(camera_axes.z) * -1) * (self.__num_squares / 2))
        max_z_coord = z_origin + int((self.__num_squares / 2) + (sign(camera_axes.z) * -1) * (self.__num_squares / 2))

        # XZ plane
        for x_point in range(min_x_coord, max_x_coord + 1):
            # Draw a line across for each x coord, along the same y-axis, from min to max z coord
            xz_lines.append(create_line(
                vector(x_point, y_origin, min_z_coord),
                vector(x_point, y_origin, max_z_coord),
                self.__scene
            ))
        for z_point in range(min_z_coord, max_z_coord + 1):
            # Draw a line across each z coord, along the same y-axis, from min to max z coord
            xz_lines.append(create_line(
                vector(min_x_coord, y_origin, z_point),
                vector(max_x_coord, y_origin, z_point),
                self.__scene
            ))

        # XY plane
        for x_point in range(min_x_coord, max_x_coord + 1):
            # Draw a line across each x coord, along the same z-axis, from min to max y coord
            xy_lines.append(create_line(
                vector(x_point, min_y_coord, z_origin),
                vector(x_point, max_y_coord, z_origin),
                self.__scene
            ))
        for y_point in range(min_y_coord, max_y_coord + 1):
            # Draw a line across each y coord, along the same z-axis, from min to max x coord
            xy_lines.append(create_line(
                vector(min_x_coord, y_point, z_origin),
                vector(max_x_coord, y_point, z_origin),
github ethz-asl / reinmav-gym / gym_reinmav / envs / native / quadrotor3d.py View on Github external
def make_grid(unit, n):
			nunit = unit * n
			for i in range(n+1):
				if i%5==0:
					lcolor = vector(0.5,0.5,0.5)
				else:
					lcolor = vector(0.5, 0.5, 0.5)
				curve(pos=[(0,i*unit,0), (nunit, i*unit, 0)],color=lcolor)
				curve(pos=[(i*unit,0,0), (i*unit, nunit, 0)],color=lcolor)
				curve(pos=[(0,-i*unit,0), (-nunit, -i*unit, 0)],color=lcolor)
				curve(pos=[(-i*unit,0,0), (-i*unit, -nunit, 0)],color=lcolor)
				curve(pos=[(0,i*unit,0), (-nunit, -i*unit, 0)],color=lcolor)
				curve(pos=[(i*unit,0,0), (-i*unit, -nunit, 0)],color=lcolor)
github petercorke / robotics-toolbox-python / graphics / graphics_grid.py View on Github external
vector(max_x_coord, y_origin, z_point),
                self.__scene
            ))

        # XY plane
        for x_point in range(min_x_coord, max_x_coord + 1):
            # Draw a line across each x coord, along the same z-axis, from min to max y coord
            xy_lines.append(create_line(
                vector(x_point, min_y_coord, z_origin),
                vector(x_point, max_y_coord, z_origin),
                self.__scene
            ))
        for y_point in range(min_y_coord, max_y_coord + 1):
            # Draw a line across each y coord, along the same z-axis, from min to max x coord
            xy_lines.append(create_line(
                vector(min_x_coord, y_point, z_origin),
                vector(max_x_coord, y_point, z_origin),
                self.__scene
            ))

        # YZ plane
        for y_point in range(min_y_coord, max_y_coord + 1):
            # Draw a line across each y coord, along the same x-axis, from min to max z coord
            yz_lines.append(create_line(
                vector(x_origin, y_point, min_z_coord),
                vector(x_origin, y_point, max_z_coord),
                self.__scene
            ))
        for z_point in range(min_z_coord, max_z_coord + 1):
            # Draw a line across each z coord, along the same x-axis, from min to max y coord
            yz_lines.append(create_line(
                vector(x_origin, min_y_coord, z_point),
github liuxingzhi / pyfun / ElectricField / charge.py View on Github external
Q = 1e-8  # total charges
    # dq = 1e-8 / 6  # define charge of electrons
    dq = Q / num_charge
    charges = []
    space_between = 2 * view_space_length / (num_charge + 1)  # evenly divide space between each electrons
    for x in arange(-view_space_length + space_between, view_space_length, space_between):
        q = ElectricBall(pos=vector(x, 0, 0), radius=1, color=color.red, charge=dq)
        charges.append(q)

    # creat arrow around electric balls
    arrows = []
    observe_points_dis = 0.5 * view_space_length
    for x in arange(-1.5 * view_space_length, 1.51 * view_space_length, observe_points_dis):
        for y in arange(-1.0 * view_space_length, 1.01 * view_space_length, observe_points_dis):
            for z in arange(-1.0 * view_space_length, 1.01 * view_space_length, observe_points_dis):
                pointer = arrow(pos=vector(x, y, z), color=color.blue, opacity=0.0)
                electrical_vector = vector(0, 0, 0)

                # infinity large arrows will be ignored
                infinity = False

                # each arrow is affected by all of the charges
                for q in charges:
                    direction_vector = pointer.pos - q.pos
                    if direction_vector.mag == 0:
                        infinity = True
                        break
                    # calculate electric field affected by each electric ball
                    E = (k * dq / direction_vector.mag ** 2) * direction_vector.norm()
                    # sum electric field at the each point
                    electrical_vector += E
github liuxingzhi / pyfun / ElectricField / charge.py View on Github external
def draw_electrical_field(num_charge):
    scene = canvas(title='electric field of a list of charges', width=800, height=600, background=color.magenta)
    # dx = L / num_charge
    Q = 1e-8  # total charges
    # dq = 1e-8 / 6  # define charge of electrons
    dq = Q / num_charge
    charges = []
    space_between = 2 * view_space_length / (num_charge + 1)  # evenly divide space between each electrons
    for x in arange(-view_space_length + space_between, view_space_length, space_between):
        q = ElectricBall(pos=vector(x, 0, 0), radius=1, color=color.red, charge=dq)
        charges.append(q)

    # creat arrow around electric balls
    arrows = []
    observe_points_dis = 0.5 * view_space_length
    for x in arange(-1.5 * view_space_length, 1.51 * view_space_length, observe_points_dis):
        for y in arange(-1.0 * view_space_length, 1.01 * view_space_length, observe_points_dis):
            for z in arange(-1.0 * view_space_length, 1.01 * view_space_length, observe_points_dis):
                pointer = arrow(pos=vector(x, y, z), color=color.blue, opacity=0.0)
                electrical_vector = vector(0, 0, 0)

                # infinity large arrows will be ignored
                infinity = False

                # each arrow is affected by all of the charges
                for q in charges: