# LDRAW Question

Hi -

I am currently writing a python script to export models from the Lego modeling program LDRAW to another program – currently I do not know which one, but it will most likely be Blender.

An LDRAW file includes different types of lines, as follows…

For a line (linetype 2):
2 color x1 y1 z1 x2 y2 z2

For a triangular face (linetype 3):
3 color x1 y1 z1 x2 y2 z2 x3 y3 z3

For a quadrilateral face (linetype 4):

4 color x1 y1 z1 x2 y2 z2 x3 y3 z3 x4 y4 z4

where (xi, yi, zi) are points. These are easy. The hard part is the linetype 1 statement, which allows you to include another LDRAW file in the current file. It looks like this:

1 color x y z a b c d e f g h i part.dat

where fields x through i fit into a standard transformation matrix:

[ a b c x ]
[ d e f y ] = [T]
[ g h i z ]
[ 0 0 0 1 ]

[locx ]
[locy ] = [P]
[locz ]
[1 ]

and a position [P] in homogeneous coordinates is transformed by:
[T] x [P] = [P’]

I have already written the recursive code that processes all LDRAW files included at any level in the file being exported, so that is not a problem. The problem lies in the transformation matrix – it is a combination of rotations about the x, y and z axes, and nonuniform scales about each axis. Correct me if I’m wrong, but I cannot recover any of the Euler rotation angles or any of the scale information from the matrix if I do not know any of it to begin with.

Is it possible in Blender to enter a transformation matrix directly, or do I need to write the script to transform all of the points before importing? (I am pretty sure it’s not possible). If it isn’t possible, is there any format that does allow matrices to be entered directly?

[Eeshlo – do you have any quick advice on using the Lightflow ASCII or binary formats?]

Thanks a lot guys -->
Matt

You can get that information from a matrix, if you remember this is exactly what I did first in the LF script, since you can’t set the matrix directly in LF. That part is still used in the new script, but only for the camera, it converts a matrix to the rotation in euler angles, scaling values and translation. That particular part of the script can also be found here:
https://blenderartists.org/forum/viewtopic.php?t=2855

So you can still write your exporter for blender.

edit: I forgot, this doesn’t work for non-uniform scaling if the matrix also contains a shearing component, like when parenting objects in blender and scaling the parent in one axis, the information from the child matrix is then not recoverable any longer.
btw, I also have a python extension module (.dll) that is even better at recovering the information from the matrix using something called ‘polar decomposition’ by Ken Shoemake.

THANKS EESHLO!!!

I did a Google search on Ken Shoemake and his polar decomposition; I will take a look at in a second version of the script. It looks like it will be useful. It looks like I need to do some more reading about the different types transformation matrices that use homogeneous coordinates – any suggestions on good sources?

For now I’ll just transform the points myself and make sure it works that way (hey, I could use some practice writing in Python).

What programs/formats (other than POV) allow you to directly input a transformation matrix?

Matt