Open topic with navigation
3D objects on-the-fly
\examples\nodes\onthefly.py
This script demonstrates how to create 3d objects on-the-fly.
"""
This script demonstrates how to create 3d objects on-the-fly.
Use the 'f' and 'r' keys to see a spinning cube around the sphere
and 'h' to hide the cube.
"""
import viz
import math
import random
import vizact
viz.setMultiSample(4)
viz.fov(60)
viz.go()
import vizinfo
vizinfo.InfoPanel()
NUM_DOTS = 350
RADIUS = 1
print("\n***************************************")
print("Press 'f' or 'r' to see a cube spinning")
print("along with the sphere and 'h' to hide the cube.")
print("\n***************************************")
#Build sphere
viz.startLayer(viz.POINTS)
viz.vertexColor(viz.WHITE)
viz.pointSize(2)
for i in range(0, NUM_DOTS):
x = random.random() - 0.5
y = random.random() - 0.5
z = random.random() - 0.5
length = math.sqrt(x*x + y*y + z*z)
x = x / length * RADIUS
y = y / length * RADIUS
z = z / length * RADIUS
viz.vertex([x, y, z])
sphere = viz.endlayer()
sphere.setPosition([0,1.8,4])
sphere.addAction(vizact.spin(0,1,0,45))
#Build cube
viz.startLayer(viz.LINE_STRIP)
viz.vertexColor([0, .6, 0])
viz.vertex([-RADIUS, -RADIUS, -RADIUS])
viz.vertex([-RADIUS, -RADIUS, RADIUS])
viz.vertex([ RADIUS, -RADIUS, RADIUS])
viz.vertex([ RADIUS, -RADIUS, -RADIUS])
viz.vertex([-RADIUS, -RADIUS, -RADIUS])
viz.vertex([-RADIUS, RADIUS, -RADIUS])
viz.vertex([-RADIUS, RADIUS, RADIUS])
viz.vertex([ RADIUS, RADIUS, RADIUS])
viz.vertex([ RADIUS, RADIUS, -RADIUS])
viz.vertex([-RADIUS, RADIUS, -RADIUS])
viz.startLayer(viz.LINES)
viz.vertex([-RADIUS, -RADIUS, RADIUS])
viz.vertex([-RADIUS, RADIUS, RADIUS])
viz.vertex([ RADIUS, -RADIUS, RADIUS])
viz.vertex([ RADIUS, RADIUS, RADIUS])
viz.vertex([ RADIUS, -RADIUS, -RADIUS])
viz.vertex([ RADIUS, RADIUS, -RADIUS])
cube = viz.endLayer()
cube.setPosition([0,1.8,4])
cube.visible(viz.OFF)
#add functions to be called on keypresses
def cubeForward():
cube.runAction(vizact.spin(0,1,0,45))
cube.visible(viz.ON)
vizact.onkeydown('f', cubeForward)
def cubeReverse():
cube.runAction(vizact.spin(0,1,0,-45))
cube.visible(viz.ON)
vizact.onkeydown('r', cubeReverse)
#hide cube when 'h' is pressed
vizact.onkeydown('h', cube.visible, viz.OFF)
# Add a shaded square spinning in the background
viz.startLayer(viz.QUADS)
viz.vertexColor(viz.RED)
viz.vertex([-1, 0, 0])
viz.vertexColor(viz.GREEN)
viz.vertex([-1, 2, 0])
viz.vertexColor(viz.BLUE)
viz.vertex([1, 2, 0])
viz.vertexColor(viz.BLACK)
viz.vertex([1, 0, 0])
myObj = viz.endLayer()
myObj.setPosition([0,1.8,5])
myObj.addAction(vizact.spin(0,0,1, 100))