I’m trying out anvil as a possible way of building some pipeline tools for Blender.
I successfully got uplink to work, I can call a function inside blender from the web interface, and get a message back.
But when I try to call some native blender code, I get some errors.
If I call the same code in the blender console, it works without any issues.
Code Sample:
import anvil.server
import bpy
import threading
anvil.server.connect("my uplink key")
def to_run(): # should not take arguments.
... # code to execute in the thread (can be a loop, ...)
# it uses the same variables as in the main thread.
anvil.server.wait_forever()
new_thread = threading.Thread() # create a new thread object.
new_thread.run = to_run
new_thread.start() # the new thread is created and then is running.
def selected_objects():
return bpy.context.selected_objects
@anvil.server.callable
def get_selected_objects():
selected_objects_names = []
objects = selected_objects()
for ob in objects:
selected_objects_names.append(ob.name)
return selected_objects_names ```
This is the error I am getting inside anvil:
AttributeError: 'Context' object has no attribute 'selected_objects'
at /Anvil.py, line 24
called from Form1, line 14
The anvil app is a simple textbox and button, and it’s supposed to show the list of selected objects from blender, in the text box when pressing the button.
Doesn’t work, I also tried including all code in 1 function. Some other people mentioned it might be because the new thread doesn’t have access to the same variables as the main thread.
Hmm, as far as I understand this would be the case if having multiple environments.
I’m on the free tier, so only have 1 environment.
It works partially, because if I call a function in blender that returns a string, I can display it my anvil app, but I cannot call internal blender functions from the browser.
That’s why I’m leaning to believe it is a problem about how I am setting up the threads inside blender, and not a problem about anvil itself.
From what I understand blender is running it’s own python interpreter, and it’s not possible to set-up a virtual environment. But I’m not an experienced developer so can’t say for sure.
Ok, I have found a solution, it seems that for some reason bpy.context doesn’t work remotely. But bpy.data gives me access to all the scene data directly, and I can get access to everything.
But I just realised a problem.
I am using a @anvil.server.callable function. I want to be able to give this script to multiple people, to let the control blender from their browser.
Is there any way to let each user call their own @anvil.server.callable function? From their instance of blender? Or is there any other better way to do this?
EDIT:
Nevermind, I have found this other thread that provides a solution by using custom function names based on the ID: