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.