3D model loading from blender (PLY) and Suggestion: Need to load local files

While developing my light racer, I wanted to use a 3D model for the bike or other things. As I like to work with Blender, I decided to write a PLY loader. Also, it is a very simple file format. I even made it simpler by stripping the header, because how I use Blender, I always get the same format.

But I ran into some problems: There is no way (or I didn’t find one) to load a non-graphic file like I do it for PNGs in the DropBox. I don’t want to access them online, just like with the PNGs, synchronize them and then access them. This would be really huge so I could create any kind of accessory files I need for my project. Even better would be to have them synchronized to the projects folders for more structure :slight_smile:

To get around this problem for now, I’v put the content of the PLY into a LUA file in a string. Looks like this then:

plyCube =
"-1.000000 -1.000000 1.000000 -1.000000 0.000000 0.000000 0.666667 0.332314\
"-1.000000 1.000000 1.000000 -1.000000 0.000000 0.000000 0.334353 0.333333\
"-1.000000 1.000000 -1.000000 -1.000000 0.000000 0.000000 0.333333 0.001019\
"-1.000000 1.000000 1.000000 0.000000 1.000000 -0.000000 0.001020 0.333333\
"1.000000 1.000000 1.000000 0.000000 1.000000 -0.000000 0.000000 0.001020\
"1.000000 1.000000 -1.000000 0.000000 1.000000 -0.000000 0.332314 0.000000\
"1.000000 1.000000 1.000000 1.000000 0.000000 0.000000 0.333333 0.665647\
"1.000000 -1.000000 1.000000 1.000000 0.000000 0.000000 0.001019 0.666667\
"1.000000 -1.000000 -1.000000 1.000000 0.000000 0.000000 0.000000 0.334353\
"-1.000000 -1.000000 1.000000 -0.000000 -1.000000 0.000000 0.333333 0.334353\
"-1.000000 -1.000000 -1.000000 -0.000000 -1.000000 0.000000 0.665647 0.333333\
"1.000000 -1.000000 1.000000 -0.000000 -1.000000 0.000000 0.334353 0.666667\
"-1.000000 -1.000000 -1.000000 -0.000000 -0.000000 -1.000000 1.000000 0.332314\
"-1.000000 1.000000 -1.000000 -0.000000 -0.000000 -1.000000 0.667686 0.333333\
"1.000000 1.000000 -1.000000 -0.000000 -0.000000 -1.000000 0.666667 0.001019\
"1.000000 -1.000000 1.000000 -0.000000 0.000000 1.000000 0.333333 0.667686\
"1.000000 1.000000 1.000000 -0.000000 0.000000 1.000000 0.665647 0.666667\
"-1.000000 1.000000 1.000000 -0.000000 0.000000 1.000000 0.666667 0.998980\
"-1.000000 -1.000000 -1.000000 -1.000000 -0.000000 -0.000000 0.665647 0.000000\
"-1.000000 1.000000 -1.000000 0.000000 1.000000 0.000000 0.333333 0.332314\
"1.000000 1.000000 -1.000000 1.000000 -0.000000 0.000000 0.332314 0.333333\
"1.000000 -1.000000 -1.000000 0.000000 -1.000000 0.000000 0.666667 0.665647\
"1.000000 -1.000000 -1.000000 0.000000 -0.000000 -1.000000 0.998980 0.000000\
"-1.000000 -1.000000 1.000000 0.000000 0.000000 1.000000 0.334353 1.000000\
"3 0 1 2\
"3 3 4 5\
"3 6 7 8\
"3 9 10 11\
"3 12 13 14\
"3 15 16 17\
"3 18 0 2\
"3 19 3 5\
"3 20 6 8\
"3 11 10 21\
"3 22 12 14\
"3 23 15 17"

That means, the file contains 24 vertices and 12 polygons. Each vertex has xyz position, xyz normal (ignored for now) and uv texture coordinate. The polygons have to be triangulated in Blender before exporting, so I can ignore the first number 3 there. I made this restrictions to keep the loader simple, but I have to be aware of these in Blender.

Now the problem is, that I wanted a more complex shape (Blender Monkey). But LUA gives me an error, when I want to concatenate more than 200 lines… so I made very long lines and finally it accepted the Monkey… phew. Its 2868 vertices and 968 triangles including texture coordinates in one big string. I always have to create this string, which is a bit annoying, but Notepad++ macros are a help here. You see in the picture, that the monkey is very coarse. It would not be a problem for Codea to display thousands of triangles, but I think I can’t put all of them into one large string… I really need that file loader :slight_smile:

Finally after loading I use a texture which I created in Blender → It is an ambient occlusion map, which contains the lighting, so I don’t have to calculate it in realtime. Basically it would be possible with the normals, but it is faster, although a bit static, with the textures.

If you like to try it for yourself, here are the files. You need to upload the monkey_2.png to your Dropbox and synchronize it. Files are too large to post here, so here are the links:


And finally a screenshot in action:

redo your timing without the print statements (use text() instead) - print is HORRIBLY slow.

BTW: I’ve been using MeshLab for most of my stuff - it’s fast enough on the triangles and does a good job of deciamtion, but it is really really buggy - like, save after each transform or point/vertex/face command. Ad it’s bad at handling textures.

sigh - looks like I need to get into Blender again. If it trangilates and decimante, it might be all I need.

BTW: We should make a guidline that generally, an object in a game should be low poly and depend on textures. I’m pretty sure that’s the way a lot of games pull of many fps on user interactive objects. So, no 70,000 triangle bunnies - decimate that thing down to several hundred tops.

i know that print is slow, but the 7 seconds wont change wihtout print and the 0.1 also wont significantly, as i have that same time for the first calculation too, where no print slowed down. if you want fps with a history graph you could use my fps library, i posted a few weeks ago.

the bunny was just a test to see what is possible. i would never use such a detailed model in a game, nobody could see that details. but i need to know the limits of the system, so i could see how many low-poly-objects could be shown in a scene.

Wow, that’s a cool looking model!

Forgot to mention, I have (and others have too) functions that load and save data locally. You need one of those for your ply file so that you don’t have to goback to dropbox every time.

It’ll still take a long to to parse and load your file, though, your ply file looks simpler that my X3D files.

This post has someone doing what you are looking for (more or less):


I also rolled my own, but it’s pretty complex with all sorts of chace mechanisms and online/offline loading.

Hmm, so you download it once from dropbox with HTTP calls and then save it into a graphic so you can later read it? Interesting workaround.

That PLY takes 0.6 seconds to load and parse and 0.04 seconds to setup the mesh then.

Yeah, I have polys that are a fewhundred verts, up to a thousand or so, it takes about 10 secs to initialize the whole scene, so I lazy load a bunch of it and cahce load the rest.

Nice monkey. I was thinking of adding model loading to mesh:

-- 1. load model from a string
model = mesh(string)

-- 2. load model from a file
model = mesh("Dropbox:Monkey.mesh")

The question is what formats to support, and how to support textures and such. Any thoughts?

X3D is my vote. Web standard…

Integrated model loading would be great :slight_smile:

Regarding the format I agree, that X3D is a standard. But it has a huge feature set. It can contain complete scenes including material definitions, lights, animations etc. But the mesh in Codea only needs a few things:

  • Vertices.
  • Texture coordinates.
  • Triangles.

Maybe normals would be good to read, although not yet used, they could be used to calculate dynamic lighting later.

PLY has exactly that set of features. Also, PLY can be exported from blender, a wide spread, free modeling and rendering system. there are also x3d exporters for blender, but there is not only one, there are a few. thats because every exporter exports a different feature set. I have experienced something similar with collada at my work. it is also a standard, similar rich of features like x3d. now we wrote an exporter of our software to collada. what happened is, that not every software could read it. the format has so many possibilities and its hard to write an exporter to satisfy all importers. what i want to say is, that such complex formats like x3d and collada will make problems with codea as not every software which can export x3d will do it like the codea reader will need it. i think, we need a simple format.

Hi @KilamMalik, I agree with you on those points. I checked out X3D but it does look incredibly complex for such a simple use case. I’m currently working on new extensions to mesh that allow for normals and various other things, which should be coming in 1.5. Something like PLY, or OBJ is probably a good idea but I’ll have to look into it further.

@John. My preference would be .obj - probably one of the oldest and widely used polygonal data formats in the CG industry. Pretty much all the 3d packages can export to this (3dsMax, Blender, Maya…bla bla bla) with many free online model repositories. :slight_smile:

Yes, obj is fine too. its also a simple format with vertices, normals, texture and faces like ply. like @andymac3d said it is widely used, i think more than ply, so i would also vote for obj.

if you think of 3ds format, dont do it… the lighting groups are a pain.

@John yes +1 for .obj you can use it in programs like Hash’s Animation Master (All 3D models in Hash are made with the .obj format as far as I know)

Would it be out of the question to support .dxf (Autodesk) files? There’s a lot of info online about the format, including full C++ code. The reason I ask is because I can build some serious 3D models in 3D Studio Max and Maya for some of the games being developed here. I can’t contribute much by way of programming, but I can contribute professional-quality artwork.

Hi @KilamMalik, great model script! You can do away with concatenation … By using literal string delimiters you won’t have to break up your string into lines etc. here is an example:

    String = [[
     <A HREF="http://www.lua.org">Lua</A>
     [[a text between double brackets]]

FBX format is probably the best to implement, as it is the format most widely used to export from many 3D applications, either directly or via free plugins. I don’t know the full details, but it seems to be a Universal Format, that is well supported.