Rob Wrote:I think I have to start at the 'deepest' nested model and work backwards ?
It depends on what you want if you want a single flattened ldr containing the visual same model as the level 0 one it's best to start at top level and go recursively through all it's branches, this will automatically account for multiple references to the same submodel (e.g. model 1a could be referenced using a completely different position from model 3 or something). The merging you speak of will essentially be done like a zipper this way.
Rob Wrote:The merging of X,Y,Z I can handle - its the matrix math where I struggle.
You are essentially right about what you call a 'pseudo' origin cause the x y z in combination with the other 9 number makeup a transformation matrix which describe how to place/rotate the referenced part/model in the referring document.
So you have to use all 12 numbers in a single 4x4 matrix (see LDraw spec documents for more info on this), you don't need to handle them separately.
By multiplying these matrices with the ones of higher levels you adjust their transformation to the absolute space of the top level document. 'spitting' these new lines (with adjusted 12 numbers) into a new ldr results in a flattened version of the original set of ldr/mpd's.
As for the matrix math, depending on the program/script language you use multiplying two matrices is fairly simple, there are probably even default libraries for it. But you would only be needing a multiplying function which is little more then two for loops, source for those can be found all over the net.
You might also need to look deeper in the inner workings of MPD's to correctly handle the submodels of a single mpd file.