|
# move FreeCAD camera with a gamepad |
|
|
|
import ctypes |
|
import time |
|
from sdl2 import * |
|
|
|
from pivy.coin import SbVec3f, SbRotation |
|
from PySide import QtCore |
|
from math import pi |
|
|
|
class JoystickNavi: |
|
def __init__(self): |
|
SDL_Init(SDL_INIT_JOYSTICK) |
|
self.axis = {1: 0, 2: 0, 3: 0, 4: 0} |
|
self.button = {} |
|
# adjust linear and angular velocity coefficient below |
|
self.vel_coeff = 0.001 |
|
self.angv_coeff = 0.0000001 |
|
self.camera = Gui.ActiveDocument.ActiveView.getCameraNode() |
|
self.timer = QtCore.QTimer() |
|
QtCore.QObject.connect(self.timer, QtCore.SIGNAL("timeout()"), self.update) |
|
self.timer.start(16) |
|
|
|
def update(self): |
|
event = SDL_Event() |
|
while SDL_PollEvent(ctypes.byref(event)) != 0: |
|
if event.type == SDL_JOYDEVICEADDED: |
|
self.device = SDL_JoystickOpen(event.jdevice.which) |
|
elif event.type == SDL_JOYAXISMOTION: |
|
self.axis[event.jaxis.axis] = event.jaxis.value |
|
elif event.type == SDL_JOYBUTTONDOWN: |
|
self.button[event.jbutton.button] = True |
|
elif event.type == SDL_JOYBUTTONUP: |
|
self.button[event.jbutton.button] = False |
|
# print(joystick.axis) |
|
# print(joystick.button) |
|
# axes numbers may need adjustments for your gamepad |
|
dz = -self.axis[1] * self.vel_coeff |
|
dy = self.axis[2] * self.vel_coeff |
|
dx = self.axis[4] * self.vel_coeff |
|
drot = -self.axis[3] * self.angv_coeff |
|
cam_rot = self.camera.orientation.getValue() |
|
cam_pos = self.camera.position.getValue() |
|
cam_rot_mod = SbRotation(SbVec3f(0, 0, 1), drot) |
|
cam_pos_mod = cam_rot.multVec(SbVec3f(dy, dz, dx)) |
|
|
|
self.camera.orientation.setValue(cam_rot * cam_rot_mod) |
|
self.camera.position.setValue(cam_pos + cam_pos_mod) |
|
|
|
def stop(self): |
|
self.timer.stop() |
|
|
|
if __name__ == "__main__": |
|
navi = JoystickNavi() |
|
|
|
# type navi.stop() to stop script |