The
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 you can open 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. Features of
1, obj files
OBJ3.0 file formats support straight line, Polygon (Polygon), surface (surface), and freeform curves ( Free-form Curve). Lines and polygons 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: The
(1) obj file is a 3D model file. Does not contain information such as animations, material properties, map paths, dynamics, particles, and so on. The
(2) obj file mainly 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. This is useful if the
(3) obj file supports polygons with more than 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. the
(4) 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 file
The 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 normals (Vertex normals)
VP parameter Space Vertex (Parameter space vertices)
Free-form curve (free-form curve)/surface properties (surface attributes):
DEG degrees (degree)
Bmat Foundation Matrix (Basis matrix)
Step Dimension (step size)
Cstype curves or surface types (Curve or surfaces type)
Element (Elements):
P Points (point)
L Lines (line)
F Surface (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 cycle (Outer trimming loop)
Hole internal refurbishment cycle (Inner trimming loop)
SCRV Special curve (special curve)
SP Special points (special point)
End statement (End statement)
the connection between free-form surfaces (Connectivity between free-form surfaces):
Con connection (Connect)
Group (Grouping):
Group G name (team name)
s smooth set (smoothing group)
MG Consolidation Group (merging)
O Object Name
Display/Render properties (render attributes):
Bevel angular interpolation (bevel interpolation)
C_interp colour interpolation (color interpolation)
D_interp dissolution interpolation (dissolve interpolation)
Lod levels of detail (level of detail)
USEMTL Material name (Material name)
Mtllib Material Gallery (Material library)
Shadow_obj cast Shadow (shadow casting)
Trace_obj ray Tracing (ray tracing)
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. When you import the "myobj.obj" file in Maya, you see a quad. This quadrilateral shape is entirely determined by the preceding 5 lines of code.
Let's examine the code.
v-0.58 0.84 0 draw a quadrilateral requires four vertices, this is the first vertex, "V" represents the vertex (vertex), "0.58" is the x-axis 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.
The connection points for
polygons are 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 files exported by Maya. Create a polygon cube in Maya, select the cube, select the menu "file-and export Selection ..." Export format is obj, the file name is "Cube.obj", if you do not have this format, Please load "OBJEXPORT.MLL" into plug-in manager. 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, so you can check the meaning of each keyword against the previous list. Let me explain:
"VT 1.000000 0.000000" is the "VT" that 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 phrase "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_FAC E5
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 polygon of the cube is detached, and each face is named Pcube_face (1~6), and the name of the visible group is actually the name of the individual geometry.
It is not possible to name geometry (groups) in Chinese. Try it out and change the previous code to:
USEMTL Initialshadinggroup
g cubic 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 face 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/ 6/12/19 4/4/20
g cubic face 6
f 7/13/21 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 line which are 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.
4. The actual problem with the obj file:
Now let's discuss a more practical question, that is, once you have encountered a faulty obj file, what to do in the end.
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.000 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. Therefore, the equivalent positive index of this line is written as: "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.000000 2.000000
v 0.000000 2.000000 0.000000
v 0.000000 0.000000 0.000000
v 2.000000 0.0000 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
Bezier One-sided (Bezier 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< C10/>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
# vertices Cstype
Bézier
deg 3 3
# Example of line continuation
surf 0.000000 1.000000 0.000000 1.000000 16 \
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
parm 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< C17/>v 2.000000 2.000000 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
poi NT,
# then the slash, # and the second are the
texture point
F 1/1 2/2 3/3 4/4
# 1 element
Reprinted from: http://www.cppblog.com/lovedday/archive/2008/06/13/53153.html
———— the End ————