The obj file format in 3D is detailed (reproduced)

Source: Internet
Author: User

Obj file is alias| Wavefront company developed a standard 3D model file format for its set of workstation-based 3D modeling and animation software "Advanced Visualizer", which is well suited for mutual guidance between 3D software models and can be read and written by Maya. For example, if you build a model in 3dsMax or Lightwave, and you want to transfer it to Maya for rendering or animation, exporting the obj file is a good choice. At present, almost all the well-known 3D software supports the reading and writing of obj files, but many of them need to be implemented by plugins.
The obj file is a text file that can be opened directly with WordPad for viewing and editing changes. In addition, there is a binary file format associated with this (*. MOD), which is not disclosed as a patent and is therefore not discussed here.
1, the characteristics of the obj file
The OBJ3.0 file format supports straight line, polygon (Polygon), surface (surface), and free-form curves (Free-form Curve). The lines and the corners are described by their points, and the curves and surfaces are defined according to their control points and additional information attached to the curve type, which supports rules and irregular curves, including those based on Bezier (Bezier), B-spline (B-spline), cardinality (cardinal/ Catmull-rom) and the Taylor equation (Taylor equations) curve. Other features are as follows:
(1) The obj file is a 3D model file. Does not contain information such as animations, material properties, map paths, dynamics, particles, and so on.
(2) The obj file primarily supports the polygon (polygons) model. Although Curve (Curves), surface (surfaces), point group materials are also supported, the obj file exported by Maya does not include this information.
(3) It is useful to have an obj file that supports polygons above three points. Many other model file formats support only three-point polygons, so the model that imports Maya is often triangulated, which is detrimental to our model reprocessing.
(4) The obj file supports normal and mapping coordinates. After adjusting the map in other software, the mapping coordinates information can be stored in the obj file, so that the file is imported into Maya just specify the map file path, you do not need to adjust the mapping coordinates.

2. The basic structure of the obj fileThe obj file does not require any kind of file header, although several lines of file information are often used as the beginning of a file. The obj file consists of one line of text, the comment line begins with the symbol "#", and spaces and blank lines can be added to the file to increase the readability of the file. The lines that have words begin with the one or two mark letters, which is the keyword (Keyword), which can indicate what kind of data this line is. Multiple rows can be logically concatenated together to represent a row by adding a connector (/) at the end of each line. Note A space or tab cannot appear after the connector (/), or it will cause a file error.
The following keywords can be used in the obj file. In this list, the keywords are sorted by data type, with a short description for each keyword.
Vertex (Vertex data):V Geometry vertex (geometric vertices) VT Map coordinate point (Texture vertices) vn vertex normal (Vertex normals) VP parameter Space Vertex (Parameter space vertices)
Free-form curve (free-form curve)/surface properties (surface attributes):DEG (degree) bmat basic matrix (Basis matrix) Step Step size (step size) cstype curve or surface type (Curve or surfaces types)
Element (Elements):P points (point) L lines (line) f plane (face) curv curve (Curve) Curv2 2D curve (2D Curve) surf surface (surface)
free Form curve (free-form curve)/Surface body Statement (surface body statements):Parm parameter value (Parameter values) trim external trim loop (Outer trimming loop) hole internal refurbishment cycle (Inner trimming loop) SCRV Special curve (special curve) SP Special point (Special) END End statement (end statement)
the connection between free-form surfaces (Connectivity between free-form surfaces) :Con connection (Connect)
Group (Grouping):G Group name (group name) s Smooth Group (smoothing group) MG Merge Group (merging group) O Object name
Display/Render properties (render attributes):Bevel angular interpolation (bevel interpolation) c_interp color interpolation (color interpolation) D_interp dissolve interpolation (dissolve interpolation) LOD levels of detail (level O F detail) USEMTL material name (Material name) mtllib Material Library (Material library) Shadow_obj cast shadow (shadow casting) Trace_obj ray tracing (Ray Traci NG) CTech curve approximation technique (Curve approximation technique) Stech surface approximation technology (surface approximation technique)

3. obj File instance
Really difficult to understand, the following examples to explain the specific. Let's create an obj file with a quadrilateral, but this time we don't use 3D software, we create it with WordPad. Open WordPad, write the following 5 lines of code, you can add a little comment. Save the file as a text format with the file name "Myobj.obj".
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
Note: The code must finally press ENTER to switch the cursor to the next line, which means adding a newline character (/n). Otherwise you will see the following error message://error:line 1:obj file line 5:index out of range. Error:line 1:error reading file. //
Import the "myobj.obj" file in Maya, see, import a quadrilateral. This quadrilateral shape is entirely determined by the preceding 5 lines of code. (No picture)

Let's examine the code. v-0.58 0.84 0
To draw a quadrilateral requires four vertices, which is the first vertex, "V" represents the vertex (vertex), "0.58" is the x-coordinate value of the vertex, "0.84" is the y-coordinate value, "0" is the z-coordinate value. Its index number is 1. The index number is used for the screen. V 2.68 1.17 0 v 2.84-2.03 0 v-1.92-2.89 0 This is the 第二、三、四个 vertex, respectively, and their index number is 2,3,4.
F 1 2 3 4 Now the Start screen, "F" represents the face, 1,2,3,4 is the index number of the preceding four vertices. Please note that the order of the connection points is drawn from the first point, connecting the 第二、三、四个 point in turn.
If the order of the connections is different, the resulting polygons will be distinct, for example, "F 1 2 4 3" produces a overlapping polygon, such as.

The connection point of a polygon is arranged clockwise or counterclockwise, which determines the normal direction of the face (the face's anyway). For example: "F 1 2 3 4" Face normals Outward, "F 4 3 2 1" face normal inward. The wrong order of connection points in a polygon is an important reason for the imported model to produce a broken surface. A polygon cannot have more than two identical vertices, which is also an important point in checking the obj file for errors. For example: "F 1 2 3 4 3", there are two identical vertices, and the index number is 3. Two identical vertices appear on a polygon, which can cause memory allocation errors for the program.
The following is a look at the obj file exported by Maya.
Create a polygon cube in Maya, select the cube, select the menu "file--, export Selection ..." Export format is obj, the file name is "Cube.obj", and if this format is not available, in Plug-in manager, load " OBJEXPORT.MLL ". Using WordPad to open "Cube.obj", you can see the following code:
# The units used in this file is 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.00 0000 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.00 0000 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 vn-1.00 0000 0.000000 0.000000 vn-1.000000 0.000000 0.000000 vn-1.000000 0.000000 0.000000 s off G pCube1 USEMTL Initialshadingg Roup 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 more complicated and uses a lot of keywords, and you can check the meaning of each keyword against the previous list. Let me explain: the "VT 1.000000 0.000000", the sentence, represents the mapping coordinates of the point. "VN 0.000000 0.000000-1.000000" The phrase "vn" represents the normal of the point. "s off" means to turn off the smoothing group. "Usemtl Initialshadinggroup" represents the material used. "F 7/13/21" at this point in the polygon data is more map coordinates UV point and the index number of normal, the index number is separated by a left slash (/). Format: "F vertex index/UV Point index/Method lead".
"G-PCube1" represents the group, where groups are not the same as in Maya, where a group refers to the "G pCube1" that appear after the surface is combined together to form an entire polygon geometry.
Modify the "cube.obj" file to know the meaning of the group. Replace the code behind the "s off" 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 When you import maya, you can see that each face of the cube is detached, with the name "Pcube_face (1~6)" For each face, respectively. The name of the visible group is actually the name of the individual geometry.
Can you name the geometry (group) in Chinese? Try it out, change the previous code to: USEMTL initialshadinggroup G cube face 1 F 1/1/1 2/2/2 4/4/3 3/3/4 g cubic face 2 f 3/3/5 4/4/6 6/6/7 5/5/8 g cubic Surface 3 F 5/5/9 6/6/10 8/8/11 7/7/12 g cubic face 4 F 7/7/13 8/8/14 2/10/15 1/9/16 g cubic face 5 F 2/2/17 8/11/18 6/12/19 4/4/20 g cubic face 6 F 7/13/2 1 1/1/22 3/3/23 5/14/24
Try it and you will find that the model has been imported smoothly. Although the name of the object has been garbled, but this is not very serious matter. However, the use of Chinese names is not always so smooth, the "G cube face 1" This line to "G-select" and try again, this time the import model does not appear at all, only the following error message appears://error:line 1:your OBJ file contains a lines which is too long to be parsed. Please edit your obj file. Error:line 1:error reading file. This shows that the object naming of the nonstandard is also the cause of the error in the obj file. About Maya object naming, English name is very safe, punctuation only the underscore (_) is available, numbers can not be put to the beginning of the name, try not to use Chinese, Japanese, Korean and other double-byte text. The obj file does not support polygon polygons with holes. For example: Choose Maya's Create polygon tool (polygons-I, creating Polyon tool), draw a quad in the view, do not press ENTER, press CTRL in the middle of the quadrilateral point, you can continue to dig a hole in the quadrilateral. Save this perforated polygon in obj format, and when you import Maya, you'll find that the polygon is missing a piece. If you see this as a mistake, at least now you know the reason for the error, that is, the obj file does not support polygon polygons with holes.

Transferred from: http://www.cppblog.com/lovedday/archive/2008/06/13/53153.html

4. The actual problem with the obj file:
Now let's talk a little bit more about the actual problem, that is, once you have encountered a wrong obj file, what should I do?
When you open the obj file, you will often see tens of thousands of lines of code, you may not have the ability to look at the wrong line, unless the error message in the program has already told you the wrong line. If you don't know where the error is, you can use the exclusion method to figure out the correct code range, and locate the error by reducing the error code range. For example, you create a new empty obj file, paste the wrong obj file code in half, and then import the new obj file with only half the code into Maya. If there is no error message, the error line is in the other half of the code, you can try to paste a portion of the code from the other half of the code, if there is an error, the error line in the pasted code, you can paste the code to delete part of it and try again. In this way, gradually reduce the range until the wrong behavior is found.
This approach, though cumbersome, is quite effective. This approach is worth trying if you don't have programming and are in a very urgent situation.
5. More details of the obj file:
Simple obj format notation. # 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
Polygons can use negative indexes, and sometimes it is easier to describe polygons with negative indexes.
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
"F-4-3-2-1" The Index value "3" means that from the line "F" to the 3rd vertex, that is, "v-0.500000 0.000000-0.800000", the other index values and so on. So the positive index equivalent to this line is: "F 1 2 3 4"
The obj file does not contain color definition information for polygons, but can reference material libraries, and material library information is stored in a separate file with a suffix of ". MTL". The keyword "Mtllib" is the meaning of the material library. The material library contains the material's diffuse (diffuse), environment (ambient), gloss (specular)-defined values of RGB (red-green-blue), and Reflection (Specularity), refraction (refraction), transparency (transparency) and other characteristics. After "USEMTL" specifies the material, subsequent polygons use this material until the next "USEMTL" is encountered to specify a new material.

to specify a material's method: Cube with materials: # This cube had 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.0000 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.0 00000 # 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 G Old 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

Bézier-sided (Bézier Patch): Maya cannot export a Bezier one-sided format in obj, but can import it. The imported Bezier one-sided conversion is automatically converted to a NURBS surface. # 3.0 Bézier 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.0 00000 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.00000 0 0.000000 # vertices cstype Bézier deg 3 3 # Example of line continuation surf 0.000000 1.000000 0.000000 1.000000 13 14/15 9 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 (Cardinal Curve): Maya does not seem to support the curve in obj format, and the import does not appear with an error message, but there is no curve. # 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 par M u 0.000000 1.000000 2.000000 3.000000 End # 1 element

map Map (texture-mapped): # a 2 x 2 square Mapped with A 1 x 1 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.0000 XX 0.000000 vt 0.000000 1.000000 0.000000 vt 0.000000 0.000000 0.000000 vt 1.000000 0.000000 0.000000 vt 1.000000 1.000000 0.000000 # 4 vertices usemtl Wood # The first number is the point, # then the slash, # and the second are the texture Poin T F 1/1 2/2 3/3 4/4 # 1 element

The obj file format in 3D is detailed (reproduced)

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.