Posts Tagged ‘python’

Suppressing the default namespace in ElementTree

Monday, July 7th, 2014

xmlThis solution removes the default namespace from the input XML and makes typing ElementTree Xpath expressions a lot easier.  The trick is to remove the xmlns=”…” attribute from the input XML before parsing it.  Most other workarounds suggest traversing the tree of elements after parsing, and modifying the tag entries.


import ElementTree as ET

# Parse the contents of the XML file
tree = ET.parse(xmlFile)

# Get the root element
root = tree.getroot()

# Use fully qualified tag names in path expressions
extentX = root.find(".//{http://some/namespace}Coverage/{http://some/namespace}Extent/x")


import ElementTree as ET
import re # regular expression module

# Read the contents of the XML file into xmlstring
with open(xmlFile) as f:
    xmlstring =

# Remove the default namespace definition (xmlns="http://some/namespace")
xmlstring = re.sub('\\sxmlns="[^"]+"', '', xmlstring, count=1)

# Parse the XML string
root = ET.fromstring(xmlstring)

# Use much simpler path expressions
extentX = root.find(".//Coverage/Extent/x")

Thanks to this post on the always useful StackOverflow for suggesting the solution.

Simple Popup Requester in Python

Wednesday, March 14th, 2012

Sometimes I think I should change the name of this blog to Mind Like a Sieve, subtitle Straining to Remember.

I’ve built some miscellaneous one-off GUIs with various Python toolkits like Tkinter and wxPython, but this time I wanted just a simple confirmation popup.  In this case it was to pause so I could attach a debugger.

The comment on this page of the tkinterbook had exactly what I needed.  Also, there is a page showing examples of all the different simple popups you can make using this approach.  One benefit of using this approach is that Tkinter is bundled with Python, so there are no additional packages to install.

Here’s the simplest code snippet:

import Tkinter, tkMessageBox
Tkinter.Tk().withdraw() # suppress blank Tk root window in Windows
tkMessageBox.showinfo("Debugging", "Attach debugger, then click OK")

You can, of course, get much fancier with some of the popup versions, and you can capture the return value from the user interaction.

Convert 3ds files to obj with Blender

Tuesday, January 24th, 2012


3DS is an aging binary interchange format for 3D models. OBJ is another aging, but ASCII-based format for 3D models.

Here is a simple Python script that converts one or more files from 3DS format to OBJ format using Blender:

# Convert 3ds to obj using Blender
# Run as follows:
#   blender -b dummy.blend -P -- file.3ds ...
# dummy.blend is just an empty Blender file needed as an argument.
# Put one or more .3ds files on the end of the command.
# The .obj files will be created with the same name (and path) as
# the .3ds file, but with the .obj extension.
# The export creates a .mtl file for each .obj file also.

import bpy
import sys
import os.path

for i in range(1, len(sys.argv)):
    if sys.argv[i] == "--":

for file in sys.argv[i+1:]:
    # Start with an empty scene

    # Read a .3ds file

    # Write a .obj file
    outfile = os.path.splitext(file)[0]+".obj"


Save the script code in a file named  You will also need a Blender file to use as a placeholder in the command line.  You can save an empty file from Blender, or use an existing one.  Assume it is called dummy.blend.

To convert 3DS files, use the following command:

blender -b dummy.blend -P -- file1.3ds file2.3ds file3.3ds

Tack on as many 3DS files to the end of the command as you want.

Note:  This assumes that dummy.blend and are in the same folder as your 3DS files.  If not, you will need to specify the proper path to each.