Jump to content

.MD2 Blender Import/Export Add-on Early Release


Irup
 Share

Recommended Posts

Currently it only supports importing basic meshes, but it exports functional models that can be seen in the game.

I'd greatly appreciate help in developing this add-on; please post any fixes or added functionality you can make to the scripts here.

 

Download the add-on here.

 

Installation instructions:

Download the .zip file (don't open), and in Blender, go in File > User Preferences, and go in the Add-ons tab.

 

From there, press "Install Add-on from file", and navigate to the .zip file.

 

Once you've hit install on that, it's been added to the add-ons list, which you can find and enable by searching "Lego".

Lastly, hit Save User Settings in the bottom left.

 

Usage instructions:

Every .md2 model in the game (with the sole exception of those with their filename starting with ARCTIC in EDITOR GEN\PATCHES\MODELS, and some slight polygon issues with 26 other models) can now be imported. Simply go in File > Import, and select the .md2 option.

 

There are currently some issues with all the polygons being inverted in blender, but keep them this way for export, as they'll get inverted again.

To flip normals, in edit mode, press space, and use "Flip Normals".

 

If you want to export a model, you must stick to a strict scene setup, which emulates the way the model file is structured. An imported model is immediately ready for export.

Imported scene example, ready for export:

Spoiler

xNyiblz.png

 

The rules are:

  1. An empty, referred to as the root, must be the parent of one or more child empties, referred to as detail levels, which in turn are the parents of one or more child meshes objects, referred to as render groups.
  2. These child meshes are split according to material, which is shown by how they're named in imports. Every mesh needs 1 material.
  3. The material name is the entire path, which is directly inserted into the file, and must start with "game data". The ending of the path is .tga, but the target file ends with a .MIP extension (which is actually just a renamed .tga).
  4. Every polygon must be a triangle, which means no quads or ngons. Triangulate your mesh(es) by being in edit mode, selecting everything (A), and hitting Ctrl + T.
  5. There must be a UV map present, but this is usually not a problem.
  6. Make sure your polygons face inwards before export, as they'll get inverted in the resulting file. This is a bug that I can't figure out how to fix.

 

The exporter cycles through every object, and finds a root, and determines if it's valid. This means it disregards your selection, and completely ignores all objects directly under the scene that isn't an empty.

 

You can open .MIP files, which are automatically recognized as .tga files, by opening an image and unchecking the filter. The importer has an option checked on by default that does this for every image used by the model, though you'll have to apply them to the meshes yourself.

 

This is the import:

O7lOoUp.png

 

This is after importing the model, then exporting it with no changes:

4hr4inT.png

 

Features I want before I can call this add-on finished:

  • Full shader support, by means of an easily editable text file created on import.
  • More flexible scene requirements for export.
  • Weights and animation support (perhaps the SKN0 chunk in the model, and BSA and BSB files).
  • Collision data ("COLD" chunk in the files). I have not been successful in deciphering them.
  • Fixed normals.

 

Here's a GitHub repository for it.

Link to comment
Share on other sites

Updated with functional UVs. In the future, things like shaders could be imported and exported by making a shader script file in Blender's text editor, sort of like how .qc files in Valve games work, which is important considering how bad exports currently look.

 

The COLD chunk in models determine their COLlision Data, and can be copied from one model over to another, so it's completely independent. I'd like to import it, I just can't figure out how it works though.

Link to comment
Share on other sites

Updated again because the normals are fixed now.

 

This is before, with Blender's automatically generated normals:

qD5oEB5.png

 

This is now, with the original normals preserved:

4hr4inT.png

 

Shaders are still not a reality just yet.

  • Like 1
Link to comment
Share on other sites

  • lol username featured this topic
  • lol username unfeatured this topic
Fluffy Cupcake

It works in 2.79 also.

 

You may or may not be aware but the palm trees under sandy isle are throwing an error on import:

Spoiler

Traceback (most recent call last):
  File "C:\Users\Xir\AppData\Roaming\Blender Foundation\Blender\2.79\scripts\addons\io_lr2\__init__.py", line 61, in execute
    import_lr2.open_lr2(path, **keywords)
  File "C:\Users\Xir\AppData\Roaming\Blender Foundation\Blender\2.79\scripts\addons\io_lr2\import_lr2.py", line 33, in open_lr2
    if   first4 == b'MDL2': return open_mdl2(filepath, **kwargs)
  File "C:\Users\Xir\AppData\Roaming\Blender Foundation\Blender\2.79\scripts\addons\io_lr2\import_lr2.py", line 213, in open_mdl2
    work_bmesh.faces.new((work_bmesh.verts[x] for x in unpack('3H', f.read(6))))#.material_index = geo1_texblend_blends[0][1]
ValueError: faces.new(verts): face already exists

location: <unknown location>:-1

 

 

In other news, I very much approve of this in-development plugin

Now if we just had a WRL editor...

testin_LR2.png

 

 

Tips for those wanting to export so you don't run into some of the issues I did.

 

In edit mode:

Press A to select your entire model then hit ctrl+t to turn your model intro triangles. Quads aren't supported. After that hit P and do the separate by material option. This is needed because the model doesn't support multi-texture in any other manner than on separate parts.

On a additional note, before you do separate it into parts, you made need to flip faces, as it seems the backface in blender is the side of the face that shows in LR2.

 

  • Like 1
Link to comment
Share on other sites

You're right, the normals are not fixed at all! There's still some inversion going on; it imports inverted, then exports inverted again, which comes out as normal if you don't do anything else, which led me to believe that it works as intended. I have no idea how to deal with this.

I've made the requirements for export very inflexible due to the differences in how Blender and the .md2 format handles things (and because I'm not so good at the API), so it's very poor of me to not have made any sort of guide. I'll write one shortly.

 

I just updated the add-on to address the palm tree problem by just ignoring the faces, because it seems unfixable without further complicating the scene because it really is defining the exact same face twice, which LR2's engine interprets as having 2 polygons with different directions, as to have textures that show on both sides, and this simply isn't possible in Blender, to my knowledge. I've scanned all the model files for oddities and uploaded my findings to the GitHub repo, which includes a full list of these "double" models, as I've concisely decided to call them.

 

I really appreciate any and all feedback, especially bug reports that point to further progress.

Link to comment
Share on other sites

Fluffy Cupcake

Files under \game data\editor gen\patches\models\ still throwing an error? I see you've written them down as old though. Which types currently are and aren't supported?

 

3 hours ago, Irup said:

because it really is defining the exact same face twice, which LR2's engine interprets as having 2 polygons with different directions, as to have textures that show on both sides, and this simply isn't possible in Blender

So basically have double the mesh data instead of having the backface specified to render? How unoptimal of LR2.

Link to comment
Share on other sites

lol username
2 hours ago, Xir said:

So basically have double the mesh data instead of having the backface specified to render? How unoptimal of LR2.

Not really... Honestly I don't feel like going into detail as it's past midnight, but in short... it's a straightforward way of doing it, and enabling/disabling backface culling for specific parts of a model would be more convoluted and/or inefficient than this.

Link to comment
Share on other sites

Fluffy Cupcake

Why not just have the material additionally show on the backside for certain areas? Then again, I'm not sure how they would pull that off. I might be using too much Unity where based off material type you can choose to have it show double sided or not. =P

Link to comment
Share on other sites

lol username
12 hours ago, Xir said:

Why not just have the material additionally show on the backside for certain areas? Then again, I'm not sure how they would pull that off.

 

TIME FOR A BIG WALL OF TEXT NOT ESPECIALLY RELEVANT TO THE PROBLEM AT HAND :D

 

nltmIwR.png

 

There's a model of the policeman's car sitting behind the station. It's visually the same as the car he drives, but as one solid MD2. Conveniently enough, the player happens to be driving the policeman's car (as an actual car) for comparison.

 

Note how the interior faces of the 1x2 transparent blue slopes are kept, to keep the appearance consistent with the "real" car, and to keep it from looking like a solid 1x4 piece. That means there's two faces in the middle, occupying the same space, but facing opposite directions, like the "real" car. But this time they're part of the same model, not parts of two separate 1x2 slope models.

 

If you were the artist making this model, would you have:

a) Exchanged two triangles for an entirely new material (and thus an extra draw call) with backface culling disabled, just for the two remaining triangles (absolutely ridiculous, would be a cost on performance, not a gain), or

b) Just done the easy + harmless option they went with?

 

12 hours ago, Xir said:

I might be using too much Unity where based off material type you can choose to have it show double sided or not. =P

Depending on what exactly you're doing, you could be costing yourself there... There's overhead between switching materials and shaders ("material type") while rendering out a frame. It's usually a good idea to try to share as many materials and shaders between things as you can, within reason.

 


 

Now then, onto the actual problem... A while back I made a quick little thing using LibLR2 to load up MD2s in the Unity editor and export to OBJ. It doesn't support every feature of the format of course (just basic geometry + original normals and UVs) but it's enough to look at these problem models with the double faces.

 

They're almost as expected... The weird bit is how the two sides seem welded to each other/the normals are pretty unusual.

 

X1dDJzn.png

 

nIvpwMj.png

 

In-game, the effect you'd get from this would be the leaves appearing at a consistent flat brightness, top and bottom, with the brightness varying depending on the model's angle to the directional light... I've been trying to load up the game to have a look, but it's not working that well on this laptop...

 

The police car lights in the above example are actually fine from the get-go, the weird parts (that I've noticed anyway) are on a lower LOD: the bar in front of the lights, the antenna, and steering wheel...

 

Did they edit these vertex normals by hand or something? It could kiiiiinda make sense for the trees (I could see what they were going for at least, people hand edit vertex normals on plants all the time)... But the police car is a big bundle of wat.

 

Like, this vert here - its normal is skewed for no apparent reason:

 

9tJTUAJ.png

 

And the normals for the antenna, bar, and steering wheel are all pointing to the left...

 

CcUtJBi.png

 

SRiwn5p.png

 

Link to comment
Share on other sites

Updated with the ability to open MDL1 and MDL0 format models, which I was surprised to find actually works in the game. They can't do anything MDL2 can't do, so export options for them is not high priority.

 

The MDL0 models, all in EDITOR GEN\PATCHES\MODELS, all appear to be flat terrain tests, with some of them having a slight bump in the middle. Those that start with ARCTIC use a different mesh building scheme (the only instances of this ever happening in .md2 files), which LibLR2 describes as being triangle strips, but I can't work out how this can be done with the vertex and polygon data provided. The importer doesn't work on them because they report a vertex count lower than is actually present, so it tries reading polygon data in the middle of the vertex data, and crashes.

 

How did Will retrieve all the variable names, a significant portion of which don't even seem to be used, and find out how the file is read? If I knew, this project would be made many times easier.

 

Regarding the police car; the steering wheel, antenna, and and the bar behind the seat (the wide plane in your picture), are all the 14 problem faces in this model, wherein the same 3 vertices are used twice to define the same face, which show on both sides in the game. The windshield is equally as flat, but it's got two vertices in the same place, allowing for more traditional two-sided planes. Truly a Frankenstein's monster.

Link to comment
Share on other sites

lol username

The engine was used in other games besides LR2 (including non-LEGO games; it was a general purpose engine made and owned by ATD). LR2 didn't use all the features provided, and some didn't exist at the time LR2 was made.

Link to comment
Share on other sites

Perhaps we should be calling them ATD models? I've read that Drome Racers uses a different version of the model, a newer one that won't immediately work with the importer, but LR2 and the games prior to LR2 should work now.

Link to comment
Share on other sites

lol username

Yeah, the formats can vary a bit between games. Also, missed this earlier:

 

45 minutes ago, Irup said:

Regarding the police car; the steering wheel, antenna, and and the bar behind the seat (the wide plane in your picture), are all the 14 problem faces in this model, wherein the same 3 vertices are used twice to define the same face, which show on both sides in the game. The windshield is equally as flat, but it's got two vertices in the same place, allowing for more traditional two-sided planes. Truly a Frankenstein's monster.

 

Yeah, it's pretty weird. What I find almost weirder is how the normals for those areas aren't like what you'd get if they were autocalculated in the usual fashion, but all pointing in one direction... Not sure if they were doing this intentionally, or if their weird modeling broke the calculation of the normals, haha.

Link to comment
Share on other sites

I exported a test model to see how that worked. I'm not used to Blender, but I was able to get it to work. The crystal is supposed to have a red texture on it, but it didn't seem to load COMMON\TEXTURES\PIN_RED.MIP

 

Spoiler

large.LetTheCrystalsGrow.png.86919d0ba74

 

The crystal does not have collision, as is expected at this time. Good progress so far.

Link to comment
Share on other sites

OI

 

That's partly Milkshape's fault for cutting off the filepath, but I also didn't notice. Even if I did, I tried .MIP and it didn't work. I didn't think to try .TGA. It works now, thank you. Red crystals are filling Sandy Bay.

Link to comment
Share on other sites

  • 4 months later...
Fluffy Cupcake

Hey @Irup - I can only hope that ping gets your attention, but any chance on working more on this? I'd really like to see it done and in a usable state.

Link to comment
Share on other sites

Sorry for the absence. I perform poorly with forums; I can only really be comfortable with instant messaging.

I lost interest in the project and just left it, but I'll try and hop back in tomorrow.

Just before I left, I implemented triangle strips in the repository, so I'm at least going to update the add-on with that.

Link to comment
Share on other sites

We do have a Discord server if that makes things easier for you. Sign up if you want and maybe some other members will be able to give you a hand if you need it. I just hate to see projects like this fizzle away.

 

 

Link to comment
Share on other sites

  • 4 years later...

ik this is a pretty old conversation but I am having trouble with this. I have followed everything as instructed but every time I boot up the game, It crashes. 

All I want is to make new head pieces for some of the characters and later on eventually add in new parts for the car builder 

 

Link to comment
Share on other sites

 Share

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.