Running Julia inside Blender through VSCode (using PythonCall/JuliaCall)

Hi all, I’ve been using a workflow that I came up with that consists of using Blender through its GUI and programmatically with Julia at the same time.

I usually develop in Julia using VSCode and its “execute line” and “execute cell” commands, so I was able to do that but with Julia running inside the Python that is running inside Blender, which allows me to interface with Blender itself through the Blender Python API (bpy module).

I’m dropping in here the instructions to set up such a workflow since this can be helpful to someone else or someone can even improve the usability of this.

Running Julia inside Blender through VSCode (using PythonCall/JuliaCall)

Install Blender

  • Install Blender into a user folder (any folder where user has rights to write). This allows us to install Python packages with Blender’s pip into Blender’s internal Python “site-packages” folder.

Install JuliaCall within Blender Python environment

  • Open Blender and run this on the Blender’s Python console:
    import pip
    pip.main(['install', 'juliacall'])
    
  • This needs to be done only once on each new Blender installation.

Connect a VSCode session to a Blender instance

  • Open VSCode.
  • Execute Blender from some terminal (may be a VSCode integrated terminal). This allows us to see Julia output there.
  • In VSCode, call the command Julia: Connect external REPL.
  • Copy the generated code and paste it into the indicated line of the following code
    c = r'''
    PASTE CODE GENERATED BY `Julia: Connect external REPL` COMMAND INTO THIS LINE
    '''
    
    import sys
    sys.ps1 = '>>> ' # make PythonCall believe that Python is interactive so that display uses terminal size
    import juliacall
    juliacall.Main.seval(c)
    
    def julia_work():
        juliacall.Main.sleep(0.001)
        return 0.02
    
    julia_timer = bpy.app.timers.register(julia_work, persistent=True)
    
  • Copy the entire code above, paste it and run it on the Blender’s Python console.
  • Check terminal for feedback and VSCode notification for successful connection.

Profit!

  • From now on, Julia code executed on that VSCode session with Julia: Execute... commands will run inside Julia inside Python inside Blender.
  • That code can be related to package and environment management, using the Pkg module, for example:
    import Pkg
    Pkg.activate()
    Pkg.add("PythonCall")
    
  • Most useful, we can now call back into Blender from Julia!:
    using PythonCall
    bpy = pyimport("bpy")
    scn = bpy.context.scene
    objnames = [obj.name for obj in scn.objects]
    
  • Blender GUI will still be usable and responsible, although it can freeze while Julia is doing computations.
6 Likes

Cool! Could you make a PR to link to this post from the PythonCall FAQ please? PythonCall.jl/faq.md at main · cjdoris/PythonCall.jl · GitHub

Can you explain julia_work? Is it to keep Julia’s event loop going?

It seems that Python’s main thread (with GIL) is the Blender GUI thread too. So when something in Python is running the GUI is blocked and vice versa. As far as I understand the Julia main thread when starting through JuliaCall is the same one as Python. So no computation in Python or in Julia can constantly permanently hold the thread execution or the other parts will freeze.

julia_work is a callback that is set to be called by Blender with 0.02 secs timer interval. juliacall.Main.sleep(0.001) allows Julia schedule do its computations. I’ve tried juliacall.Main.yield() but that didn’t work. These times have been tunned empirically.

Done Add link in faq.md to instructions to run Julia within Blender by cdsousa · Pull Request #288 · JuliaPy/PythonCall.jl · GitHub. I’m not sure if that was what you had in mind. Feel free to completely reformulate it.

I’ve found yet another much better way to use Julia within Blender.

The idea is to launch JuliaCall with Blender’s Python and with it launch a Julia REPL in Blender’s terminal.
Hence, we get a Julia REPL which is running inside Python inside Blender and has full access to Blender bpy API.

To do that, we must execute Blender from a terminal and pass it a small Python script to be run:

blender.exe --python blender_julia.py

where blender_julia.py contents are: (update version here: Julia inside Python inside Blender · GitHub)

# install JuliaCall within Blender's Python environment (if not already installed)
import importlib
if importlib.util.find_spec('juliacall') is None:
    import pip
    pip.main(['install', 'juliacall'])

# set number of Julia threads to 1 (otherwise bpy usage will crash)
import os
os.environ["PYTHON_JULIACALL_THREADS"] = "1"

# load Julia with JuliaCall
import juliacall

# start Julia REPL in the terminal
juliacall.Main.seval('import REPL; import Pkg; Base.active_repl = REPL.LineEditREPL(REPL.Terminals.TTYTerminal(get(ENV,"TERM",""),stdin,stdout,stderr), true); Threads.@spawn :interactive REPL.run_repl(Base.active_repl, backend->(Base.active_repl_backend = backend));')

# load PythonCall and bpy Python module within Julia
juliacall.Main.seval('using PythonCall; bpy = pyimport("bpy")')

# schedule a Blender timer to allow Julia run
import bpy
def julia_work():
    juliacall.Main.sleep(0.01)
    return 0.01

julia_timer = bpy.app.timers.register(julia_work, persistent=True)


(the first run may take a while to install JuliaCall)

After that, we have a Julia REPL in the terminal where Blender is running. Additionally, we can connect to the VSCode Julia extension with its Julia: Connect external REPL command.

2 Likes

Nice! Installing PythonCall should never be necessary, it is always installed in the Julia environment used by JuliaCall.

You could make a PR to the PythonCall FAQ with this.

We could also bundle most of this code under a juliacall.blender module.

Thanks!

Edited (and linked to a future editable gist).

I would say this FAQ entry Julia inside Python inside Blender · GitHub already covers this.

Regarding the juliacall.blender module, what would go there, the code after import juliacall?

BTW, I am still experimenting with ways to make REPL IO a little faster.

Yeah exactly, could be a function like juliacall.blender.start_julia_repl(). Or at least a link to your gist from the FAQ.