collision.resolve(pair[0], pair[1]) return True def hit_aabb(collision, pairs): for pair in pairs: pair[0].material.color = [0.0, 1.0, 0] pair[1].material.color = [0.0, 1.0, 0] # Once there is a hit, system will not check # for the same collision, if you want to have the objects # back in the collision detection pipeline, you have to do collision.resolve(pair[0], pair[1]) return True scene = Scene(width=600, height=600) spherical_collision = CollisionTest(callback=hit_sphere, level=CollisionTest.SPHERICAL) aabb_collision = CollisionTest(callback=hit_aabb, level=CollisionTest.AABB) car_object_file = os.path.join(os.path.dirname(__file__), "lib", "Low-Poly-Racing-Car.obj") spherical_car_1 = Wavefront(filename=car_object_file) spherical_car_2 = Wavefront(filename=car_object_file) aabb_car_1 = Wavefront(filename=car_object_file) aabb_car_2 = Wavefront(filename=car_object_file) spherical_car_1.position = [-2, 0, 0] spherical_car_2.position = [-2, 0, 4]
import os from payton.scene import Scene from payton.scene.wavefront import Wavefront object_file = os.path.join(os.path.dirname(__file__), "monkey.obj") scene = Scene() monkey = Wavefront(filename=object_file) scene.add_object("monkey", monkey) scene.run()
# NOTE! This example requires "requests" to be installed # You can it install via `pip install requests` import os import requests from payton.scene import Scene from payton.scene.geometry import Cube from payton.scene.gui import Hud, Text y = 0 z = 0 scene = Scene() font_file = os.path.join( os.path.dirname(__file__), "../static/arial_narrow_7.ttf" ) def fetch_instagram(name, scene, period, total): """This method runs once and continues to build the scene""" global y, z scene.clocks["fetch_instagram"].pause() url = "https://www.instagram.com/nasa/?__a=1" data = requests.get(url) images = data.json()["graphql"]["user"]["edge_owner_to_timeline_media"][ "edges" ] for image in images: node = image["node"] for thum in node["thumbnail_resources"]: if thum["config_width"] == 640:
import sdl2 from payton.scene import Scene from payton.scene.controller import Controller class CustomKeyboardControls(Controller): def keyboard(self, event, scene): # Do we want all other keyboard maps? if so, lets super this! super().keyboard(event, scene) if event.type == sdl2.SDL_KEYUP: key = event.key.keysym.sym if key == sdl2.SDLK_s: print("Key S is pressed!") scene = Scene() scene.controller = CustomKeyboardControls() scene.run()
from payton.scene import Scene from payton.scene.geometry import Cube scene = Scene() cube = Cube() scene.add_object("cube", cube) scene.run()
import random from payton.scene import Scene from payton.scene.geometry import Cube def select(list): for obj in list: obj.material.color = [1, 1, 1] scene = Scene(on_select=select) for i in range(10): x = random.randint(-5, 5) y = random.randint(-5, 5) z = random.randint(-5, 5) r = random.randint(0, 255) / 255.0 g = random.randint(0, 255) / 255.0 b = random.randint(0, 255) / 255.0 cube = Cube() cube.material.color = [r, g, b] cube.position = [x, y, z] scene.add_object("cube_{}".format(i), cube) print("Try clicking on objects") scene.run()
from payton.scene import Scene from payton.scene.geometry import Plane scene = Scene() plane = Plane(width=2, height=2) scene.add_object("plane", plane) scene.run()
def logger(name, scene, period, total): if scene.objects["ball"].matrix[3][2] < 0: # Do not continue simulation if we hit the ground. scene.clocks[name].kill() # We do not need this clock anymore return None # Log ball location logging.debug("Ball position: x:{} y:{} z:{} t={}".format( scene.objects["ball"].matrix[3][0], scene.objects["ball"].matrix[3][1], scene.objects["ball"].matrix[3][2], total, )) # Definitions pm_scene = Scene() ball = Sphere(radius=1, track_motion=True) # Add ball to the scene pm_scene.add_object("ball", ball) pm_scene.observers[0].target_object = ball # Track the ball pm_scene.grid.resize(30, 30, 2) pm_scene.create_clock("motion", 0.01, projectile_motion) pm_scene.create_clock("logger", 0.05, logger) pm_scene.run()
from payton.scene import Scene from payton.scene.gui import Hud, Text from payton.scene.geometry import PointCloud def generate(name, scene, period, total): x = random.randint(-10, 10) y = random.randint(-10, 10) z = random.randint(-10, 10) r = random.randint(0, 255) / 255 g = random.randint(0, 255) / 255 b = random.randint(0, 255) / 255 scene.objects["pc"].add([[x, y, z]], [[r, g, b]]) scene = Scene() hud = Hud() text = Text( label="Hit Space to create points", position=(5, 5), size=(200, 35), color=(1, 1, 1), ) hud.add_child("text", text) scene.add_object("hud", hud) pc = PointCloud() scene.add_object("pc", pc)
from payton.scene import Scene scene = Scene() scene.run()
import random from payton.scene import Scene from payton.scene.geometry import Sphere from payton.scene.gui import Hud, Text from payton.scene.material import YELLOW, RED sphere_count = 0 score_board = 0 game = Scene() def create_balloon(name, scene, period, total): global sphere_count x = random.randint(-5, 5) y = random.randint(-5, 5) sphere = Sphere() sphere.material.color = [1, 1, 1] sphere.position = [x, y, 10] scene.add_object(f"sphere_{sphere_count}", sphere) sphere_count += 1 def move_balloons(name, scene, period, total): global sphere_count for k in range(sphere_count): sphere_name = f"sphere_{k}" if sphere_name in scene.objects: if not scene.objects[sphere_name].visible: continue pos = scene.objects[sphere_name].position pos[2] -= 0.01
import time from payton.scene import Scene from payton.scene.collision import CollisionTest from payton.scene.geometry import Cylinder myScene = Scene() c1 = Cylinder() c1.position = (3, 3, 0) c2 = Cylinder() c2.position = (-3, -3, 0) c3 = Cylinder() c3.position = (-3, -3, 3) moving_part = c2 def makeItRed(collision, pairs): global moving_part for pair in pairs: pair[1].material.color = [1.0, 0, 0] collision.resolve(pair[0], pair[1]) c3.position = [c3.position[0], c3.position[1], c3.position[2] + 0.11] moving_part = c3 myScene.clocks["c2_clocks"].pause() print("Collision triggered")
import os import math from payton.scene import Scene from payton.scene.geometry import Cylinder def rotate(name, scene, period, total): scene.objects["cylinder"].rotate_around_z(math.radians(1)) scene = Scene() cyl = Cylinder(height=2.0) scene.create_clock("rotate", 0.01, rotate) texture_file = os.path.join(os.path.dirname(__file__), "barrel.jpg") cyl.material.texture = texture_file scene.add_object("cylinder", cyl) scene.run()
import os from payton.scene import Scene from payton.scene.geometry import Cube from payton.scene.observer import Observer from payton.scene.gui import Hud, Text scene = Scene() texture_file = os.path.join(os.path.dirname(__file__), "cube.png") cube = Cube(width=5.0, height=5.0, depth=5.0) cube.position = [0, 0, 2.5] cube.material.texture = texture_file scene.add_object("cube", cube) inside_box = Observer( position=[-1.7898840267533351, 2.210322695165203, 1.400984730396208], target=[0, 0, 1], fov=110, ) scene.add_observer(inside_box) hud = Hud() font_file = os.path.join( os.path.dirname(__file__), "../static/arial_narrow_7.ttf" ) hud.set_font(font_file, 15) info_text = "Cycle through cameras using F2 and F3"
from payton.scene import Scene from payton.scene.geometry import Sphere, Cube from payton.scene.collision import CollisionTest def hit(collision, pairs): for pair in pairs: pair[0].material.color = [1.0, 0, 0] pair[1].material.color = [1.0, 0, 0] # Once there is a hit, system will not check # for the same collision, if you want to have the objects # back in the collision detection pipeline, you have to do # collision.resolve(pair[0], pair[1]) scene = Scene() collision = CollisionTest(callback=hit) for i in range(50): x = random.randint(-5, 5) y = random.randint(-5, 5) z = random.randint(-5, 5) if i % 2 == 0: s = Sphere() s.position = [x, y, z] scene.add_object("s_{}".format(i), s) collision.add_object(s) else: c = Cube() c.position = [x, y, z] scene.add_object("c_{}".format(i), c) collision.add_object(c)
import os import math from payton.scene import Scene from payton.scene.geometry import Cube def rotate(name, scene, period, total): y = math.radians(period * 100) y = -y if int(total) % 2 == 0 else y scene.objects["cube"].rotate_around_x(math.radians(period * 50)) scene.objects["cube"].rotate_around_y(y) scene.objects["cube"].rotate_around_z(math.radians(period * 150)) scene = Scene() cube = Cube() texture_file = os.path.join(os.path.dirname(__file__), "cube.png") scene.observers[0].distance_to_target(3) cube.material.texture = texture_file scene.add_object("cube", cube) scene.create_clock("rotate", 0.01, rotate) scene.run()
def motion(name, scene, period, total): angle = (total * 10) % 360 px = math.cos(math.radians(angle)) * 8 py = math.sin(math.radians(angle)) * 8 scene.objects["nucleus"].children["particle"].position = [px, py, 0] sx = math.cos(math.radians(angle * 10)) * 2 # 10 times faster sy = math.sin(math.radians(angle * 10)) * 2 scene.objects["nucleus"].children["particle"].children[ "sub_particle" ].position = [sx, sy, 0] scene.lights[0].position = [px, py, 0] scene.lights[1].position = [-px, -py, 0] space = Scene() space.lights.append(Light()) space.observers[0].position = [20, 20, 20] space.grid.resize(40, 40, 1) texture_file = os.path.join(os.path.dirname(__file__), "map.png") nucleus = Sphere(radius=5, parallels=36, meridians=36) nucleus.material.texture = texture_file particle = Sphere() particle.position = [8, 0, 0] sub_particle = Sphere(radius=0.5) sub_particle.position = [0, 2, 0] nucleus.add_child("particle", particle)
if sum(c) >= sum(target): rise = False else: if c[0] >= initial[0]: c[0] -= 0.01 if c[1] >= initial[1]: c[1] -= 0.01 if c[2] >= initial[2]: c[2] -= 0.01 if sum(c) <= sum(initial) + 0.01: rise = True scene._background.top_color = c scene = Scene() scene.create_clock("sun", 0.1, change_background) hud = Hud() font_file = os.path.join(os.path.dirname(__file__), "../static/arial_narrow_7.ttf") hud.set_font(font_file, 15) info_text = "Hit space to start background animation" info = Text(label=info_text, position=(550, 0), color=(1, 1, 1), size=(300, 200)) hud.add_child("info", info)
from payton.scene import Scene from payton.scene.geometry import Line scene = Scene() line = Line( vertices=[ [0, 0, 0], [0, 0, 1], [0.5, 0, 1.5], [1, 0, 1], [0, 0, 1], [1, 0, 0], [0, 0, 0], [1, 0, 1], [1, 0, 0], ] ) scene.add_object("line", line) scene.run()
import os from payton.scene import Scene from payton.scene.geometry import Mesh scene = Scene() mesh = Mesh() mesh.add_triangle( [[0, 0, 0], [2, 0, 0], [2, 2, 0]], texcoords=[[0, 0], [1, 0], [1, 1]] ) mesh.add_triangle( [[0, 0, 0], [2, 2, 0], [0, 2, 0]], texcoords=[[0, 0], [1, 1], [0, 1]] ) texture_file = os.path.join(os.path.dirname(__file__), "cube.png") mesh.material.texture = texture_file scene.add_object("mesh", mesh) scene.run()
import random from payton.scene import Scene from payton.scene.geometry import Cube scene = Scene() for i in range(100): x = random.randint(-10, 10) y = random.randint(-10, 10) z = random.randint(-10, 10) r = random.randint(0, 255) / 255.0 g = random.randint(0, 255) / 255.0 b = random.randint(0, 255) / 255.0 cube = Cube() cube.material.color = [r, g, b] cube.material.opacity = random.randint(0, 255) / 255.0 cube.position = [x, y, z] scene.add_object("cube_{}".format(i), cube) scene.run()