Code covered by the BSD License  

Highlights from
Wavefront OBJ toolbox

4.33333

4.3 | 7 ratings Rate this file 95 Downloads (last 30 days) File Size: 203.49 KB File ID: #27982

Wavefront OBJ toolbox

by Dirk-Jan Kroon

 

22 Jun 2010 (Updated 09 Sep 2011)

Read and Write Wavefront OBJ geometry and MTL files

| Watch this File

File Information
Description

Read and Write Wavefront OBJ geometry and MTL files.

A Wavefront Geometry Object file is supported by many 3D programs, it looks like:
v 0.000000 2.000000 0.000000
v 0.000000 0.000000 0.000000
v 2.000000 0.000000 0.000000
v 2.000000 2.000000 0.000000
% Vertices Coordinates 4
f 1 2 3 4
% Faces 1

The function read_wobj allow Wavefront object files including material MTL files to be read into a Matlab structure.
The function write_wobj exports a Matlab structure to an OBJ file.

It doesn't matter if the object file contains splines or polygon based geometry, it reads and writes almost all .obj files.

See "help read_wobj" and "help write_wobj"

Comments and bug reports are welcome

MATLAB release MATLAB 7.10 (2010a)
Tags for This File  
Everyone's Tags
Tags I've Applied
Add New Tags Please login to tag files.
Comments and Ratings (13)
30 Jul 2010 Jun wan

it's great work! thanks

02 Aug 2010 Jun wan

Hi,Jan:
     thanks for your code.I have a question that how can i using the texture data when show the 3D objtect? In your code ,you only used in vertices and faces. Looking forword to your answer.

12 Sep 2010 haidi

Too slow for large models!

14 Sep 2010 Dirk-Jan Kroon

*haidi
I know the for-loop is killing the speed in Matlab, especially because the wavefront obj. format is really broad.
If you know your data at forehand, you can remove probably more than 80% of the lines of code.

25 Apr 2011 Paul

So far this is a good find. I have found a bug if the file name of the obj contains a space. In line 69 of read_wobj.m: filename_mtl=fullfile(filefolder,data); returns the error:

??? Undefined function or method 'eq' for input arguments of type 'cell'.

Error in ==> fullfile at 39
      elseif (f(end)==fs) || (part(1)==fs || (bIsPC && (f(end)=='/' || part(1)=='/')) )

22 Sep 2011 Jaroslaw Tuszynski

Worked great!

28 Sep 2011 Oliver Woodford

Works well, good help text, well written.

11 Oct 2011 Oliver Woodford

However, it is painfully slow at reading larger files.

19 Oct 2011 mourad raafat

Rely good work but it give me that error so any one can help me cause i'm anew user for matlab
??? Error using ==> fread
Invalid file identifier. Use fopen to generate a valid file identifier.

Error in ==> read_wobj>file2cellarray at 298
file_text=fread(fid, inf, 'uint8=>char')';

Error in ==> read_wobj>readmtl at 363
file_words=file2cellarray(filename_mtl);

Error in ==> read_wobj at 78
            material=readmtl(filename_mtl,verbose);
and that in read file
thanks for help

08 Dec 2011 mclaudt

mourad, check filename for correct slash orientation.

Under Linux, you should use
OBJ=read_wobj('examples/example10.obj')
instead of
OBJ=read_wobj('examples\example10.obj')
from commented example.

13 Feb 2012 Dalia Nabil

I tried this on an obj file created by meshlab and it returned this error

 
% OBJ File Generated by Meshlab
  %
  % Object example-cloud.obj
  %
  % Vertices: 197749
  % Faces: 0
  %
??? Subscripted assignment dimension mismatch.

Error in ==> read_wobj at 90
                vertices(nv,1:4)=data;

do you know how to fix it ? or how to obtain a 3d coordinates from meshlab?
thanks

28 Mar 2012 amroun

I tried this on an obj file created by meshlab and it returned this error

  
% OBJ File Generated by Meshlab
  %
  % Object example-cloud.obj
  %
  % Vertices: 197749
  % Faces: 0
  %
??? Subscripted assignment dimension mismatch.

Error in ==> read_wobj at 90
                vertices(nv,1:4)=data;

do you know how to fix it ? or how to obtain a 3d coordinates from meshlab?
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

I have the same problem as Nabil. you have found the solution? Thank you

22 May 2012 Joe Wang

Works well for me but question is how to show the mesh model in Matlab?

Please login to add a comment or rating.
Updates
23 Jun 2010

Add file-format help and made the code compatible with all most all possible .obj files.

23 Feb 2011

Fixed Nan Bug (Detected by Owens M.A.)

09 Sep 2011

Bug fixes,
- Filename '.mtl' with space now supported
- Blender uses face objects in case of a line

Tag Activity for this File
Tag Applied By Date/Time
obj Dirk-Jan Kroon 22 Jun 2010 11:25:22
wavefront Dirk-Jan Kroon 22 Jun 2010 11:25:22
wobj Dirk-Jan Kroon 22 Jun 2010 11:25:22
mtl Dirk-Jan Kroon 22 Jun 2010 11:25:22
material Dirk-Jan Kroon 22 Jun 2010 11:25:22
3d Dirk-Jan Kroon 22 Jun 2010 11:25:22
mesh Dirk-Jan Kroon 22 Jun 2010 11:25:22
import Dirk-Jan Kroon 22 Jun 2010 11:25:22
blender Dirk-Jan Kroon 22 Jun 2010 11:25:22
polygon Dirk-Jan Kroon 22 Jun 2010 11:25:22
3d studio max Dirk-Jan Kroon 22 Jun 2010 11:25:22
export Dirk-Jan Kroon 22 Jun 2010 11:25:22
maya Dirk-Jan Kroon 22 Jun 2010 11:25:23
read Dirk-Jan Kroon 22 Jun 2010 11:25:23
write Dirk-Jan Kroon 22 Jun 2010 11:25:23
3d kyunghwan 01 May 2011 05:08:06
3d studio max kyunghwan 01 May 2011 05:08:09

Contact us at files@mathworks.com