Detailed description of OBJ file format in 3D

Source: Internet
Author: User

Common *. there are two types of obj Files: The first is an OBJ File (also called the target File) in COFF (Common Object File Format) Format, which is used to compile applications; the second type is the OBJ model file launched by Alias | Wavefront. This article analyzes the second obj model file.

3D file format, common "*. 3ds ","*. max ","*. lw ","*. mb ","*. dxf ","*. obj ". However, the specific features of the OBJ file are rarely described satisfactorily. Many people know that OBJ files start from using Poser. Poser is a human body modeling software that exports the human body generated by Poser to other 3D software for further processing, the OBJ file is used.OBJ files are a standard format of 3D model files and are suitable for intercommunication between 3D software models.. For example, if you create a model in 3dsMax or LightWave and want to adjust it to rendering or animation in Maya, exporting the OBJ file is a good choice. Currently, almost all well-known 3D software supports reading and writing OBJ files. However, many software requires plug-ins to do this.
In addition, as an excellent file format, many game engines also support reading OBJ files. The interaction between 3D software models is a very common thing. Unfortunately, the current 3D software model export functions are not so perfect, and there is often a shortage of lines, sometimes the exported model cannot be opened.

OBJA file is a text file format. Compared to a binary file that is used as the primary file and connects to each block, you have to test the 3DS. A text file-based OBJ is more friendly to us. Different from the tree [block structure] of the 3DS File, The OBJ file is a very simple dictionary structure. It is represented by easy-to-understand ideographic characters without a block ID. In short, it looks pleasing to the eye, and it is only when the code is actually written and imported --. The OBJ file optimizes storage but degrades reading and writing.

If an error occurs in the model of Maya, you can convert it to the OBJ format and import it back to Maya.

OBJFILE-concept

OBJ files are a file format developed by Wavefront for its workstation-based 3D modeling and animation software "Advanced Visualizer". They are also read and written through Maya.

An OBJ file is a text file that can be opened directly on the WordPad for viewing, editing, and modification.

In addition, there is a binary file format (with the suffix ". MOD") related to this. The binary format is not disclosed as a patent, so we will not discuss it here.

The latest file version of OBJ is v3.0, which replaces the previous version of v2.11.

The OBJ3.0 format supports Polygon, Lines, Surfaces, and Free-form Curves ).

Straight lines and polygons are described by their points, and curves and surfaces are defined based on their control points and additional information attached to the curve type. This information supports regular and irregular curves, including those based on the besserx curve, B-spline, Cardinal/Catmull-Rom spline ), and the curve of Taylor equations.

1And OBJ files-features

(1) OBJ is a 3D model File. Therefore, it does not contain animation, material properties, texture paths, dynamics, particles, and other information.

(2) The OBJ file mainly supports Polygons models.

Although the OBJ file also supports Curves, Surfaces, and Point Group Materials, the OBJ file exported by Maya does not include this information.

(3) The OBJ file supports three or more vertices, which is useful.

Many other model file formats only support three vertices, so the models we import to Maya are often triangular, which is very unfavorable for us to re-process the models.

(4) OBJ files support normal and texture coordinates.

After the Paster is adjusted in other software, the Paster coordinate information can be saved to the OBJ file. After the file is imported to Maya, you only need to specify the path of the Paster file, you do not need to adjust the texture coordinates.

 

2, OBJ file-Basic structure

The OBJ File does not need any File Header, although the comments of several lines of File information are often used as the beginning of the File.

The OBJ file consists of a line of text. The comment line starts with a "well" (#). Spaces and blank lines can be added to the file at will to increase the readability of the file. A row with words starts with one or two marked letters, that is, the key word. A Keyword can indicate the data of a row. Multiple rows can be logically connected together to represent a row by adding a connector (\) at the end of each row (\).

Note that there cannot be spaces or tabs after the connector (\); otherwise, an error will occur in the file.

The following keywords can be used in the OBJ file.

In this list, keywords are arranged according to the data type. Each keyword has a brief description.

Vertex data ):

V Ric vertices)

Vt Texture coordinate point (Texture vertices)

Vn Vertex normal (Vertex normals)

Vp Parameter space vertex (Parameter space vertices)

Free-form curve/surface attributes ):

Deg (Degree)

Bmat base matrix)

Step size)

Cstype Curve or surface type (Curve or surface type)

Element (Elements ):

P Point (Point)

L Line)

F (Face)

Curv Curve (Curve)

Curv2 2D curve (2D curve)

Surf Surface)

Free-form curve/surface body statements ):

Parm Parameter value (Parameter values)

Trim external trim loop)

Inner trimming loop)

Scrv Special curve (Special curve)

Sp Special point)

End statement)

Connectivitybetween free-form surfaces ):

Con connection)

Grouping ):

G Group name)

S smooth group)

Mg merge group)

O Object name)

Display/render attributes ):

Bevel interpolation)
C_interp color interpolation (Colorinterpolation)
D_interp dissolve interpolation (Dissolveinterpolation)
Level of detail)
Usemtl Material name (Material name)
Mtllib Material library (Material library)
Shadow_obj Shadowcasting)
Trace_obj Ray tracing)
Curveapproximation technique)
Surface approximationtechnique)

3, OBJ file -- instance:
The following is an example.
The OBJ file records code of a quadrilateral:
V-0.58, 0.84, 0
V 2.68 1.17 0
V 2.84-2.03 0
V-1.92-2.89 0
F 1 2 3 4

Let's create an OBJ file, but this time we don't need 3D software, but use a WordPad to create it.
Open the WordPad and write the above five lines of code. You can add some comments as appropriate.
Save the file as a text file named "myObj. obj ",.

Note: At the end of the code, you must press enter to switch the cursor to the next line, that is, add a line break (\ n ). Otherwise, the following error message is displayed:
// Error: line 1: OBJ file line 5: index out ofrange .//
// Error: line 1: Error reading file .//

Import the "myObj. obj" file in Maya. You can see it and import a quadrilateral. The shape of this quadrilateral is completely determined by the preceding five lines of code.

            

Next we will analyze these codes.

V-0.58, 0.84, 0
Four vertices are required to draw a quadrilateral. This is the first vertex. "v" indicates vertex and "-0.58" indicates the x axis coordinate value of the vertex, "-0.84" is the y axis coordinate value, and "0" is the Z axis coordinate value. This is the first vertex, and its index number is 1. The index number is used in the screen.
V 2.68 1.17 0
V 2.84-2.03 0
V-1.92-2.89 0
These are the second, third, and fourth vertices. Their index numbers are 2, 3, and 4, respectively.

Now on the screen, "f" indicates the index number of the first four vertices (face), 1, 2, and 3. Pay attention to the order of drawing the surface connection points, starting from the first point and connecting the second, third, and fourth points in sequence. If the order of the connections is different, the planes are generated completely differently. For example, "f 1 2 4 3" produces an overlapping plane ,. The connection points of a plane are arranged clockwise or counterclockwise, which determines the direction of the plane's normal (the plane's anyway ). For example, the normal direction of the "f 1 2 3 4" plane is inside the normal direction of the "f 4 3 2 1" plane. An error in the connection point sequence of the above is an important cause for the broken surface of the import model.

A plane cannot contain more than two identical vertices, which is also the key to checking for errors in the OBJ file.

For example, "f 1 2 3 4 3" has two identical vertices, and the index number is 3. Two identical vertices appear on one side, which may cause memory allocation errors.

Next we will study the OBJ file exported by Maya.
Create a polygon cube in Maya, select the cube, and select "File-> Export Selection... "Export Format: OBJ, File name:" cube. obj ". If this format is not available, load" objExport. mll ". Open "cube. obj" on the WordPad and you can see the following code:
# The units used in this file are centimeters.
G default
V-0.500000-0.500000 0.500000
V 0.500000-0.500000 0.500000
V-0.500000 0.500000 0.500000
V 0.500000 0.500000 0.500000
V-0.500000 0.500000-0.500000
V 0.500000 0.500000-0.500000
V-0.500000-0.500000-0.500000
V 0.500000-0.500000-0.500000
Vt 0.000000 0.000000
Vt 1.000000 0.000000
Vt 0.000000 1.000000
Vt 1.000000 1.000000
Vt 0.000000 2.000000
Vt 1.000000 2.000000
Vt 0.000000 3.000000
Vt 1.000000 3.000000
Vt 0.000000 4.000000
Vt 1.000000 4.000000
Vt 2.000000 0.000000
Vt 2.000000 1.000000
Vt-1.000000 0.000000
Vt-1.000000 1.000000
Vn 0.000000 0.000000 1.000000
Vn 0.000000 0.000000 1.000000
Vn 0.000000 0.000000 1.000000
Vn 0.000000 0.000000 1.000000
Vn 0.000000 1.000000 0.000000
Vn 0.000000 1.000000 0.000000
Vn 0.000000 1.000000 0.000000
Vn 0.000000 1.000000 0.000000
Vn 0.000000 0.000000-1.000000
Vn 0.000000 0.000000-1.000000
Vn 0.000000 0.000000-1.000000
Vn 0.000000 0.000000-1.000000
Vn 0.000000-1.000000 0.000000
Vn 0.000000-1.000000 0.000000
Vn 0.000000-1.000000 0.000000
Vn 0.000000-1.000000 0.000000
Vn 1.000000 0.000000 0.000000
Vn 1.000000 0.000000 0.000000
Vn 1.000000 0.000000 0.000000
Vn 1.000000 0.000000 0.000000
VPC-1.000000 0.000000 0.000000
VPC-1.000000 0.000000 0.000000
VPC-1.000000 0.000000 0.000000
VPC-1.000000 0.000000 0.000000
S off
G pCube1
Usemtl initialShadingGroup
F 1/1, 1 2/2, 2 4/4, 3 3/3, 4
F 3/3/5 4/4/6 6/6/7 5/5/8
F 5/5/9 6/6/10 8/8/11 7/7/12
F 7/7/13 8/8/14 2/10/15 1/9/16
F 2/2/17 8/11/18 6/12/19 4/4/20
F 7/13/21 1/1/22 3/3/23 5/14/24
This file looks a little complicated and uses many keywords. You can view the meaning of each keyword in the previous list.

 

Let me explain it.
"Vt 1.000000 0.000000" indicates the texture coordinate of the point.
"Vn 0.000000 0.000000-1.000000" indicates the normal of the vertex.

"S off" indicates that the smooth group is disabled.
"Usemtl initialShadingGroup" indicates the material used.
"F 7/13/21" in this case, the texture coordinate uv points and the normal index numbers are added to the data, and the index numbers are separated by the left slash.
Format: "f vertex index/uv point index/normal Index ".
"G pCube1" indicates a group. The group here is different from the group in Maya. The group here refers to combining all the faces after "g pCube1, form an integral polygon Ry.

Modify the "cube. obj" file to understand the meaning of grouping. Replace "soff" with the following code:
Usemtl initialShadingGroup
G pCube_Face1
F 1/1, 1 2/2, 2 4/4, 3 3/3, 4
G pCube_Face2
F 3/3/5 4/4/6 6/6/7 5/5/8
G pCube_Face3
F 5/5/9 6/6/10 8/8/11 7/7/12
G pCube_Face4
F 7/7/13 8/8/14 2/10/15 1/9/16
G pCube_Face5
F 2/2/17 8/11/18 6/12/19 4/4/20
G pCube_Face6
F 7/13/21 1/1/22 3/3/23 5/14/24
After importing the image to Maya, we can see that each plane of the cube is separated, and each plane is named "pCube_Face (1 ~ 6) ", the visible group name is actually the name of a separate Ry.

Can I name the ry (group) in Chinese? Try and change the previous code:
Usemtl initialShadingGroup
G cube surface 1
F 1/1, 1 2/2, 2 4/4, 3 3/3, 4
G cube surface 2
F 3/3/5 4/4/6 6/6/7 5/5/8
G cube surface 3
F 5/5/9 6/6/10 8/8/11 7/7/12
G cube surface 4
F 7/7/13 8/8/14 2/10/15 1/9/16
G cube surface 5
F 2/2/17 8/11/18 6/12/19 4/4/20
G cube surface 6
F 7/13/21 1/1/22 3/3/23 5/14/24

Try it and you will find that the model is successfully imported. Although the object names are all garbled, this is not a serious issue.
However, the use of the Chinese name is not always so smooth. Change the line "g cube surface 1" to "g Select" and try again. The model cannot appear at the time of import, but only the following error message will appear:
// Error: line 1: Your OBJ file contains a line whichis too long to be parsed. Please edit your obj file .//
// Error: line 1: Error reading file .//
It can be seen that the nonstandard object naming is also one of the causes of errors in the OBJ file.
The name of an object in Maya is safe. Only the underscore (_) is available in punctuation marks, and numbers cannot start with a name, do not use double-byte text such as Chinese, Japanese, and Korean.

 

The OBJ file does not support polygon with holes.
For example:
Select the Polygons creation Tool (Polygons-> Create Polyon Tool) of Maya and draw a quadrilateral in the view. Do not press enter and press Ctrl to click in the center of the quadrilateral, you can continue to dig a hole in the quadrilateral. Save the polygon with holes into the OBJ format. When importing the polygon to Maya, you will find that there is one less polygon. If you think of this as an error, at least you already know the cause of the error, that is, the OBJ file does not support polygon with holes.

OBJ file -- actual problem:
Now let's discuss a more practical problem, that is, what should you do if you encounter an incorrect OBJ file?
When you open the OBJ file, you will often see tens of thousands of lines of code, you may not be able to give a look at the error line, unless the program's error message has told you the error line. If you do not know where the error is, you can use exclusion to find out the correct code range and locate the error by reducing the error code range. For example, you first create an empty OBJ file, paste the wrong OBJ file code in half, and then import the new OBJ file with only half of the code into Maya. If there is no error message at this time, it indicates that the error line is in the other half of the code. You can try another part of the code from the other half. If an error occurs at this time, the error message is displayed in the pasted code. You can delete a part of the pasted code and try again. In this way, the range is gradually reduced until the error line is found.
Although this method is troublesome, it is quite effective. If you don't know how to program and encounter a very urgent situation, this method is worth a try.

OBJ file -- details:
It is almost enough to have mastered so many files. However, since there are few Chinese documents on the OBJ file in detail on the Internet, I 'd like to explain some more examples and provide you with more information.

Simple OBJ format.
# Simple Wavefront file
V 0.0 0.0 0.0
V 0.0 1.0 0.0
V 1.0 0.0 0.0
F 1 2 3

You can use a negative index. Sometimes it is easier to use a negative index to describe the surface.
"F-4-3-2-1" this index value "-3" indicates to count 3rd vertices from the "f" line, it is "v-0.500000 0.000000-0.800000", and other index values are similar. Therefore, the equivalent positive value index of this row is written as: "f 12 3 4"
V-0.500000 0.000000 0.400000
V-0.500000 0.000000-0.800000
V-0.500000 1.000000-0.800000
V-0.500000 1.000000 0.400000
F-4-3-2-1

The OBJ file does not contain the color definition information, but can reference the material Library. The material library information is stored in an independent file with the suffix ". mtl. The keyword "mtllib" indicates the meaning of the material Library.
The material Library contains the definition values of diffuse, ambient, specular RGB (red, green, and blue), specularity, and refraction of materials ), transparency and other features.
After "usemtl" specifies the material, this material will be used in the future until the next "usemtl" is used to specify the new material.

 

The following example illustrates how to specify a material.
Cube with Materials:
# This cube has a different material
# Applied to each of its faces.
Mtllib master. mtl
V 0.000000 2.000000 2.000000
V 0.000000 0.000000 2.000000
V 2.000000 0.000000 2.000000
V 2.000000 2.000000 2.000000
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
#8 vertices
G front
Usemtl red
F 1 2 3 4
G back
Usemtl blue
F 8 7 6 5
G right
Usemtl green
F 4 3 7 8
G top
Usemtl gold
F 5 1 4 8
G left
Usemtl orange
F 5 6 2 1
G bottom
Usemtl purple
F 2 6 7 3
#6 elements

Besserl one-sided (bezerpatch ):
Maya cannot export the one-sided besell of OBJ format, but can import it. The imported Bessert is automatically converted to a green surface.
#3.0 besuppliers patch
V-5.000000-5.000000 0.000000
V-5.000000-1.666667 0.000000
V-5.000000 1.666667 0.000000
V-5.000000 5.000000 0.000000
V-1.666667-5.000000 0.000000
V-1.666667-1.666667 0.000000
V-1.666667 1.666667 0.000000
V-1.666667 5.000000 0.000000
V 1.666667-5.000000 0.000000
V 1.666667-1.666667 0.000000
V 1.666667 1.666667 0.000000
V 1.666667 5.000000 0.000000
V 5.000000-5.000000 0.000000
V 5.000000-1.666667 0.000000
V 5.000000 1.666667 0.000000
V 5.000000 5.000000 0.000000
#16 vertices
Cstype betiller
Deg 3 3
# Example of line continuation
Surf 0.000000 1.000000 0.000000 1.000000 13 14 \
15 16 9 10 11 12 5 6 7 8 1 2 3 4
Parm u 0.000000 1.000000
Parm v 0.000000 1.000000
End
#1 element

Cardinal Curve ):
Maya does not seem to support curves in the OBJ format. There is no error message or curve during import.
#3.0 Cardinal curve
V 0.940000 1.340000 0.000000
V-0.670000 0.820000 0.000000
V-0.770000-0.940000 0.000000
V 1.030000-1.350000 0.000000
V 3.070000-1.310000 0.000000
#6 vertices
Cstype cardinal
Deg 3
Curv 0.000000 3.000000 1 2 3 4 5 6
Parm u 0.000000 1.000000 2.000000 end
#1 element

Texture-Mapped ):
# A 2x2 square mapped with a 1x1 square
# Texture stretched to fit the square exactly.
Mtllib master. mtl
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
VVT 0.000000 1.000000 0.000000
VVT 0.000000 0.000000 0.000000
VVT 1.000000 0.000000 0.000000
VVT 1.000000 1.000000 0.000000
#4 vertices
Usemtl wood
# The first number is the point,
# Then the slash,
# And the second is the texture point
F 1/1 2/2 3/3 4/4
#1 element

Detailed description of OBJ file format in 3D

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.