# # export colorband as an xml-file # for use as input for my fortran fractal generator # (see: http://users.telenet.be/tuinbels/fortran_fractals.html) # # Copyright (c) 2008 Wim Van Hoydonck # import Blender import xml.dom.minidom import xml.dom.ext from Blender import Material def create_colorband_document(): """create an xml file containing the top-level element colorband""" doc = xml.dom.minidom.Document() colorband = doc.createElement("colorband") doc.appendChild(colorband) return doc, colorband def add_marker(doc,colorband): """add a marker element to the document under the top-level colorband element""" marker_element = doc.createElement("marker") colorband.appendChild(marker_element) return marker_element def add_marker_contents(doc,marker,color): """add color and position contents to a marker element""" # red component red_element = doc.createElement("red") red_value = doc.createTextNode(str(round(color[0]*255))) red_element.appendChild(red_value) # green component green_element = doc.createElement("green") green_value = doc.createTextNode(str(round(color[1]*255))) green_element.appendChild(green_value) # blue component blue_element = doc.createElement("blue") blue_value = doc.createTextNode(str(round(color[2]*255))) blue_element.appendChild(blue_value) # position of marker position_element = doc.createElement("position") position_value = doc.createTextNode(str(color[4])) position_element.appendChild(position_value) # append everything to the marker marker.appendChild(red_element) marker.appendChild(green_element) marker.appendChild(blue_element) marker.appendChild(position_element) # return all new elements return red_element,green_element,blue_element,position_element def write_to_screen(doc): """write the complete xml structure to screen""" xml.dom.ext.PrettyPrint(doc) def write_to_file(doc,filename): """write the complete xml structure to the file named filename""" file_object = open(filename,"w") xml.dom.ext.PrettyPrint(doc,file_object) file_object.close() # # main part of code # # create xml document and toplevel element doc, colorband = create_colorband_document() # get materials mats = Material.Get() # loop over materials and get all materials that contain the word # 'Colorband' # and add elements to the for mat in mats: if mat.getName() == "Colorband.002": print for col in mat.colorbandDiffuse: # add a marker element marker_element = add_marker(doc,colorband) # fill marker element with its colour components and position red,green,blue,pos = add_marker_contents(doc,marker_element,col) # show result on screen #write_to_screen(doc) # write result to a file write_to_file(doc,"colorband002.xml")