destruction of previous slot api. if it returns, it'll
[blender.git] / release / scripts / mesh_edges2curves.py
1 #!BPY
2 """ Registration info for Blender menus:
3 Name: 'Edges to Curve'
4 Blender: 241
5 Group: 'Mesh'
6 Tip: 'Edges not used by a face are converted into polyline(s)'
7 """
8 __author__ = ("Campbell Barton")
9 __url__ = ("blender", "blenderartists.org")
10 __version__ = "1.0 2006/02/08"
11
12 __bpydoc__ = """\
13 Edges to Curves
14
15 This script converts open and closed edge loops into curve polylines
16
17 Supported:<br>
18          Polylines where each vert has no more then 2 edges attached to it.
19 """
20
21 # ***** BEGIN GPL LICENSE BLOCK *****
22 #
23 # Script copyright (C) Campbell J Barton
24 #
25 # This program is free software; you can redistribute it and/or
26 # modify it under the terms of the GNU General Public License
27 # as published by the Free Software Foundation; either version 2
28 # of the License, or (at your option) any later version.
29 #
30 # This program is distributed in the hope that it will be useful,
31 # but WITHOUT ANY WARRANTY; without even the implied warranty of
32 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
33 # GNU General Public License for more details.
34 #
35 # You should have received a copy of the GNU General Public License
36 # along with this program; if not, write to the Free Software Foundation,
37 # Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
38 #
39 # ***** END GPL LICENCE BLOCK *****
40 # --------------------------------------------------------------------------
41
42 from Blender import *
43
44 def polysFromMesh(me):
45         # a polyline is 2 
46         #polylines are a list
47         polyLines = []
48         
49         # Get edges not used by a face
50         edgeDict= dict([ (ed.key, ed) for ed in me.edges ])
51         for f in me.faces:
52                 for key in f.edge_keys:
53                         try:
54                                 del edgeDict[key]
55                         except:
56                                 pass
57         
58         edges= edgeDict.values()
59         
60         
61         while edges:
62                 currentEdge= edges.pop()
63                 startVert= currentEdge.v2
64                 endVert= currentEdge.v1
65                 polyLine= [startVert, endVert]
66                 ok= 1
67                 while ok:
68                         ok= 0
69                         #for i, ed in enumerate(edges):
70                         i=len(edges)
71                         while i:
72                                 i-=1
73                                 ed= edges[i]
74                                 if ed.v1 == endVert:
75                                         polyLine.append(ed.v2)
76                                         endVert= polyLine[-1]
77                                         ok=1
78                                         del edges[i]
79                                         #break
80                                 elif ed.v2 == endVert:
81                                         polyLine.append(ed.v1)
82                                         endVert= polyLine[-1]
83                                         ok=1
84                                         del edges[i]
85                                         #break
86                                 elif ed.v1 == startVert:
87                                         polyLine.insert(0, ed.v2)
88                                         startVert= polyLine[0]
89                                         ok=1
90                                         del edges[i]
91                                         #break  
92                                 elif ed.v2 == startVert:
93                                         polyLine.insert(0, ed.v1)
94                                         startVert= polyLine[0]
95                                         ok=1
96                                         del edges[i]
97                                         #break
98                 polyLines.append((polyLine, polyLine[0]==polyLine[-1]))
99                 # print len(edges), len(polyLines)
100         return polyLines
101
102
103 def mesh2polys():
104         scn= Scene.GetCurrent()
105         scn.objects.selected = []
106         
107         meshOb= scn.objects.active
108         if meshOb==None or meshOb.type != 'Mesh':
109                 Draw.PupMenu( 'ERROR: No Active Mesh Selected, Aborting' )
110                 return
111         Window.WaitCursor(1)
112         Window.EditMode(0)
113         me = meshOb.getData(mesh=1)
114         polygons= polysFromMesh(me)
115         w=t=1
116         cu= Curve.New()
117         cu.name = me.name
118         cu.setFlag(1)
119         
120         ob = scn.objects.active = scn.objects.new(cu)
121         ob.setMatrix(meshOb.matrixWorld)
122         
123         i=0
124         for poly, closed in polygons:
125                 if closed:
126                         vIdx= 1
127                 else:
128                         vIdx= 0
129                 
130                 v= poly[vIdx]
131                 cu.appendNurb([v.co.x, v.co.y, v.co.z, w, t])
132                 vIdx += 1
133                 cu[i].type= 0 # Poly Line
134                 
135                 # Close the polyline if its closed.
136                 if closed:
137                         cu[i].setFlagU(1)
138                 
139                 # Add all the points in the polyline.
140                 while vIdx<len(poly):
141                         v= poly[vIdx]
142                         cu.appendPoint(i, [v.co.x, v.co.y, v.co.z, w])
143                         vIdx+=1
144                 i+=1
145         Window.WaitCursor(0)
146
147 # not used as yet.
148 """
149 def writepolys():
150         me = Scene.GetCurrent().getActiveObject().getData(mesh=1)
151         polygons= polysFromMesh(me)
152         file=open('/polygons.txt', 'w')
153         for ply in polygons:
154                 file.write('polygon ')
155                 if ply[1]:
156                         file.write('closed ')
157                 else:
158                         file.write('open ')
159                 file.write('%i\n' % len(ply[0]))
160                 for pt in ply[0]:
161                         file.write('%.6f %.6f %.6f\n' % tuple(pt.co) )
162         file.close()
163 """
164
165 if __name__ == '__main__':
166         mesh2polys()