3D OBJ model importer (for potential future inclusion with Codea. Now with wireframe mode)

@yojimbo2000 - strike that. It seems I downloaded an HTML mess by “saving as” from the file links.

The wireframe is great for seeing how the vertices are arranged.

I suggest there should be an option to download model files automatically, rather than having to copy and paste mtl and obj files and then sync manually.

wrt model selection, I suggest that vehicles would be a good starting point, because they require little or no animation, as opposed to people figures.

I like the tank, but there are so many vertices in the tracks, which is something I am trying to get around, for example by using an image for the wheels and most of the track, instead of vertices.

wrt anti-aliasing, given that Codea automatically does this, do you need to program it yourself?

I suggest there should be an option to download model files automatically, rather than having to copy and paste mtl and obj files and then sync manually.

Download from where? These 4 models were downloaded as blender files, I did the .obj export myself (I didn’t make any other changes to the models), and this is how I imagine this kind of code being used, people processing their own files (or, I would urge, creating their own models). So it’s a lot easier just to export it straight into your Dropbox and hit sync, then have to upload it to some server, type in a long and complex URL to your Codea project, and then have a class that waits for the various files needed to asynchronously downloaded. If there was a massive library somewhere of good models ready to go in .obj format, then I’d see the point of adding a download function, but there isn’t, as far as I’m aware. So I’m assuming that a blender object importer does involve some blender usage. Also, if you export to Xcode, you want the model files to be included in one of the asset folders.

wrt model selection, I suggest that vehicles would be a good starting point, because they require little or no animation, as opposed to people figures.

The models are mainly to test the loader. I browsed through the “low poly” tag in blendswap, and downloaded models that were a megabyte or less. Nowadays though it seems that anything that isn’t absolutely photo-realistic can be considered “low poly”. The tank does have too much detail in the undercarriage (which you’ll hardly see). This is where the “decimate” modifier comes in handy in Blender.

If models were to be bundled with Blender, I guess the best thing would be a themed set, a 3d equivalent to “Planet Cute” or whatever. I’ll keep looking.

wrt anti-aliasing, given that Codea automatically does this, do you need to program it yourself?

Codea doesn’t alias the edges of meshes (though it does alias any textures on the mesh), so yes you do (the built-in primitives alias the edges, though they appear to use different techniques, the thickness of the stroke is not quite consistent between rectangle and ellipse) . Given that the wireframe shader is already calling fwidth, it’s almost free in this case.

I was thinking of having a download option for situations where you share code on the forum, it makes it easier for other people to try out. The models could be stored anywhere in the cloud.

The most common models likely to be used in Codea are likely to be vehicles, so if we build a common library, that’s what I would start with. (Anyone who can animate humanoid figures doesn’t need our help finding models!).

Anyway, if you’ve finished tweaking the loader, I’ll put some models through it.

pretty awesome

If anyone’s using this who’s a Codea beta-tester, note that in 2.3.2 build 51, matrix.rotate command takes radians, instead of degrees, so the models will spin round way too fast when you swipe the screen. Add math.rad to the 3 rot:rotate commands ie rot:rotate(math.rad(dx), x:unpack()). Or you could just wait, as apparently it will revert back to degrees in a future build.

Hi @yojimbo2000,

Been in circles with your app, my iPad problem with Dropbox (now resolved) and the change with the Dropbox.asset… Nomenclature. Now up and running - very impressed.

One suggestion, I have a number of models without materials, detecting this in the obj file and setting up a default material would avoid error generation and allow display of models. There is also the instances where the obj file incorporates the mtl file. Just trying to think of a way to load most files without error.

Thanks for your work/code.



OK, I didn’t realise the mtl file could be part of the obj file. Do you know what software exports that way? If you could link to a source file example of an object that incorporates the mtl file, that would be really helpful.

Checking for the absence of mtl files shouldn’t be too hard to add.

Hi @yojimbo2000,

See the following link, it appears this is incorporating vertex colouring in the vertices data and is not legal obj.

Vertex colouring

@Ignatz included the text of the mtl file in some of his earlier 3D modelling files. Also I think the full spec on obj models includes a usemtl feature.

obj spec

The other option is the ply model which does incorporate vertex colouring.

Question is, if we are looking for a model loading package in Codea how far do you take it?


I would just stick to standard obj/mtl files, and not get too fancy

Only a handful of users will need it anyway

Adding vertex colouring to OBJ files is not standard, and not something I plan on adding.

I have written a PLY importer, but haven’t updated it in a while, mainly because the Blender PLY exporter is not as fully-featured as the OBJ one, so it’s not as useful a format. PLY though does support vertex colouring as standard (if you search the forum, I posted the code and some images).

I don’t think that vertex painting is as useful a technique as just defining colours for various materials.

I believe @Ignatz concatenated the files manually in a text editor, I’m not keen on that approach, because it slows the workflow too much, eg if you just want to make a small change to a model.

The usemtl field is the name of the mtl file, and not a way to include the mtl data directly in the obj file.

Hi @yojimbo2000, @ignatz,

Thanks for the feedback, agree that any Codea model loader should cover the dominant obj format. Other formats could be covered by user code for specific needs.


embarassing question!..how do i copy the model files from the github into the dropbox?

Save them to a file in the Codea folder of your Dropbox account, then go into Codea and sync your Dropbox.

i can copy into the pasteboard but i dont see the way to save the file from the github?

@piinthesky you can download the entire repository as a zip file (should be a download zip button on the right, might not appear if you use a content blocker)

Or, I have just uploaded a new version that automatically downloads the models for you from the repository. It’s also designed to address a few changes coming in Codea 2.3.2. You can install it with the installer called Codea 2_3_2 OBJ Importer Installer.lua. If you’re not a beta-tester, then in the readAssets tab you’ll have to change the Dropbox.assets string back to Dropbox.assetpack but other than that, it should work.

@Ignatz adding asynchronous download of remote files does increase the code base quite a bit. File loading is now separate from OBJ parsing, which is probably a good thing. It doesn’t yet support remote download of textures though, that’s next.

thanks @yojimbo2000 the automatic download works great.

@piinthesky I’m not sure what this means, but when I examine the file for the sub, the faces are defined as 3 sets of 3 values separated with slashes. eg f 270/270/69 217/217/70 271/271/108. Usually, the .obj files created by Blender just have three values per face, separated by spaces, eg f 120 121 127, and this is the only .obj format that the importer knows about. My guess is that the the three sets of values for the face refer to the vertices, texCoords, and normals, perhaps it’s v/tc/n v/tc/n v/tc/n for each face. That’s just my guess though. What I suggest you do is this:

  • try to find some documentation online about different forms of the .obj format (this is one of the most frustrating things about 3d work, that many of the formats are so poorly specced), to confirm this alternative format for faces.
  • fork the Codea obj importer code on GitHub
  • modify it so that it works with this format
  • submit a pull request (if you like) and I can merge your changes back in. Or you can keep your version as a separate fork, and just let us know on the forum.

@yojimbo2000 i would like to use this .obj model of a submarine…


i managed to read in the files, but it processes 0 vertices. Any ideas to fix this?

@yojimbo2000 the obj specs seems to be quite well documented in wikipedia and it confirms your guess.

I had the idea to load the model into blender and then export in the preferred codea format. That worked well and i can now load it into codea.

Thanks for your help.

That’s interesting. I tried loading the model into blender and re-exporting as an .obj, but it still had the same format for the faces. Did you do anything to the model or change the export settings?

Glad you got it working.