def __init__(self): self.black = vpy.vector(0.2, 0.2, 0.2) self.light = vpy.vector(0.7, 0.7, 0.7) self.red = vpy.vector(1, 0.1, 0) self.orange = vpy.vector(1, 0.5, 0) self.green = vpy.vector(0.5, 1, 0.3) self.blue = vpy.vector(0.3, 0.5, 1) self.white = vpy.vector(1, 1, 1) self.yellow = vpy.vector(1, 1, 0) self.faceColors = [ self.green, self.blue, self.yellow, self.white, self.red, self.orange ] self.zero = vpy.vector(0, 0, 0) self.canvas = vpy.canvas(background=vpy.vector(0.8, 0.8, 0.8)) # self.canvas.lights = [] # vpy.distant_light(direction=vpy.vector( 1, 0, 0), color=self.light) # vpy.distant_light(direction=vpy.vector(-1, 0, 0), color=self.light) vpy.distant_light(direction=vpy.vector(0, 1, 0), color=self.light) # vpy.distant_light(direction=vpy.vector( 0,-1, 0), color=self.light) # vpy.distant_light(direction=vpy.vector( 0, 0, 1), color=self.light) # vpy.distant_light(direction=vpy.vector( 0, 0,-1), color=self.light) self.faceIndexDict = {"t": 3, "g": 2, "f": 1, "b": 0, "r": 5, "l": 4} self.genCube()
def create_canvas(): """ create a vpython canvas with a few light sources """ canvas = vpy.canvas(background=vpy.vector(0.8, 0.8, 0.8), up=vpy.vector(0, 0, 1), forward=vpy.vector(-1, 0, 0)) canvas.lights = [] canvas.ambient = vpy.vec(1, 1, 1) * 0.3 vpy.distant_light(direction=vpy.vector(1, 1, 1), color=vpy.vector(0.7, 0.7, 0.7)) vpy.distant_light(direction=vpy.vector(-1, -1, -1), color=vpy.vector(0.7, 0.7, 0.7)) return canvas
def _init_anim(self): import vpython as vp # Convert to float for VPython Lr = float(self.domain_param['Lr']) Lp = float(self.domain_param['Lp']) # Init render objects on first call self._anim['canvas'] = vp.canvas(width=800, height=600, title="Quanser Qube") scene_range = 0.2 arm_radius = 0.003 pole_radius = 0.0045 self._anim['canvas'].background = vp.color.white self._anim['canvas'].lights = [] vp.distant_light(direction=vp.vec(0.2, 0.2, 0.5), color=vp.color.white) self._anim['canvas'].up = vp.vec(0, 0, 1) self._anim['canvas'].range = scene_range self._anim['canvas'].center = vp.vec(0.04, 0, 0) self._anim['canvas'].forward = vp.vec(-2, 1.2, -1) vp.box(pos=vp.vec(0, 0, -0.07), length=0.09, width=0.1, height=0.09, color=vp.color.gray(0.5)) vp.cylinder(axis=vp.vec(0, 0, -1), radius=0.005, length=0.03, color=vp.color.gray(0.5)) # Joints self._anim['joint1'] = vp.sphere(radius=0.005, color=vp.color.white) self._anim['joint2'] = vp.sphere(radius=pole_radius, color=vp.color.white) # Arm self._anim['arm'] = vp.cylinder(radius=arm_radius, length=Lr, color=vp.color.blue) # Pole self._anim['pole'] = vp.cylinder(radius=pole_radius, length=Lp, color=vp.color.red) # Curve self._anim['curve'] = vp.curve(color=vp.color.white, radius=0.0005, retain=2000)
def __init__(self, cube): faces = cube.faces # scene and lights definition self.scene = v.canvas(background=GRAY, width=500, height=500) self.scene.range = 2 self.scene.ambient = WHITE lights = [] lights.append( v.distant_light(direction=v.vector(0.22, 0.44, -0.88), color=WHITE)) lights.append( v.distant_light(direction=v.vector(-0.22, -0.44, 0.88), color=WHITE)) self.scene.lights + lights # corner pieces definition self.corners = {} self.corners["0"] = Corner(self.scene, "0", faces["F"][0], faces["L"][1], faces["U"][2]) self.corners["1"] = Corner(self.scene, "1", faces["B"][1], faces["L"][0], faces["U"][0]) self.corners["2"] = Corner(self.scene, "2", faces["B"][0], faces["R"][1], faces["U"][1]) self.corners["3"] = Corner(self.scene, "3", faces["F"][1], faces["R"][0], faces["U"][3]) self.corners["4"] = Corner(self.scene, "4", faces["F"][2], faces["L"][3], faces["D"][0]) self.corners["5"] = Corner(self.scene, "5", faces["B"][3], faces["L"][2], faces["D"][2]) self.corners["6"] = Corner(self.scene, "6", faces["B"][2], faces["R"][3], faces["D"][3]) self.corners["7"] = Corner(self.scene, "7", faces["F"][3], faces["R"][2], faces["D"][1]) # initialize positios self.pos = {} self.pos["0"] = "0" self.pos["1"] = "1" self.pos["2"] = "2" self.pos["3"] = "3" self.pos["4"] = "4" self.pos["5"] = "5" self.pos["6"] = "6" self.pos["7"] = "7"
""" import vpython as vp from numpy import pi scene = vp.canvas(title='Paha paikka', x=0, y=0, width=1280, height=768, center=vp.vector(0, 0, 0), background=vp.vector(0, 0, 0)) # Reset lightning scene.lights = [] vp.distant_light(direction=vp.vec(0.0, 1, 0.0), color=vp.vector(1, 1, 1)) vp.local_light(pos=vp.vec(0.0, 700, 0.0), color=vp.vector(1, 1, 1)) ## Create table #table = vp.box(pos=vp.vec(0,-15,0) # , length = 1400 # , height = 10 # , width = 1400 # , color=vp.vec(0,103/255,1/255) # , texture={'file':'./snooker_texture.jpg','place':'all'} # ) #table.shininess=10000 def draw_ball(pt, radius, color): vp.sphere(pos=vp.vector(pt[0], 0, pt[1]), radius=radius,
the radius of the drawn sphere returns: -------- None outputs: -------- draws a vpython cone for the given point """ if radius == None: radius = point_dict["size"]/50 if not isinstance(point_dict["coords"], vpy.vector): dict_to_vpy(point_dict) vpy.cone(pos=point_dict["coords"], color=point_dict["vpy_color"], radius=radius, axis=PUZZLE_COM-point_dict["coords"]) if __name__ == "__main__": canvas = vpy.canvas(background = vpy.vector(0.8,0.8,0.8), up = vpy.vector(0,0,1), forward = vpy.vector(-1,0,0)) canvas.lights = [] vpy.distant_light(direction = vpy.vector( 1, 1, 1), color = vpy.vector(0.7,0.7,0.7)) vpy.distant_light(direction = vpy.vector(-1,-1,-1), color = vpy.vector(0.7,0.7,0.7)) # draw_points(get_point_dicts("mixup cube points.ggb")) draw_points(get_point_dicts("rubiks cube points.ggb"))
------- raises a ValueError if obj is not in object_list """ for i, test_obj in enumerate(object_list): if test_obj == obj: return i raise ValueError(f"'object' {obj} not in 'object_list'") if __name__=="__main__": def genCubes(): """ generate 8 cubes around (0,0,0) each cube has sidelength 0.5 """ offset = vpy.vector(.5, .5, .5) size = vpy.vector(.2,.2,.2) vpy.box(pos = vpy.vector(0,0,0)-offset, color = vpy.vector(0.3,0.5,1), size = size) vpy.box(pos = vpy.vector(0,0,1)-offset, color = vpy.vector(0.3,0.5,1), size = size) vpy.box(pos = vpy.vector(0,1,0)-offset, color = vpy.vector(0.3,0.5,1), size = size) vpy.box(pos = vpy.vector(0,1,1)-offset, color = vpy.vector(0.3,0.5,1), size = size) vpy.box(pos = vpy.vector(1,0,0)-offset, color = vpy.vector(0.3,0.5,1), size = size) vpy.box(pos = vpy.vector(1,0,1)-offset, color = vpy.vector(0.3,0.5,1), size = size) vpy.box(pos = vpy.vector(1,1,0)-offset, color = vpy.vector(0.3,0.5,1), size = size) vpy.box(pos = vpy.vector(1,1,1)-offset, color = vpy.vector(0.3,0.5,1), size = size) canvas = vpy.canvas(background = vpy.vector(0.8,0.8,0.8)) canvas.lights = [] vpy.distant_light(direction = vpy.vector(-1, 0.8, 1.5), color = vpy.vector(0.7,0.7,0.7)) genCubes()
import vpython as vpy import time canvas = vpy.canvas(background=vpy.vector(1, 1, 1)) #white background canvas.lights = [] # vpy.distant_light( direction=vpy.vector(-1,-1,-1), color=vpy.vector(1,0.8,0.5) ) vpy.distant_light(direction=vpy.vector(-1, 0, 0), color=vpy.vector(1, 0.8, 0.5)) boxColor = vpy.vector(1, 0.5, 0) boxHighlightColor = vpy.vector(1, 0.8, 0.5) box = vpy.box(pos=vpy.vector(0, 0, 0), color=boxColor, opacity=0.5, shininess=0) sphere1 = vpy.sphere(pos=vpy.vector(0, 1.5, 0), color=vpy.vector(1, 1, 1), radius=0.8) sphere2 = vpy.sphere(pos=vpy.vector(0, -1.5, 0), color=vpy.vector(0.3, 0.5, 1), radius=0.5, make_trail=True, retain=20, trail_color=vpy.vector(0.5, 1, 0.3), trail_radius=0.05) sphere1.direction = vpy.vector(-1, 1, 0) vpy.attach_arrow(sphere1, "direction", scale=1.5, shaftwidth=0.1)
########################################################################################################################## HERE THE PROGRAM ENDS ########################################################################################################################## HERE THE PROGRAM ENDS ########################################################################################################################## HERE THE PROGRAM ENDS ########################################################################################################################## HERE THE PROGRAM ENDS if toggle_3D == True: import vpython as vp #Creates the window scene = vp.canvas(width=1280, height=720, center=vp.vector(0, 0, 0), background=vp.color.white) scene.lights = [] vp.distant_light(direction=vp.vector(1, 1, -1), color=vp.color.gray(0.9)) t = 0 i = 0 #floor dim_x_floor = 1000 dim_z_floor = 1000 vp.box(pos=vp.vector(dim_x_floor / 2, -0.5, dim_z_floor / 2), size=vp.vector(dim_x_floor, 1, dim_z_floor), texture={'file': 'grass_texture.jpg'}) # Sky vp.box(pos=vp.vector(dim_x_floor / 2, dim_z_floor / 2, dim_z_floor / 2 + 20 * 2), size=vp.vector(dim_x_floor, dim_z_floor, 1),
site_radius = 0.1 link_radius = 0.05 # Site and link colors in RGB (decimal) values link_color = vector(.26, .75, .96) site_color = vector(.14, .03, .96) # Canvas and background color scene = canvas(title='Lattice', width=1000, height=700, center=vector(0,0,0), background=color.white) # Lighting scene.lights = [] distant_light(direction=vector(2,2,2), color=color.gray(0.9)) # Generate the lattice for i in range(lattice_size + 1): for j in range(lattice_size + 1): for k in range(lattice_size + 1): sphere(pos=vector(i,j,k),radius=site_radius, color=site_color) cylinder(pos=vector(i,j,k), axis=vector(1,0,0), radius=link_radius, color=link_color) cylinder(pos=vector(i,j,k), axis=vector(0,1,0), radius=link_radius, color=link_color) cylinder(pos=vector(i,j,k), axis=vector(0,0,1), radius=link_radius, color=link_color) # Create origin/axes arrow(pos=vector(-1,-1,-1), axis=vector(0,0,1), radius=.1, color=color.green) arrow(pos=vector(-1,-1,-1), axis=vector(0,1,0), radius=.1, color=color.green) arrow(pos=vector(-1,-1,-1), axis=vector(1,0,0), radius=.1, color=color.green) label(pos=vector(0,-1,-1), text='x')
# modify init axis upper_arm_main_axis = (joint_anchor.pos-shoulder_anchor.pos).norm() upper_arm_sub_axis = upper_arm_main_axis.cross(front).cross( upper_arm_main_axis).norm() small_arm_main_axis = (hand_anchor.pos-joint_anchor.pos).norm() small_arm_sub_axis = small_arm_main_axis.cross(front).cross( small_arm_main_axis).norm() [e.rotate(origin=joint_anchor.pos, axis=small_arm_main_axis, angle=radians(-80)) for e in [small_arm]] [e.rotate(origin=joint_anchor.pos, axis=small_arm_sub_axis, angle=radians(20)) for e in [small_arm, hand_anchor]+joint_axis] small_arm_main_axis = (hand_anchor.pos-joint_anchor.pos).norm() small_arm_sub_axis = small_arm_main_axis.cross(front).cross( small_arm_main_axis).norm() distant_light(direction=up*10-front*2, color=color.gray(0.8)) print('Click scene to continue') scene.waitfor('click keydown') def motion_backing(): m1 = motion_backing_upper_arm() if m1: return True m2 = motion_backing_small_arm() return any([m1, m2]) def motion_backing_upper_arm(angle=radians(2)): # return False, if we are fine
def turtle_interpretation_3D(scene, instructions, delta=22.5, width=0.3, widthScaling=0.7, tropismVec=vector(0, 0, 0), tropismStrength=0): """Interprets a set of instructions for a turtle to draw a tree in 3D. """ ############ INITIALIZATION ############# bob = Turtle3D(width=width) turtlePosStack = [] turtleHeadStack = [] turtleXmax = 0 turtleXmin = 0 turtleYmax = 0 turtleYmin = 0 turtleZmax = 0 turtleZmin = 0 ######### MAIN FUNCTION ############## for mod in instructions: if mod.symbol == "F": bob.applyTropism(tropismVec, tropismStrength) if mod.param == []: bob.forward() else: bob.forward(mod.param[0]) #Note that we only need to update the max coordinates if the turtle has moved turtleXmax = max(turtleXmax, bob.xcor()) turtleXmin = min(turtleXmin, bob.xcor()) turtleYmax = max(turtleYmax, bob.ycor()) turtleYmin = min(turtleYmin, bob.ycor()) turtleZmax = max(turtleZmax, bob.zcor()) turtleZmin = min(turtleZmin, bob.zcor()) elif mod.symbol == "f": bob.penUp() if mod.param == []: bob.forward() else: bob.forward(mod.param[0]) bob.penDown() elif mod.symbol == "+": if mod.param == []: bob.turnLeft(delta) else: bob.turnLeft(mod.param[0]) elif mod.symbol == "-": if mod.param == []: bob.turnRight(delta) else: bob.turnRight(mod.param[0]) elif mod.symbol == "&": if mod.param == []: bob.pitchDown(delta) else: bob.pitchDown(mod.param[0]) elif mod.symbol == "^": if mod.param == []: bob.pitchUp(delta) else: bob.pitchUp(mod.param[0]) elif mod.symbol == "\\": if mod.param == []: bob.rollLeft(delta) else: bob.rollLeft(mod.param[0]) elif mod.symbol == "/": if mod.param == []: bob.rollRight(delta) else: bob.rollRight(mod.param[0]) elif mod.symbol == "|": bob.turnLeft(180) elif mod.symbol == "[": turtlePosStack.insert(0, bob.getPosition()) turtleHeadStack.insert(0, bob.getHeading()) bob.pushCurve() elif mod.symbol == "]": bob.setPosition(turtlePosStack.pop(0)) bob.setHeading(turtleHeadStack.pop(0)) bob.popCurve() elif mod.symbol == "!": if mod.param == []: bob.decrementDiameter(widthScaling) else: bob.setWidth(mod.param[0]) elif mod.symbol == "{": bob.startPolygon() elif mod.symbol == "}": bob.endPolygon() elif mod.symbol == "'": bob.setColor(mod.param) #point the camera to the center of our drawing xcenter = 0 ycenter = (turtleYmax + turtleYmin) / 2 zcenter = 0 scene.center = vector(xcenter, ycenter, zcenter) # draw a ground plane bob.drawGround(max(turtleXmax, turtleZmax) * 2) # set lighting scene.lights = [] # reset default lighting scene.ambient = color.gray(0.3) distant_light(direction=vector(0.22, 0.44, 0.88), color=color.gray(0.7)) distant_light(direction=vector(-0.88, -0.22, -0.44), color=color.gray(0.3)) distant_light(direction=vector(-0.22, -0.44, -0.88), color=color.gray(0.7)) distant_light(direction=vector(0.88, 0.22, 0.44), color=color.gray(0.3)) return 0
# # Andrew Williams, 2002 # # Toggle 'pause' mode with the space bar key. import vpython from vpython import vector as v import math import time win = 700 scene = vpython.canvas(title="Moon's Orbit", width=win, height=win) scene.lights = [] scene.ambient = vpython.color.gray(0.1) sunlight = [ vpython.distant_light(direction=v(-1, 0, 0), color=v(1.0, 1.0, 1.0)), vpython.distant_light(direction=v(-1, 0, 0), color=v(1.0, 1.0, 1.0)), vpython.distant_light(direction=v(-1, 0, 0), color=v(1.0, 1.0, 1.0)) ] starteaxis = v(1, 0, 0) earth = vpython.sphere(texture=vpython.textures.earth, up=v(0, 0, 1), axis=starteaxis) earth.pos = v(0, 0, 0) earth.radius = 1.5 earth.rotperiod = 1.0 moon = vpython.sphere() moon.radius = 0.5 moon.color = vpython.color.gray(0.3)