Blender Python Mode - Blender Python development with Emacs

The Blender Python Mode makes it easy to use Emacs for Blender Python development. It is based on the Command Port branch of Blender and on Dave Love's python.el mode included in Emacs 22.

The Blender Python Mode is still experimental and in the process of development - so please don't wonder if not everything works as it should :)

If you encounter problems while using it, have comments how to make it better - or even some patch to extend its functionality please send me an email.

Screenshot


Console, Blender Server, emacs client and shell client

Links

Example - a simple pyramid

  • Download and install the Blender Python Mode
  • Start emacs with the following command
    emacs \
      --no-init-file --no-site-file --no-splash \
      --debug-init \
      --eval "
          (progn
            (define-key global-map \"\\C-h\" (quote delete-backward-char)) ;; mapping Control-h to delete-backward-char
            (defvar blender-python-mode-installation-dir \"$BPM/blender-python-mode\")
            (setq load-path (add-to-list 'load-path blender-python-mode-installation-dir))
            (require 'blender-python-mode)
            (find-file (concat blender-python-mode-installation-dir \"/python-example-file.py\")))
        "
    
    where $BPM is the directory where you installed the Blender Python Mode sources.
  • Emacs should start and display the file blender-python-mode/python-example-file.py as found in the Blender Python Mode source distribution:
    # -*- mode: blender-python -*-
    
    # a one-line command
    
    print 'Hello %s :)' % 'Blender Python Mode'
    
    # region-begin - a little script to generate a simple pyramid
    
    from Blender import *
    
    vertexes = [[1, 1, 0], [-1, 1, 0], [-1, -1, 0], [1, -1, 0], [0, 0, 1.27]]
    faces    = [[3, 2, 1, 0], [0, 1, 4], [1, 2, 4], [2, 3, 4], [3, 0, 4]]
    
    mesh = Mesh.New('mesh')
    mesh.verts.extend(vertexes)
    mesh.faces.extend(faces)
    
    scene  = Scene.GetCurrent()
    object = scene.objects.new(mesh, 'object')
    Redraw()
    
    # region-end
    
  • Start the Blender server and the emacs blash client with
    Emacs Menu: Blender -> Start
    or one by one with
    Emacs Menu: Blender -> Start Blender Server
    and after
    Emacs Menu: Blender -> Start Blash
  • If you now move the Emacs point to line
    print 'Hello %s :)' % 'Blender Python Mode'
    and hit C-return the *Blash* buffer should pop up and the string "Hello Blender Python Mode :)" should be printed.
  • If you evaluate the following code from file python-example-file.py line by line in the same way a little pyramid should be displayed in the blender 3D view:
    from Blender import *
    
    vertexes = [[1, 1, 0], [-1, 1, 0], [-1, -1, 0], [1, -1, 0], [0, 0, 1.27]]
    faces    = [[3, 2, 1, 0], [0, 1, 4], [1, 2, 4], [2, 3, 4], [3, 0, 4]]
    
    mesh = Mesh.New('mesh')
    mesh.verts.extend(vertexes)
    mesh.faces.extend(faces)
    
    scene  = Scene.GetCurrent()
    object = scene.objects.new(mesh, 'object')
    Redraw()
    

If you want to execute more than one line at once, different emacs commands can be used:

  • You can mark a region in the emacs buffer and use the emacs menu Blender -> Execute region (Shortcut: C-xC-r) to execute the region;
  • You also can move the point into a region delimited by the comment lines # region-begin and # region-end and use Blender -> Execute region between `region-begin' and `region-end' (Shortcut: C-xC-e) to execute all lines between region-begin and region-end at once.

Installation

Customization

  • You can customize emacs to load the Blender Python Mode when starting up by adding the following lines to your .emacs file:
    (defvar blender-python-mode-installation-dir "$BPM/blender-python-mode")
    (setq load-path (add-to-list 'load-path blender-python-mode-installation-dir))
    (require 'blender-python-mode)
    
    Please substitute the token $BPM with the directory where you installed the Blender Python Mode sources.
  • Other customization options are available from
    Emacs Menu: Blender -> Customization

fin