Skip to content

Instantly share code, notes, and snippets.

@mokocchi
Forked from isti115/gamepad-control.py
Last active April 2, 2023 14:22
Show Gist options
  • Save mokocchi/5d887d28804fd28da770f456094e32dd to your computer and use it in GitHub Desktop.
Save mokocchi/5d887d28804fd28da770f456094e32dd to your computer and use it in GitHub Desktop.
Proof of concept blender addon for controlling the viewport with a gamepad (evdev python module needed, only tested under linux)
bl_info = {
"name" : "Gamepad Control",
"author" : "Mokocchi",
"description" : "Control the blender viewport using a gamepad - based on István Donkó PoC",
"blender" : (3, 4, 1),
"category" : "Generic"
}
import evdev
import bpy
from mathutils import *
def get_device():
device_list = evdev.list_devices()
if (len(device_list) > 0):
return evdev.InputDevice(device_list[0])
else:
print("No device found")
exit(1)
def view3d():
return bpy.data.screens['Layout'].areas[3].spaces[0].region_3d
class GamepadControl:
main_device = None
def __init__(self, device):
self.data = []
self.device = device
self.speed = 0.05
GamepadControl.main_device = self
def create_main_device(device):
main_device = GamepadControl(device)
def get_main_device():
return GamepadControl.main_device
def speed(self, speed):
self.speed = speed
def get_axis_value(self, i):
return (self.device.absinfo(i).value - 128) / 128
def check_gamepad(self):
[a, b, c, d] = map(self.get_axis_value, [0, 1, 2, 5])
speed = self.speed
view3d().view_rotation.rotate(Euler((c*speed,b*speed,a*speed)))
view3d().view_distance += d
def main_loop():
GamepadControl.get_main_device().check_gamepad()
return (1 / 30)
def register():
device = get_device()
print("gamepad control enabled")
if device:
GamepadControl.create_main_device(device)
bpy.app.timers.register(main_loop)
def unregister():
if(bpy.app.timers.is_registered(main_loop)):
bpy.app.timers.unregister(main_loop)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment