og-libsurvive/bindings/python/libsurvive.py

84 lines
2.9 KiB
Python
Raw Permalink Normal View History

2018-04-04 10:42:51 -06:00
import ctypes
_libsurvive = ctypes.CDLL('./_libsurvive.so')
LP_c_char = ctypes.POINTER(ctypes.c_char)
LP_LP_c_char = ctypes.POINTER(LP_c_char)
2018-04-10 23:48:36 -06:00
_libsurvive.survive_simple_init.argtypes = (ctypes.c_int, LP_LP_c_char) # argc, argv
_libsurvive.survive_simple_init.restype = ctypes.c_void_p
2018-04-04 10:42:51 -06:00
2018-04-10 23:48:36 -06:00
_libsurvive.survive_simple_get_next_object.argtypes = [ctypes.c_void_p, ctypes.c_void_p]
_libsurvive.survive_simple_get_next_object.restype = ctypes.c_void_p
2018-04-04 10:42:51 -06:00
2018-04-10 23:48:36 -06:00
_libsurvive.survive_simple_get_first_object.argtypes = [ctypes.c_void_p]
_libsurvive.survive_simple_get_first_object.restype = ctypes.c_void_p
2018-04-04 10:42:51 -06:00
2018-04-10 23:48:36 -06:00
_libsurvive.survive_simple_get_next_updated.argtypes = [ctypes.c_void_p]
_libsurvive.survive_simple_get_next_updated.restype = ctypes.c_void_p
2018-04-04 10:42:51 -06:00
2018-04-10 23:48:36 -06:00
_libsurvive.survive_simple_object_name.argtypes = [ ctypes.c_void_p ]
_libsurvive.survive_simple_object_name.restype = ctypes.c_char_p
2018-04-04 10:42:51 -06:00
2018-04-10 23:48:36 -06:00
_libsurvive.survive_simple_is_running.argtypes = [ctypes.c_void_p]
_libsurvive.survive_simple_is_running.restype = ctypes.c_bool
2018-04-04 10:42:51 -06:00
2018-04-10 23:48:36 -06:00
_libsurvive.survive_simple_start_thread.argtypes = [ctypes.c_void_p]
_libsurvive.survive_simple_start_thread.restype = None
2018-04-04 10:42:51 -06:00
class SurvivePose(ctypes.Structure):
_fields_ = [
('Pos', ctypes.c_double * 3),
('Rot', ctypes.c_double * 4)
]
def __repr__(self):
return '[{0} {1} {2}], [{3} {4} {5} {6}]'.format(self.Pos[0],self.Pos[1],self.Pos[2],self.Rot[0],self.Rot[1],self.Rot[2],self.Rot[3])
2018-04-10 23:48:36 -06:00
_libsurvive.survive_simple_object_get_latest_pose.argtypes = [ctypes.c_void_p, ctypes.POINTER(SurvivePose)]
_libsurvive.survive_simple_object_get_latest_pose.restype = ctypes.c_uint
2018-04-04 10:42:51 -06:00
2018-04-10 23:48:36 -06:00
class SimpleObject:
2018-04-04 10:42:51 -06:00
ptr = 0
def __init__(self, ptr):
self.ptr = ptr
def Name(self):
2018-04-10 23:48:36 -06:00
return _libsurvive.survive_simple_object_name(self.ptr)
2018-04-04 10:42:51 -06:00
def Pose(self):
pose = SurvivePose()
2018-04-10 23:48:36 -06:00
time = _libsurvive.survive_simple_object_get_latest_pose(self.ptr, pose)
2018-04-04 10:42:51 -06:00
return (pose, time)
2018-04-10 23:48:36 -06:00
class SimpleContext:
2018-04-04 10:42:51 -06:00
ptr = 0
objects = []
def __init__(self, args):
argc = len(args)
argv = (LP_c_char * (argc + 1))()
for i, arg in enumerate(args):
enc_arg = arg.encode('utf-8')
argv[i] = ctypes.create_string_buffer(enc_arg)
2018-04-10 23:48:36 -06:00
self.ptr = _libsurvive.survive_simple_init(argc, argv)
2018-04-04 10:42:51 -06:00
self.objects = []
2018-04-10 23:48:36 -06:00
curr = _libsurvive.survive_simple_get_first_object(self.ptr);
2018-04-04 10:42:51 -06:00
while curr:
2018-04-10 23:48:36 -06:00
self.objects.append(SimpleObject(curr))
curr = _libsurvive.survive_simple_get_next_object(self.ptr, curr);
_libsurvive.survive_simple_start_thread(self.ptr)
2018-04-04 10:42:51 -06:00
def Objects(self):
return self.objects
def Running(self):
2018-04-10 23:48:36 -06:00
return _libsurvive.survive_simple_is_running(self.ptr)
2018-04-04 10:42:51 -06:00
def NextUpdated(self):
2018-04-10 23:48:36 -06:00
ptr = _libsurvive.survive_simple_get_next_updated(self.ptr)
2018-04-04 10:42:51 -06:00
if ptr:
2018-04-10 23:48:36 -06:00
return SimpleObject(ptr)
2018-04-04 10:42:51 -06:00
return None