Skip to content

Instantly share code, notes, and snippets.

@isti115
Created June 19, 2020 20:10
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save isti115/8a12d617f56185a5d16b2008d97ae7ce to your computer and use it in GitHub Desktop.
Save isti115/8a12d617f56185a5d16b2008d97ae7ce 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" : "István Donkó",
"description" : "Control the blender viewport using a gamepad",
"blender" : (2, 80, 1),
# "location" : "View3D",
"category" : "Generic"
}
import bpy
from mathutils import *
# Getting evdev
import subprocess
import ensurepip
ensurepip.bootstrap()
pybin = bpy.app.binary_path_python
subprocess.check_call([pybin, '-m', 'pip', 'install', 'evdev'])
#
import evdev
def get_device():
device_list = evdev.list_devices()
if (len(device_list) > 0):
return evdev.InputDevice(device_list[0])
else:
return None
# device = evdev.InputDevice('/dev/input/event26')
# rv3d = bpy.context.screen.areas[5].spaces[0].region_3d
# rv3d = bpy.data.screens[2].areas[3].spaces[0].region_3d
# for area in bpy.context.screen.areas:
# if area.type == 'VIEW_3D':
# if context.active_object.mode == 'EDIT':
# area.spaces[0].use_occlude_geometry = True
# device.absinfo(0).value
# mode = 'absolute'
mode = 'relative'
def view3d():
return bpy.data.screens[2].areas[3].spaces[0].region_3d
class GamepadControl:
# def __init__(self, device):
def __init__(self):
self.data = []
# self.device = device
# self.rv3d = bpy.data.screens[2].areas[3].spaces[0]
def get_axis_value(self, i):
return (self.device.absinfo(i).value - 128) / 128
def check_gamepad(self):
# while (event := device.read_one()):
# v = (event.value - 128) / 128
# e = rv3d.view_rotation.to_euler()
# if event.code == 2:
# # rv3d.view_rotation.y = (event.value - 128) / 128
# # print((event.value - 128) / 128)
# rv3d.view_rotation = Euler((v*5,0,0)).to_quaternion()
# # e[0] = v*5
# if event.code == 5:
# # rv3d.view_rotation.x = (event.value - 128) / 128
# rv3d.view_rotation = Euler((0,0,v*5)).to_quaternion()
# # e[2] = v*5
# rv3d.view_rotation = e.to_quaternion()
[a, b, c, d] = map(self.get_axis_value, [0, 1, 2, 5])
# print([a,b,c,d])
if (mode == 'absolute'):
# rv3d.view_rotation = Euler((b*2,a*2,d*2)).to_quaternion()
view3d().view_rotation = Euler((b*2,a*2,d*2)).to_quaternion()
if (mode == 'relative'):
# rv3d.view_rotation.rotate(Euler((b*0.1,a*0.1,d*0.1)))
speed = 0.05
view3d().view_rotation.rotate(Euler((b*speed,a*speed,d*speed)))
# rv3d.view_distance += c
view3d().view_distance += c
# return (1 / 30)
gamepad_control = GamepadControl()
def main_loop():
gamepad_control.check_gamepad()
return (1 / 30)
def register():
device = get_device()
if device:
# gamepad_control = GamepadControl(device)
gamepad_control.device = device
# bpy.app.timers.register(gamepad_control.check_gamepad)
bpy.app.timers.register(main_loop)
def unregister():
# print(gamepad_control)
# if gamepad_control:
# print("asdf")
if(bpy.app.timers.is_registered(main_loop)):
bpy.app.timers.unregister(main_loop)
# if(bpy.app.timers.is_registered(gamepad_control.check_gamepad)):
# bpy.app.timers.unregister(gamepad_control.check_gamepad)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment