def _spnav_event_loop(self): import spnav kill_pending_events = False try: while self._running: if not self.focus_test(): kill_pending_events = True self.cmd._sdof(0., 0., 0., 0., 0., 0.) time.sleep(0.1) continue if kill_pending_events: kill_pending_events = False while spnav.spnav_poll_event() is not None: pass event = spnav.spnav_wait_event() if event.ev_type == spnav.SPNAV_EVENT_MOTION: t, r = event.translation, event.rotation args = (t[0], -t[2], -t[1], r[0], -r[2], -r[1]) args = [v * SCALE for v in args] self.cmd._sdof(*args) elif event.ev_type == spnav.SPNAV_EVENT_BUTTON: if event.press: self.cmd._sdof_button(event.bnum + 1) except spnav.SpnavWaitException: print('SpnavWaitException: spacenavd stopped?') finally: spnav.spnav_close()
def run(self): """ Main Loop """ ret = None while ret == None: for event in pygame.event.get(): if event.type == pygame.QUIT: ret = pygame.QUIT continue spnav_event = spnav.spnav_poll_event() if spnav_event is not None: if isinstance(spnav_event, spnav.SpnavMotionEvent): evt = { 'period': spnav_event.period, 'rotation': spnav_event.rotation, 'translation': spnav_event.translation } self.old_evt = self.evt self.evt = evt self.old_delta = self.delta self.delta['period'] = evt['period'] try: self.delta['period'] = self.delta['period'] / self.old_delta['period'] except: pass self.delta['x'] += evt['rotation'][0] * self.old_delta['period'] self.delta['y'] += evt['rotation'][1] * self.old_delta['period'] self.delta['z'] += evt['rotation'][2] * self.old_delta['period'] spnav.spnav_remove_events(spnav.SPNAV_EVENT_MOTION) elif isinstance(spnav_event, spnav.SpnavButtonEvent): ret = spnav.SpnavButtonEvent continue self.clock.tick(50) self.screen.fill((0,32,0)) # It will hold transformed vertices. t = [] for v in self.vertices: # Rotate the point around X axis, then around Y axis, and finally around Z axis. r = v.rotateX(self.delta['x']).rotateY(self.delta['y']).rotateZ(self.delta['z']) # Transform the point from 3D to 2D p = r.project(self.screen.get_width(), self.screen.get_height(), 256, 4) # Put the point in the list of transformed vertices t.append(p) # Calculate the average Z values of each face. avg_z = [] i = 0 for f in self.faces: z = (t[f[0]].z + t[f[1]].z + t[f[2]].z + t[f[3]].z) / 4.0 avg_z.append([i,z]) i = i + 1 # Draw the faces using the Painter's algorithm: # Distant faces are drawn before the closer ones. for tmp in sorted(avg_z,key=itemgetter(1),reverse=True): face_index = tmp[0] f = self.faces[face_index] pointlist = [(t[f[0]].x, t[f[0]].y), (t[f[1]].x, t[f[1]].y), (t[f[1]].x, t[f[1]].y), (t[f[2]].x, t[f[2]].y), (t[f[2]].x, t[f[2]].y), (t[f[3]].x, t[f[3]].y), (t[f[3]].x, t[f[3]].y), (t[f[0]].x, t[f[0]].y)] pygame.draw.polygon(self.screen,self.colors[face_index], pointlist) pygame.display.flip() # gracefully exit spnav.spnav_close() if ret == pygame.QUIT: print('Closed the window?') pass if ret == spnav.SpnavButtonEvent: print('Clicked button to exit.') pass pygame.quit() sys.exit()
from spnav import spnav_open, spnav_poll_event, spnav_close if __name__ == '__main__': spnav_open() try: while True: event = spnav_poll_event() if event is not None: print event except KeyboardInterrupt: print '\nQuitting...' finally: spnav_close()
def test_spnav_close(): m = mock_libspnav({'spnav_close': 0}) spnav.spnav_close() assert m.spnav_close.called
def poll_loop(): event = spnav_poll_event() if event is not None: if event.ev_type == SPNAV_EVENT_MOTION: process_spnav_evnt(event) try: while True: poll_loop() except KeyboardInterrupt: print '\nQuitting...' except socket.error, err: print "Connection error: " + err.strerror finally: spnav_close() socket_close(s) def send(msg, sock=None): data = msg + "\n" sock.send(data) if not QUITE: print msg def socket_close(sock): sock.close() def normalize(event):