Welcome to mirror list, hosted at ThFree Co, Russian Federation.

objimport.py « objexport « python « intern - git.blender.org/blender.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 0150a2571ad3a5789fd2225292320efa6aaec127 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
#! /usr/bin/env python

#######################
# (c) Jan Walter 2000 #
#######################

# CVS
# $Author$
# $Date$
# $RCSfile$
# $Revision$

import BlendImport
import os
import sys
import string

def usage():
    print "usage: python objimport.py objfile"

def testObjImport(filename):
    print filename
    vcount = 0
    vncount = 0
    fcount = 0
    file = open(filename, "r")
    lines = file.readlines()
    linenumber = 1
    for line in lines:
        words = string.split(line)
        if words and words[0] == "#":
            pass # ignore comments
        elif words and words[0] == "v":
            vcount = vcount + 1
            x = string.atof(words[1])
            y = string.atof(words[2])
            z = string.atof(words[3])
            ##print "Vertex %s: (%s, %s, %s)" % (vcount, x, y, z)
            index = BlendImport.addVertex(x, y, z)
            ##print "addVertex(%s)" % index
        elif words and words[0] == "vn":
            vncount = vncount + 1
            i = string.atof(words[1])
            j = string.atof(words[2])
            k = string.atof(words[3])
            ##print "VertexNormal %s: (%s, %s, %s)" % (vncount, i, j, k)
            index = BlendImport.addVertexNormal(i, j, k)
            ##print "addVertexNormal(%s)" % index
        elif words and words[0] == "f":
            fcount = fcount + 1
            vi = [] # vertex  indices
            ti = [] # texture indices
            ni = [] # normal  indices
            words = words[1:]
            lcount = len(words)
            for index in (xrange(lcount)):
                vtn = string.split(words[index], "/")
                vi.append(string.atoi(vtn[0]))
                if len(vtn) > 1 and vtn[1]:
                    ti.append(string.atoi(vtn[1]))
                if len(vtn) > 2 and vtn[2]:
                    ni.append(string.atoi(vtn[2]))
            ##print "Face %s: (%s, %s, %s)" % (fcount, vi, ti, ni)
            index = BlendImport.addFace(vi, ti, ni)
            ##print "addFace(%s)" % index
        elif words and words[0] == "mtllib":
            # try to export materials
            directory, dummy = os.path.split(filename)
            filename = os.path.join(directory, words[1])
            try:
                file = open(filename, "r")
            except:
                print "no material file %s" % filename
            else:
                file = open(filename, "r")
                line = file.readline()
                while line:
                    words = string.split(line)
                    if words and words[0] == "newmtl":
                        name = words[1]
                        file.readline() # Ns .
                        file.readline() # d .
                        file.readline() # illum .
                        line = file.readline()
                        words = string.split(line)
                        Kd = [string.atof(words[1]),
                              string.atof(words[2]),
                              string.atof(words[3])]
                        line = file.readline()
                        words = string.split(line)
                        Ks = [string.atof(words[1]),
                              string.atof(words[2]),
                              string.atof(words[3])]
                        line = file.readline()
                        words = string.split(line)
                        Ka = [string.atof(words[1]),
                              string.atof(words[2]),
                              string.atof(words[3])]
                        index = BlendImport.addMaterial(name, Kd, Ks, Ka)
                    line = file.readline()
                file.close()
        elif words and words[0] == "usemtl":
            name = words[1]
            BlendImport.setCurrentMaterial(name)
        elif words:
            pass
            ##print "%s: %s" % (linenumber, words)
        linenumber = linenumber + 1
    file.close()
    # export to OpenInventor
    BlendImport.OpenInventor().export("test.iv",
                                      useNormals = 1, useMaterials = 1)

if __name__ == "__main__":
    if len(sys.argv) != 2:
        usage()
    else:
        filename = sys.argv[1]
        testObjImport(filename)