If you add
while True:
print("hi")
to an otherwise functional script in Blender’s scripting window and run it, Blender hangs.
This is undesirable because I’m trying to use Redis as a pub-sub broker, such that another script can send positions for Blender to display:
bpy.ops.mesh.primitive_uv_sphere_add(radius=0.005, location=(0, 0, 0))
ball = bpy.context.object
ball.name = 'sphere'
for message in pubsub.listen():
if message['type'] == 'message':
position_str = message['data'].decode('utf-8')
position = ast.literal_eval(position_str)
print(f"Received position: {position}")
ball.location = position
This forever loop also hangs Blender and doesn’t move the ball as I want.
Is there any way to get this to work?
Update:
I tried doing it in a subprocess, it no longer hangs but it doesn’t create a sphere either:
def update_sphere_from_redis():
r = redis.StrictRedis(host='localhost', port=6379, db=0)
pubsub = r.pubsub()
pubsub.subscribe('positions')
bpy.ops.mesh.primitive_uv_sphere_add(radius=0.005, location=(0, 0, 0))
ball = bpy.context.object
ball.name = 'sphere'
for message in pubsub.listen():
if message['type'] == 'message':
position_str = message['data'].decode('utf-8')
position = ast.literal_eval(position_str)
print(f"Received position: {position}")
ball.location = position
process = multiprocessing.Process(target=update_sphere_from_redis)
# Start the processB
process.start()
2
Answers
In blender a while true loop will cause the program to freeze. Add an exception even if you are not going to activate it.
Blender scripts execute on the same interpreter as the UI (check out this answer and the relevant blender docs for more information) meaning that blender will wait for whatever script is running to finish before continuing which in the case of an infinite loop will freeze blender’s UI.
I don’t know much about blender scripting but if you want to know where to start when looking to implement your script, I suggest you look into python modules like asyncio and concurrent.futures. This seems like a good post to start looking into.