Lego io scripts, need help (dl @ 1st page 2004.04.18)

Question 1: how to tell the similarity of 2 colors. Lego uses only a limited color set. I’ve to choose a similar color during write a dat file. I’ve tried
to get the lego color, which results in a min in the above formulas. But they often pick up a incorrect color.

Question 2: add face without adding dupliacted vetrex? I know a veterx list and a face list will work, but if there are many faces, the lists grow heavily. Can python script do ‘remove dup’?

Current state - very elementary :smiley:
mesh: done
color: wip
type 0, Comment or META Command: wip
type 1, Part file reference: wip, the most complex parts
type 2, Line: done
type 3, Triangle: done
type 4, Quadrilateral: done
type 5, Optional Line: wip


The current API does not have a remove duplicate call, but it is possible to do it with python.
I’ve done this with the Knife Tool to get rid of the duplicated vertices.

But it is better not to create doubles at all. I do not know what the files look like you want to import, but normally, they consist of a list with vertex coordinates and a list with faces (with the indices of the vertices they consist of). First you add all vertices, and after that, the faces can be added without creating duplicated vertices.
In the above link, there are 2 import scripts that do just that.

I hope this is useful for you,


You could make a dictionary with the vertex coordinates (as tuples) as keys and pointers to the vertices themselves as the content. That way you can check back with the dictionary whether a vertex at the given position exists and if so it will give you the vertex so you can include it into the face, if not you can create the vertex and add it to the dictionary.

Ok. you may dl it from




  1. copy *.py and lego.cfg to the Blender script directory
  2. dl Complete Parts Library from
    and decompress it with directory using winzip
    The car.dat and pyramid.dat is coming with
  3. edit lego.cfg according to your installation in step 2
  4. update your blender scripts menu. The script can be found under Export and Import


  1. choice of simular colors (still). btw:The color is taken from
  2. type 1 during reading data file


notice the length of 2 lines
3. write to data


notice the locations of cube and sphere. don’t know the reason

to do

  1. assign color to face. a mesh can have 16 materials at most, what if a Lego part has more than 16 colors?
  2. type 0, 5 when read data file
  3. speed up. The re-index on vertices and faces needs a very big memeory, and costs too much time. Maybe skip it and let the user “remove doubles”. Well, I prefer to a python interface to “remove doubles” very much.
  4. GUI

a result of the script:

could it be possible to make from each lego piece one mesh and then parent it to one mesh? im having trouble seperating the legopieces because sometimes they share the same vertices.
or is it possible to assign multiple matirials to one mesh?

If the vertices at the same location doesn’t join together, there will be so many vertices for a mesh. Then more PC memory is need.

Furthermore, the “lego piece” conception is obscure, the real elements of a gizmo is lines and faces difined by vertices. If the same vertices for different parts is not merged, you can’t still get the wanted mesh, e.g. for a box piece, you will get exactly 4 rectangle faces and maybe 12 lines seperately.

And there is another reason: a “lego piece” file A may link another file B, and the mesh in B file is treated as a block. A joined mesh is easy for programming.

My script will not be updated till I kill the bugs I’ve mentioned.

P.S. I notice that every Blender mesh can have at most 16 materials, but a lego mesh can have more materials. I have no idea in this case.

what my biggest concern is that i want a nice colored mesh. a single mesh can be very handy for a programmer but the customer (me :wink: ) wants a nice sollution.

i see that mlcad has 16 standard colors. i can stick to them if i can get a nice collored lego model.

but i cant do your job (yet) so ill have to wait for you.

and as the puppet repair man says in toy story 2: “you cant rush art”.
i can wait.


i have programed an exporter in Java which reads LDraw files an convert them into another format (f.e. Wavefront .OBJ which could imported by blender very good)

Look at

you can also open LDraw files in Leocad, and then export them to OBJ. I’ve got great results this way.