Install script for Blender Addon Development in Eclipse

I constantly fiddle with a lot of Python scripts to automate or conduct experiments for my Geometry Processing endeavors using Blender. Usually, these scripts begin as a standalone and soon evolves as a fully functional addon. For the purpose of writing scripts in an efficient manner, I use Eclipse along with PyDev plugin. Once I have reached a certain stage of my addon development it becomes important to install and test the addon script often. In such a scenario the conventional route is to copy the addon folder with the python code to the Blender’s add-on directory. This proved to be a cumbersome process all the while and wasn’t a lot of fun, to begin with. But hey I use PyDev and Eclipse know well that a Python interpreter is available for it to use and run scripts from Eclipse.

The external tools configuration of Eclipse IDE. We can exploit this functionality to make our add-on development testing more easier.

In Eclipse IDE it is possible to create External tools configuration to run custom tasks and automate various process. With the python interpreter available it is possible to run a python script in the add-on directory that will copy itself to the Blender’s add-on folder to be installed as a plugin.

Firstly, we will look into the python script that can take care of installing (or copy) the add-on folder and its contents to the Blender add-on directory. Actually, the code is pretty much simple in its underlying logic. It reads the arguments supplied when running the script and then uses that information to install the plugin. The idea is to zip the contents of the folder and unzip it to the Blender’s add-on location.

import os, sys;
import subprocess;
from subprocess import Popen, PIPE;
import zipfile;
from platform import platform

BLENDER_ADDON_PREFIX = os.path.abspath(sys.argv[3].strip());
BLENDER_VERSION = sys.argv[4].strip();
BLENDER_ADDON_LOCATION = "%s/%s/scripts/addons/"%(BLENDER_ADDON_PREFIX,BLENDER_VERSION);
BLENDER_ADDON_LOCATION = os.path.abspath(BLENDER_ADDON_LOCATION);
PROJECT_FOLDER = os.path.abspath(sys.argv[1].strip());
PROJECT_NAME = sys.argv[2].strip();
EXTRAS_BLENDER_PATH = "";
try:
    EXTRAS_BLENDER_PATH = sys.argv[5];
except IndexError:
    pass;

print("Prefix: %s\nVERSION: %s\nADDON_LOCATION_FULL: %s\n"%(BLENDER_ADDON_PREFIX, BLENDER_VERSION, BLENDER_ADDON_LOCATION));

def zipdir(path, zipper):
    for root, dirs, files in os.walk(path):
        for file in files:
            if("." not in file[0] and not file.endswith('.zip') and "installaddon.py" not in file):
                file_path = os.path.abspath(os.path.join(root, file));
                parent_dir = os.path.abspath(os.path.join(os.getcwd(), os.pardir));
                rel_path = os.path.relpath(file_path, parent_dir);
                zipper.write(file_path, rel_path);
    
    return zipper;
    
if __name__ == "__main__":
    zipper = zipfile.ZipFile(PROJECT_NAME+".zip", 'w', zipfile.ZIP_DEFLATED);
    zipper = zipdir(PROJECT_FOLDER, zipper);
    zipper.close();
    zip_file_path = os.path.abspath(PROJECT_FOLDER+"/"+PROJECT_NAME+".zip");
    zipper = zipfile.ZipFile(zip_file_path, "r");
    
    zipper.extractall(BLENDER_ADDON_LOCATION);
    zipper.close();
    if(EXTRAS_BLENDER_PATH != ""):
        output = subprocess.Popen([EXTRAS_BLENDER_PATH], stdout=subprocess.PIPE).communicate()[0];
```   

Create a python script ‘installaddon.py’ and copy the above contents to this file. Now this file has to be available in the main folder where the __init__.py with all the bl_info values.

The typical folder structure of my Blender add-ons.

Now we have the needed script in place its time to configure Eclipse IDE to use this script. To do this you have to open the external tools configuration and create a new launch configuration.

Create a new launch configuration and supply the values as shown in the coming steps.

In the above screen shot the fields and their values are,

  • Name: The name of this tool and its entirely your choice
  • Location: The path to the executable to launch. This should point to the path of the Python interpreter binary. If you are on windows then this will point to the location of python.exe
  • Working Directory: It will be ${project_loc}. This will be an Eclipse variable pointing to the Eclipse project on which this tool is being executed.
  • Arguments: The arguments that need to be supplied to the executable, which in our case is the python interpreter.
    1. installaddon.py – Ask the python interpreter to run this script
    2. ${project_loc} – The first argument to the python script is the location of the project on the disk
    3. ${project_name} – The name of the project that will be the name of the Blender add-on as well
    4. ${string_prompt:Blender Addon Location:/path/to/blender/addon/location/before/version/folder}
    5. ${string_prompt:Version Text:2.79} – Which version of Blender are we targetting? Well, one can supply the full and final path to the Blender add-on location as the previous argument (argument 4). However, in my case, I test my addons with many versions of Blender. So this will help in that case
    6. ${string_prompt:Binary Text:blender} – The path to the Blender binary or executable. If supplied the addon will be installed and Blender will be called as a subprocess. It is a sheer matter of convenience. If left blank only the addon will be installed without invoking Blender.


Well, that’s it. I make it a point to copy this script to every Blender-addon project I have on Eclipse. Also by supply the arguments one can also run this script from the command line and achieve the same goal without using Eclipse. I have given the downloadables for everyone to use. Feel free to experiment and explore.

Hope this information is useful and be of some help to everyone out there. Goodluck and have a great life ahead.

Regards,

#0K