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

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.

3 Likes