add ps4 controller
This commit is contained in:
@@ -9,17 +9,21 @@ import adsk.fusion
|
||||
import adsk.cam
|
||||
import traceback
|
||||
import json
|
||||
from ...lib import hid
|
||||
|
||||
CMD_NAME = 'View Controller backend'
|
||||
|
||||
app = adsk.core.Application.get()
|
||||
ui = app.userInterface
|
||||
|
||||
myThread = None
|
||||
stopFlag = None
|
||||
|
||||
cameraViewEvent = 'CameraViewEventId'
|
||||
updateConstantsEvent = "UpdateConstantsEventId"
|
||||
|
||||
customEvents = []
|
||||
local_handlers = []
|
||||
l_handlers = []
|
||||
|
||||
X_ROTATION = 0
|
||||
Y_ROTATION = 0
|
||||
@@ -44,6 +48,7 @@ class UpdateConstantsEventHandler(adsk.core.CustomEventHandler):
|
||||
X_PAN = float(eventArgs["X_pan"])
|
||||
Y_PAN = float(eventArgs["Y_pan"])
|
||||
Z_PAN = float(eventArgs["Z_pan"])
|
||||
myThread.start()
|
||||
|
||||
|
||||
# The event handler that responds to the custom event being fired.
|
||||
@@ -93,9 +98,9 @@ class CameraViewEventHandler(adsk.core.CustomEventHandler):
|
||||
eye.transformBy(rotate_matrix)
|
||||
up.transformBy(rotate_matrix)
|
||||
|
||||
X_pan = float(eventArgs.get('X_pan', 0)) * X_pan
|
||||
Y_pan = float(eventArgs.get('Y_pan', 0)) * Y_pan
|
||||
Z_pan = float(eventArgs.get('Z_pan', 0)) * Z_pan
|
||||
X_pan = float(eventArgs.get('X_pan', 0)) * X_PAN
|
||||
Y_pan = float(eventArgs.get('Y_pan', 0)) * Y_PAN
|
||||
Z_pan = float(eventArgs.get('Z_pan', 0)) * Z_PAN
|
||||
if any([X_pan, Z_pan]):
|
||||
move_vector = adsk.core.Vector3D.create(X_pan, 0, Z_pan)
|
||||
move_vector.transformBy(rotate_matrix)
|
||||
@@ -105,7 +110,7 @@ class CameraViewEventHandler(adsk.core.CustomEventHandler):
|
||||
if Y_pan:
|
||||
zoom = zoom + Y_pan * 10
|
||||
|
||||
camera.isSmoothTransition = True
|
||||
camera.isSmoothTransition = False
|
||||
camera.eye = eye
|
||||
camera.upVector = up
|
||||
camera.target = target
|
||||
@@ -128,13 +133,30 @@ class ControllerThread(threading.Thread):
|
||||
self.stopped = event
|
||||
|
||||
def run(self):
|
||||
futil.log(f'{CMD_NAME} ControllerThread started')
|
||||
gamepad = hid.Device(vid=1356, pid=1476)
|
||||
gamepad.nonblocking = True
|
||||
# Every five seconds fire a custom event, passing a random number.
|
||||
while not self.stopped.wait(2):
|
||||
futil.log(f'{CMD_NAME} Thread start sending data')
|
||||
args = {'X_rotation': 0.0, 'Y_rotation': 0.0, 'Z_rotation': 0.0,
|
||||
'X_pan': 0.0, 'Y_pan': 0.0, 'Z_pan': 0.0}
|
||||
while not self.stopped.wait(0.01):
|
||||
# futil.log(f'{CMD_NAME} Thread start sending data')
|
||||
report = list(gamepad.read(128))
|
||||
args = {
|
||||
'X_rotation': 0.0,
|
||||
'Y_rotation': self.convert(report[4], 10),
|
||||
'Z_rotation': self.convert(report[3], 10),
|
||||
'X_pan': self.convert(report[1], 10),
|
||||
'Y_pan': self.convert(report[2], 10),
|
||||
'Z_pan': 0.0
|
||||
}
|
||||
app.fireCustomEvent(cameraViewEvent, json.dumps(args))
|
||||
|
||||
def convert(self, input, dedzone) -> float:
|
||||
normal: float = input - 127
|
||||
if abs(normal) < dedzone:
|
||||
return 0.0
|
||||
normal = normal / 128
|
||||
return normal
|
||||
|
||||
|
||||
def start():
|
||||
|
||||
@@ -144,26 +166,25 @@ def start():
|
||||
customEvents.append(app.registerCustomEvent(cameraViewEvent))
|
||||
onCameraViewEvent = CameraViewEventHandler()
|
||||
customEvents[0].add(onCameraViewEvent)
|
||||
local_handlers.append(onCameraViewEvent)
|
||||
l_handlers.append(onCameraViewEvent)
|
||||
futil.log(f'{CMD_NAME} CameraViewEvent register')
|
||||
|
||||
customEvents.append(app.registerCustomEvent(updateConstantsEvent))
|
||||
onUpadateConstantEvent = UpdateConstantsEventHandler()
|
||||
customEvents[1].add(onUpadateConstantEvent)
|
||||
local_handlers.append(onUpadateConstantEvent)
|
||||
l_handlers.append(onUpadateConstantEvent)
|
||||
futil.log(f'{CMD_NAME} UpdateConstantsEvent register')
|
||||
|
||||
# Create a new thread for the other processing.
|
||||
global stopFlag
|
||||
global stopFlag, myThread
|
||||
stopFlag = threading.Event()
|
||||
myThread = ControllerThread(stopFlag)
|
||||
# myThread.start()
|
||||
futil.log(f'{CMD_NAME} ControllerThread started')
|
||||
|
||||
|
||||
def stop():
|
||||
try:
|
||||
for event, handler in zip(customEvents, local_handlers):
|
||||
for event, handler in zip(customEvents, l_handlers):
|
||||
event.remove(handler)
|
||||
# if local_handlers.count:
|
||||
# customEvent.remove(local_handlers[0])
|
||||
|
||||
Reference in New Issue
Block a user