How do I use the Blender API to create an armature from XML data that contains bone coordinates, names, and parenting hierarchies? And as a follow-up question, how can I parent a mesh to the armature and apply bone weights (also pulled from an XML file) to the vertices?
I’m working on an Ogre importer script (specifically for Torchlight characters), and I’ve figured out how to load in a mesh with help from a Blender Cookie tutorial, but after searching through the Blender API, I haven’t been able to find similar commands to load in an armature.
Creating a mesh is simple, once you’ve used an XML parser to get lists of vertices and faces (designated here by the variables “verts” and “faces”). The command “from_pydata” takes a tuple of three lists (vertices, edges, and faces) and creates the mesh geometry. You can even pass an empty edge list, and use the command “update(calc_edges)” to fill them in, as long as you have your vertex and face lists.
import bpy
verts = [...] # this is the vertex position list generated by your XML parser
faces = [...] # this is the face list generated by your XML parser
## create a new mesh; all it needs is a name
mesh = bpy.data.meshes.new("OgreObject")
## create a new object; all it needs is a name and a mesh
object = bpy.data.objects.new("OgreObject", mesh)
## set the location for your object in your scene
object.location = bpy.context.scene.cursor_location
## link your object to the scene
bpy.context.scene.objects.link(object)
## add the vertex and face data to your mesh
mesh.from_pydata(verts, [], faces)
## update the mesh to flush the buffered data
mesh.update(calc_edges=True)
This is the simple way to create a mesh. Note that I haven’t figured out yet how to add things like bone assignments, normals, and UV coordinates. Those are on my to-do list, but of lower priority than importing the armature.
(If anyone is interested, I can post the code for my XML parser later; I have it stored on another computer.)