SaltwashAR is a Python Augmented Reality application, and it is now available on GitHub!
Arkwood and I were so excited that we threw a party for all the trendy people of Bo’ness. It was a roaring success. The mayor cut a yellow ribbon and declared “SaltwashAR now open!” (though he had not a clue what it was). Mrs Henderson turned up with one of her famous fruit punches, half drunk and full of hiccups. Even Old Man Parsons had a jolly good time, cutting a rug on the dance floor to Shakin’ Stevens whilst blowing his hooter into a handkerchief. Anyways, I digress…
What the application needs next is a configuration file, so that features can be switched on and off. We will always want the core of the application – namely to project a 3D robot upon a 2D surface – but other stuff like animation and the speech-enabled browser can be optional.
There are a number of options when it comes to handling configuration with Python, as Martin Thoma points out. We could use XML, or maybe JSON. But for now, I am going with the built-in support for ConfigParser.
First, let’s create an appsettings.INI file
[Features] Animation=False Browser=True
Dead simple to start with. We have one section called Features and two key/value pairs for Animation and Browser. Nothing too complicated here – if Animation is set to True then our augmented reality experience has robot animation. If Browser=True then we can talk to our robots and ask them to fetch web content for us.
So how do we make these application settings available to our SaltwashAR application? Easy – we create a Configuration Provider class:
import ConfigParser class ConfigProvider: def __init__(self): self.config = ConfigParser.ConfigParser() self.config.read("appsettings.ini") @property def animation(self): return self.config.getboolean("Features", "Animation") @property def browser(self): return self.config.getboolean("Features", "Browser")
Lovely. As you can see, when the class is instantiated, our __init__ method will load all the config from our appsettings.INI file into memory. We then define a property for retrieving each config value. Notice how we are fetching the Animation and Browser settings using getboolean – the ConfigParser handles the data type for us.
So now that we have the contents of the config file available to our application via the Configuration Provider, let’s have a look at how we are using it:
# load robots self.rocky_robot.load('rocky_robot', self.config_provider.animation) self.sporty_robot.load('sporty_robot', self.config_provider.animation)
We are passing our animation value into the load method for each robot.
# load frames from directory def load(self, directory, animation): os.chdir(directory) for file in glob.glob("*.obj"): self.frames.append(OBJ(file)) if not animation: break os.chdir('..') self.frames_length = len(self.frames)
And the load method will load only a single robot frame if animation is set to False. However, if our config file has Animation=True then all frames of the robot are loaded, thus providing an animated experience.
Why turn animation off I the first place? I hear you cry. Well, animation takes a while to load for one thing, so having a handy setting can allow us to start the application in a zippy manner if need be.
Let’s take a look at the config switch for the browser:
# initialise browser self.browser = None if self.config_provider.browser: self.browser = Browser()
We only instantiate our browser class if Browser=True in our settings file. Without a browser class, our robots are unable to fetch web content for us.
Now for the proof of the pudding – let’s see the config in action:
Hurray! As you can see, the SaltwashAR application is giving us robot ‘head-bobbing’ animation and browser interaction (with Text To Speech and Speech To Text conversation). But as soon as we set Animation=False and Browser=False in the appsetting.INI file – and restart the application – the robot is motionless and mute.
‘This is a big step forward for SaltwashAR,’ I told my Belgian buddy, ‘It means that I can develop components for the Python Augmented Reality application, and plug them in as meets my needs.’
Arkwood smiled. ‘Does that mean I can throw another debauched party at the flat, into the sordid early hours of the morning?’
No, I replied coolly. God knows I don’t want to fish Old Man Parsons socks out of the fruit punch bowl again!