This article is the 8th day article of Lisp Advent Carendar 2019.
Let's create an Add-on for Blender, a DCC tool, using Hy, a Clojure-like Lisp dialect that runs on Python.
Blender
https://www.blender.org
--People who want to do what they do in Python with Lisp --People who want to play with Blender in Lisp
The following is for Windows 10. If you are using another OS, please read as appropriate (I will explain a little).
First, install Python 3 (64bit). At the moment (December 2019), the latest 3.8 series or the previous 3.7 is good. Drop the installer from the official website and install it. Be careful not to install the 32-bit version.
Once Python 3 is installed, create a virtual Python environment, `venv```, and install Hy in it to keep the environment clean. This time, create it with the name
venv_hy_blender``` in the ``
.virtualenvs``` directory under your home directory.
The latest release version of Hy 0.17.0 doesn't yet implement Type Annotation, so install the latest master branch from GitHub.
In Windows 10 cmd, type:
$ cd %USERPROFILE%
$ mkdir .virtualenvs
$ cd .virtualenvs
$ py -3 -m venv venv_hy_blender
$ cd venv_hy_blender
$ Scripts\activate.bat
$ python -m pip install git+https://github.com/hylang/hy.git
$ deactivate.bat
py
Is a windows-only command, so for other ospy -3
Replace with the python 3 interpreter startup command on that os (usuallypython3
Orpython
is).
Also, since the command activate to activate the virtual environment differs depending on the OS and Shell, refer to "Command to activate the virtual environment" in the link "venv --- Create virtual environment" below.
-venv --- Create Virtual Environment
Drop and install the latest Blender 2.81 installer from the official.
Below is a Hy rewrite of the Bringing It All Together Python code from the Blender Add-on Tutorial.
If you're using Python, you'll find that you can write Python almost as-is.
Suppose you create this in the blender_sandbox
directory.
The only Type Annotation not found in Hy's current documentation is `(^ type variable name)`
. See Implement PEP 3107 & 526 annotations for more information.
cursor_array.hy
(import bpy)
(setv bl-info {
:name "Cursor Array"
:blender (, 2 81 0)
:category "Object"
})
(defclass ObjectCursorArray [(. bpy types Operator)]
"Object Cursor Array"
(setv bl-idname "object.cursor_array")
(setv bl-label "Cursor Array")
(setv bl-options #{"REGISTER" "UNDO"})
(^((. bpy props IntProperty) :name "Steps" :default 2 :min 1 :max 100) total)
(defn execute [self context]
(setv scene (. context scene))
(setv cursor (. scene cursor location))
(setv obj (. context active-object))
(for [i (range (. self total))]
(setv obj-new ((. obj copy)))
((. scene collection objects link) obj-new)
(setv factor (/ i (. self total)))
(setv (. obj-new location) (+ (* (. obj location) factor) (* cursor (- 1.0 factor)))))
#{"FINISHED"}))
(defn menu_func [self context]
((. self layout operator) (. ObjectCursorArray bl-idname)))
; store keymaps here to access after registration
(setv addon-keymaps [])
(defn register []
((. bpy utils register-class) ObjectCursorArray)
((. bpy types VIEW3D-MT-object append) menu-func)
;; handle the keymap
(setv wm (. bpy context window-manager))
;; Note that in background mode (no GUI available), keyconfigs are not available either,
;; so we have to check this to avoid nasty errors in background case.
(setv kc (. wm keyconfigs addon))
(when kc
(setv km ((. wm keyconfigs addon keymaps new) :name "Object Mode" :space-type "EMPTY"))
(setv kmi ((. km keymap-items new) (. ObjectCursorArray bl-idname) "T" "PRESS" :ctrl True :shift True))
(setv (. kmi properties total) 4)
((. addon-keymaps append) (, km kmi))))
(defn unregister []
;; Note: when unregistering, it's usually good practice to do it in reverse order you registered.
;; Can avoid strange issues like keymap still referring to operators already unregistered...
;; handle the keymap
(for [(, km kmi) addon-keymaps]
((. km keymap-items remove) kmi))
((. addon-keymaps clear))
((. bpy utils unregister-class) ObjectCursorArray)
((. bpy types VIEW3D-MT-object remove) menu-func))
(when (= --name-- "__main__")
(register))
blender_Blender2 in the sandbox directory.81.Create a startup batch file with the name bat.
```venv_hy_blender```of```site-packages```And the created add-on directory (```blender_sandbox```) Is a batch file that starts blender.
```site-packages```Is the installation directory for third-party modules of python, which is owned by python itself and each virtual environment.
Originally, the virtual environment is switched with ```activate``` and `` `deactivate```, and the` `` site-packages``` is used, but this time Blender 2.81 built-in Python 3.7. I'm doing something a bit wicked to import and use Hy in `` `venv_hy_blender``` to 4.
#### **`dos:Blender2.81.bat`**
@ECHO OFF SETLOCAL
SET THISDIR=%~dp0 SET PYTHONPATH=%THISDIR%;%USERPROFILE%.virtualenvs\venv_hy_blender\Lib\site-packages;%PYTHONPATH% SET BLENDER_EXE="C:\Program Files\Blender Foundation\Blender 2.81\blender.exe"
%BLENDER_EXE%
# Run
Launch Blender from this batch file and select ``` Scripting``` from the menu bar above.
To import the above script from the Console, type:
```python
>>> import hy
>>> import cursor_array
>>> cursor_array.register()
You can now run Cursor Array
from Layout
> Object
. let's do it.
Yes.
--It is good to build a Python environment on the virtual environment `` `venv``` because the original environment will not be polluted. --Type Annotation is not available in Hy 0.17.0 yet, but it will be available in the future. If you want to use Hy solidly, wait for the official release. --You can run Hy in various places (for example, on Python with built-in tools), such as by making full use of Python's evil methods. --Most of the tools in the 3DCG area use Python as the scripting language, so Hy also works. However, it is currently Python 2 except for some tools such as Blender. Hy also supports Python 2 up to 0.17.0, but will no longer support it.
Recommended Posts