Loading a .obj file into P5

Im a teacher at a school who is teaching P5.js to my students.

A student of mine exported a model from the 3D program Blender as a .obj file and had trouble using the loadModel() function when previewing it.

Upon inspection it may have been because her model was roughly 21mb. Are there technical limits for .obj files when using the loadModel() function. Things like file size, poly count, etc.

I did a run test using a simple .obj model and it worked fine, but the model was simple and was only 300kb.

Any information or point to information would be appreciated.

Hi @davidhurwich,

Without student model or code, there are a few simple issues to rule out first. A few diagnostic checks I run through when I have problems with model exporting and importing:

  1. In Blender:
    A. Was the model centered at the origin? If not, in View3D, go to Object > Set Origin > Geometry To Origin.
    B. What was the scale of the model?
    C. In the OBJ export menu, when you or the student open(s) up the options menu with the N key:
    D. When exporting, was the “LImit to selection only” check box ticked? If yes, was the model selected?
    E. Was the model transformed so the world coordinates matched the environment where it will be imported, i.e. p5’s? Even if the model does appear, an inappropriate transform may make the model look mirrored, upside-down, sideways.
    F. Does the model include the information you want (UVs, normals) and none of the info you don’t?

  2. In p5.js:
    A. When calling loadModel, is the normalize flag true or false?
    B. Where is loadModel called, in preload?
    C. Did any warning or error messages appear in the console?
    D. What information, if any, does the model variable contain when printed to the console? Knowing this may help you find out whether the model is there, but invisible, or not there.
    E. Where is the camera looking?
    F. How do the origin and scale of the p5 sketch’s imaginary world compare to Blender’s?
    G. How might appearances impact whether you can see the model: stroke, fill, material properties, lights?

Sounds like you already have a hunch as to what’s afoot. I recommend searching issues and/or looking at the source code on Github. In issues, this one from 2017 looks relevant. The source code here addresses the parsing. If size is the issue, the student can use a decimate modifier in Blender to reduce poly count.

Another step – if nothing else pans out – would be to open the .obj model in a text or code editor and compare what you see there against an overview of the file format, such as the one at Wikipedia. If there’s structure (groups, multiple materials, etc.) which might gum up a simplified parsing process, then you have another lead to pursue.

Hope that helps you find your answer. Best,

1 Like