Exemple #1
0
 def setupScene(self):
   #create a new scene
   scene = scn.Scene()
   
   #global environment
   self.setupEnvironment(scene)
       
   #add elements
   self.setupSceneElements(scene)
       
   #setup vehicle
   self.myVehicleNode = self.setupVehicle(scene)
       
   #create a main camera
   self.myCameraNode = scn.Node()
   self.myCameraNode.camera = scn.Camera()
   self.myCameraNode.camera.xFov = 75
   self.myCameraNode.camera.zFar = 500
   self.myCameraNode.position = scn.Vector3(0, 60, 50)
   self.myCameraNode.rotation  = scn.Vector4(1, 0, 0, -M_PI_4*0.75)
   scene.rootNode.addChildNode(self.myCameraNode)
       
   #add a secondary camera to the car
   frontCameraNode = scn.Node()
   frontCameraNode.position = scn.Vector3(0, 3.5, 2.5)
   frontCameraNode.rotation = scn.Vector4(0, 1, 0, M_PI)
   frontCameraNode.camera = scn.Camera()
   frontCameraNode.camera.xFov = 75
   frontCameraNode.camera.zFar = 500
       
   self.myVehicleNode.addChildNode(frontCameraNode)
   
   self.pointOfViews = [self.myCameraNode, frontCameraNode]
   
   return scene
def setup_selector_views():
    selector_views = []
    for aVariant in Puzzle_variant:
        selector_view = scn.View(None, superView=data.main_view)
        selector_view.preferredFramesPerSecond = 30
        selector_view.rendersContinuously = False
        selector_view.autoenablesDefaultLighting = True
        selector_view.allowsCameraControl = False

        selector_view.scene = scn.Scene()
        selector_view.scene.background.contents = 'beige'

        title_geometry = scn.Text(str(aVariant.name).replace('_', ' '), 0.3)
        title_geometry.font = ('Chalkboard SE', 4.)
        title_geometry.firstMaterial.contents = (.48, .14, .14)
        title_geometry.flatness = 0.1
        title_geometry.chamferRadius = 0.08
        bbox_min, bbox_max = title_geometry.boundingBox
        title_size = bbox_max.x - bbox_min.x
        title_node = scn.Node.nodeWithGeometry(title_geometry)
        title_node.position = (-title_size / 2, -1., -25.)
        selector_view.scene.rootNode.addChildNode(title_node)

        puzzle_node = scn.Node()

        table_geometry = scn.Cylinder(6., 0.5)
        table_geometry.firstMaterial.diffuse.contents = 'resources/marble.jpg'
        table_node = scn.Node.nodeWithGeometry(table_geometry)
        table_node.position = (0., -0.75, 0.)
        puzzle_node.addChildNode(table_node)

        puzzle_node.position = (0., -0.25, 0.)
        piece_set = Piece_set(aVariant)
        pieces = len(piece_set.pieces)
        for index, aPiece in enumerate(piece_set.pieces):
            show_piece = aPiece.drawer_handle_node
            show_piece.rotation = (0, 1, 0,
                                   index / pieces * 2 * math.pi - math.pi / 4.)
            show_piece.position = scn.Vector3(
                4. * math.sin(index / pieces * 2 * math.pi), 0.,
                4. * math.cos(index / pieces * 2 * math.pi))
            puzzle_node.addChildNode(show_piece)

        puzzle_node.runAction(rotate_action)
        selector_view.scene.rootNode.addChildNode(puzzle_node)

        camera_node = scn.Node()
        camera_node.name = 'selector_camera'
        camera_node.position = (0., 7., 11.)
        camera_node.lookAt((0., 0., 0.))
        camera_node.camera = scn.Camera()
        selector_view.scene.rootNode.addChildNode(camera_node)

        selector_views.append(selector_view)
    return selector_views
Exemple #3
0
def demo():
    main_view = ui.View()
    w, h = ui.get_screen_size()
    main_view.frame = (0, 0, w, h)
    main_view.name = 'textDemo'

    scene_view = scn.View(main_view.frame, superView=main_view)
    scene_view.autoresizingMask = scn.ViewAutoresizing.FlexibleHeight | scn.ViewAutoresizing.FlexibleRightMargin
    scene_view.antialiasingMode = scn.AntialiasingMode.Multisampling16X

    scene_view.allowsCameraControl = True

    scene_view.backgroundColor = 'white'

    scene_view.scene = scn.Scene()

    root_node = scene_view.scene.rootNode
    text_mesh = scn.Text.textWithString('Pythonista', 6.0)
    text_mesh.flatness = 0.2
    text_mesh.chamferRadius = 0.4
    text_mesh.font = ('HelveticaNeue-Bold', 18)
    bbox_min, bbox_max = text_mesh.boundingBox
    text_width = bbox_max.x - bbox_min.x
    text_node = scn.Node.nodeWithGeometry(text_mesh)
    text_node.castsShadow = True
    text_container = scn.Node.node()
    text_container.addChildNode(text_node)
    text_container.position = (0, 40, 0)
    text_node.position = (-text_width / 2, 0, 0)
    box = scn.Box(width=150, height=4, length=150, chamferRadius=1)
    box_node = scn.Node.nodeWithGeometry(box)
    root_node.addChildNode(box_node)
    rotate_action = scn.Action.repeatActionForever(
        scn.Action.rotateBy(0, math.pi * 2, math.pi * 2, 10))
    text_container.runAction(rotate_action)
    root_node.addChildNode(text_container)
    light_node = scn.Node.node()
    light_node.position = (0, 105, 5)
    light_node.rotation = (1, 0, 0, -math.pi / 2)
    light = scn.Light.light()
    light.type = 'spot'
    light.spotOuterAngle = 65
    light.castsShadow = True
    light.shadowSampleCount = 16
    light.color = 'cyan'
    light_node.light = light
    root_node.addChildNode(light_node)

    main_view.present(style='fullscreen', hide_title_bar=False)
Exemple #4
0
  def main(self):
    main_view = ui.View()
    w, h = ui.get_screen_size()
    main_view.frame = (0,0,w,h)
    main_view.name = 'MDL import demo'
  
    scene_view = scn.View(main_view.frame, superView=main_view)
    scene_view.autoresizingMask = scn.ViewAutoresizing.FlexibleHeight | scn.ViewAutoresizing.FlexibleWidth
    scene_view.allowsCameraControl = True
    scene_view.backgroundColor = 'white'
    scene_view.rendersContinuously = True
    scene_view.scene = scn.Scene()
    
    root_node = scene_view.scene.rootNode

    floor_geometry = scn.Floor()
    floor_node = scn.Node.nodeWithGeometry(floor_geometry)
    root_node.addChildNode(floor_node)
    
    asset = MDLAsset.alloc().initWithURL_(nsurl('Lucy.obj'))
    mesh = asset.objectAtIndex_(0)
    lucy_geometry = scn.Geometry.geometryWithMDLMesh(mesh)
    
    lucy_node_1 = scn.Node.nodeWithGeometry(lucy_geometry)
    root_node.addChildNode(lucy_node_1)
    
    lucy_node_2 = scn.Node.nodeWithMDLObject(mesh)
    lucy_node_2.position = (10., 0., 0.)
    root_node.addChildNode(lucy_node_2)

    camera_node = scn.Node()
    camera_node.camera = scn.Camera()
    camera_node.position = (10., 10., 10.)
    camera_node.lookAt(root_node.position)
    root_node.addChildNode(camera_node)
    
    light_node = scn.Node()
    light_node.position = (-20., 20., 20)
    light = scn.Light()
    light.type = scn.LightTypeDirectional
    light.castsShadow = True
    light.shadowSampleCount = 32
    light.color = (.99, 1.0, .86)
    light_node.light = light
    light_node.lookAt(root_node.position)
    root_node.addChildNode(light_node)
    
    main_view.present(hide_title_bar=False)
def setup_center_view(piece_set):
    center_view = scn.View(None, superView=data.main_view)
    center_view.preferredFramesPerSecond = 30
    center_view.rendersContinuously = False
    center_view.autoenablesDefaultLighting = True
    center_view.autoresizingMask = scn.ViewAutoresizing.FlexibleHeight | scn.ViewAutoresizing.FlexibleWidth
    center_view.allowsCameraControl = True
    center_view.jitteringEnabled = True

    center_view.backgroundColor = 'beige'

    box_size = 3.0
    box = scn.Box(box_size, box_size, box_size, 0.0)
    box.firstMaterial.diffuse.contents = (.86, .86, .86)
    box.firstMaterial.transparency = 0.15
    box.firstMaterial.cullMode = scn.CullMode.Front
    box_node = scn.Node.nodeWithGeometry(box)
    box_node.name = 'box'
    box_node.categoryBitMask = 2

    tray_handle = setup_tray(piece_set)
    axis_handle = setup_axes()
    tray_handle.addChildNode(axis_handle)
    box_node.addChildNode(tray_handle)

    scene = scn.Scene()
    scene.rootNode.addChildNode(box_node)

    camera_node = scn.Node()
    camera_node.name = 'center_camera'
    camera_node.position = (8., 10., 12)
    camera_node.lookAt((1., 3., -1.5))
    camera_node.camera = scn.Camera()
    camera_node.camera.fieldOfView = 50
    scene.rootNode.addChildNode(camera_node)

    center_view.scene = scene
    return center_view
def setup_drawer_views(piece_set):
    for aPiece in piece_set.pieces:
        drawer_view = scn.View(None, superView=data.main_view)
        drawer_view.preferredFramesPerSecond = 30
        drawer_view.rendersContinuously = False
        drawer_view.autoenablesDefaultLighting = True
        drawer_view.allowsCameraControl = True

        drawer_view.scene = scn.Scene()
        drawer_view.scene.background.contents = 'beige'
        show_piece = aPiece.drawer_handle_node
        show_piece.rotation = (0, 1, 0, math.pi / 2.5)
        drawer_view.scene.rootNode.addChildNode(show_piece)

        min, max = drawer_view.scene.rootNode.boundingBox

        camera_node = scn.Node()
        camera_node.position = (2.5, 3., 2.5)
        camera_node.lookAt(
            ((min.x + max.x) / 2 + 1., (min.y + max.y) / 2 + 0.5, 0.))
        camera_node.camera = scn.Camera()
        drawer_view.scene.rootNode.addChildNode(camera_node)

        aPiece.drawer_view = drawer_view
  def main(self):
    main_view = ui.View()
    w, h = ui.get_screen_size()
    main_view.frame = (0,0,w,h)
    main_view.name = 'physics experiment demo - 2'
  
    scene_view = scn.View(main_view.frame, superView=main_view)
    scene_view.autoresizingMask = scn.ViewAutoresizing.FlexibleHeight | scn.ViewAutoresizing.FlexibleRightMargin
    scene_view.allowsCameraControl = True
    
    self.counter_scene = Counter()
    counter_view = SceneView()
    counter_view.frame = (0., 0., 100., 50.)
    counter_view.scene = self.counter_scene    
    main_view.add_subview(counter_view)
    
#    scene_view.debugOptions = scn.DebugOption.ShowBoundingBoxes | scn.DebugOption.ShowCameras | scn.DebugOption.ShowLightInfluences
    
#    scene_view.debugOptions = scn.DebugOption.ShowPhysicsShapes
    
    scene_view.backgroundColor = 'white'
    scene_view.scene = scn.Scene()
    
    physics_world = scene_view.scene.physicsWorld
    physics_world.contactDelegate = self
    
    root_node = scene_view.scene.rootNode
    
    box_size = 10
    box = scn.Box(box_size, box_size, box_size, 0.0)
    box.firstMaterial.diffuse.contents = (.86, .86, .86)
    box.firstMaterial.transparency = 0.15
    box.firstMaterial.cullMode = scn.CullMode.Front
    box_node = scn.Node.nodeWithGeometry(box)
    root_node.addChildNode(box_node)
    
    self.particle_number = 25
    particle_max_r = box_size/20
    particle_colors = ['black', 'blue', 'green', 'pink', 'yellow', 'red', 'cyan', 'gray', 'magenta', 'brown', 'crimson', 'gold', 'indigo', 'olive']
    particles_node = scn.Node()
    particles_node.physicsField = scn.PhysicsField.electricField()
    particles_node.physicsField.strength = 5.0

    root_node.addChildNode(particles_node)
    for i in range(self.particle_number):
      r = random.uniform(0.35*particle_max_r, particle_max_r)
      particle = scn.Sphere(r)
      particle.firstMaterial.diffuse.contents = random.choice(particle_colors)
      particle.firstMaterial.specular.contents = (.86, .94, 1.0, 0.8)
      particle_node = scn.Node.nodeWithGeometry(particle)
      particle_node.position = (random.uniform(-(box_size/2-particle_max_r)*0.95, (box_size/2-particle_max_r)*0.95), random.uniform(-(box_size/2-particle_max_r)*0.95, (box_size/2-particle_max_r)*0.95), random.uniform(-(box_size/2-particle_max_r)*0.95, (box_size/2-particle_max_r)*0.95))
      particle_node.physicsBody = scn.PhysicsBody.dynamicBody()
      particle_node.physicsBody.mass = 1.2*r
      particle_node.physicsBody.charge = random.uniform(-10*r, +0*r)
      particle_node.physicsBody.restitution = 1.0
      particle_node.physicsBody.damping = 0.0
      particle_node.physicsBody.angularDamping = 0.0
      particle_node.physicsBody.continuousCollisionDetectionThreshold = 1*r
      particle_node.physicsBody.affectedByGravity = False
      particle_node.physicsBody.contactTestBitMask = scn.PhysicsCollisionCategory.Default.value
      particle_node.physicsBody.velocity = (random.uniform(-box_size, box_size), random.uniform(-box_size, box_size), random.uniform(-box_size, box_size))
      particles_node.addChildNode(particle_node)
    
    box_nodes = scn.Node()
    d = box_size/2
    for pos in [(d,0,0), (-d,0,0), (0,d,0), (0,-d,0), (0,0,d), (0,0,-d)]:
      aNode = scn.Node()
      aNode.position = pos
      aNode.lookAt((0,0,0))
      aNode.physicsBody = scn.PhysicsBody.staticBody()
      aNode.physicsBody.physicsShape = scn.PhysicsShape.shapeWithGeometry(scn.Plane(box_size, box_size))
      box_nodes.addChildNode(aNode)
    root_node.addChildNode(box_nodes)
    
    constraint = scn.LookAtConstraint.lookAtConstraintWithTarget(root_node)
    constraint.gimbalLockEnabled = True
    
    camera_node = scn.Node()
    camera_node.camera = scn.Camera()
    camera_node.position = (-2.5*box_size, 1.5*box_size, 2*box_size)
    camera_node.constraints = constraint
    root_node.addChildNode(camera_node)
    
    light_node = scn.Node()
    light_node.position = (box_size, box_size, box_size)
    light = scn.Light()
    light.type = scn.LightTypeDirectional
    light.castsShadow = True
    light.shadowSampleCount = 32
    light.color = (.95, 1.0, .98)
    light_node.light = light
    light_node.constraints = constraint
    root_node.addChildNode(light_node)
    
    main_view.present(style='fullscreen', hide_title_bar=False)
Exemple #8
0
def main():
    main_view = ui.View()
    w, h = ui.get_screen_size()
    main_view.frame = (0, 0, w, h)
    main_view.name = 'Photos cube'

    scene_view = scn.View(main_view.frame)
    scene_view.autoresizingMask = (scn.ViewAutoresizing.FlexibleHeight,
                                   scn.ViewAutoresizing.FlexibleRightMargin)

    scene_view.allowsCameraControl = True

    scene_view.backgroundColor = 'white'

    scene_view.addToSuperview(main_view)

    scene = scn.Scene()
    scene_view.scene = scene

    root_node = scene.rootNode

    cube_geometry = scn.Box(width=1, height=1, length=1, chamferRadius=0.05)
    cube_geometry_inside = scn.Box(width=1 - 0.001,
                                   height=1 - 0.001,
                                   length=1 - 0.001,
                                   chamferRadius=0.04)

    Material_inside = scn.Material()
    Material_inside.diffuse.contents = inside_image

    cube_geometry_inside.materials = [Material_inside for i in range(6)]

    cube_geometry_materials = [scn.Material() for i in range(6)]
    for i in range(6):
        cube_geometry_materials[i].diffuse.contents = cube_images[i]

    cube_geometry.materials = cube_geometry_materials

    cube_node = scn.Node.nodeWithGeometry(cube_geometry)
    cube_node_inside = scn.Node.nodeWithGeometry(cube_geometry_inside)
    cube_node.addChildNode(cube_node_inside)

    cube_action = scn.Action.scaleTo(1.5, 3.0)
    cube_reversed_action = scn.Action.scaleTo(1.0, 1.0)
    cube_wait_action = scn.Action.waitForDuration(0.8)
    cube_y_rot_action1 = scn.Action.rotateBy(0, 0.2, 0, 0.3)
    cube_y_rot_action2 = scn.Action.rotateBy(0, -0.4, 0, 0.3)
    cube_y_rot_action3 = scn.Action.rotateBy(0, 0.2, 0, 0.3)
    cube_sequence = scn.Action.sequence([
        cube_action, cube_reversed_action, cube_wait_action,
        cube_y_rot_action1, cube_y_rot_action2, cube_y_rot_action3,
        cube_wait_action
    ])
    cube_forever = scn.Action.repeatActionForever(cube_sequence)

    cube_node.runAction(cube_forever)

    constraint = scn.LookAtConstraint.lookAtConstraintWithTarget(cube_node)
    constraint.gimbalLockEnabled = True

    camera = scn.Camera()
    camera_node = scn.Node()
    camera_node.name = 'camera!'
    camera_node.camera = camera
    camera_node.position = (-3.0, 3.0, 3.0)
    camera_node.constraints = [constraint]

    lights_node = scn.Node()

    ambient_light = scn.Light()
    ambient_light.type = scn.LightTypeAmbient
    ambient_light.name = 'ambient light'
    ambient_light.color = (.99, 1.0, .86)
    ambient_node = scn.Node()
    ambient_node.light = ambient_light
    lights_node.addChildNode(ambient_node)

    directional_light = scn.Light()
    directional_light.type = scn.LightTypeDirectional
    directional_light.name = 'directional light'
    directional_light.color = 'white'
    directional_node = scn.Node()
    directional_node.light = directional_light
    directional_node.position = camera_node.position
    directional_node.constraints = [constraint]

    lights_node.addChildNode(directional_node)

    root_node.addChildNode(camera_node)
    root_node.addChildNode(lights_node)
    root_node.addChildNode(cube_node)
    scene_view.pointOfView = camera_node

    scn.Transaction.setAnimationDuration(10.0)
    directional_light.color = 'yellow'

    main_view.present(hide_title_bar=False)
  def main(self):
# actually you need only to preserve those properties that are needed after the main_view.present call, 
# in this case the self.morpher. All the other self. prefixes are not needed for the same functionality
    self.q=queue.PriorityQueue()
    self.q1=queue.PriorityQueue()
    self.Eprom=Eprom1()
    self.sv=scene.SceneView()
    self.sv.scene=MyScene()
    self.sv.anti_alias = False
    self.sv.frame_interval = 1
    self.sv.multi_touch_enabled = True
    self.sv.shows_fps = True
    self.sv.bg_color=(1,1,1,1)
    v1width=650
    self.view1=ui.View(frame=(256+768-v1width,0,v1width,v1width))
    self.messagetext=''
    self.rbtn1=ui.SegmentedControl(frame=(5.0,340.0,204.0,34.0), segments=('auto','xyz','123','maze'), action= self.rbutton_tapped)
    self.switch1=ui.Switch(frame=(6.0,34.0,51.0,31.0),action=self.setPin)
    self.switch1.targetPin=2
    self.switch2=ui.Switch(frame=(197,167,51.0,31.0),action=self.setPin)
    self.switch2.targetPin=21
    self.sv.add_subview(self.view1)
    self.sv.add_subview(self.rbtn1)
    self.sv.add_subview(self.switch1)
    self.sv.add_subview(self.switch2)
    self.keypad1=keypadNode(scale=1.15,radius=150,position=(15,35),anchor_point=(0,0),
      keytitles=['inc','y','X','Z','dec','z','r/g','x','Y','../_'], on_output_change=self.keypad_output_changed)
    self.keypad2=keypadNode(scale=1.15,radius=150,position=(15,35),anchor_point=(0,0),
      keytitles=['1','2','3','4','5','6','7','8','9','0'],
      orientation=((0,-1),(1,0),),
      on_output_change=self.keypad_output_changed)
    self.keypad3=keypadNode(scale=1.15,radius=150,position=(15,35),anchor_point=(0,0),
      keytitles=['Put\n{NE}','N','E','[NW]\nU','Take\n{SW}','D\n{SE}','[Ctrl]','W','S','{Alt}'], on_output_change=self.keypad_output_changed)
    scene.LabelNode(position=(-30,-120), anchor_point=(0,0), text='Reset: [Ctrl Alt D]',font=('Helvetica',15),parent=self.keypad3,color='black')
    self.mode=0
    self.key=''
    self.scene_view = scn.View((0,0,self.view1.frame[2],self.view1.frame[3]), superView=self.view1)
    self.scene_view.allowsCameraControl = True
    
    self.scene_view.scene = scn.Scene()
    
    self.scene_view.delegate = self
    
    self.root_node = self.scene_view.scene.rootNode
    
    self.camera_node = scn.Node()
    self.camera_node.camera = scn.Camera()
    self.camera_node.position = (-10,1.5,2)
    self.camera_node.camera.focalLength=70
    self.root_node.addChildNode(self.camera_node)    
    self.origin_node = scn.Node()
    self.root_node.addChildNode(self.origin_node)    
    self.floor_node=scn.Node(geometry=scn.Floor())
    self.floor_node.position=(0,-1.25,0)
#    self.root_node.addChildNode(self.floor_node)    
    n=4
    scale=0.1/n
    r=3
#    self.off_led = scn.Sphere(radius=r*scale)  
    self.off_led = scn.Capsule(capRadius=r*scale,height=3*r*scale) 
    self.off_led.firstMaterial.contents=UIColor.lightGrayColor().CGColor()
#    off_led.firstMaterial().emission().setColor_(UIColor.greenColor().CGColor())
    self.green_led = scn.Capsule(capRadius=r*scale*1.1,height=3*r*scale*1.05) 
    self.green_led.firstMaterial.contents=(UIColor.grayColor().CGColor())
    self.green_led.firstMaterial.emission.contents=(UIColor.greenColor().CGColor())
    self.red_led = scn.Capsule(capRadius=r*scale*1.1,height=3*r*scale*1.05)  
    self.red_led.firstMaterial.contents=UIColor.grayColor().CGColor()
    self.red_led.firstMaterial.emission.contents=(UIColor.redColor().CGColor())
    self.led_nodes = [[[scn.Node.nodeWithGeometry(self.off_led) for k in range(n)]for j in range(n)]for i in range(n)]
    self.off_wire = scn.Capsule(capRadius=r*0.25*scale,height=20*(n+0.5)*scale) 
    self.off_wire.firstMaterial.contents=UIColor.lightGrayColor().CGColor()
    self.pos_wire = scn.Capsule(capRadius=r*0.25*scale,height=20*(n+0.5)*scale) 
    self.pos_wire.firstMaterial.contents=UIColor.lightGrayColor().CGColor()
    self.pos_wire.firstMaterial.emission.contents=(0.7,0,0)#UIColor.magentaColor().CGColor())
    self.neg_wire = scn.Capsule(capRadius=r*0.25*scale,height=20*(n+0.5)*scale) 
    self.neg_wire.firstMaterial.contents=UIColor.lightGrayColor().CGColor()
    self.neg_wire.firstMaterial.emission.contents=(0,0,0.75)#(UIColor.blueColor().CGColor())
    self.wire_nodes=[[[scn.Node.nodeWithGeometry((self.off_wire,self.neg_wire,self.pos_wire)[0]) for j in range(n)]for i in range(n)]for k in range(3)]
    wireoffset=r*scale
    for i in range(n):
      for j in range(n):
        x=(i-(n-1)/2)*20*scale
        y=(j-(n-1)/2)*20*scale
        self.root_node.addChildNode(self.wire_nodes[0][i][j])
        self.wire_nodes[0][i][j].setPosition((x+wireoffset,0,y))
        self.root_node.addChildNode(self.wire_nodes[1][i][j])
        self.wire_nodes[1][i][j].setPosition((x,y-wireoffset,0))
        self.wire_nodes[1][i][j].eulerAngles=(math.pi/2,0,0)        
        self.root_node.addChildNode(self.wire_nodes[2][i][j])
        self.wire_nodes[2][i][j].setPosition((0,x,y-wireoffset))
        self.wire_nodes[2][i][j].eulerAngles=(0,0,math.pi/2)        
        for k in range(n):
          z=(k-(n-1)/2)*20*scale
          self.root_node.addChildNode(self.led_nodes[i][j][k])
          self.led_nodes[i][j][k].setPosition((x,y,z))
          self.led_nodes[i][j][k].eulerAngles=(0.61547970867039,0,math.pi/4)
    self.index=0
    self.oldindex=0
    constraint = scn.LookAtConstraint(self.root_node)#(self.sphere_nodes[2][2][2])    
    constraint.gimbalLockEnabled = True
    self.camera_node.constraints = constraint
    
    self.light_node = scn.Node()
    self.light_node.position = (100, 0, -10)
    self.light = scn.Light()
    self.light.type = scn.LightTypeDirectional
    self.light.castsShadow = False
    self.light.color = 'white'
    self.light_node.light = self.light
    self.root_node.addChildNode(self.light_node)
    
    self.action = scn.Action.repeatActionForever(scn.Action.rotateBy(0, math.pi*2, 0, 10))
    self.origin_node.runAction(self.action)  
    
    self.sv.present(orientations= ['landscape'])
Exemple #10
0
    def main(self):
        # actually you need only to preserve those properties that are needed after the main_view.present call,
        # in this case the self.morpher. All the other self. prefixes are not needed for the same functionality
        self.main_view = ui.View()
        w, h = ui.get_screen_size()
        self.main_view.frame = (0, 0, w, h)
        self.main_view.name = 'Morpher demo'

        self.scene_view = scn.View(self.main_view.frame,
                                   superView=self.main_view)
        self.scene_view.autoresizingMask = scn.ViewAutoresizing.FlexibleHeight | scn.ViewAutoresizing.FlexibleRightMargin
        self.scene_view.allowsCameraControl = True

        self.scene_view.scene = scn.Scene()

        self.scene_view.delegate = self

        self.root_node = self.scene_view.scene.rootNode

        self.camera_node = scn.Node()
        self.camera_node.camera = scn.Camera()
        self.camera_node.position = (0, 0, 5)
        self.root_node.addChildNode(self.camera_node)

        verts = [
            scn.Vector3(0, 1, 0),
            scn.Vector3(-0.5, 0, 0.5),
            scn.Vector3(0.5, 0, 0.5),
            scn.Vector3(0.5, 0, -0.5),
            scn.Vector3(-0.5, 0, -0.5),
            scn.Vector3(0, -1, 0)
        ]

        verts_2 = [
            scn.Vector3(0, 2.5, 0),
            scn.Vector3(-0.4, 0, 0.4),
            scn.Vector3(0.4, 0, 0.4),
            scn.Vector3(0.4, 0, -0.4),
            scn.Vector3(-0.4, 0, -0.4),
            scn.Vector3(0, -1.5, 0)
        ]

        self.source = scn.GeometrySource.geometrySourceWithVertices(verts)
        self.source_2 = scn.GeometrySource.geometrySourceWithVertices(verts_2)

        indexes = [
            0, 1, 2, 2, 3, 0, 3, 4, 0, 4, 1, 0, 1, 5, 2, 2, 5, 3, 3, 5, 4, 4,
            5, 1
        ]

        self.elements = scn.GeometryElement.geometryElementWithData(
            indexes, scn.GeometryPrimitiveType.Triangles)

        self.geometry = scn.Geometry.geometryWithSources(
            self.source, self.elements)
        self.geometry_2 = scn.Geometry.geometryWithSources(
            self.source_2, self.elements)

        self.material = scn.Material()
        self.material.contents = scn.RGBA(1, 0.9, 0.9, 1.0)
        self.geometry.materials = self.material

        self.morpher = scn.Morpher()
        self.morpher.targets = [self.geometry_2]
        self.morpher.setWeightForTargetAtIndex(0.0, 0)

        self.geometry_node = scn.Node.nodeWithGeometry(self.geometry)
        self.geometry_node.name = 'hero'
        self.geometry_node.morpher = self.morpher
        self.root_node.addChildNode(self.geometry_node)

        # Add a constraint to the camera to keep it pointing to the target geometry
        constraint = scn.LookAtConstraint.lookAtConstraintWithTarget(
            self.geometry_node)
        constraint.gimbalLockEnabled = True
        self.camera_node.constraints = constraint

        self.light_node = scn.Node()
        self.light_node.position = (100, 0, -10)
        self.light = scn.Light()
        self.light.type = scn.LightTypeDirectional
        self.light.castsShadow = True
        self.light.color = 'white'
        self.light_node.light = self.light
        self.root_node.addChildNode(self.light_node)

        self.rotate_action = scn.Action.repeatActionForever(
            scn.Action.rotateBy(0, math.pi * 2, 0, 10))
        self.geometry_node.runAction(self.rotate_action)

        self.main_view.present(style='fullscreen', hide_title_bar=False)
Exemple #11
0
    def main(self):
        main_view = ui.View()
        w, h = ui.get_screen_size()
        main_view.frame = (0, 0, w, h)
        main_view.name = 'avoid occluder demo'

        scene_view = scn.View(main_view.frame, superView=main_view)
        scene_view.autoresizingMask = scn.ViewAutoresizing.FlexibleHeight | scn.ViewAutoresizing.FlexibleWidth
        scene_view.allowsCameraControl = True
        scene_view.delegate = self
        scene_view.backgroundColor = 'white'
        scene_view.rendersContinuously = True
        scene_view.scene = scn.Scene()

        root_node = scene_view.scene.rootNode

        floor_geometry = scn.Floor()
        floor_node = scn.Node.nodeWithGeometry(floor_geometry)
        root_node.addChildNode(floor_node)

        ball_radius = 0.2
        ball_geometry = scn.Sphere(radius=ball_radius)
        ball_geometry.firstMaterial.diffuse.contents = (.48, .48, .48)
        ball_geometry.firstMaterial.specular.contents = (.88, .88, .88)
        self.ball_node_1 = scn.Node.nodeWithGeometry(ball_geometry)
        self.ball_node_2 = scn.Node.nodeWithGeometry(ball_geometry)

        root_node.addChildNode(self.ball_node_1)
        root_node.addChildNode(self.ball_node_2)

        occluder_geometry = scn.Box(0.3, 2., 15., 0.2)
        occluder_geometry.firstMaterial.diffuse.contents = (.91, .91, .91)
        occluder_node = scn.Node.nodeWithGeometry(occluder_geometry)
        occluder_node.position = (0., 0.8, 0.)
        root_node.addChildNode(occluder_node)

        self.orbit_r = 10
        self.omega_speed_1 = math.pi / 1500
        self.omega_speed_2 = 1.5 * self.omega_speed_1
        self.ball_node_1.position = (self.orbit_r, 0.5, 0.)
        self.ball_node_2.position = (0., 0.5, self.orbit_r)

        constraint = scn.AvoidOccluderConstraint.avoidOccluderConstraintWithTarget(
            self.ball_node_1)
        self.ball_node_2.constraints = [constraint]

        camera_node = scn.Node()
        camera_node.camera = scn.Camera()
        camera_node.position = (0.5 * self.orbit_r, 0.5 * self.orbit_r,
                                1.5 * self.orbit_r)
        camera_node.lookAt(root_node.position)
        root_node.addChildNode(camera_node)

        light_node = scn.Node()
        light_node.position = (self.orbit_r, self.orbit_r, self.orbit_r)
        light = scn.Light()
        light.type = scn.LightTypeDirectional
        light.castsShadow = True
        light.shadowSampleCount = 32
        light.color = (.99, 1.0, .86)
        light_node.light = light
        light_node.lookAt(root_node.position)
        root_node.addChildNode(light_node)

        main_view.present(style='fullscreen', hide_title_bar=False)
Exemple #12
0
def demo():
  main_view = ui.View()
  main_view.frame = (0,0,w,h)
  main_view.name = 'shapeDemo'
  main_view.background_color = 'white'
  
  image_view = ui.ImageView()
  image_view.frame = image_frame
  image_view.background_color = 'white'
  image_view.content_mode = ui.CONTENT_CENTER
  image_view.image = img
  
  main_view.add_subview(image_view)
  
  scene_view = scn.View(scene_frame, superView=main_view)
  scene_view.autoresizingMask = scn.ViewAutoresizing.FlexibleHeight | scn.ViewAutoresizing.FlexibleRightMargin  
  scene_view.antialiasingMode = scn.AntialiasingMode.Multisampling16X
  scene_view.allowsCameraControl = True 
  scene_view.backgroundColor = 'white' 
  scene_view.scene = scn.Scene()

  root_node = scene_view.scene.rootNode

  shape = scn.Shape.shapeWithPath(path, 40)
  shape.chamferRadius = 8
  shape.firstMaterial.contents = 'yellow'
  
  shape_node = scn.Node.nodeWithGeometry(shape)
  shape_node.castsShadow = True
  bbox_min, bbox_max = shape.boundingBox
  shape_width = bbox_max.x - bbox_min.x
  shape_height = bbox_max.y - bbox_min.y
  shape_tr = list(scn.Matrix4Identity)
  shape_tr[13] = bbox_min.y+0.5*shape_height
  shape_node.pivot = shape_tr
  shape_node.position = (-bbox_min.x-0.5*shape_width, 0, 0)

  shape_container = scn.Node()
  shape_container.addChildNode(shape_node)

  scale = image_size.width/shape_width
  shape_container.scale = (scale, scale, 1)
  shape_width *= scale
  shape_height *= scale
  
  shape_container.position = (0, 0.2*shape_height, 0.5*shape_height)
  shape_container.rotation = (0, 1, 0, pi) # compensates for the differen coordinate systems of drawing vs. scene
  
  rotate_action = scn.Action.rotateBy(0, 0, 2*pi, 10)
  rotate_action.timingMode = scn.ActionTimingMode.EaseInEaseOut
  invers_action = rotate_action.reversedAction()  
  combined_action = scn.Action.sequence([rotate_action, invers_action])  
  shape_container.runAction(scn.Action.repeatActionForever(combined_action))
  
  box = scn.Box(width=2*shape_width, height=4, length=1.6*shape_width, chamferRadius=1)
  box.firstMaterial.contents = (.76, .91, 1.0)
  
  boxBoxMin, boxBoxMax = box.boundingBox
  box.boundingBox = ((box_bubble*boxBoxMin.x, boxBoxMin.y, boxBoxMin.z), (box_bubble*boxBoxMax.x, boxBoxMax.y, boxBoxMax.z))
  box_node = scn.Node.nodeWithGeometry(box)
  box_node.rotation = (1, 0, 0, pi/4)

  root_node.addChildNode(box_node)
  root_node.addChildNode(shape_container)
  
  constraint = scn.LookAtConstraint.lookAtConstraintWithTarget(shape_container)
  constraint.gimbalLockEnabled = True
  
  light_node = scn.Node()
  light_z = boxBoxMax.z + 0.0*boxBoxMax.z
  light_y = (shape_container.position.y+0.5*shape_height/2)/shape_container.position.z*light_z
  light_node.position = (20, light_y, light_z)
  light_node.constraints = [constraint]
  light = scn.Light()
  light.type = 'spot'
  light.spotOuterAngle = 90
  light.castsShadow = True
  light.zFar = 1500
  light.shadowSampleCount = 16
  light.color = 'white'
  light_node.light = light
  root_node.addChildNode(light_node)
  
  ambient_light = scn.Light() 
  ambient_light.type = scn.LightTypeAmbient
  ambient_light.color = (.41, .32, .0)
  ambient_node = scn.Node()
  ambient_node.light = ambient_light
  root_node.addChildNode(ambient_node)
    
  main_view.present(style='fullscreen', hide_title_bar=False)
Exemple #13
0
  def main(self):
    self.main_view = ui.View()
    w, h = ui.get_window_size()
    self.main_view.frame = (0, 0, w, h)
    self.main_view.name = 'vehicle demo'
    
    self.close_button = ui.Button()
    self.close_button.action = self.close
    self.close_button.frame = (20, 40, 40, 40)
    self.close_button.background_image = ui.Image.named('emj:No_Entry_2')

    if DEBUG:  
      self.scene_view = scn.View((0,25,w,h-25), superView=self.main_view)
      self.scene_view.showsStatistics = True
    else:
      self.scene_view = scn.View((0, 0, w, h), superView=self.main_view)
    self.scene_view.preferredFramesPerSecond = 30
      
    self.scene_view.autoresizingMask = scn.ViewAutoresizing.FlexibleHeight | scn.ViewAutoresizing.FlexibleWidth
    self.scene_view.allowsCameraControl = False
    
    self.scene_view.scene = scn.Scene()
    self.root_node = self.scene_view.scene.rootNode 
    
    self.scene_view.backgroundColor = (.77, .97, 1.0)
    self.scene_view.delegate = self
    
    self.physics_world = self.scene_view.scene.physicsWorld
    self.physics_world.speed = 2.
    self.physics_world.contactDelegate = self
    
    if DEBUG:
      marker1 = scn.Sphere(0.1)
      marker1.firstMaterial.emission.contents = (1.0, .14, .14)
      marker1.firstMaterial.diffuse.contents = (.0, .0, .0)
      Demo.marker_node1 = scn.Node.nodeWithGeometry(marker1)
      Demo.marker_node1.name = 'marker1'
      self.root_node.addChildNode(Demo.marker_node1)
     
      marker2 = scn.Sphere(0.1)
      marker2.firstMaterial.emission.contents = (.17, .0, 1.0)
      marker2.firstMaterial.diffuse.contents = (.0, .0, .0)
      Demo.marker_node2 = scn.Node.nodeWithGeometry(marker2)
      Demo.marker_node2.name = 'marker2'
      self.root_node.addChildNode(Demo.marker_node2)
    
    floor_geometry = scn.Floor()
    floor_geometry.reflectivity = 0.05
    tile_image = ui.Image.named('plf:Ground_DirtCenter')
    tile_number = 5
    tile_factor = scn.Matrix4(tile_number, 0.0, 0.0, 0.0, 0.0, tile_number, 0.0, 0.0, 0.0, 0.0, tile_number, 0.0, 0.0, 0.0, 0.0, 1.0)
    floor_geometry.firstMaterial.diffuse.contents = tile_image
    floor_geometry.firstMaterial.diffuse.intensity = 0.8
    floor_geometry.firstMaterial.diffuse.contentsTransform = tile_factor
    floor_geometry.firstMaterial.diffuse.wrapS, floor_geometry.firstMaterial.diffuse.wrapT = scn.WrapMode.Repeat, scn.WrapMode.Repeat
    floor_geometry.firstMaterial.locksAmbientWithDiffuse = True
    self.floor_node = scn.Node.nodeWithGeometry(floor_geometry)
    self.floor_node.name = 'Floor'
    self.floor_node.physicsBody = scn.PhysicsBody.staticBody()
    self.root_node.addChildNode(self.floor_node)
    
    cars = [dict(name='red', position=(5, 0, 0), volume=1.),
            dict(name='yellow', too_far=25, body_color=(1.0, .78, .0), position=(-5, 0, -2), sound='game:Pulley', volume=0.1),
            dict(name='blue', too_far=30, body_color=(.0, .61, 1.0), position=(-12, 0, -6), sound='game:Woosh_1', volume=0.5),
            dict(name='green', too_far=18, body_color=(.0, .82, .28), position=(10, 0, -10), sound='casino:DiceThrow3', volume=0.8),
            dict(name='pink', too_far=20, body_color=(.91, .52, .62), position=(5, 0, 10), sound='casino:DieThrow3', volume=0.5)]      
    self.cars =[Car(world=self, props=cars[i]) for i in range(min(MAXCARS, len(cars)))]
    
    self.free_flags = []
    for i in range(2*len(self.cars)):
      node = scn.Node()
      self.free_flags.append(node)
      self.root_node.addChildNode(node)
    self.used_flags = {}
    
    self.crash = Sparks().particleSystem
    self.crash_sound = scn.AudioSource('game:Crashing')
    self.crash_action = scn.Action.playAudioSource(self.crash_sound, False)
    
    self.road_blocks_node = scn.Node()
    self.road_blocks = []
    
    self.road_blocks.append(RoadBlock(w=1.6, l=25, name='block 0', position=(28, 6)))
    self.road_blocks.append(RoadBlock(w=20, l=1.6, name='block 1', position=(-2, -12)))
    self.road_blocks.append(RoadBlock(w=8, l=1.6, name='block 2', position=(-10, 6), rotation=-math.pi/6))
    self.road_blocks.append(RoadBlock(w=40, l=1.6, name='block 3', position=(-40, 0), rotation=-math.pi/3))
    self.road_blocks.append(RoadBlock(w=0.8, h=3, l=0.8, name='start', position=(0, 0)))
    
    for aBlock in self.road_blocks: self.road_blocks_node.addChildNode(aBlock.block_node)    
    self.root_node.addChildNode(self.road_blocks_node)

    self.camera_node = scn.Node()
    self.camera_node.camera = scn.Camera()
    self.camera_node.camera.zFar = 150
    carPos = self.cars[0].node.position
    self.camera_node.position = scn.Vector3(carPos.x+5, 10, carPos.z+30)    
    self.root_node.addChildNode(self.camera_node)
    
    self.light_node = scn.Node()
    self.light_node.position = (50, 50, 50)
    self.light_node.lookAt(self.root_node.position)
    self.light = scn.Light()
    self.light.type = scn.LightTypeDirectional
    self.light.castsShadow = True
    self.light.shadowSampleCount = 16
    self.light.color = (.95, 1.0, .98)
    self.light_node.light = self.light
    self.root_node.addChildNode(self.light_node)
    
    self.ambient_node = scn.Node()
    self.ambient = scn.Light()
    self.ambient.type = scn.LightTypeAmbient
    self.ambient.color = (.38, .42, .45, .1)
    self.ambient_node.light = self.ambient
    self.root_node.addChildNode(self.ambient_node)
    
    self.scene_view.pointOfView = self.camera_node
    
    if DEBUG: self.main_view.add_subview(Demo.status_label)
    
    self.close = False
    self.shut_down = False
    self.main_view.add_subview(self.close_button)
    self.main_view.present(hide_title_bar=~DEBUG)
def demo():
    main_view = ui.View()
    w, h = ui.get_screen_size()
    main_view.frame = (0, 0, w, h)
    main_view.name = 'particles demo'

    scene_view = scn.View(main_view.frame, superView=main_view)
    scene_view.autoresizingMask = scn.ViewAutoresizing.FlexibleHeight | scn.ViewAutoresizing.FlexibleWidth
    scene_view.antialiasingMode = scn.AntialiasingMode.Multisampling16X

    scene_view.allowsCameraControl = True

    scene_view.backgroundColor = 'black'

    scene_view.scene = scn.Scene()

    root_node = scene_view.scene.rootNode
    text_mesh = scn.Text.textWithString('Pythonista', 6.0)
    text_mesh.flatness = 0.2
    text_mesh.chamferRadius = 0.4
    text_mesh.font = ('HelveticaNeue-Bold', 18)
    bbox_min, bbox_max = text_mesh.boundingBox
    text_width = bbox_max.x - bbox_min.x
    text_node = scn.Node.nodeWithGeometry(text_mesh)
    text_node.castsShadow = False
    text_container = scn.Node.node()
    text_container.addChildNode(text_node)
    text_container.position = (0, 40, 0)
    text_node.position = (-text_width / 2, 0, 0)

    fire = scn.ParticleSystem()
    fire.birthRate = 300000
    fire.loops = True
    fire.emissionDuration = 8
    fire.emissionDurationVariation = 4
    fire.idleDuration = 2
    fire.idleDurationVariation = 0.5
    fire.emittingDirection = (0, 1, 0)
    fire.spreadingAngle = 15
    fire.particleDiesOnCollision = False
    fire.particleLifeSpan = 0.4
    fire.particleLifeSpanVariation = 0.5
    fire.particleVelocity = 20
    fire.particleVelocityVariation = 30
    fire.particleImage = ui.Image.named('shp:WhitePuff05')
    fire.particleSize = 0.4
    fire.particleSizeVariation = 0.2
    fire.particleIntensity = 1.5
    fire.particleIntensityVariation = 2
    fire.stretchFactor = 0.02
    colorAnim = scn.CoreKeyframeAnimation()
    colorAnim.values = [(.99, 1.0, .71, 0.8), (1.0, .52, .0, 0.8),
                        (1., .0, .1, 1.), (.78, .0, .0, 0.3)]
    colorAnim.keyTimes = (0., 0.1, 0.8, 1.)
    fire.timingFunctions = [
        scn.CoreMediaTimingFunction.functionWithName(aFunc) for aFunc in [
            scn.MediaTimingFunctionEaseOut,
            scn.MediaTimingFunctionEaseInEaseOut, scn.MediaTimingFunctionEaseIn
        ]
    ]
    prop_con = scn.ParticlePropertyController.controllerWithAnimation(
        colorAnim)
    fire.propertyControllers = {scn.SCNParticlePropertyColor: prop_con}
    fire.emitterShape = text_mesh
    fire.birthLocation = scn.ParticleBirthLocation.SCNParticleBirthLocationSurface
    text_node.addParticleSystem(fire)

    root_node.addChildNode(text_container)

    light_node = scn.Node.node()
    light_node.position = (0, 105, 5)
    light_node.rotation = (1, 0, 0, -math.pi / 2)
    light = scn.Light.light()
    light.type = 'spot'
    light.spotOuterAngle = 90
    light.castsShadow = True
    light.shadowSampleCount = 16
    light.color = 'white'
    light_node.light = light
    root_node.addChildNode(light_node)

    main_view.present(hide_title_bar=False)
    def main(self):
        main_view = ui.View()
        w, h = ui.get_screen_size()
        main_view.frame = (0, 0, w, h)
        main_view.name = 'physics experiment demo - 1'

        scene_view = scn.View(main_view.frame, superView=main_view)
        scene_view.autoresizingMask = scn.ViewAutoresizing.FlexibleHeight | scn.ViewAutoresizing.FlexibleRightMargin
        scene_view.allowsCameraControl = True

        scene_view.backgroundColor = 'white'
        scene_view.scene = scn.Scene()

        physics_world = scene_view.scene.physicsWorld

        root_node = scene_view.scene.rootNode

        floor_geometry = scn.Floor()
        floor_geometry.reflectivity = 0.1
        floor_node = scn.Node.nodeWithGeometry(floor_geometry)
        root_node.addChildNode(floor_node)

        ball_radius = 0.2
        ball_geometry = scn.Sphere(radius=ball_radius)
        ball_geometry.firstMaterial.diffuse.contents = (.48, .48, .48)
        ball_geometry.firstMaterial.specular.contents = (.88, .88, .88)
        ball_node = scn.Node.nodeWithGeometry(ball_geometry)

        rope_seg_nr = 45
        rope_length = 5.0
        rope_radius = 0.025
        rope_seg_length = rope_length / rope_seg_nr

        rope_element_geometry = scn.Capsule(rope_radius, rope_seg_length)
        rope_element_geometry.firstMaterial.diffuse.content = (.77, .67, .09)

        rope_nodes = []
        for i in range(rope_seg_nr):
            rope_nodes.append(scn.Node.nodeWithGeometry(rope_element_geometry))
            if i > 0:
                rope_nodes[-1].position = (0., -rope_seg_length, 0.)
                rope_nodes[-2].addChildNode(rope_nodes[-1])
                aConstraint = scn.DistanceConstraint.distanceConstraintWithTarget(
                    rope_nodes[-2])
                aConstraint.maximumDistance = rope_seg_length
                aConstraint.minimumDistance = rope_seg_length
                rope_nodes[-1].constraints = aConstraint
            else:
                rope_nodes[0].position = (0, rope_length + 1.0 -
                                          rope_seg_length / 2, 0)
                aConstraint = scn.DistanceConstraint.distanceConstraintWithTarget(
                    root_node)
                aConstraint.maximumDistance = rope_length + 1.0 - rope_seg_length / 2
                aConstraint.minimumDistance = rope_length + 1.0 - rope_seg_length / 2
                rope_nodes[0].constraints = aConstraint

        ball_node.position = (0,
                              -rope_seg_length / 2 - rope_radius - ball_radius,
                              0)
        aConstraint = scn.DistanceConstraint.distanceConstraintWithTarget(
            rope_nodes[-1])
        aConstraint.maximumDistance = rope_seg_length / 2 + rope_radius + ball_radius
        aConstraint.minimumDistance = rope_seg_length / 2 + rope_radius + ball_radius
        ball_node.constraints = aConstraint

        rope_nodes[-1].addChildNode(ball_node)
        root_node.addChildNode(rope_nodes[0])

        ball_physicsBody = scn.PhysicsBody.dynamicBody()
        ball_physicsBody.physicsShape = scn.PhysicsShape.shapeWithGeometry(
            ball_geometry)
        ball_physicsBody.mass = 5.0
        ball_physicsBody.damping = 0.005
        ball_physicsBody.angularDamping = 0.00
        ball_node.physicsBody = ball_physicsBody

        rope_element_physicsShape = scn.PhysicsShape.shapeWithGeometry(
            rope_element_geometry)

        for i in range(rope_seg_nr):
            rope_nodes[i].physicsBody = scn.PhysicsBody.dynamicBody()
            rope_nodes[i].physicsBody.physicsShape = rope_element_physicsShape
            rope_nodes[i].physicsBody.mass = 1.1
            rope_nodes[i].physicsBody.damping = 0.00
            rope_nodes[i].physicsBody.angularDamping = 0.00
            if i > 0:
                physics_world.addBehavior(
                    scn.PhysicsBallSocketJoint.joint(
                        rope_nodes[i - 1].physicsBody,
                        (0, -rope_seg_length / 2, 0),
                        rope_nodes[i].physicsBody,
                        (0, rope_seg_length / 2, 0)))

        physics_world.addBehavior(
            scn.PhysicsBallSocketJoint.joint(rope_nodes[0].physicsBody,
                                             (0, rope_seg_length / 2, 0)))
        physics_world.addBehavior(
            scn.PhysicsBallSocketJoint.joint(rope_nodes[-1].physicsBody,
                                             (0, -rope_seg_length / 2, 0),
                                             ball_node.physicsBody,
                                             (0, ball_radius, 0)))

        ball_node.physicsBody.applyForce((45.0, 0.0, 0.0), True)

        camera_node = scn.Node()
        camera_node.camera = scn.Camera()
        camera_node.position = (0, rope_length + 1.0, rope_length)
        camera_node.lookAt((0, rope_length / 2 + 1.0, 0))
        root_node.addChildNode(camera_node)

        light_node = scn.Node()
        light_node.position = (rope_length, rope_length, rope_length)

        light = scn.Light()
        light.type = scn.LightTypeDirectional
        light.castsShadow = True
        light.shadowSampleCount = 32
        light.color = (.99, 1.0, .86)
        light_node.light = light
        light_node.lookAt((0, rope_length / 2 + 1.0, 0))
        root_node.addChildNode(light_node)

        main_view.present(style='fullscreen', hide_title_bar=False)
Exemple #16
0
 def addTrainToScene(self, scene=None, pos=None):    
   def addTrainToSceneBlock(child):
     if child.geometry is not None and child.geometry != scn.nil:
       node = child.clone()
       node.position = scn.Vector3(node.position.x + pos.x, node.position.y + pos.y, node.position.z + pos.z)
       min, max = node.getBoundingBox()
               
       body = scn.PhysicsBody.dynamicBody()
       boxShape = scn.Box(width=(max.x - min.x), height=(max.y - min.y), length=(max.z - min.z), chamferRadius=0.0)
               
       body.physicsShape = scn.PhysicsShape(geometry=boxShape, options=None)
       
       trans = list(scn.Matrix4Identity)
       trans[13] = -min.y
       node.pivot = trans
       node.physicsBody = body
       scene.rootNode.addChildNode(node)
       return False
   
   trainScene = scn.Scene(url="resources/train_flat_PY.scn")
       
   #physicalize the train with simple boxes
   trainScene.rootNode.enumerateChildNodesUsingBlock(addTrainToSceneBlock)
       
   #add smoke
   self.smokeHandle = scene.rootNode.childNodeWithName("Smoke", recursively=True)
   self.smoke = scn.ParticleSystem()
   
   self.smoke.birthRate = 30
   self.smoke.birthLocation = scn.ParticleBirthLocation.SCNParticleBirthLocationSurface
   self.smoke.loops = True
   self.smoke.emissionDuration = 1
   self.smoke.emissionDurationVariation = 0
   self.smoke.idleDuration = 0
   self.smoke.idleDurationVariation = 0.
   self.smoke.emittingDirection = (0, 1, 0)
   self.smoke.spreadingAngle = 10
   self.smoke.particleAngleVariation = 180
   self.smoke.particleDiesOnCollision = False
   self.smoke.particleLifeSpan = 5.
   self.smoke.particleLifeSpanVariation = 0.
   self.smoke.particleVelocity = 6.
   self.smoke.particleVelocityVariation = 1
   self.smoke.particleImage = ui.Image.named('resources/tex_smoke.png')
   self.smoke.particleSize = 3.
   self.smoke.particleSizeVariation = 0.5
   self.smoke.particleIntensity = 1.
   self.smoke.particleIntensityVariation = 0.
   self.smoke.stretchFactor = 0.0
   self.smoke.particleColor = (.41, .42, .48)
   self.smoke.blendMode = scn.ParticleBlendMode.SCNParticleBlendModeScreen
   
   self.smokeHandle.addParticleSystem(self.smoke)
       
   #add physics constraints between engine and wagons
   engineCar = scene.rootNode.childNodeWithName("EngineCar", recursively=False)
   wagon1 = scene.rootNode.childNodeWithName("Wagon1", recursively=False)
   wagon2 = scene.rootNode.childNodeWithName("Wagon2", recursively=False)
   
   min, max = engineCar.getBoundingBox()        
   wmin, wmax = wagon1.getBoundingBox()
       
   #Tie EngineCar & Wagon1
   joint = scn.PhysicsBallSocketJoint(bodyA=engineCar.physicsBody, anchorA=scn.Vector3(max.x, min.y, 0), bodyB=wagon1.physicsBody, anchorB=scn.Vector3(wmin.x, wmin.y, 0))
   scene.physicsWorld.addBehavior(joint)
       
   #Wagon1 & Wagon2
   joint = scn.PhysicsBallSocketJoint(bodyA=wagon1.physicsBody, anchorA=scn.Vector3(wmax.x + 0.1, wmin.y, 0), bodyB=wagon2.physicsBody, anchorB=scn.Vector3(wmin.x - 0.1, wmin.y, 0))
   scene.physicsWorld.addBehavior(joint)
Exemple #17
0
  def setupVehicle(self, scene):
    chassisNode = scn.Node()
    carScene = scn.Scene(url="resources/rc_car_PY.scn")
    chassisNode = carScene.rootNode.childNodeWithName("rccarBody", recursively=False)
        
    #setup the chassis
    chassisNode.position = scn.Vector3(0, 10, 30)
    chassisNode.rotation = scn.Vector4(0, 1, 0, M_PI)
        
    body = scn.PhysicsBody.dynamicBody()
    body.allowsResting = False
    body.mass = 80
    body.restitution = 0.1
    body.friction = 0.5
    body.rollingFriction = 0
        
    chassisNode.physicsBody = body
    scene.rootNode.addChildNode(chassisNode)
        
    self.pipeNode = chassisNode.childNodeWithName("pipe", recursively=False)
    self.myReactor = scn.ParticleSystem()
    
    self.myReactor.birthRate = 1000
    self.myReactor.birthLocation = scn.ParticleBirthLocation.SCNParticleBirthLocationSurface
    self.myReactor.loops = True
    self.myReactor.emissionDuration = 1
    self.myReactor.emissionDurationVariation = 0
    self.myReactor.idleDuration = 0
    self.myReactor.idleDurationVariation = 0.
    self.myReactor.emittingDirection = (0, 1, 0)
    self.myReactor.spreadingAngle = 10
    self.myReactor.particleAngleVariation = 180
    self.myReactor.particleDiesOnCollision = False
    self.myReactor.particleLifeSpan = 0.15
    self.myReactor.particleLifeSpanVariation = 0.05
    self.myReactor.particleVelocity = 40.
    self.myReactor.particleVelocityVariation = 40.
    self.myReactor.particleImage = ui.Image.named('resources/spark.png')
    self.myReactor.particleSize = 1.
    self.myReactor.particleSizeVariation = 1.
    self.myReactor.particleIntensity = 1.
    self.myReactor.particleIntensityVariation = 0.4
    self.myReactor.stretchFactor = 0.0
    self.myReactor.particleColor = (.87, .45, .0)
    self.myReactor.blendMode = scn.ParticleBlendMode.SCNParticleBlendModeAdditive
        
    self.myReactorDefaultBirthRate = self.myReactor.birthRate
    self.myReactor.birthRate = 0
    self.pipeNode.addParticleSystem(self.myReactor)
        
    #add wheels
    wheel0Node = chassisNode.childNodeWithName("wheelLocator_FL", recursively=True)
    wheel1Node = chassisNode.childNodeWithName("wheelLocator_FR", recursively=True)
    wheel2Node = chassisNode.childNodeWithName("wheelLocator_RL", recursively=True)
    wheel3Node = chassisNode.childNodeWithName("wheelLocator_RR", recursively=True)

    wheel0 = scn.PhysicsVehicleWheel(node=wheel0Node)
    wheel1 = scn.PhysicsVehicleWheel(node=wheel1Node)
    wheel2 = scn.PhysicsVehicleWheel(node=wheel2Node)
    wheel3 = scn.PhysicsVehicleWheel(node=wheel3Node)

    min, max = wheel0Node.getBoundingBox()
       
    wheelHalfWidth = 0.5 * (max.x - min.x)

    p = wheel0Node.convertPosition((0, 0, 0), toNode=chassisNode)
    p = (p.x+wheelHalfWidth, p.y, p.z)
    wheel0.connectionPosition = p
    
    p = wheel1Node.convertPosition((0, 0, 0), toNode=chassisNode)
    p = (p.x-wheelHalfWidth, p.y, p.z)
    wheel1.connectionPosition = p
        
    p = wheel2Node.convertPosition((0, 0, 0), toNode=chassisNode)
    p = (p.x+wheelHalfWidth, p.y, p.z)
    wheel2.connectionPosition = p
    
    p = wheel3Node.convertPosition((0, 0, 0), toNode=chassisNode)
    p = (p.x-wheelHalfWidth, p.y, p.z)
    wheel3.connectionPosition = p
        
    #create the physics vehicle
    vehicle = scn.PhysicsVehicle(chassisBody=chassisNode.physicsBody, wheels=[wheel0, wheel1, wheel2, wheel3])
    scene.physicsWorld.addBehavior(vehicle)
        
    self.myVehicle = vehicle
    return chassisNode
def main():
    main_view = ui.View()
    w, h = ui.get_screen_size()
    main_view.frame = (0, 0, w, h)
    main_view.name = 'Photos cube'

    scene_view = scn.View(main_view.frame, superView=main_view)
    scene_view.autoresizingMask = scn.ViewAutoresizing.FlexibleHeight | scn.ViewAutoresizing.FlexibleRightMargin
    scene_view.antialiasingMode = scn.AntialiasingMode.Multisampling16X

    scene_view.allowsCameraControl = True

    scene_view.backgroundColor = 'white'

    scene_view.delegate = RendererDelegateForMyScene()

    scene = scn.Scene()
    scene_view.scene = scene

    scn.Transaction.disableActions = True

    scene.background.contents = tile_image
    scene.background.contentsTransform = tile_factor
    scene.background.wrapS, scene.background.wrapT = scn.WrapMode.Repeat, scn.WrapMode.Repeat

    root_node = scene.rootNode

    cube_geometry = scn.Box(width=1, height=1, length=1, chamferRadius=0.05)
    cube_geometry_inside = scn.Box(width=1 - 0.001,
                                   height=1 - 0.001,
                                   length=1 - 0.001,
                                   chamferRadius=0.04)

    material_inside = scn.Material()
    material_inside.diffuse.contents = inside_image

    cube_geometry_inside.materials = [material_inside for i in range(6)]

    cube_geometry_materials = [scn.Material() for i in range(6)]
    for i in range(6):
        cube_geometry_materials[i].diffuse.contents = cube_images[
            random.randrange(len(cube_image_names))]
    cube_geometry.materials = cube_geometry_materials

    cube_node = scn.Node.nodeWithGeometry(cube_geometry)
    cube_node.name = 'cube node'
    cube_node_inside = scn.Node.nodeWithGeometry(cube_geometry_inside)
    cube_node.addChildNode(cube_node_inside)

    cube_node.position = (0., 0., -5.)

    cube_action = scn.Action.scaleTo(1.5, 3.0)
    cube_reversed_action = scn.Action.scaleTo(1.0, 1.0)
    cube_wait_action = scn.Action.waitForDuration(0.8)
    cube_y_rot_action1 = scn.Action.rotateBy(0, 0.2, 0, 0.3)
    cube_y_rot_action2 = scn.Action.rotateBy(0, -0.4, 0, 0.3)
    cube_y_rot_action3 = scn.Action.rotateBy(0, 0.2, 0, 0.3)

    cube_sequence = scn.Action.sequence([
        cube_action, cube_reversed_action, cube_wait_action,
        cube_y_rot_action1, cube_y_rot_action2, cube_y_rot_action3,
        cube_wait_action
    ])
    cube_forever = scn.Action.repeatActionForever(cube_sequence)

    cube_node.runAction(cube_forever)

    constraint = scn.LookAtConstraint.lookAtConstraintWithTarget(cube_node)
    constraint.gimbalLockEnabled = True

    camera = scn.Camera()
    camera_node = scn.Node()
    camera_node.name = 'camera!'
    camera_node.camera = camera
    camera_node.position = (-3.0, 3.0, 3.0)
    camera_node.constraints = [constraint]

    lights_node = scn.Node()

    ambient_light = scn.Light()
    ambient_light.type = scn.LightTypeAmbient
    ambient_light.name = 'ambient light'
    ambient_light.color = (.99, 1.0, .86)
    ambient_node = scn.Node()
    ambient_node.light = ambient_light
    lights_node.addChildNode(ambient_node)

    directional_light = scn.Light()
    directional_light.type = scn.LightTypeDirectional
    directional_light.name = 'directional light'
    directional_light.color = 'white'
    directional_node = scn.Node()
    directional_node.light = directional_light
    directional_node.position = camera_node.position
    directional_node.constraints = [constraint]

    lights_node.addChildNode(directional_node)

    root_node.addChildNode(camera_node)
    root_node.addChildNode(lights_node)
    root_node.addChildNode(cube_node)
    scene_view.pointOfView = camera_node

    scn.Transaction.begin()
    scn.Transaction.disableActions = False
    scn.Transaction.animationDuration = 10.0
    directional_light.color = 'yellow'
    scn.Transaction.commit()

    scene_view.playing = True

    main_view.present(style='fullscreen', hide_title_bar=False)
Exemple #19
0
def demo():
    main_view = ui.View()
    w, h = ui.get_screen_size()
    main_view.frame = (0, 0, w, h)
    main_view.name = 'Tetrahedron'

    scene_view = scn.View(main_view.frame, superView=main_view)
    scene_view.autoresizingMask = scn.ViewAutoresizing.FlexibleHeight | scn.ViewAutoresizing.FlexibleRightMargin
    scene_view.allowsCameraControl = True

    scene_view.scene = scn.Scene()

    root_node = scene_view.scene.rootNode

    camera_node = scn.Node()
    camera_node.camera = scn.Camera()
    camera_node.position = (0, 0, 5)
    root_node.addChildNode(camera_node)

    verts = [
        scn.Vector3(0, 1, 0),
        scn.Vector3(-0.5, 0, 0.5),
        scn.Vector3(0.5, 0, 0.5),
        scn.Vector3(0.5, 0, -0.5),
        scn.Vector3(-0.5, 0, -0.5),
        scn.Vector3(0, -1, 0)
    ]

    source = scn.GeometrySource.geometrySourceWithVertices(verts)

    indexes = [
        3, 3, 3, 3, 3, 3, 3, 3, 0, 1, 2, 2, 3, 0, 3, 4, 0, 4, 1, 0, 1, 5, 2, 2,
        5, 3, 3, 5, 4, 4, 5, 1
    ]

    elements = scn.GeometryElement.geometryElementWithData(
        indexes, scn.GeometryPrimitiveType.Polygon)

    geometry = scn.Geometry.geometryWithSources(source, elements)

    material = scn.Material()
    material.contents = scn.RGBA(1, 0.9, 0.9, 1.0)
    geometry.materials = material

    geometry_node = scn.Node.nodeWithGeometry(geometry)
    root_node.addChildNode(geometry_node)

    elements2 = []
    materials = []
    colors = [
        'red', 'blue', 'green', 'yellow', 'orange', 'pink', 'cyan', 'orchid'
    ]
    for i in range(8):
        j = 8 + i * 3
        indexes2 = [indexes[j], indexes[j + 1], indexes[j + 2]]
        element = scn.GeometryElement.geometryElementWithData(
            indexes2, scn.GeometryPrimitiveType.Triangles)
        elements2.append(element)
        material = scn.Material()
        material.contents = colors[i]
        materials.append(material)
    geometry2 = scn.Geometry.geometryWithSources(source, elements2)
    geometry2.materials = materials
    geometry2_node = scn.Node.nodeWithGeometry(geometry2)
    tx, ty, tz = (-1.5, 0, 0)
    translation = (1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, tx, ty, tz, 1)
    geometry2_node.pivot = translation
    root_node.addChildNode(geometry2_node)

    constraint = scn.LookAtConstraint.lookAtConstraintWithTarget(geometry_node)
    constraint.gimbalLockEnabled = True
    camera_node.constraints = constraint

    light_node = scn.Node()
    light_node.position = (100, 0, -10)
    light = scn.Light()
    light.type = scn.LightTypeDirectional
    light.castsShadow = True
    light.color = 'white'
    light_node.light = light
    root_node.addChildNode(light_node)

    rotate_action = scn.Action.repeatActionForever(
        scn.Action.rotateBy(0, math.pi * 2, 0, 10))
    geometry_node.runAction(rotate_action)
    geometry2_node.runAction(rotate_action)

    main_view.present(hide_title_bar=False)