Anvil Blender Uplink connection

Hi Guys,

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.

include @anvil.server.callable above

def selected_objects():

and try?

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.

You may be inadvertently running your callables from another module.

Check all your environments for the setting enviornment - show advanced settings - uplink keys - send server calls from other environments to this uplink.

Make sure you are running the uplink version 0.3.42 or above.

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.

is it possible if you could set up a virtual-env in blender and then try to run the code in that new virtual-env?

i’m guessing you installed anvil uplink in a virtual-env and run the code there. however when u run from the console the code is running in root-env ?

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:

1 Like