i have a hardware with some keys and i want control some objects in blender with those keys. for example if i pushed #1 key... camera rotate 1 degree
i can send, receive and move objects but my problem is freezing !!!!
i want blender receive data in real time ( always ) and process incoming data. I'm sending data unceasing (non stop). is it possible to run a python script (unlimited loop within) in the background to prevent freezing blender ?
for example i have following code :
import bpy
import serial
import mathutils
from time import sleep
ser = serial.Serial("COM7", 9600)
while True:
data = ser.readline()
obj = bpy.data.objects["Lamp"]
# one blender unit in x-direction
vec = mathutils.Vector((int(data), 0, 0))
inv = obj.matrix_world.copy()
inv.invert()
# vec aligned to local axis
vec_rot = vec * inv
obj.location = obj.location - vec_rot
serialport.close()
it's work well outside the loop. but in this case, blender is going to Not Responding. i want object's movement in real time
i am not sure you can understand my problem .... sorry for my bad En language
Edit: i want to run this process in the background .... Modal Operator is working but still freezing blender .... i want do other works and in the background, blender receive serial data and process it
it's my modal operator ( receive degree from serial and rotate an object ). but in this case rotating only works when i move the mouse, if i leave that, rotating will be fully lagged :
import bpy
from bpy import context
import serial
from time import sleep
ser = serial.Serial("COM7", 9600)
class ModalOperator(bpy.types.Operator):
bl_idname = "object.modal_operator"
bl_label = "Simple Modal Operator"
x = 0
def modal(self, context, event):
self.x = int(ser.readline())
if self.x >0:
value = int(self.x)
value += 10
degree = value * 3.14/180
bpy.context.object.rotation_euler[0] = degree
print(degree)
if event.type == 'LEFTMOUSE':
ser.close()
return {'FINISHED'}
return {'RUNNING_MODAL'}
def invoke(self, context, event):
if context.object:
print("hi")
context.window_manager.modal_handler_add(self)
return {'RUNNING_MODAL'}
else:
self.report({'WARNING'}, "No active object")
return {'CANCELLED'}
def register():
bpy.utils.register_class(ModalOperator)
def unregister():
bpy.utils.unregister_class(ModalOperator)
if __name__ == "__main__":
register()
bpy.ops.object.modal_operator('INVOKE_DEFAULT')