merge runk 16887:16950
[blender.git] / release / scripts / reeb.py
1 #!BPY
2  
3 """
4 Name: 'Reeb graph import'
5 Blender: 245
6 Group: 'Import'
7 Tooltip: 'Imports a reeb graph saved after skeleton generation'
8 """
9 import Blender
10
11 def name(count):
12         if count == -1:
13                 return ""
14         else:
15                 return "%05" % count
16
17 def importGraph(count):
18         bNode = Blender.Draw.Create(1)
19         bSize = Blender.Draw.Create(0.01)
20
21         Block = []
22         
23         Block.append(("Size: ", bSize, 0.01, 10.0, "Size of the nodes"))
24         Block.append(("Nodes", bNode, "Import nodes as tetras"))
25         
26         retval = Blender.Draw.PupBlock("Reeb Graph Import", Block)
27         
28         if not retval:
29                 return
30
31
32         me = Blender.Mesh.New("graph%s" % name(count))
33         scn = Blender.Scene.GetCurrent()
34         
35         f = open("test%s.txt" % name(count), "r")
36         
37         verts = []
38         edges = []
39         faces = []
40         
41         i = 0
42         first = False
43         
44         SIZE = float(bSize.val)
45         WITH_NODE = bool(bNode.val)
46         
47         def addNode(v, s, verts, faces):
48                 if WITH_NODE:
49                         v1 = [v[0], v[1], v[2] + s]
50                         i1 = len(verts)
51                         verts.append(v1)
52                         v2 = [v[0], v[1] + 0.959 * s, v[2] - 0.283 * s]
53                         i2 = len(verts)
54                         verts.append(v2)
55                         v3 = [v[0] - 0.830 * s, v[1] - 0.479 * s, v[2] - 0.283 * s]
56                         i3 = len(verts)
57                         verts.append(v3)
58                         v4 = [v[0] + 0.830 * s, v[1] - 0.479 * s, v[2] - 0.283 * s]
59                         i4 = len(verts)
60                         verts.append(v4)
61                         
62                         faces.append([i1,i2,i3])
63                         faces.append([i1,i3,i4])
64                         faces.append([i2,i3,i4])
65                         faces.append([i1,i2,i4])
66                         
67                         return 4
68                 else:
69                         return 0
70                 
71         for line in f:
72                 data = line.strip().split(" ")
73                 if data[0] == "v1":
74                         v = [float(x) for x in data[-3:]]
75                         i += addNode(v, SIZE, verts, faces)
76                         verts.append(v)
77                         i += 1
78                 elif data[0] == "v2":
79                         pass
80                         v = [float(x) for x in data[-3:]]
81                         verts.append(v)
82                         edges.append((i-1, i))
83                         i += 1
84                         i += addNode(v, SIZE, verts, faces)
85                 elif data[0] == "b":
86                         verts.append([float(x) for x in data[-3:]])
87                         edges.append((i-1, i))
88                         i += 1
89 #               elif data[0] == "angle":
90 #                       obj = scn.objects.new('Empty')
91 #                       obj.loc = (float(data[1]), float(data[2]), float(data[3]))
92 #                       obj.properties["angle"] = data[4]
93 #                       del obj
94                         
95                         
96         me.verts.extend(verts)
97         me.edges.extend(edges)
98         me.faces.extend(faces)
99         
100         
101         ob = scn.objects.new(me, "graph%s" % name(count))
102         del ob
103         del scn
104         
105
106 #for i in range(16):
107 #       importGraph(i)
108
109 if __name__=='__main__':
110         importGraph(-1)