f9ab52b37241598951752ac53746c236dc97977f
[blender.git] / release / scripts / slp_import.py
1 #!BPY
2
3 """
4 Name: 'Pro Engineer (.slp)...'
5 Blender: 232
6 Group: 'Import'
7 Tooltip: 'Import Pro Engineer (.slp) File Format'
8 """
9
10 __author__ = "Anthony D'Agostino (Scorpius)"
11 __url__ = ("blender", "elysiun",
12 "Author's homepage, http://www.redrival.com/scorpius")
13 __version__ = "Part of IOSuite 0.5"
14
15 __bpydoc__ = """\
16 This script imports Pro Engineer files to Blender.
17
18 Usage:
19
20 Execute this script from the "File->Import" menu and choose an SLP file to
21 open.
22 """
23
24 # $Id$
25 #
26 # +---------------------------------------------------------+
27 # | Copyright (c) 2004 Anthony D'Agostino                   |
28 # | http://www.redrival.com/scorpius                        |
29 # | scorpius@netzero.com                                    |
30 # | May 3, 2004                                             |
31 # | Released under the Blender Artistic Licence (BAL)       |
32 # | Import Export Suite v0.5                                |
33 # +---------------------------------------------------------+
34 # | Read and write SLP Triangle File Format (*.slp)         |
35 # +---------------------------------------------------------+
36
37 import Blender, mod_meshtools
38 #import time
39
40 # ================================
41 # === Read SLP Triangle Format ===
42 # ================================
43 def read(filename):
44         #start = time.clock()
45         file = open(filename, "rb")
46
47         raw = []
48         for line in file.readlines():
49                 data = line.split()
50                 if data[0] == "vertex":
51                         vert = map(float, data[1:])
52                         raw.append(vert)
53
54         tri = []
55         for i in range(0, len(raw), 3):
56                 tri.append(raw[i] + raw[i+1] + raw[i+2])
57
58         #$import pprint; pprint.pprint(tri)
59
60         # Collect data from RAW format
61         faces = []
62         for line in tri:
63                 f1, f2, f3, f4, f5, f6, f7, f8, f9 = line
64                 faces.append([(f1, f2, f3), (f4, f5, f6), (f7, f8, f9)])
65
66         # Generate verts and faces lists, without duplicates
67         verts = []
68         coords = {}
69         index = 0
70         for i in range(len(faces)):
71                 for j in range(len(faces[i])):
72                         vertex = faces[i][j]
73                         if not coords.has_key(vertex):
74                                 coords[vertex] = index
75                                 index += 1
76                                 verts.append(vertex)
77                         faces[i][j] = coords[vertex]
78
79         objname = Blender.sys.splitext(Blender.sys.basename(filename))[0]
80
81         mod_meshtools.create_mesh(verts, faces, objname)
82         Blender.Window.DrawProgressBar(1.0, '')  # clear progressbar
83         file.close()
84         #end = time.clock()
85         #seconds = " in %.2f %s" % (end-start, "seconds")
86         message = "Successfully imported " + Blender.sys.basename(filename)# + seconds
87         mod_meshtools.print_boxed(message)
88
89 def fs_callback(filename):
90         read(filename)
91
92 Blender.Window.FileSelector(fs_callback, "Import SLP")