# Hugin Scripting Interface

From PanoTools.org Wiki

## Introduction

Scripting has been implemented both ways:

- HSI (Hugin Scripting Interface): enables calling Hugin functionality from Python scripts
- HPI (Hugin Plugin Interface): enables calling Python functionality from within Hugin

If you have access to a Hugin binary that comes with scripting capability, you'll notice a "Run Python script" entry in the Edit menu. You are all set and ready to go. If that menu command is not available, you will need to upgrade your Hugin binary, your system, or both.

You can call Python from Hugin and just carry on in Python with the same objects, then return to C++. As a user, you are enabled to use and write Python plugins with access to the Hugin dataverse. The plugin interface provided by Hugin now is still experimental. As it attracts interest, we expect it to grow and mature.

## HPI

HPI is embedding Python into Hugin. It allows you to call arbitrary Python code Use the file-select dialog Edit->Run Python Script to pick a plugin file and it will be executed without further ado. You can edit your Python scripts with any text editor and launch them within Hugin.

Hpi uses hsi, so it can deal with all data types wrapped with hsi and call all their methods.

### Limitations

Currently this functionality is available in varied degrees only in FreeBSD, Linux and Windows.

Currently there is no feedback from the plugin apart from its success or failure, which is communicated in a dialog. If you are using FreeBSD or Linux, you can start Hugin from the command line and switch to the window you started it from while the plugin is running. You'll see any console output it may produce. The effect of the plugin manifests itself after the plugin's termination. While the plugin runs, Hugin will not respond. If the plugin's effect is undesirable you should be able to return to the previous state by using undo.

If you are using plugins that have been designed to also work as standalone Python programs, you can pass parameters to them on the command line. If called from Hugin, no parametrization is possible, apart from modifying the Python code, using an .ini file, as demonstrated with woa.py, or taking input via GUI elements, as demonstrated in crop_cp.py, which will only succeed on Linux currently.

## HSI

HSI is an extension of Python. Import the hsi module and access Hugin functionality from your scripts.

To see what objects and methods are available, run

    python -c 'import hsi; help(hsi)' > help.txt

If you're exploring hsi in an interactive Python session, all the wrapped objects will provide some help if you're stuck, but it's admittedly quite basic.

    $ python
    >>> from hsi import *
    >>> help(CPoint)

There is sparse documentation of the hugin data types and their methods beyond call signatures and member lists, so you have to guess your way when you want to use them. Luckily most of them are aptly and expressively named, so you can figure it out. I hope that eventually something like an API documentation will arise.

### Example

    #!/usr/bin/env python

    from hsi import *         # load the module
    p=Panorama()              # make a new Panorama object
    ifs=ifstream('xx.pto')    # create a C++ std::ifstream
    p.readData(ifs)           # read the pto file into the Panorama object
    del ifs                   # don't need anymore
    img0=p.getImage(0)        # access the first image
    print img0.getWidth()     # print the image's width
    cpv=p.getCtrlPoints()     # get the control points in the panorama
    for cp in cpv[:30:2] :    # print some data from some of the CPs
      print cp.x1
    cpv=cpv[30:50]            # throw away most of the CPs
    p.setCtrlPoints(cpv)      # pass that subset back to the panorama
    ofs=ofstream('yy.pto')    # make a c++ std::ofstream to write to
    p.writeData(ofs)          # write the modified panorama to that stream
    del ofs                   # done with it

## Scripts

This functionality is very recent and at the moment there are only a few simple sample Python plugins in the hugin_script_interface/plugins directory as well as some more involved ones to give you ideas.

Hopefully a body of scripts will develop which you can use for templates.

Retrieved from "https://wiki.panotools.org/index.php?title=Hugin_Scripting_Interface&oldid=15944"