Scripts updated:
[blender.git] / release / scripts / discombobulator.py
1 #!BPY
2
3 """
4 Name: 'Discombobulator'
5 Blender: 237
6 Group: 'Mesh'
7 Tip: 'Adds random geometry to a mesh'
8 """
9
10 __author__ = "Evan J. Rosky (syrux)"
11 __url__ = ("Script's homepage, http://evan.nerdsofparadise.com/programs/discombobulator/index.html")
12 __version__ = "237"
13 __bpydoc__ = """\
14 Discombobulator adds random geometry to a mesh.
15
16 As an example, this script can easily give a "high-tech"
17 look to walls and spaceships.
18
19 Definitions:<br>
20   - Protrusions: extrusions of each original face on the mesh.
21 You may have from 1 to 4 protrusions on each face.<br>
22   - Taper: The tips of each protrusion will be a percentage
23 smaller than the base.<br>
24   - Doodads: small extruded blocks/shapes that are randomly placed
25 about the top of a protrusion or face.
26
27
28 Usage:<br>
29   Input your settings, make sure the mesh you would like to modify
30 is selected (active) and then click on "Discombobulate".<br>
31   See the scripts tutorial page (on the homepage) for more info.
32
33
34 New Features:<br>
35   - Will use existing materials if there are any.<br>
36   - Clicking "Assign materials by part" will allow assigning
37 of different material indices to Protrusion or Doodad Sides
38 and Tops in the gui element below it.<br>
39   - Setting a material index to 0 will use whatever material
40 is assigned to the face that is discombobulated.
41   - You can now scroll using the arrow keys.
42
43
44 Notes:<br>
45   - Modifications can be restricted to selected faces
46 by setting "Only selected faces" for protrusions and/or
47 doodads.<br>
48   - It's possible to restrict mesh generation to add only
49 protrusions or only doodads instead of both.<br>
50   - You may also choose to have Discombobulator select the
51 tops of created protrusions by clicking the corresponding
52 number of protrusion buttons under "Select Tops". You may 
53 also do the same for doodads by choosing "Select Doodads" and
54 "Only Select Tops". You may choose to select the whole doodads 
55 by leaving "Only Select Tops" off.<br>
56   - By selecting "Deselect Selected" you can have
57 discombobulator deselect everything but the selections it
58 makes.<br>
59   - The "Face %" option will set the percentage of faces that
60 will be modified either for the doodads or the protrusions.<br>
61   - "Copy Before Modifying" will create a new object with the
62 modifications where leaving it off will overwrite the original
63 mesh.<br>
64
65 You can find more information at the Link above.
66 """
67
68
69 # $Id$
70
71 # Updated 2006-09-26
72 # Changes since last version: 
73 #     > Works with Blender CVS and hopefully with Blender 2.40.
74 #     > Swaps min/max values when min>max rather than complaining.
75 #     > Will keep previously assigned materials.
76 #     > Now allows user to assign custom material indices to
77 #            Protrusion and Doodad Sides and Tops.
78 #     > The initial Gui Layout will change depending on the aspect
79 #            ratio of the window it is in.
80 #     > Using the arrow keys will scroll the gui.
81
82 # --------------------------------------------------------------------------
83 # Discombobulator v2.1
84 # by Evan J. Rosky, 2005
85 # This plugin is protected by the GPL: Gnu Public Licence
86 # GPL - http://www.gnu.org/copyleft/gpl.html
87 # --------------------------------------------------------------------------
88 # ***** BEGIN GPL LICENSE BLOCK *****
89 #
90 # Copyright (C) 2005: Evan J. Rosky
91 #
92 # This program is free software; you can redistribute it and/or
93 # modify it under the terms of the GNU General Public License
94 # as published by the Free Software Foundation; either version 2
95 # of the License, or (at your option) any later version.
96 #
97 # This program is distributed in the hope that it will be useful,
98 # but WITHOUT ANY WARRANTY; without even the implied warranty of
99 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
100 # GNU General Public License for more details.
101 #
102 # You should have received a copy of the GNU General Public License
103 # along with this program; if not, write to the Free Software Foundation,
104 # Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
105 #
106 # ***** END GPL LICENCE BLOCK *****
107 # --------------------------------------------------------------------------
108
109 #Hit Alt-P to run
110
111 import Blender
112 from Blender import NMesh,Object,Material,Window,Types
113 from Blender.NMesh import Vert,Face
114 from Blender.Mathutils import *
115
116 import defaultdoodads
117 import BPyMathutils
118 from BPyMathutils import genrand
119 a = BPyMathutils.sgenrand(int(round(Rand(1000,99999),0)))
120
121 #Create random numbers
122 def randnum(low,high):
123         num = genrand()
124         num = num*(high-low)
125         num = num+low
126         return num
127
128 #Object Vars
129 origmesh = NMesh.GetRaw()
130 newmesh = NMesh.GetRaw()
131 origobj = Object.Get()
132 newobj = Object.Get()
133 materialArray = [0]
134
135 #Material Vars
136 reassignMats = 0
137 protSideMat = 1
138 protTopMat = 2
139 doodSideMat = 3
140 doodTopMat = 4
141 thereAreMats = 0
142 currmat = 0
143
144 #Global Vars
145 makenewobj = 1
146 errortext = "Remember to select an object."
147 editmode = 0
148
149 #Protrusion Vars
150 makeprots = 1
151 faceschangedpercent = 1.0
152 minimumheight = 0.2
153 maximumheight = 0.4
154 subface1 = 1
155 subface2 = 1
156 subface3 = 1
157 subface4 = 1
158 subfaceArray = [1,2,3,4]
159 minsubfaces = 1
160 minimumtaperpercent = 0.15
161 maximumtaperpercent = 0.35
162 useselectedfaces = 0
163 selectface1 = 1
164 selectface2 = 1
165 selectface3 = 1
166 selectface4 = 1
167 deselface = 1
168
169 #Doodad Vars
170 makedoodads = 1
171 doodadfacepercent = 1.0
172 selectdoodad = 0
173 onlyonprotrusions = 0
174 doodonselectedfaces = 0
175 selectdoodadtoponly = 0
176 doodad1 = 1
177 doodad2 = 1
178 doodad3 = 1
179 doodad4 = 1
180 doodad5 = 1
181 doodad6 = 1
182 doodadminperface = 2
183 doodadmaxperface = 6
184 doodadminsize = 0.15
185 doodadmaxsize = 0.45
186 doodadminheight = 0.0
187 doodadmaxheight = 0.1
188 doodadArray = [1,2,3,4,5,6]
189
190 SEL = NMesh.FaceFlags['SELECT']
191
192 def isselectedface(theface):
193         for vertic in theface.v:
194                 if vertic.sel == 0:
195                         return 0
196         return 1
197
198 def arrayInInclusive(start,end):
199         arr = []
200         i = start
201         while i <= end:
202                 arr.append(i)
203                 i = i + 1
204         return arr
205
206 def makeSubfaceArray():
207         global subfaceArray
208         global subface1
209         global subface2
210         global subface3
211         global subface4
212         
213         subfaceArray = []
214         if subface1 > 0:
215                 subfaceArray.append(1)
216         if subface2 > 0:
217                 subfaceArray.append(2)
218         if subface3 > 0:
219                 subfaceArray.append(3)
220         if subface4 > 0:
221                 subfaceArray.append(4)
222
223 def makeDoodadArray():
224         global doodadArray
225         global doodad1
226         global doodad2
227         global doodad3
228         global doodad4
229         global doodad5
230         global doodad6
231         
232         doodadArray = []
233         if doodad1 > 0:
234                 doodadArray.append(1)
235         if doodad2 > 0:
236                 doodadArray.append(2)
237         if doodad3 > 0:
238                 doodadArray.append(3)
239         if doodad4 > 0:
240                 doodadArray.append(4)
241         if doodad5 > 0:
242                 doodadArray.append(5)
243         if doodad6 > 0:
244                 doodadArray.append(6)
245
246 def copyObjStuff(startObj,endObj):
247         endObj.setDeltaLocation(startObj.getDeltaLocation())
248         endObj.setDrawMode(startObj.getDrawMode())
249         endObj.setDrawType(startObj.getDrawType())
250         endObj.setEuler(startObj.getEuler())
251         if(startObj.getIpo() != None):
252                 endObj.setIpo(startObj.getIpo())
253         endObj.setLocation(startObj.getLocation())
254         endObj.setMaterials(startObj.getMaterials())
255         endObj.setMatrix(startObj.getMatrix())
256         endObj.setSize(startObj.getSize())
257         endObj.setTimeOffset(startObj.getTimeOffset())
258         
259
260 def extrude(mid,nor,protrusion,v1,v2,v3,v4,tosel=1,flipnor=0):
261         taper = 1 - randnum(minimumtaperpercent,maximumtaperpercent)
262         
263         vert = newmesh.verts[v1]
264         point = (vert.co - mid)*taper + mid + protrusion*Vector(nor)
265         ver = Vert(point[0],point[1],point[2])
266         ver.sel = tosel
267         newmesh.verts.append(ver)
268         vert = newmesh.verts[v2]
269         point = (vert.co - mid)*taper + mid + protrusion*Vector(nor)
270         ver = Vert(point[0],point[1],point[2])
271         ver.sel = tosel
272         newmesh.verts.append(ver)
273         vert = newmesh.verts[v3]
274         point = (vert.co - mid)*taper + mid + protrusion*Vector(nor)
275         ver = Vert(point[0],point[1],point[2])
276         ver.sel = tosel
277         newmesh.verts.append(ver)
278         vert = newmesh.verts[v4]
279         point = (vert.co - mid)*taper + mid + protrusion*Vector(nor)
280         ver = Vert(point[0],point[1],point[2])
281         ver.sel = tosel
282         newmesh.verts.append(ver)
283         
284         faceindex = len(newmesh.verts) - 4
285         
286         #side face 1
287         face = Face()
288         face.v.append(newmesh.verts[v1])
289         face.v.append(newmesh.verts[v2])
290         face.v.append(newmesh.verts[faceindex+1])
291         face.v.append(newmesh.verts[faceindex])
292         if flipnor != 0:
293                 face.v.reverse()
294         if thereAreMats == 1:
295                 if reassignMats == 0 or protSideMat == 0:
296                         face.materialIndex = currmat
297                 else:
298                         face.materialIndex = protSideMat-1
299         newmesh.faces.append(face)
300         
301         #side face 2
302         face = Face()
303         face.v.append(newmesh.verts[v2])
304         face.v.append(newmesh.verts[v3])
305         face.v.append(newmesh.verts[faceindex+2])
306         face.v.append(newmesh.verts[faceindex+1])
307         if flipnor != 0:
308                 face.v.reverse()
309         if thereAreMats == 1:
310                 if reassignMats == 0 or protSideMat == 0:
311                         face.materialIndex = currmat
312                 else:
313                         face.materialIndex = protSideMat-1
314         newmesh.faces.append(face)
315         
316         #side face 3
317         face = Face()
318         face.v.append(newmesh.verts[v3])
319         face.v.append(newmesh.verts[v4])
320         face.v.append(newmesh.verts[faceindex+3])
321         face.v.append(newmesh.verts[faceindex+2])
322         if flipnor != 0:
323                 face.v.reverse()
324         if thereAreMats == 1:
325                 if reassignMats == 0 or protSideMat == 0:
326                         face.materialIndex = currmat
327                 else:
328                         face.materialIndex = protSideMat-1
329         newmesh.faces.append(face)
330         
331         #side face 4
332         face = Face()
333         face.v.append(newmesh.verts[v4])
334         face.v.append(newmesh.verts[v1])
335         face.v.append(newmesh.verts[faceindex])
336         face.v.append(newmesh.verts[faceindex+3])
337         if flipnor != 0:
338                 face.v.reverse()
339         if thereAreMats == 1:
340                 if reassignMats == 0 or protSideMat == 0:
341                         face.materialIndex = currmat
342                 else:
343                         face.materialIndex = protSideMat-1
344         newmesh.faces.append(face)
345         
346         #top face       
347         face = Face()
348         face.v = newmesh.verts[-4:]
349         if flipnor != 0:
350                 face.v.reverse()
351         if tosel == 1:
352                 face.sel = 1
353         if thereAreMats == 1:
354                 if reassignMats == 0 or protTopMat == 0:
355                         face.materialIndex = currmat
356                 else:
357                         face.materialIndex = protTopMat-1
358         newmesh.faces.append(face)
359         return face
360
361 #Sets the global protrusion values
362 def setProtrusionValues(p0,p1,p2,p3,p4,p5,p6,p7,p8,p9,p10,p11,p12,p13,p14,p15):
363         
364         #Protrusions
365         global makeprots
366         global minimumtaperpercent
367         global maximumtaperpercent
368         global faceschangedpercent
369         global minimumheight
370         global maximumheight
371         global subface1
372         global subface2
373         global subface3
374         global subface4
375         global useselectedfaces
376         global selectface1
377         global selectface2
378         global selectface3
379         global selectface4
380         global deselface
381         global subfaceArray
382         
383         #Protrusions
384         makeprots = p0
385         faceschangedpercent = p1
386         minimumheight = p2
387         maximumheight = p3
388         subface1 = p4
389         subface2 = p5
390         subface3 = p6
391         subface4 = p7
392         minimumtaperpercent = p8
393         maximumtaperpercent = p9
394         useselectedfaces = p10
395         selectface1 = p11
396         selectface2 = p12
397         selectface3 = p13
398         selectface4 = p14
399         deselface = p15
400         makeSubfaceArray()
401         if len(subfaceArray) == 0:
402                 makeprots = 0
403         
404         if minimumheight > maximumheight:
405                 a = maximumheight
406                 maximimheight = minimumheight
407                 minimumheight = a
408         elif minimumtaperpercent > maximumtaperpercent:
409                 a = maximumtaperpercent
410                 maximimtaperpercent = minimumtaperpercent
411                 minimumtaperpercent = a
412
413 #Sets the global Doodad values
414 def setDoodadValues(d0,d1,d2,d3,d4,d5,d6,d7,d8,d9,d10,d11,d12,d13,d14,d15,d16,d17):
415         
416         #Doodads
417         global makedoodads
418         global doodadfacepercent
419         global selectdoodad
420         global onlyonprotrusions
421         global doodad1
422         global doodad2
423         global doodad3
424         global doodad4
425         global doodad5
426         global doodad6
427         global doodadminperface
428         global doodadmaxperface
429         global doodadminsize
430         global doodadmaxsize
431         global doodadminheight
432         global doodadmaxheight
433         global doodadArray
434         global doodonselectedfaces
435         global selectdoodadtoponly
436         
437         #Doodads
438         makedoodads = d0
439         doodadfacepercent = d1
440         selectdoodad = d2
441         onlyonprotrusions = d3
442         doodad1 = d4
443         doodad2 = d5
444         doodad3 = d6
445         doodad4 = d7
446         doodad5 = d8
447         doodad6 = d9
448         doodadminperface = d10
449         doodadmaxperface = d11
450         doodadminsize = d12
451         doodadmaxsize = d13
452         doodadminheight = d14
453         doodadmaxheight = d15
454         doodonselectedfaces = d16
455         selectdoodadtoponly = d17
456         makeDoodadArray()
457         if len(doodadArray) == 0:
458                 makedoodads = 0
459         
460         elif doodadminperface > doodadmaxperface:
461                 a = doodadmaxperface
462                 doodadmaxperface = doodadminperface
463                 doodadminperface = a
464         elif doodadminsize > doodadmaxsize:
465                 a = doodadmaxsize
466                 doodadmaxsize = doodadminsize
467                 doodadminsize = a
468         elif doodadminheight > doodadmaxheight:
469                 a = doodadmaxheight
470                 doodadmaxheight = doodadminheight
471                 doodadminheight = a
472
473 #Sets other global values
474 def setOtherValues(g0,m0,m1,m2,m3,m4):
475         
476         #Global
477         global reassignMats
478         global makenewobj
479         global protSideMat
480         global protTopMat
481         global doodSideMat
482         global doodTopMat
483         
484         #Get Misc Variables
485         makenewobj = g0
486         reassignMats = m0
487         protSideMat = m1
488         protTopMat = m2
489         doodSideMat = m3
490         doodTopMat = m4
491
492 def discombobulate():
493         
494         #Global
495         global origmesh
496         global newmesh
497         global makenewobj
498         global origobj
499         global newobj
500         global messagetext
501         global errortext
502         global editmode
503         
504         #Protrusions
505         global makeprots
506         global minimumtaperpercent
507         global maximumtaperpercent
508         global faceschangedpercent
509         global minimumheight
510         global maximumheight
511         global subface1
512         global subface2
513         global subface3
514         global subface4
515         global useselectedfaces
516         global selectface1
517         global selectface2
518         global selectface3
519         global selectface4
520         global deselface
521         global subfaceArray
522         
523         #Doodads
524         global makedoodads
525         global doodadfacepercent
526         global selectdoodad
527         global onlyonprotrusions
528         global doodad1
529         global doodad2
530         global doodad3
531         global doodad4
532         global doodad5
533         global doodad6
534         global doodadminperface
535         global doodadmaxperface
536         global doodadminsize
537         global doodadmaxsize
538         global doodadminheight
539         global doodadmaxheight
540         global doodadArray
541         global doodonselectedfaces
542         global selectdoodadtoponly
543         
544         #Global
545         global materialArray
546         global reassignMats
547         global protSideMat
548         global protTopMat
549         global doodSideMat
550         global doodTopMat
551         global thereAreMats
552         global currmat
553         
554         try:
555                 origobj = Object.GetSelected()[0]
556         except:
557                 glRasterPos2d(10,50)
558                 errortext = "YOU MUST SELECT AN OBJECT!"
559                 messagetext = ErrorText(errortext)
560                 Blender.Redraw()
561                 return
562
563         #Leave Editmode
564         editmode = Window.EditMode()
565         if editmode: Window.EditMode(0)
566
567         #Get Major Variables
568         newobj = Object.Get()
569         origmesh = origobj.getData()
570         if type(origmesh) != Types.NMeshType:
571                 glRasterPos2d(10,50)
572                 errortext = "OBJECT MUST BE MESH!"
573                 messagetext = ErrorText(errortext)
574                 Blender.Redraw()
575                 return
576         newmesh = NMesh.GetRaw()
577         newmesh.verts = []
578         materialArray = origmesh.getMaterials()
579         if len(materialArray) < 1:
580                 thereAreMats = 0
581         else:
582                 thereAreMats = 1
583         
584         #add material indices if necessary (only up to 4)
585         if thereAreMats == 1 and reassignMats == 1:
586                 if len(materialArray) < 4:
587                         if protSideMat > 4: protSideMat = 4
588                         if protTopMat > 4: protTopMat = 4
589                         if doodSideMat > 4: doodSideMat = 4
590                         if doodTopMat > 4: doodTopMat = 4
591                 else:
592                         if protSideMat > len(materialArray): protSideMat = len(materialArray)
593                         if protTopMat > len(materialArray): protTopMat = len(materialArray)
594                         if doodSideMat > len(materialArray): doodSideMat = len(materialArray)
595                         if doodTopMat > len(materialArray): doodTopMat = len(materialArray)
596                 
597                 #This only does something if there are less than 4 verts
598                 for matind in [protSideMat,protTopMat,doodSideMat,doodTopMat]:
599                         if matind > len(materialArray) and matind <= 4:
600                                 for i in arrayInInclusive(len(materialArray),matind):
601                                         materialArray.append(Material.New("AddedMat " + str(i)))
602                                         
603         #Sets the materials
604         newmesh.setMaterials(materialArray)
605         
606         #Set the doodad settings
607         defaultdoodads.settings(selectdoodadtoponly,materialArray,reassignMats,thereAreMats,doodSideMat,doodTopMat)
608         #defaultdoodads.settings(selectdoodadtoponly,materialArray,reassignMats,thereAreMats,currmat)
609         
610         newmesh.verts.extend(origmesh.verts)
611         
612         #Start modifying faces
613         for currface in origmesh.faces:
614                 
615                 currmat = currface.materialIndex
616                 defaultdoodads.setCurrMat(currmat)
617                 
618                 #Check if it is a triangle
619                 if len(currface.v)<4:
620                         face = Face()
621                         face.v.extend([newmesh.verts[currface.v[0].index],newmesh.verts[currface.v[1].index],newmesh.verts[currface.v[2].index]])
622                         if thereAreMats == 1:
623                                 face.materialIndex = currmat
624                         newmesh.faces.append(face)
625                         continue
626                 
627                 #Check whether or not to make protrusions
628                 if makeprots == 0:
629                         face = Face()
630                         face.v.extend([newmesh.verts[currface.v[0].index],newmesh.verts[currface.v[1].index],newmesh.verts[currface.v[2].index],newmesh.verts[currface.v[3].index]])
631                         if thereAreMats == 1:
632                                 face.materialIndex = currmat
633                         newmesh.faces.append(face)
634                         if makedoodads == 1 and onlyonprotrusions == 0:
635                                 if doodonselectedfaces == 1:
636                                         if currface.sel:
637                                                 tempmesh = NMesh.GetRaw()
638                                                 tempmesh = defaultdoodads.createDoodad(doodadArray,face, doodadminsize, doodadmaxsize, doodadminheight,doodadmaxheight, selectdoodad, doodadminperface, doodadmaxperface, doodadfacepercent)
639                                                 newmesh.verts.extend(tempmesh.verts)
640                                                 newmesh.faces.extend(tempmesh.faces)
641                                 else:
642                                         tempmesh = NMesh.GetRaw()
643                                         tempmesh = defaultdoodads.createDoodad(doodadArray,face, doodadminsize, doodadmaxsize, doodadminheight,doodadmaxheight, selectdoodad, doodadminperface, doodadmaxperface, doodadfacepercent)
644                                         newmesh.verts.extend(tempmesh.verts)
645                                         newmesh.faces.extend(tempmesh.faces)
646                         continue
647                 
648                 #Check if only changing selected faces
649                 if useselectedfaces == 1:
650                         #check if currface is selected
651                         if currface.sel:
652                                 a = 1
653                         else:
654                                 face = Face()
655                                 face.v.extend([newmesh.verts[currface.v[0].index],newmesh.verts[currface.v[1].index],newmesh.verts[currface.v[2].index],newmesh.verts[currface.v[3].index]])
656                                 if thereAreMats == 1:
657                                         face.materialIndex = currmat
658                                 newmesh.faces.append(face)
659                                 if makedoodads == 1 and onlyonprotrusions == 0:
660                                         if doodonselectedfaces != 1:
661                                                 tempmesh = NMesh.GetRaw()
662                                                 tempmesh = defaultdoodads.createDoodad(doodadArray,face, doodadminsize, doodadmaxsize, doodadminheight,doodadmaxheight, selectdoodad, doodadminperface, doodadmaxperface, doodadfacepercent)
663                                                 newmesh.verts.extend(tempmesh.verts)
664                                                 newmesh.faces.extend(tempmesh.faces)
665                                 continue
666                 #Check if face should be modified by random chance
667                 if randnum(0,1)>faceschangedpercent: 
668                         face = Face()
669                         face.v.extend([newmesh.verts[currface.v[0].index],newmesh.verts[currface.v[1].index],newmesh.verts[currface.v[2].index],newmesh.verts[currface.v[3].index]])
670                         if thereAreMats == 1:
671                                 face.materialIndex = currmat
672                         newmesh.faces.append(face)
673                         if makedoodads == 1 and onlyonprotrusions == 0:
674                                 if doodonselectedfaces == 1:
675                                         if currface.sel:
676                                                 tempmesh = NMesh.GetRaw()
677                                                 tempmesh = defaultdoodads.createDoodad(doodadArray,face, doodadminsize, doodadmaxsize, doodadminheight,doodadmaxheight, selectdoodad, doodadminperface, doodadmaxperface, doodadfacepercent)
678                                                 newmesh.verts.extend(tempmesh.verts)
679                                                 newmesh.faces.extend(tempmesh.faces)
680                                 else:
681                                         tempmesh = NMesh.GetRaw()
682                                         tempmesh = defaultdoodads.createDoodad(doodadArray,face, doodadminsize, doodadmaxsize, doodadminheight,doodadmaxheight, selectdoodad, doodadminperface, doodadmaxperface, doodadfacepercent)
683                                         newmesh.verts.extend(tempmesh.verts)
684                                         newmesh.faces.extend(tempmesh.faces)
685                         continue
686                 
687                 center = Vector([0,0,0])
688                 for pt in currface.v:
689                         center = center + pt.co
690                 center = divideVectorByInt(center,len(currface.v))
691                 
692                 #Determine amount of subfaces
693                 subfaces = round(randnum(1,len(subfaceArray)),0)
694                 subfaces = subfaceArray[(int(subfaces) - 1)]
695                 
696                 ######################## START DEALING WITH PROTRUSIONS #####################
697                 
698                 if subfaces == 1:
699                         prot = randnum(minimumheight,maximumheight)
700                         tempface = extrude(center,currface.no,prot,currface.v[0].index,currface.v[1].index,currface.v[2].index,currface.v[3].index,selectface1)
701                         if makedoodads == 1:
702                                 if doodonselectedfaces == 1:
703                                         if currface.sel:
704                                                 tempmesh = NMesh.GetRaw()
705                                                 tempmesh = defaultdoodads.createDoodad(doodadArray, tempface, doodadminsize, doodadmaxsize, doodadminheight,doodadmaxheight, selectdoodad, doodadminperface, doodadmaxperface, doodadfacepercent)
706                                                 newmesh.verts.extend(tempmesh.verts)
707                                                 newmesh.faces.extend(tempmesh.faces)
708                                 else:
709                                         tempmesh = NMesh.GetRaw()
710                                         tempmesh = defaultdoodads.createDoodad(doodadArray, tempface, doodadminsize, doodadmaxsize, doodadminheight,doodadmaxheight, selectdoodad, doodadminperface, doodadmaxperface, doodadfacepercent)
711                                         newmesh.verts.extend(tempmesh.verts)
712                                         newmesh.faces.extend(tempmesh.faces)
713                 
714                 elif subfaces == 2:
715                         orientation = int(round(randnum(0,1)))
716                         p1 = currface.v[orientation]
717                         p2 = currface.v[orientation + 1]
718                         p3 = divideVectorByInt((p2.co - p1.co),2) + p1.co
719                         ve1 = Vert(p3[0],p3[1],p3[2])
720                         ve1.sel = 0
721                         p1 = currface.v[2 + orientation]
722                         if orientation < 0.5:
723                                 p2 = currface.v[3]
724                         else:
725                                 p2 = currface.v[0]
726                         p3 = divideVectorByInt((p2.co - p1.co),2) + p1.co
727                         ve2 = Vert(p3[0],p3[1],p3[2])
728                         ve2.sel = 0
729                         if orientation < 0.5:
730                                 verti = currface.v[3]
731                                 p3 = verti.index
732                                 v1 = p3
733                                 verti = currface.v[0]
734                                 p0 = verti.index
735                                 v2 = p0
736                         else:
737                                 verti = currface.v[0]
738                                 p0 = verti.index
739                                 v1 = p0
740                                 verti = currface.v[1]
741                                 p1 = verti.index
742                                 v2 = p1
743                         newmesh.verts.append(ve1)
744                         newmesh.verts.append(ve2)
745                         index = len(newmesh.verts) - 2
746                         v4 = index + 1
747                         v3 = index
748                         center = Vector([0, 0, 0])
749                         for pt in [newmesh.verts[v1],newmesh.verts[v2],newmesh.verts[v3],newmesh.verts[v4]]:
750                                 center = center + pt.co
751                         center = divideVectorByInt(center,4)
752                         prot = randnum(minimumheight,maximumheight)
753                         tempface = extrude(center,currface.no,prot,v1,v2,v3,v4,selectface2)
754                         if makedoodads == 1:
755                                 if doodonselectedfaces == 1:
756                                         if currface.sel:
757                                                 tempmesh = NMesh.GetRaw()
758                                                 tempmesh = defaultdoodads.createDoodad(doodadArray, tempface, doodadminsize, doodadmaxsize, doodadminheight,doodadmaxheight, selectdoodad, doodadminperface, doodadmaxperface, doodadfacepercent)
759                                                 newmesh.verts.extend(tempmesh.verts)
760                                                 newmesh.faces.extend(tempmesh.faces)
761                                 else:
762                                         tempmesh = NMesh.GetRaw()
763                                         tempmesh = defaultdoodads.createDoodad(doodadArray, tempface, doodadminsize, doodadmaxsize, doodadminheight,doodadmaxheight, selectdoodad, doodadminperface, doodadmaxperface, doodadfacepercent)
764                                         newmesh.verts.extend(tempmesh.verts)
765                                         newmesh.faces.extend(tempmesh.faces)
766                         if orientation < 0.5:
767                                 verti = currface.v[1]
768                                 p1 = verti.index
769                                 v1 = p1
770                                 verti = currface.v[2]
771                                 p2 = verti.index
772                                 v2 = p2
773                         else:
774                                 verti = currface.v[2]
775                                 p2 = verti.index
776                                 v1 = p2
777                                 verti = currface.v[3]
778                                 p3 = verti.index
779                                 v2 = p3
780                         center = Vector([0]*3)
781                         for pt in [newmesh.verts[v1],newmesh.verts[v2],newmesh.verts[v3],newmesh.verts[v4]]:
782                                 center = center + pt.co
783                         center = divideVectorByInt(center,4)
784                         prot = randnum(minimumheight,maximumheight)
785                         tempface = extrude(center,currface.no,prot,v1,v2,v4,v3,selectface2)
786                         if makedoodads == 1:
787                                 if doodonselectedfaces == 1:
788                                         if currface.sel:
789                                                 tempmesh = NMesh.GetRaw()
790                                                 tempmesh = defaultdoodads.createDoodad(doodadArray, tempface, doodadminsize, doodadmaxsize, doodadminheight,doodadmaxheight, selectdoodad, doodadminperface, doodadmaxperface, doodadfacepercent)
791                                                 newmesh.verts.extend(tempmesh.verts)
792                                                 newmesh.faces.extend(tempmesh.faces)
793                                 else:
794                                         tempmesh = NMesh.GetRaw()
795                                         tempmesh = defaultdoodads.createDoodad(doodadArray, tempface, doodadminsize, doodadmaxsize, doodadminheight,doodadmaxheight, selectdoodad, doodadminperface, doodadmaxperface, doodadfacepercent)
796                                         newmesh.verts.extend(tempmesh.verts)
797                                         newmesh.faces.extend(tempmesh.faces)
798                         if orientation < 0.5:
799                                 face = Face()
800                                 face.v.extend([newmesh.verts[p0],newmesh.verts[p1],newmesh.verts[v3]])
801                                 if thereAreMats == 1:
802                                         if reassignMats == 0 or protSideMat == 0:
803                                                 face.materialIndex = currmat
804                                         else:
805                                                 face.materialIndex = protSideMat-1
806                                 newmesh.faces.append(face)
807                                 face = Face()
808                                 face.v.extend([newmesh.verts[p2],newmesh.verts[p3],newmesh.verts[v4]])
809                                 if thereAreMats == 1:
810                                         if reassignMats == 0 or protSideMat == 0:
811                                                 face.materialIndex = currmat
812                                         else:
813                                                 face.materialIndex = protSideMat-1
814                                 newmesh.faces.append(face)
815                         else:
816                                 face = Face()
817                                 face.v.extend([newmesh.verts[p1],newmesh.verts[p2],newmesh.verts[v3]])
818                                 if thereAreMats == 1:
819                                         if reassignMats == 0 or protSideMat == 0:
820                                                 face.materialIndex = currmat
821                                         else:
822                                                 face.materialIndex = protSideMat-1
823                                 newmesh.faces.append(face)
824                                 face = Face()
825                                 face.v.extend([newmesh.verts[p3],newmesh.verts[p0],newmesh.verts[v4]])
826                                 if thereAreMats == 1:
827                                         if reassignMats == 0 or protSideMat == 0:
828                                                 face.materialIndex = currmat
829                                         else:
830                                                 face.materialIndex = protSideMat-1
831                                 newmesh.faces.append(face)
832                         
833                 elif subfaces == 3:
834                         layer2inds = []
835                         layer2verts = []
836                         orientation = int(round(randnum(0,1)))
837                         rotation = int(round(randnum(0,1)))
838                         p1 = currface.v[orientation]
839                         p2 = currface.v[orientation + 1]
840                         p3 = divideVectorByInt((p2.co - p1.co),2) + p1.co
841                         ve1 = Vert(p3[0],p3[1],p3[2])
842                         ve1.sel = 0
843                         p1 = currface.v[2 + orientation]
844                         if orientation < 0.5:
845                                 p2 = currface.v[3]
846                         else:
847                                 p2 = currface.v[0]
848                         p3 = divideVectorByInt((p2.co - p1.co),2) + p1.co
849                         ve2 = Vert(p3[0],p3[1],p3[2])
850                         ve2.sel = 0
851                         fp = []
852         
853                         #make first protrusion
854                         if rotation < 0.5:
855                                 if orientation < 0.5:
856                                         verti = currface.v[3]
857                                         fp.append(verti.index)
858                                         v1 = verti.index
859                                         verti = currface.v[0]
860                                         fp.append(verti.index)
861                                         v2 = verti.index
862                                         layer2verts.extend([newmesh.verts[currface.v[1].index],newmesh.verts[currface.v[2].index]])
863                                 else:
864                                         verti = currface.v[0]
865                                         fp.append(verti.index)
866                                         v1 = verti.index
867                                         verti = currface.v[1]
868                                         fp.append(verti.index)
869                                         v2 = verti.index
870                                         layer2verts.extend([newmesh.verts[currface.v[2].index],newmesh.verts[currface.v[3].index]])
871                                 newmesh.verts.append(ve1)
872                                 newmesh.verts.append(ve2)
873                                 index = len(newmesh.verts) - 2
874                                 v4 = index + 1
875                                 v3 = index
876                                 center = Vector([0]*3)
877                                 for pt in [newmesh.verts[v1],newmesh.verts[v2],newmesh.verts[v3],newmesh.verts[v4]]:
878                                         center = center + pt.co
879                                 center = divideVectorByInt(center,4)
880                                 prot = randnum(minimumheight,maximumheight)
881                                 layer2inds.extend([v3,v4])
882                                 tempface = extrude(center,currface.no,prot,v1,v2,v3,v4,selectface3)
883                                 if makedoodads == 1:
884                                         if doodonselectedfaces == 1:
885                                                 if currface.sel:
886                                                         tempmesh = NMesh.GetRaw()
887                                                         tempmesh = defaultdoodads.createDoodad(doodadArray, tempface, doodadminsize, doodadmaxsize, doodadminheight,doodadmaxheight, selectdoodad, doodadminperface, doodadmaxperface, doodadfacepercent)
888                                                         newmesh.verts.extend(tempmesh.verts)
889                                                         newmesh.faces.extend(tempmesh.faces)
890                                         else:
891                                                 tempmesh = NMesh.GetRaw()
892                                                 tempmesh = defaultdoodads.createDoodad(doodadArray, tempface, doodadminsize, doodadmaxsize, doodadminheight,doodadmaxheight, selectdoodad, doodadminperface, doodadmaxperface, doodadfacepercent)
893                                                 newmesh.verts.extend(tempmesh.verts)
894                                                 newmesh.faces.extend(tempmesh.faces)
895                         #Still first protrusion
896                         else:
897                                 if orientation < 0.5:
898                                         verti = currface.v[1]
899                                         fp.append(verti.index)
900                                         v1 = verti.index
901                                         verti = currface.v[2]
902                                         fp.append(verti.index)
903                                         v2 = verti.index
904                                         layer2verts.extend([newmesh.verts[currface.v[0].index],newmesh.verts[currface.v[3].index]])
905                                 else:
906                                         verti = currface.v[2]
907                                         fp.append(verti.index)
908                                         v1 = verti.index
909                                         verti = currface.v[3]
910                                         fp.append(verti.index)
911                                         v2 = verti.index
912                                         layer2verts.extend([newmesh.verts[currface.v[1].index],newmesh.verts[currface.v[0].index]])
913                                 newmesh.verts.append(ve2)
914                                 newmesh.verts.append(ve1)
915                                 index = len(newmesh.verts) - 2
916                                 v4 = index
917                                 v3 = index + 1
918                                 center = Vector([0]*3)
919                                 for pt in [newmesh.verts[v1],newmesh.verts[v2],newmesh.verts[v3],newmesh.verts[v4]]:
920                                         center = center + pt.co
921                                 center = divideVectorByInt(center,4)
922                                 prot = randnum(minimumheight,maximumheight)
923                                 layer2inds.extend([index, index +1])
924                                 tempface = extrude(center,currface.no,prot,v1,v2,v4,v3,selectface3)
925                                 if makedoodads == 1:
926                                         if doodonselectedfaces == 1:
927                                                 if currface.sel:
928                                                         tempmesh = NMesh.GetRaw()
929                                                         tempmesh = defaultdoodads.createDoodad(doodadArray, tempface, doodadminsize, doodadmaxsize, doodadminheight,doodadmaxheight, selectdoodad, doodadminperface, doodadmaxperface, doodadfacepercent)
930                                                         newmesh.verts.extend(tempmesh.verts)
931                                                         newmesh.faces.extend(tempmesh.faces)
932                                         else:
933                                                 tempmesh = NMesh.GetRaw()
934                                                 tempmesh = defaultdoodads.createDoodad(doodadArray, tempface, doodadminsize, doodadmaxsize, doodadminheight,doodadmaxheight, selectdoodad, doodadminperface, doodadmaxperface, doodadfacepercent)
935                                                 newmesh.verts.extend(tempmesh.verts)
936                                                 newmesh.faces.extend(tempmesh.faces)
937                         
938                         #split next rect(pre-arranged, no orientation crud)--make flag in extruder for only one existing vert in mesh
939                         p1 = newmesh.verts[layer2inds[0]]
940                         p2 = newmesh.verts[layer2inds[1]]
941                         p3 = divideVectorByInt((p2.co - p1.co),2) + p1.co
942                         ve3 = Vert(p3[0],p3[1],p3[2])
943                         ve3.sel = 0
944                         p1 = layer2verts[0]
945                         p2 = layer2verts[1]
946                         p3 = divideVectorByInt((p2.co - p1.co),2) + p1.co
947                         ve4 = Vert(p3[0],p3[1],p3[2])
948                         ve4.sel = 0
949                         newmesh.verts.append(ve3)
950                         newmesh.verts.append(ve4)
951                         tempindex = len(newmesh.verts) - 2
952                         v5 = tempindex
953                         v6 = tempindex + 1
954                         verti = layer2verts[0]
955                         t0 = verti.index
956                         center = Vector([0]*3)
957                         for pt in [newmesh.verts[v5],newmesh.verts[v6],newmesh.verts[t0],newmesh.verts[v3]]:
958                                 center = center + pt.co
959                         center = divideVectorByInt(center,4)
960                         prot = randnum(minimumheight,maximumheight)
961                         if rotation < 0.5: flino = 1
962                         else: flino = 0
963                         tempface = extrude(center,currface.no,prot,v3,v5,v6,t0,selectface3,flino)
964                         if makedoodads == 1:
965                                 if doodonselectedfaces == 1:
966                                         if currface.sel:
967                                                 tempmesh = NMesh.GetRaw()
968                                                 tempmesh = defaultdoodads.createDoodad(doodadArray, tempface, doodadminsize, doodadmaxsize, doodadminheight,doodadmaxheight, selectdoodad, doodadminperface, doodadmaxperface, doodadfacepercent)
969                                                 newmesh.verts.extend(tempmesh.verts)
970                                                 newmesh.faces.extend(tempmesh.faces)
971                                         tempmesh = NMesh.GetRaw()
972                                         tempmesh = defaultdoodads.createDoodad(doodadArray, tempface, doodadminsize, doodadmaxsize, doodadminheight,doodadmaxheight, selectdoodad, doodadminperface, doodadmaxperface, doodadfacepercent)
973                                         newmesh.verts.extend(tempmesh.verts)
974                                         newmesh.faces.extend(tempmesh.faces)
975                         if rotation < 0.5:
976                                 face = Face()
977                                 fpt = t0
978                                 face.v.extend([newmesh.verts[fp[1]],newmesh.verts[fpt],newmesh.verts[v3]])
979                                 if thereAreMats == 1:
980                                         if reassignMats == 0 or protSideMat == 0:
981                                                 face.materialIndex = currmat
982                                         else:
983                                                 face.materialIndex = protSideMat-1
984                                 newmesh.faces.append(face)
985                         else:
986                                 face = Face()
987                                 fpt = t0
988                                 face.v.extend([newmesh.verts[fp[0]],newmesh.verts[v3],newmesh.verts[fpt]])
989                                 if thereAreMats == 1:
990                                         if reassignMats == 0 or protSideMat == 0:
991                                                 face.materialIndex = currmat
992                                         else:
993                                                 face.materialIndex = protSideMat-1
994                                 newmesh.faces.append(face)
995                         verti = layer2verts[1]
996                         tempindex = verti.index
997                         center = Vector([0]*3)
998                         for pt in [newmesh.verts[v5],newmesh.verts[v6],newmesh.verts[tempindex],newmesh.verts[v4]]:
999                                 center = center + pt.co
1000                         center = divideVectorByInt(center,4)
1001                         prot = randnum(minimumheight,maximumheight)
1002                         tempface = extrude(center,currface.no,prot,v6,v5,v4,tempindex,selectface3,flino)
1003                         if makedoodads == 1:
1004                                 if doodonselectedfaces == 1:
1005                                         if currface.sel:
1006                                                 tempmesh = NMesh.GetRaw()
1007                                                 tempmesh = defaultdoodads.createDoodad(doodadArray, tempface, doodadminsize, doodadmaxsize, doodadminheight,doodadmaxheight, selectdoodad, doodadminperface, doodadmaxperface, doodadfacepercent)
1008                                                 newmesh.verts.extend(tempmesh.verts)
1009                                                 newmesh.faces.extend(tempmesh.faces)
1010                                 else:
1011                                         tempmesh = NMesh.GetRaw()
1012                                         tempmesh = defaultdoodads.createDoodad(doodadArray, tempface, doodadminsize, doodadmaxsize, doodadminheight,doodadmaxheight, selectdoodad, doodadminperface, doodadmaxperface, doodadfacepercent)
1013                                         newmesh.verts.extend(tempmesh.verts)
1014                                         newmesh.faces.extend(tempmesh.faces)
1015                         if rotation < 0.5:
1016                                 face = Face()
1017                                 face.v.extend([newmesh.verts[tempindex],newmesh.verts[fp[0]],newmesh.verts[v4]])
1018                                 if thereAreMats == 1:
1019                                         if reassignMats == 0 or protSideMat == 0:
1020                                                 face.materialIndex = currmat
1021                                         else:
1022                                                 face.materialIndex = protSideMat-1
1023                                 newmesh.faces.append(face)
1024                                 face = Face()
1025                                 face.v.extend([newmesh.verts[fpt],newmesh.verts[tempindex],newmesh.verts[v6]])
1026                                 if thereAreMats == 1:
1027                                         if reassignMats == 0 or protSideMat == 0:
1028                                                 face.materialIndex = currmat
1029                                         else:
1030                                                 face.materialIndex = protSideMat-1
1031                                 newmesh.faces.append(face)
1032                         else:
1033                                 face = Face()
1034                                 face.v.extend([newmesh.verts[tempindex],newmesh.verts[v4],newmesh.verts[fp[1]]])
1035                                 if thereAreMats == 1:
1036                                         if reassignMats == 0 or protSideMat == 0:
1037                                                 face.materialIndex = currmat
1038                                         else:
1039                                                 face.materialIndex = protSideMat-1
1040                                 newmesh.faces.append(face)
1041                                 face = Face()
1042                                 face.v.extend([newmesh.verts[tempindex],newmesh.verts[fpt],newmesh.verts[v6]])
1043                                 if thereAreMats == 1:
1044                                         if reassignMats == 0 or protSideMat == 0:
1045                                                 face.materialIndex = currmat
1046                                         else:
1047                                                 face.materialIndex = protSideMat-1
1048                                 newmesh.faces.append(face)
1049                         
1050                 else:
1051                         #get all points
1052                         verti = currface.v[0]
1053                         p0 = verti.index
1054                         
1055                         verti = currface.v[1]
1056                         p1 = verti.index
1057                 
1058                         pt = divideVectorByInt((newmesh.verts[p1].co - newmesh.verts[p0].co),2) + newmesh.verts[p0].co
1059                         v1 = Vert(pt[0],pt[1],pt[2])
1060                         v1.sel = 0
1061         
1062                         verti = currface.v[2]
1063                         p2 = verti.index
1064
1065                         pt =  divideVectorByInt((newmesh.verts[p2].co - newmesh.verts[p1].co),2) + newmesh.verts[p1].co
1066                         v2 = Vert(pt[0],pt[1],pt[2])
1067                         v2.sel = 0
1068
1069                         verti = currface.v[3]
1070                         p3 = verti.index
1071
1072                         pt =  divideVectorByInt((newmesh.verts[p3].co - newmesh.verts[p2].co),2) + newmesh.verts[p2].co
1073                         v3 = Vert(pt[0],pt[1],pt[2])
1074                         v3.sel = 0
1075
1076                         pt =  divideVectorByInt((newmesh.verts[p0].co - newmesh.verts[p3].co),2) + newmesh.verts[p3].co
1077                         v4 = Vert(pt[0],pt[1],pt[2])
1078                         v4.sel = 0
1079
1080                         pt =  divideVectorByInt((v3.co - v1.co),2) + v1.co
1081                         m = Vert(pt[0],pt[1],pt[2])
1082                         m.sel = 0
1083                         
1084                         #extrusion 1
1085                         newmesh.verts.extend([v1,m,v4])
1086                         index = len(newmesh.verts) - 3
1087                         v1 = index
1088                         m = index + 1
1089                         v4 = index + 2
1090                         center = Vector([0]*3)
1091                         for pt in [newmesh.verts[p0],newmesh.verts[v1],newmesh.verts[m],newmesh.verts[v4]]:
1092                                 center = center + pt.co
1093                         center = divideVectorByInt(center,4)
1094                         prot = randnum(minimumheight,maximumheight)
1095                         tempface = extrude(center,currface.no,prot,p0,v1,m,v4,selectface4)
1096                         if makedoodads == 1:
1097                                 if doodonselectedfaces == 1:
1098                                         if currface.sel:
1099                                                 tempmesh = NMesh.GetRaw()
1100                                                 tempmesh = defaultdoodads.createDoodad(doodadArray, tempface, doodadminsize, doodadmaxsize, doodadminheight,doodadmaxheight, selectdoodad, doodadminperface, doodadmaxperface, doodadfacepercent)
1101                                                 newmesh.verts.extend(tempmesh.verts)
1102                                                 newmesh.faces.extend(tempmesh.faces)
1103                                 else:
1104                                         tempmesh = NMesh.GetRaw()
1105                                         tempmesh = defaultdoodads.createDoodad(doodadArray, tempface, doodadminsize, doodadmaxsize, doodadminheight,doodadmaxheight, selectdoodad, doodadminperface, doodadmaxperface, doodadfacepercent)
1106                                         newmesh.verts.extend(tempmesh.verts)
1107                                         newmesh.faces.extend(tempmesh.faces)
1108                         
1109                         #extrusion 2
1110                         newmesh.verts.extend([v2])
1111                         index = len(newmesh.verts) - 1
1112                         v2 = index
1113                         center = Vector([0]*3)
1114                         for pt in [newmesh.verts[m],newmesh.verts[v1],newmesh.verts[p1],newmesh.verts[v2]]:
1115                                 center = center + pt.co
1116                         center = divideVectorByInt(center,4)
1117                         prot = randnum(minimumheight,maximumheight)
1118                         tempface = extrude(center,currface.no,prot,m,v1,p1,v2,selectface4)
1119                         if makedoodads == 1:
1120                                 if doodonselectedfaces == 1:
1121                                         if currface.sel:
1122                                                 tempmesh = NMesh.GetRaw()
1123                                                 tempmesh = defaultdoodads.createDoodad(doodadArray, tempface, doodadminsize, doodadmaxsize, doodadminheight,doodadmaxheight, selectdoodad, doodadminperface, doodadmaxperface, doodadfacepercent)
1124                                                 newmesh.verts.extend(tempmesh.verts)
1125                                                 newmesh.faces.extend(tempmesh.faces)
1126                                 else:
1127                                         tempmesh = NMesh.GetRaw()
1128                                         tempmesh = defaultdoodads.createDoodad(doodadArray, tempface, doodadminsize, doodadmaxsize, doodadminheight,doodadmaxheight, selectdoodad, doodadminperface, doodadmaxperface, doodadfacepercent)
1129                                         newmesh.verts.extend(tempmesh.verts)
1130                                         newmesh.faces.extend(tempmesh.faces)
1131                         
1132                         #extrusion 3
1133                         newmesh.verts.extend([v3])
1134                         index = len(newmesh.verts) - 1
1135                         v3 = index
1136                         center = Vector([0]*3)
1137                         for pt in [newmesh.verts[m],newmesh.verts[v2],newmesh.verts[p2],newmesh.verts[v3]]:
1138                                 center = center + pt.co
1139                         center = divideVectorByInt(center,4)
1140                         prot = randnum(minimumheight,maximumheight)
1141                         tempface = extrude(center,currface.no,prot,m,v2,p2,v3,selectface4)
1142                         if makedoodads == 1:
1143                                 if doodonselectedfaces == 1:
1144                                         if currface.sel:
1145                                                 tempmesh = NMesh.GetRaw()
1146                                                 tempmesh = defaultdoodads.createDoodad(doodadArray, tempface, doodadminsize, doodadmaxsize, doodadminheight,doodadmaxheight, selectdoodad, doodadminperface, doodadmaxperface, doodadfacepercent)
1147                                                 newmesh.verts.extend(tempmesh.verts)
1148                                                 newmesh.faces.extend(tempmesh.faces)
1149                                 else:
1150                                         tempmesh = NMesh.GetRaw()
1151                                         tempmesh = defaultdoodads.createDoodad(doodadArray, tempface, doodadminsize, doodadmaxsize, doodadminheight,doodadmaxheight, selectdoodad, doodadminperface, doodadmaxperface, doodadfacepercent)
1152                                         newmesh.verts.extend(tempmesh.verts)
1153                                         newmesh.faces.extend(tempmesh.faces)
1154                         
1155                         #extrusion 4
1156                         center = Vector([0]*3)
1157                         for pt in [newmesh.verts[m],newmesh.verts[v3],newmesh.verts[p3],newmesh.verts[v4]]:
1158                                 center = center + pt.co
1159                         center = divideVectorByInt(center,4)
1160                         prot = randnum(minimumheight,maximumheight)
1161                         tempface = extrude(center,currface.no,prot,v4,m,v3,p3,selectface4)
1162                         if makedoodads == 1:
1163                                 if doodonselectedfaces == 1:
1164                                         if currface.sel:
1165                                                 tempmesh = NMesh.GetRaw()
1166                                                 tempmesh = defaultdoodads.createDoodad(doodadArray, tempface, doodadminsize, doodadmaxsize, doodadminheight,doodadmaxheight, selectdoodad, doodadminperface, doodadmaxperface, doodadfacepercent)
1167                                                 newmesh.verts.extend(tempmesh.verts)
1168                                                 newmesh.faces.extend(tempmesh.faces)
1169                                 else:
1170                                         tempmesh = NMesh.GetRaw()
1171                                         tempmesh = defaultdoodads.createDoodad(doodadArray, tempface, doodadminsize, doodadmaxsize, doodadminheight,doodadmaxheight, selectdoodad, doodadminperface, doodadmaxperface, doodadfacepercent)
1172                                         newmesh.verts.extend(tempmesh.verts)
1173                                         newmesh.faces.extend(tempmesh.faces)
1174                         
1175                         face = Face()
1176                         face.v.extend([newmesh.verts[p0],newmesh.verts[p1],newmesh.verts[v1]])
1177                         if thereAreMats == 1:
1178                                 if reassignMats == 0 or protSideMat == 0:
1179                                         face.materialIndex = currmat
1180                                 else:
1181                                         face.materialIndex = protSideMat-1
1182                         newmesh.faces.append(face)
1183                         face = Face()
1184                         face.v.extend([newmesh.verts[p1],newmesh.verts[p2],newmesh.verts[v2]])
1185                         if thereAreMats == 1:
1186                                 if reassignMats == 0 or protSideMat == 0:
1187                                         face.materialIndex = currmat
1188                                 else:
1189                                         face.materialIndex = protSideMat-1
1190                         newmesh.faces.append(face)
1191                         face = Face()
1192                         face.v.extend([newmesh.verts[p2],newmesh.verts[p3],newmesh.verts[v3]])
1193                         if thereAreMats == 1:
1194                                 if reassignMats == 0 or protSideMat == 0:
1195                                         face.materialIndex = currmat
1196                                 else:
1197                                         face.materialIndex = protSideMat-1
1198                         newmesh.faces.append(face)
1199                         face = Face()
1200                         face.v.extend([newmesh.verts[p3],newmesh.verts[p0],newmesh.verts[v4]])
1201                         if thereAreMats == 1:
1202                                 if reassignMats == 0 or protSideMat == 0:
1203                                         face.materialIndex = currmat
1204                                 else:
1205                                         face.materialIndex = protSideMat-1
1206                         newmesh.faces.append(face)
1207                         
1208         #NMesh.PutRaw(newmesh)
1209         if deselface == 1:
1210                 for unvert in origmesh.verts:
1211                         newmesh.verts[unvert.index].sel = 0
1212         if makenewobj == 1:
1213                 newobj = Object.New('Mesh')
1214                 newobj.link(newmesh)
1215                 copyObjStuff(origobj,newobj)
1216                 scene = Blender.Scene.getCurrent()
1217                 scene.link(newobj)
1218                 origobj.select(0)
1219                 newobj.select(1)
1220         else:
1221                 origobj.link(newmesh)
1222         
1223         #Return to Editmode if previously in it
1224         if editmode: Window.EditMode(1)
1225
1226 def divideVectorByInt(thevect,theint):
1227         thevect.x = thevect.x/theint
1228         thevect.y = thevect.y/theint
1229         thevect.z = thevect.z/theint
1230         return thevect
1231
1232 ####################### gui ######################
1233 from Blender.BGL import *
1234 from Blender.Draw import *
1235
1236 def ErrorText(errortext):
1237         Window.WaitCursor(0)
1238         Text(errortext)
1239         PupMenu("ERROR: %s" % errortext.lower())
1240
1241 #Global Buttons
1242 makenewobject = Create(makenewobj)
1243 messagetext = Create(errortext)
1244
1245 #Protrusion Buttons
1246 doprots = Create(makeprots)
1247 facechange = Create(faceschangedpercent*100)
1248 minheight = Create(minimumheight)
1249 maxheight = Create(maximumheight)
1250 sub1 = Create(subface1)
1251 sub2 = Create(subface2)
1252 sub3 = Create(subface3)
1253 sub4 = Create(subface4)
1254 mintaper = Create(minimumtaperpercent*100)
1255 maxtaper = Create(maximumtaperpercent*100)
1256 useselected = Create(useselectedfaces)
1257 selface1 = Create(selectface1)
1258 selface2 = Create(selectface2)
1259 selface3 = Create(selectface3)
1260 selface4 = Create(selectface4)
1261 deselectvertices = Create(deselface)
1262 #selectbyverts = Create(vertselected)
1263
1264 #Doodad Buttons
1265 dodoodads = Create(makedoodads)
1266 doodadfacechange = Create(doodadfacepercent*100)
1267 seldoodad = Create(selectdoodad)
1268 onprot = Create(onlyonprotrusions)
1269 dood1 = Create(doodad1)
1270 dood2 = Create(doodad2)
1271 dood3 = Create(doodad3)
1272 dood4 = Create(doodad4)
1273 dood5 = Create(doodad5)
1274 dood6 = Create(doodad6)
1275 doodadminamount = Create(doodadminperface)
1276 doodadmaxamount = Create(doodadmaxperface)
1277 doodsizemin = Create(doodadminsize*100)
1278 doodsizemax = Create(doodadmaxsize*100)
1279 doodheightmin = Create(doodadminheight)
1280 doodheightmax = Create(doodadmaxheight)
1281 doodonselface = Create(doodonselectedfaces)
1282 seldoodtop = Create(selectdoodadtoponly)
1283
1284 #Material Buttons
1285 assignNewMats = Create(reassignMats)
1286 replProtSideIndex = Create(protSideMat)
1287 replProtTopIndex = Create(protTopMat)
1288 replDoodSideIndex = Create(doodSideMat)
1289 replDoodTopIndex = Create(doodTopMat)
1290
1291 # Events
1292 EVENT_NONE = 1
1293 EVENT_DISCOMBOBULATE = 2
1294 EVENT_EXIT = 3
1295
1296 # Additions for moving gui
1297 hadd = 0
1298 wadd = 0
1299 thadd = 410
1300 phadd = 245
1301 pwadd = 0
1302 dhadd = 55
1303 dwadd = 0
1304 ghadd = 10
1305 gwadd = 0
1306 mhadd = 55
1307 mwadd = 312
1308
1309 def colorbox(x,y,xright,bottom):
1310    glColor3f(0.75, 0.75, 0.75)
1311    glRecti(x + 1, y + 1, xright - 1, bottom - 1)
1312
1313 firstDraw = 1
1314
1315 def draw():
1316         
1317         #Protrusions
1318         global doprots
1319         global facechange
1320         global minheight
1321         global maxheight
1322         global sub1
1323         global sub2
1324         global sub3
1325         global sub4
1326         global mintaper
1327         global maxtaper
1328         global useselected
1329         global selface1
1330         global selface2
1331         global selface3
1332         global selface4
1333         global deselectvertices
1334         #global selectbyverts
1335         
1336         #Doodads
1337         global dodoodads
1338         global doodadfacechange
1339         global seldoodad
1340         global onprot
1341         global dood1
1342         global dood2
1343         global dood3
1344         global dood4
1345         global dood5
1346         global dood6
1347         global doodadminamount
1348         global doodadmaxamount
1349         global doodsizemin
1350         global doodsizemax
1351         global doodheightmin
1352         global doodheightmax
1353         global doodonselface
1354         global seldoodtop
1355         
1356         #Materials
1357         global assignNewMats
1358         global replProtSideIndex
1359         global replProtTopIndex
1360         global replDoodSideIndex
1361         global replDoodTopIndex
1362         
1363         #Global Settings
1364         global makenewobject
1365         global messagetext
1366         global errortext
1367         global EVENT_NONE,EVENT_DRAW,EVENT_EXIT,EVENT_UP,EVENT_DOWN,EVENT_LEFT,EVENT_RIGHT
1368         
1369         # Additions for moving gui
1370         global hadd
1371         global wadd
1372         global thadd
1373         global phadd
1374         global pwadd
1375         global dhadd
1376         global dwadd
1377         global ghadd
1378         global gwadd
1379         global mhadd
1380         global mwadd
1381         
1382         #This is for creating the initial layout
1383         global firstDraw
1384         if(firstDraw == 1):
1385                 if(((Window.GetAreaSize()[1])*1.7) < Window.GetAreaSize()[0]):
1386                         thadd = 180
1387                         phadd = 10
1388                         dhadd = 10
1389                         mhadd = 55
1390                         ghadd = 10
1391                         pwadd = 0
1392                         dwadd = 305
1393                         mwadd = 610
1394                         gwadd = 610
1395                 else:
1396                         thadd = 505
1397                         phadd = 346
1398                         dhadd = 160
1399                         mhadd = 56
1400                         ghadd = 10
1401                         pwadd = 0
1402                         dwadd = 0
1403                         mwadd = 0
1404                         gwadd = 0
1405                 firstDraw = 0
1406         
1407         
1408         #Title :420high
1409         glClearColor(0.6, 0.6, 0.6, 1.0)
1410         glClear(GL_COLOR_BUFFER_BIT)
1411         glColor3f(0.0,0.0,0.0)
1412         glRasterPos2d(8+wadd, thadd+hadd)
1413         Text("Discombobulator v2.1")
1414         
1415         #Protrusion
1416         colorbox(8+pwadd+wadd,150+phadd+hadd,312+pwadd+wadd,phadd-5+hadd)
1417         glColor3f(0.0,0.0,0.0)
1418         glRasterPos2d(12+pwadd+wadd, 140+phadd+hadd)
1419         Text("Protrusions:")
1420         doprots = Toggle("Make Protrusions",EVENT_NONE,12+pwadd+wadd,117+phadd+hadd,145,18,doprots.val,"Make Protrusions?")
1421         facechange = Number("Face %: ",EVENT_NONE,162+pwadd+wadd,117+phadd+hadd,145,18,facechange.val,0,100,"Percentage of faces that will grow protrusions")
1422         useselected = Toggle("Only selected faces",EVENT_NONE,12+pwadd+wadd,97+phadd+hadd,145,18,useselected.val,"If on, only selected faces will be modified")
1423         deselectvertices = Toggle("Deselect Selected",EVENT_NONE,162+pwadd+wadd,97+phadd+hadd,145,18,deselectvertices.val,"Deselects any selected vertex except for ones selected by \"Select Tops\"")
1424         
1425         #Protrusion properties
1426         glColor3f(0.0,0.0,0.0)
1427         glRasterPos2d(12+pwadd+wadd, 80+phadd+hadd)
1428         Text("Protrusion Properties:")
1429         minheight = Number("Min Height: ",EVENT_NONE,12+pwadd+wadd,57+phadd+hadd,145,18,minheight.val,-100.0,100.0,"Minimum height of any protrusion")
1430         maxheight = Number("Max Height: ",EVENT_NONE,162+pwadd+wadd,57+phadd+hadd,145,18,maxheight.val,-100.0,100.0,"Maximum height of any protrusion")
1431         mintaper = Number("Min Taper %: ",EVENT_NONE,12+pwadd+wadd,37+phadd+hadd,145,18,mintaper.val,0,100,"Minimum taper percentage of protrusion")
1432         maxtaper = Number("Max Taper %: ",EVENT_NONE,162+pwadd+wadd,37+phadd+hadd,145,18,maxtaper.val,0,100,"Maximum taper percentage of protrusion")
1433         glRasterPos2d(19+pwadd+wadd, 22+phadd+hadd)
1434         Text("Number of protrusions:")
1435         sub1 = Toggle("1",EVENT_NONE,12+pwadd+wadd,phadd+hadd,34,18,sub1.val,"One Protrusion")
1436         sub2 = Toggle("2",EVENT_NONE,48+pwadd+wadd,phadd+hadd,34,18,sub2.val,"Two Protrusions")
1437         sub3 = Toggle("3",EVENT_NONE,84+pwadd+wadd,phadd+hadd,34,18,sub3.val,"Three Protrusions")
1438         sub4 = Toggle("4",EVENT_NONE,120+pwadd+wadd,phadd+hadd,34,18,sub4.val,"Four Protrusions")
1439         glRasterPos2d(195+pwadd+wadd, 22+phadd+hadd)
1440         Text("Select tops of:")
1441         selface1 = Toggle("1",EVENT_NONE,165+pwadd+wadd,phadd+hadd,34,18,selface1.val,"Select the tip of the protrusion when it is created")
1442         selface2 = Toggle("2",EVENT_NONE,201+pwadd+wadd,phadd+hadd,34,18,selface2.val,"Select the tips of each protrusion when they are created")
1443         selface3 = Toggle("3",EVENT_NONE,237+pwadd+wadd,phadd+hadd,34,18,selface3.val,"Select the tips of each protrusion when they are created")
1444         selface4 = Toggle("4",EVENT_NONE,273+pwadd+wadd,phadd+hadd,34,18,selface4.val,"Select the tips of each protrusion when they are created")
1445         
1446         #Doodads
1447         colorbox(8+dwadd+wadd,175+dhadd+hadd,312+dwadd+wadd,dhadd-5+hadd)
1448         glColor3f(0.0,0.0,0.0)
1449         glRasterPos2d(12+dwadd+wadd, 165+dhadd+hadd)
1450         Text("Doodads:")
1451         dood1 = Toggle("1 Box",EVENT_NONE,12+dwadd+wadd,142+dhadd+hadd,45,18,dood1.val,"Creates a rectangular box")
1452         dood2 = Toggle("2 Box",EVENT_NONE,61+dwadd+wadd,142+dhadd+hadd,45,18,dood2.val,"Creates 2 side-by-side rectangular boxes")
1453         dood3 = Toggle("3 Box",EVENT_NONE,110+dwadd+wadd,142+dhadd+hadd,45,18,dood3.val,"Creates 3 side-by-side rectangular boxes")
1454         dood4 = Toggle("\"L\"",EVENT_NONE,164+dwadd+wadd,142+dhadd+hadd,45,18,dood4.val,"Creates a Tetris-style \"L\" shape")
1455         dood5 = Toggle("\"T\"",EVENT_NONE,213+dwadd+wadd,142+dhadd+hadd,45,18,dood5.val,"Creates a Tetris-style \"T\" shape")
1456         dood6 = Toggle("\"S\"",EVENT_NONE,262+dwadd+wadd,142+dhadd+hadd,45,18,dood6.val,"Creates a sort-of \"S\" or \"Z\" shape")
1457         dodoodads = Toggle("Make Doodads",EVENT_NONE,12+dwadd+wadd,120+dhadd+hadd,145,18,dodoodads.val,"Make Doodads?")
1458         doodadfacechange = Number("Face %: ",EVENT_NONE,162+dwadd+wadd,120+dhadd+hadd,145,18,doodadfacechange.val,0,100,"Percentage of faces that will gain doodads")
1459         seldoodad = Toggle("Select Doodads",EVENT_NONE,12+dwadd+wadd,100+dhadd+hadd,145,18,seldoodad.val,"Selects doodads when they are created")
1460         seldoodtop = Toggle("Only Select Tops",EVENT_NONE,162+dwadd+wadd,100+dhadd+hadd,145,18,seldoodtop.val,"Only Selects tops of doodads when\"Select Doodads\" is on")
1461         doodonselface = Toggle("Only selected faces",EVENT_NONE,12+dwadd+wadd,80+dhadd+hadd,145,18,doodonselface.val,"Only create doodads on selected faces")
1462         onprot = Toggle("Only on Protrusions",EVENT_NONE,162+dwadd+wadd,80+dhadd+hadd,145,18,onprot.val,"Only place doodads on protrusions")
1463         
1464         #Doodad Properties
1465         glColor3f(0.0,0.0,0.0)
1466         glRasterPos2d(12+dwadd+wadd, 63+dhadd+hadd)
1467         Text("Doodad Properties:")
1468         doodadminamount = Number("Min Amount: ",EVENT_NONE,12+dwadd+wadd,40+dhadd+hadd,145,18,doodadminamount.val,0,100,"Minimum number of doodads per face")
1469         doodadmaxamount = Number("Max Amount: ",EVENT_NONE,162+dwadd+wadd,40+dhadd+hadd,145,18,doodadmaxamount.val,0,100,"Maximum number of doodads per face")
1470         doodheightmin = Number("Min Height: ",EVENT_NONE,12+dwadd+wadd,20+dhadd+hadd,145,18,doodheightmin.val,0.0,100.0,"Minimum height of any doodad")
1471         doodheightmax = Number("Max Height: ",EVENT_NONE,162+dwadd+wadd,20+dhadd+hadd,145,18,doodheightmax.val,0.0,100.0,"Maximum height of any doodad")
1472         doodsizemin = Number("Min Size %: ",EVENT_NONE,12+dwadd+wadd,dhadd+hadd,145,18,doodsizemin.val,0.0,100.0,"Minimum size of any doodad in percentage of face")
1473         doodsizemax = Number("Max Size %: ",EVENT_NONE,162+dwadd+wadd,dhadd+hadd,145,18,doodsizemax.val,0.0,100.0,"Maximum size of any doodad in percentage of face")
1474         
1475         #Materials
1476         colorbox(8+mwadd+wadd,93+mhadd+hadd,312+mwadd+wadd,mhadd-5+hadd)
1477         glColor3f(0.0,0.0,0.0)
1478         glRasterPos2d(12+mwadd+wadd, 83+mhadd+hadd)
1479         Text("Materials:")
1480         glRasterPos2d(12+mwadd+wadd, 43+mhadd+hadd)
1481         Text("Assigned Material Indices:")
1482         assignNewMats = Toggle("Assign materials by part",EVENT_NONE,32+mwadd+wadd,60+mhadd+hadd,256,18,assignNewMats.val,"Otherwise, previous materials will be preserved")
1483         replProtSideIndex = Number("Protrusion Sides:",EVENT_NONE,12+mwadd+wadd,20+mhadd+hadd,145,18,replProtSideIndex.val,0,16,"Material index assigned to sides of protrusions")
1484         replProtTopIndex = Number("Protrusion Tops:",EVENT_NONE,162+mwadd+wadd,20+mhadd+hadd,145,18,replProtTopIndex.val,0,16,"Material index assigned to tops of protrusions")
1485         replDoodSideIndex = Number("Doodad Sides:",EVENT_NONE,12+mwadd+wadd,mhadd+hadd,145,18,replDoodSideIndex.val,0,16,"Material index assigned to sides of doodads")
1486         replDoodTopIndex = Number("Doodad Tops:",EVENT_NONE,162+mwadd+wadd,mhadd+hadd,145,18,replDoodTopIndex.val,0,16,"Material index assigned to tops and bottoms of doodads")
1487         
1488         #Global Parts
1489         colorbox(8+gwadd+wadd,35+ghadd+hadd,312+gwadd+wadd,ghadd-5+hadd)
1490         glColor3f(1.0,0.0,0.0)
1491         glRasterPos2d(12+gwadd+wadd,25+ghadd+hadd)
1492         messagetext = Text(errortext)
1493         glColor3f(0.0,0.0,0.0)
1494         makenewobject = Toggle("Copy Before Modifying",EVENT_NONE,162+gwadd+wadd,ghadd+hadd,145,18,makenewobject.val,"If selected, the original object will be copied before it is changed")
1495         Button("Discombobulate",EVENT_DISCOMBOBULATE,12+gwadd+wadd,ghadd+hadd,100,18)
1496         Button("Exit",EVENT_EXIT,120+gwadd+wadd,ghadd+hadd,30,18)
1497
1498 def event(evt, val):
1499         global wadd
1500         global hadd
1501         
1502         if (evt == RIGHTARROWKEY and val):
1503                 wadd = wadd + 20
1504                 Redraw(1)
1505         if (evt == LEFTARROWKEY and val):
1506                 wadd = wadd - 20
1507                 Redraw(1)
1508         if (evt == UPARROWKEY and val):
1509                 hadd = hadd + 20
1510                 Redraw(1)
1511         if (evt == DOWNARROWKEY and val):
1512                 hadd = hadd - 20
1513                 Redraw(1)
1514         if (evt == QKEY and not val): 
1515                 Exit()
1516
1517 def bevent(evt):
1518         
1519         #Protrusions
1520         global doprots
1521         global facechange
1522         global minheight
1523         global maxheight
1524         global sub1
1525         global sub2
1526         global sub3
1527         global sub4
1528         global mintaper
1529         global maxtaper
1530         global useselected
1531         global selface1
1532         global selface2
1533         global selface3
1534         global selface4
1535         global deselectvertices
1536         #global selectbyverts
1537         
1538         #Doodads
1539         global dodoodads
1540         global doodadfacechange
1541         global seldoodad
1542         global onprot
1543         global dood1
1544         global dood2
1545         global dood3
1546         global dood4
1547         global dood5
1548         global dood6
1549         global doodadminamount
1550         global doodadmaxamount
1551         global doodsizemin
1552         global doodsizemax
1553         global doodheightmin
1554         global doodheightmax
1555         global doodonselface
1556         global seldoodtop
1557         
1558         #Materials
1559         global assignNewMats
1560         global replProtSideIndex
1561         global replProtTopIndex
1562         global replDoodSideIndex
1563         global replDoodTopIndex
1564         
1565         #Global Settings
1566         global makenewobject
1567         global messagetext
1568         global errortext
1569         global EVENT_NONE,EVENT_DRAW,EVENT_EXIT
1570
1571         ######### Manages GUI events
1572         if evt==EVENT_EXIT: 
1573                 Exit()
1574         elif evt==EVENT_DISCOMBOBULATE:
1575                 Window.WaitCursor(1)
1576                 setProtrusionValues(doprots.val,facechange.val/100,minheight.val,maxheight.val,sub1.val,sub2.val,sub3.val,sub4.val,mintaper.val/100,maxtaper.val/100,useselected.val,selface1.val,selface2.val,selface3.val,selface4.val,deselectvertices.val)
1577                 setDoodadValues(dodoodads.val,doodadfacechange.val/100,seldoodad.val,onprot.val,dood1.val,dood2.val,dood3.val,dood4.val,dood5.val,dood6.val,doodadminamount.val,doodadmaxamount.val,doodsizemin.val/100,doodsizemax.val/100,doodheightmin.val,doodheightmax.val,doodonselface.val,seldoodtop.val)
1578                 setOtherValues(makenewobject.val,assignNewMats.val,replProtSideIndex.val,replProtTopIndex.val,replDoodSideIndex.val,replDoodTopIndex.val)
1579                 discombobulate()
1580                 Window.WaitCursor(0)
1581                 Blender.Redraw()
1582
1583 Register(draw, event, bevent)