The easiest way to take advantage of the qmdl library features is through the “helper” module. The Helper class offers an easy to use interface for quickly loading models making common changes and saving. I often use it straight after making a model with md3tomdl; I can cook up a script to turn the basic .mdl output by md3tomdl into a polished model with named frames, multiple skins and appropriate framegroups. By setting up a simple batch file I can “compile” md3 files into ready-to-use mdl files with a single click.
Our first helper
Here’s the simplest example of a helper. We load a mdl file, run the merge_vertices function, and save it again.
from qmdl.helper import Helper pentagram = Helper() pentagram.load("pent.mdl") pentagram.merge_vertices() pentagram.save()
The first line is the bit of magic which loads the Helper. The Helper then loads all the other code needed behind the scenes. Secondly we create a new Helper called
pentagram. Thirdly we load the model by passing a string with the filename to the
load method. Fourthly we run the
merge_vertices method, and fifthly we run
save to write the model back to the file it came from.
A sample of features
Here’s a more lengthy sample which demonstrates multiple features. Notice how we can access the underlying Mdl object on the line where we change the model’s flags. Also pay attention to how we add multiple skins using a simple python “for loop”:
#import and setup a helper model from qmdl.helper import Helper knight = Helper() #load an existing model file knight.load("dbaron.mdl") #rename the frames knight.rename_frames( [ ("stand",10), ("walk",20), ("run", 8), ("swing", 14), ("smash", 15), ("cast", 25), ("pain", 6), ("longpain", 14), ("die", 10) ]) #make the stand animation a framegroup knight.group_frames(0,9,"stand") #import alternate skins from files skinlist = ["blue_armour.tga", "green_armour.bmp"] for skin in skinlist: knight.append_skin(skin) #modify the underlying model object to set flags knight.mdl.flags = 4 #save the changes knight.save()
group_frames method creates a framegroup, be aware that the original frames are replaced. Suppose your model has frames 0-29 and you want to create two framegroups from the first 10 and last 10 frames in the model respectively. The code
may not work as you intend, because after the first line completes, your model only has 21 frames!
However, there’s a simple way to work around this problem – work backwards! Consider:
group_frames(0,9,"idle_a")This version works fine, because the frames removed in the first line come after the frames we want to change in the second line. If you always group the frames from back to front then you won’t have any trouble.
from qmdl.helper import Helper pufferfish = Helper() pufferfish.load("puffer.mdl") pufferfish.rename_frames( [ ("idle_small",10), ("inflate",10), ("idle_large", 10) ]) pufferfish.group_frames(20,29,"idle_large") pufferfish.group_frames(0,9,"idle_small")
Beyond the Helper
If you want to look at working with the Mdl module directly, why not start by taking a look at the code for the Helper module! While methods like merge_vertices are pretty dense and complicated, most of them are accomplishing straightforward tasks. After that, the modelling pipeline post has a basic example of what you can do using the mdl module directly. The Grenade Launcher comes with a script which smooths the normals of all coincident vertices, which is very handly for polishing a model edited in QME (no pun intended). The Horror model goes one step further with a script that merges all coincident vertices together (averages the normals at the same time).
The console model comes with a more complex example script and a good illustration of something which wouldn’t be possible without this kind of tool.
Last but by no means least, the fbxtomdl tool is literally just a python script that glues together the fbx python code supplied by autodesk with this qmdl module, so go check that out!