Reading XML Blender 2.5x using Python and Minidom

reading and extracting XML data in Python and Blender

Just found a Blender App for drawing charts from xml data on my HD that I wrote some months ago.
It may have a bit of use for someone learning Blender/Python who needs to process data from XML.
The Python code was cut in the built-in editor of Blender 2.5 and does not require any additions beyond the installation of Blender to process it.

The purpose of this post is simply to illustrate the reading and and extraction of data from an xml – in this case in the context of Blender. It is not to convey any knowledge of the Blender API.

The following XML is in chart.xml on a local drive as can be seen from the python code below

<?xml version="1.0" ?>
<graph height="150" width="200">
  <bar colour="red" id="1" title="WF01" value="100"/>
  <bar colour="green" id="2" title="WF02" value="114"/>
  <bar colour="yellow" id="3" title="WF03" value="80"/>
  <bar colour="blue" id="4" title="WF04" value="75"/>
</graph>

This is the bit of python code used to process the elements of the xml

import xml.dom.minidom
from xml.dom.minidom import Node
import bpy
from bpy import *
import sys,os
os.chdir('/Users/kev/Blender/xmltest')
doc = xml.dom.minidom.parse("chart.xml")

height_max=0  
columns=0                                       
layers = [False]*32
layers[0] = True
matblue = bpy.data.materials.new('blue')
matblue.diffuse_color = (0.0, 0.0, 1.0)
matblue.specular_color = (1.0, 1.0, 0.0)
matred = bpy.data.materials.new('red')
matred.diffuse_color = (1.0, 0.0, 0.0)
matred.specular_color = (0.0, 1.0, 1.0)
matgreen = bpy.data.materials.new('red')
matgreen.diffuse_color = (0.0, 1.0, 0.0)
matgreen.specular_color = (1.0, 0.0, 1.0)
matyellow = bpy.data.materials.new('yellow')
matyellow.diffuse_color = (1.0, 1.0, 0.0)
matyellow.specular_color = (0.5, 0.5, 1.0)


for element in doc.getElementsByTagName("bar"): 
    a = element.attributes["id"]
    b = element.attributes["colour"]
    c = element.attributes["title"]
    d = element.attributes["value"]
    if int(d.value) > height_max:
        height_max=int(d.value)
    columns +=1 
    bpy.ops.mesh.primitive_cube_add()
    cube = bpy.context.object
    cube.name = a.value
    ob = bpy.context.object
    bpy.ops.object.material_slot_remove()
    bpy.context.active_object.location = [0, columns *2.2, 0]
etc...

Advertisement

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Connecting to %s

Follow

Get every new post delivered to your Inbox.