Script kloputils:
[blender-staging.git] / release / scripts / kloputils.py
1 #!BPY
2
3 """ Registration info for Blender menus:
4 Name: 'Kloputils 3'
5 Blender: 234
6 Group: 'Wizards'
7 Tip: 'Set of object aligning, modifying tools'
8 """
9
10 # $Id$
11 #
12 ##################################################
13 #                                                #
14 #        KLOPUTILES v3.1 para Blender v2.33      #
15 #                   multilingue                  #
16 #                                                #
17 ##################################################
18 #               Pulsa Alt+P para ejecutar el script
19 #       Improved:       use of Mathutils
20 #       Feature:        actualize objects on ApplyLRS
21 #       Improved:       Project onto planes
22 #       Feature:        mover/rotar/escalar objetos al azar
23 #       Feature:        acercar objs al activo (fijo, abs, rel)
24 #       Improved:       Fit/Embrace
25 #       TODO: subdividir solo aristas seleccionadas (NO FUNCIONA)
26 #       TODO: Elipses
27 #
28 ##################################################
29 #
30 # --------------------------------------------------------------------------
31 # Kloputils by Carlos López (klopez)
32 # --------------------------------------------------------------------------
33 # ***** BEGIN GPL LICENSE BLOCK *****
34 #
35 # Copyright (C) 2004: Carlos López, http://klopes.tk
36 #
37 # This program is free software; you can redistribute it and/or
38 # modify it under the terms of the GNU General Public License
39 # as published by the Free Software Foundation; either version 2
40 # of the License, or (at your option) any later version.
41 #
42 # This program is distributed in the hope that it will be useful,
43 # but WITHOUT ANY WARRANTY; without even the implied warranty of
44 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
45 # GNU General Public License for more details.
46 #
47 # You should have received a copy of the GNU General Public License
48 # along with this program; if not, write to the Free Software Foundation,
49 # Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
50 #
51 # ***** END GPL LICENCE BLOCK *****
52 # --------------------------------------------------------------------------
53
54 import Blender
55 from Blender.Mathutils import *
56 from Blender.NMesh import *
57 from Blender.BGL import *
58 from Blender.Draw import *
59 from Blender.sys import *
60 from Blender.Noise import random
61
62 from math import *
63
64 #path=dirname(progname)
65 #path=[dirname(progname),'.blender','bpydata']
66 path=Blender.Get("datadir")
67 if not path:
68         errmsg = "\nNo bpydata/ dir found! It should be in Blender's home dir."
69         raise IOError, errmsg
70
71 sc=Blender.Scene.GetCurrent()
72 MId=Matrix([1,0],[0,1])
73 MId.resize4x4()
74
75 coo=['X','Y','Z']
76 l=PupMenu('Choose language%t|Espanol%x1|English%x2|Catala%x3')
77 Lang=['Espanol','English','Catala'][l-1]
78
79
80
81 #Buffer:
82 BfL=[0.,0.,0.]
83 BfR=[0.,0.,0.]
84 BfS=[1.,1.,1.]
85
86 pagina=Create(1)
87 creake=Create(1)
88 editake=Create(2)
89 moveke=Create(1)
90
91 numReal=Create(0.)
92 movekomo=Create(1)
93 menuLRS=Create(1)
94
95 ndiv=Create(3)
96 akeplano=Create(2)
97 akeplanoXYZ=Create(3)
98 enkedir=Create(3)
99
100 nLocX=Create(1.0)
101 nLocY=Create(2.0)
102 nLocZ=Create(3.0)
103 nRotX=Create(0.0)
104 nRotY=Create(0.0)
105 nRotZ=Create(0.0)
106 nSizX=Create(1.0)
107 nSizY=Create(1.0)
108 nSizZ=Create(1.0)
109 other=Create(1)
110
111 ang1=Create(0.0)
112 ang2=Create(30.0)
113 angulo=Create(30.0)
114 radio=Create(1.0)
115 pts=Create(10)
116 ctro=Create(0)
117 meTx=Create('Arc')
118
119 arcorden=Create(0)
120
121 CreaNew=Create(1)
122 Cuantos=Create(2)
123 toBuffer=Create(0)
124
125 menueqiX=Create(1)
126 sepX=Create(0.5)
127 aliX=Create(1)
128 menueqiY=Create(1)
129 sepY=Create(0.0)
130 aliY=Create(0)
131 menueqiZ=Create(2)
132 sepZ=Create(1.0)
133 aliZ=Create(0)
134
135 girX=Create(0)
136 incX=Create(0.0)
137 girY=Create(0)
138 incY=Create(0.0)
139 girZ=Create(1)
140 incZ=Create(5.0)
141
142 scaX=Create(1)
143 iScaX=Create(.5)
144 scaY=Create(1)
145 iScaY=Create(.5)
146 scaZ=Create(1)
147 iScaZ=Create(.5)
148
149 encX=Create(1)
150 encY=Create(0)
151 encZ=Create(0)
152 en2X=Create(1)
153 en2Y=Create(0)
154 en2Z=Create(0)
155
156
157 def CreaObjDeMalla(mm,o,nombre):
158         oo=Blender.Object.New('Mesh')
159         oo.link(mm)
160         oo.setName(nombre)
161         oo.LocX,oo.LocY,oo.LocZ=o.LocX,o.LocY,o.LocZ
162         oo.RotX,oo.RotY,oo.RotZ=o.RotX,o.RotY,o.RotZ
163         oo.SizeX,oo.SizeY,oo.SizeZ=o.SizeX,o.SizeY,o.SizeZ
164         sc.link(oo)
165         return oo
166
167 def rectang(x,y,l,h,r,g,b):
168         glColor3f(r,g,b)
169         glBegin(GL_POLYGON)
170         glVertex2i(x, y)
171         glVertex2i(l, y)
172         glVertex2i(l, h)
173         glVertex2i(x, h)
174         glEnd()
175
176 def SituaL(objeto,coord,X):
177         if coord==0:
178                 objeto.LocX=X
179         elif coord==1:
180                 objeto.LocY=X
181         elif coord==2:
182                 objeto.LocZ=X
183 def SituaR(objeto,coord,X):
184         if coord==0:
185                 objeto.RotX=X
186         elif coord==1:
187                 objeto.RotY=X
188         elif coord==2:
189                 objeto.RotZ=X
190 def SituaS(objeto,coord,X):
191         if coord==0:
192                 objeto.SizeX=X
193         elif coord==1:
194                 objeto.SizeY=X
195         elif coord==2:
196                 objeto.SizeZ=X
197
198 def mallaScan(o):
199         return o.getType()=='Mesh'
200
201 def Sistema(MM,D):
202         MM.invert()
203         return MatMultVec(MM,D)
204
205 def GloVrt((x,y,z),M):  # Devuelve (vector) coords globs de v
206         v=Vector([x,y,z,1])
207         v=VecMultMat(v,M)
208         v.resize3D()
209         return v
210
211 def Extremo(lista,M=MId):       # Devuelve extremos y pto medio de lista de vectores
212         o1=GloVrt(lista[0],M)
213         o2=GloVrt(lista[0],M)
214         for v in lista:
215                 va=Vector(list(v))
216                 va.resize4D()
217                 va=VecMultMat(va,M)
218                 for c in range(3):
219                         o1[c]=min(va[c],o1[c])
220                         o2[c]=max(va[c],o2[c])
221         return [o1,(o1+o2)/2,o2]
222
223 def Media(lista):       #Media
224         ctr=Vector([0,0,0])
225         for c in range(3):
226                 for l in lista: ctr[c]=ctr[c]+l[c]
227                 ctr[c]=ctr[c]/len(lista)
228         return ctr
229
230
231
232
233
234
235 def draw():
236         global pagina, toBuffer
237         glClearColor(0.4,0.2,0.2,0.0)
238         glClear(GL_COLOR_BUFFER_BIT)
239
240         pagina=Menu(menu0,98,20,370,170,28,pagina.val,"Select page")
241         toBuffer=Menu(menu4,80,200,370,20,15,toBuffer.val,"Copy values to the internal buffer")
242         glColor3f(0.6,0.6,0.2)
243         glRasterPos2i(198,388)
244         Text("Copy",'small')
245
246         if (pagina.val==1): draw1()
247         elif (pagina.val==2): draw2()
248         elif (pagina.val==3): draw3()
249         elif (pagina.val==4): draw4()
250
251         Button(menuExit,99,20,5,200,18)
252
253 def draw4():
254         global moveke,o
255         global aliX,aliY,aliZ,numReal,movekomo,menuLRS,menuPaste
256         
257
258         rectang(15,38,225,264,0.2,0.,0.)
259
260         moveke=Menu(menu5,98,10,248,200,20,moveke.val,"Modify the matrices of selected objects")
261         if moveke.val==2:
262                 aliX=Toggle("X",0, 50,160,45,30,aliX.val,"Activate modification in X")
263                 aliY=Toggle("Y",0, 95,160,45,30,aliY.val,"Activate modification in Y")
264                 aliZ=Toggle("Z",0,140,160,45,30,aliZ.val,"Activate modification in Z")
265                 menuLRS=Menu("Loc%x1|Rot%x2|Size%x3",98,20,110,50,20,menuLRS.val)
266         if moveke.val==1:
267                 movekomo=Menu(menu5a,98,20,225,200,20,movekomo.val,"Transformation type")
268                 m,M,tip= -100,100,"Quantity"
269         else:
270                 if menuLRS.val==1:
271                         m,M,tip= -1000, 1000, "Distance"
272                 elif menuLRS.val==2:
273                         m,M,tip= 0, 360, "Angle"
274                 elif menuLRS.val==3:
275                         m,M,tip= 0, 100, "Scale"
276         if moveke.val==1 and movekomo.val==3:   tip=tip+"Multiplier "
277         if moveke.val==2:       tip=tip+" (max)"
278
279         numReal=Number("",0,20,140,178,18,numReal.val,m,M,tip)
280         glColor3f(0.6,0.6,0.2)
281         glRasterPos2i(200,156)
282         Text("Paste",'small')
283         menuPaste=Menu("Paste...%t|LocX%x1|LocY%x2|LocZ%x3|\Loc\%x4|RotX%x5|RotY%x6|RotZ%x7|SizeX%x8|SizeY%x9|SizeZ%x10"
284 ,88,202,140,20,12,0,"Paste what?")
285         ok=Button("OK",50,80,110,80,20)
286
287
288 def draw3():
289         global editake,ndiv,akeplano,akeplanoXYZ,enkedir,ok
290         global other
291         global getlocrotsiz,ok,nLocX,nLocY,nLocZ
292         global nRotX,nRotY,nRotZ,nSizX,nSizY,nSizZ
293         global Cloc,Ploc,Crot,Prot,Csiz,Psiz
294
295         rectang(15,38,225,264,0.3,0.7,0.5)
296
297         editake=Menu(menu3,98,10,248,143,20,editake.val,"Mesh editing menu")
298         if(editake.val==1):
299                 ndiv=Number(menu3a,98,20,200,150,18,ndiv.val,2,1000)
300                 ok=Button("OK",20,172,200,32,18)
301         elif(editake.val==2):
302                 akeplano=Menu(menu3b,98,20,200,140,20,akeplano.val)
303                 if akeplano.val in [1,2]:
304                         akeplanoXYZ=Menu(menu3c,98,162,200,55,20,akeplanoXYZ.val) 
305                 enkedir=Menu(menu3d,98,20,100,140,20,enkedir.val)
306                 ok=Button("OK",40,162,100,55,20)
307         elif(editake.val==3):
308                 Button("P",87,192,222,30,18,"Paste buffer matrix")
309                 nLocX=Number("LocX:",98,20,200,170,19,nLocX.val,-1000.,1000.)
310                 nLocY=Number("LocY:",98,20,180,170,19,nLocY.val,-1000.,1000.)
311                 nLocZ=Number("LocZ:",98,20,160,170,19,nLocZ.val,-1000.,1000.)
312                 nRotX=Number("RotX:",98,20,140,170,19,nRotX.val,-1000.,1000.)
313                 nRotY=Number("RotY:",98,20,120,170,19,nRotY.val,-1000.,1000.)
314                 nRotZ=Number("RotZ:",98,20,100,170,19,nRotZ.val,-1000.,1000.)
315                 nSizX=Number("SizeX:",98,20, 80,170,19,nSizX.val,-1000.,1000.)
316                 nSizY=Number("SizeY:",98,20, 60,170,19,nSizY.val,-1000.,1000.)
317                 nSizZ=Number("SizeZ:",98,20, 40,170,19,nSizZ.val,-1000.,1000.)
318                 other=Toggle("Refresh others",98,20,222,170,18,other.val)
319                 oklocrotsiz=Button("OK",30,192,40,30,179)
320
321 def draw2():
322         global creake,ang1,ang2,angulo,radio,pts,ctro,meTx,arcorden
323
324         rectang(15,65,224,257,0.7,0.5,0.65)
325
326         creake=Menu(menu2,98,10,248,143,20,creake.val,"Object type")
327         if(creake.val==1):
328                 pts=Number(menu2a[0],98,20,115,80,18,pts.val,2,1000)
329                 ctro=Toggle(menu2a[1],98,102,115,50,18,ctro.val,"Close the arc with a vertex on its center")
330                 arcorden=Menu(menu2a[2]+"%t|1-2-3%x1|2-3-1%x2|3-1-2%x3",17,155,115,65,18,arcorden.val,"Vertices order (arc is created when this is chosen)")
331                 meTx=String("Object: ",98,20,70,200,22,meTx.val,30,"New object's name")
332         if(creake.val==2):
333                 ang1=Slider(menu2a[4],10,20,220,200,18,ang1.val,-360,360,0,"Initial angle in degrees")
334                 ang2=Slider(menu2a[5],11,20,200,200,18,ang2.val,ang1.val,ang1.val+360,0,"Final angle in degress")
335                 angulo=Slider(menu2a[6],12,20,170,200,18,angulo.val,-360,360,0,"Arc's angle in degress")
336                 radio=Slider(menu2a[7],13,20,140,200,18,radio.val,0,1,0)
337                 pts=Number(menu2a[8],14,20,115,80,18,pts.val,2,1000)
338                 ctro=Toggle(menu2a[9],15,102,115,50,18,ctro.val,"Close the arc with a vertex on its center")
339                 meTx=String(menu2a[10],16,20,70,200,22,meTx.val,30,"Name of the mesh to substitute")
340         if(creake.val==3):
341                 pts=Number("Pts:",98,20,115,80,18,pts.val,2,1000)
342                 ok=Button("OK",18,102,115,120,36)
343                 meTx=String("Object: ",98,20,70,200,22,meTx.val,30,"New object's name")
344                 
345 def draw1():
346         global menueqiX,sepX,aliX,menueqiY,sepY,aliY,menueqiZ,sepZ,aliZ
347         global girX,incX,girY,incY,girZ,incZ
348         global encX,encY,encZ,en2X,en2Y,en2Z
349         global scaX,scaY,scaZ,iScaX,iScaY,iScaZ
350         global CreaNew,Cuantos,toBuffer
351
352 ######################### ALINEACIONES #####################
353         rectang(5,167,254,292,0.7,0.5,0.65)
354         Button("C",81,194,270,25,18,"Copy")
355         Button("P",82,220,270,25,18,"Paste buffer Loc")
356
357         aliX=Toggle("X",0,10,270,30,18,aliX.val,"Activate X alignment")
358         menueqiX=Menu(menu1b,98,41,270,148,18,menueqiX.val,"Separation extremes")  
359         sepX=Number(menu1a[4],0,10,250,179,18,sepX.val,-1000,1000,"X separation distance")
360
361         aliY=Toggle("Y",0,10,230,30,18,aliY.val,"Activate Y alignment")
362         menueqiY=Menu(menu1b,98,41,230,148,18,menueqiY.val,"Separation extremes")  
363         sepY=Number(menu1a[4],0,10,210,179,18,sepY.val,-1000,1000,"Y separation distance")
364
365         aliZ=Toggle("Z",0,10,190,30,18,aliZ.val,"Activate Z alignment")
366         menueqiZ=Menu(menu1b,98,41,190,148,18,menueqiZ.val,"Separation extremes")  
367         sepZ=Number(menu1a[4],0,10,170,179,18,sepZ.val,-1000,1000,"Y separation distance")
368
369 ########################## GIROS ##############################
370         rectang(5,97,254,162,0.67,0.54,0.1)
371         Button("C",83,194,140,25,18,"Copy")
372         Button("P",84,220,140,25,18,"Paste buffer Rot")
373
374         girX=Toggle("X",0,10,140,30,18,girX.val,"Increment RotX values")
375         incX=Number("X: ",0,42,140,147,18,incX.val,-180,180,"X increment in degress")
376         girY=Toggle("Y",0,10,120,30,18,girY.val,"Increment RotY values")
377         incY=Number("Y: ",0,42,120,147,18,incY.val,-180,180,"Y increment in degrees")
378         girZ=Toggle("Z",0,10,100,30,18,girZ.val,"Increment RotZ values")
379         incZ=Number("Z: ",0,42,100,147,18,incZ.val,-180,180,"Z increment in degrees")
380
381 ######################### ESCALADOS ##########################
382         rectang(5,27,254,92,0.27,0.54,0.4)
383         Button("C",85,194,70,25,18,"Copy")
384         Button("P",86,220,70,25,18,"Paste buffer Sca")
385
386         scaX=Toggle("X",0,10,70,30,18,scaX.val)
387         iScaX=Number(menu1a[7],0,42,70,147,18,iScaX.val,-180,180,"X scale increment")
388         scaY=Toggle("Y",0,10,50,30,18,scaY.val)
389         iScaY=Number(menu1a[7],0,42,50,147,18,iScaY.val,-180,180,"Y scale increment")
390         scaZ=Toggle("Z",0,10,30,30,18,scaZ.val)
391         iScaZ=Number(menu1a[7],0,42,30,147,18,iScaZ.val,-180,180,"Z scale increment")
392 ########################## ENCAJES ##########################
393         rectang(5,317,254,363,0.1,0.5,0.6)
394
395         encX=Toggle(menu1a[0]+"X",0,10,340,43,18,encX.val,"Move object in X")
396         encY=Toggle(menu1a[0]+"Y",0,55,340,43,18,encY.val,"Move object in Y")
397         encZ=Toggle(menu1a[0]+"Z",0,100,340,43,18,encZ.val,"Move object in Z")
398
399         en2X=Toggle(menu1a[1]+"X",0,10,320,43,18,en2X.val,"Adjust X size")
400         en2Y=Toggle(menu1a[1]+"Y",0,55,320,43,18,en2Y.val,"Adjust Y size")
401         en2Z=Toggle(menu1a[1]+"Z",0,100,320,43,18,en2Z.val,"Adjust Z size")
402 ######################################################
403         CreaNew=Toggle(menu1a[8],98,10,295,129,18,CreaNew.val,"Create new objects or just modify selected?")
404         if CreaNew.val:
405                 Cuantos=Number('',98,139,295,30,18,Cuantos.val,1,999,"Number of copies")
406         Button(menu1a[5],1,190,170,60,98,"Align")               #Alinea
407         Button(menu1a[6],2,190,100,60,38,"Rotate")              #Gira
408         Button(menu1a[9],3,190,30,29,38,"Scale +")              #Escala+
409         Button(menu1a[10],7,220,30,30,38,"Scale *")             #Escala*
410         Button(menu1a[2],6,147,320,45,38,"Fit active in one or two others using max limits (creates new object)")               #Encaja
411         Button(menu1a[3],5,195,320,55,38,"Fit active in one or two others using min limits (creates new object)")               #Abarca
412
413 def HaceArco(pts,ang1,angul,ctro,R):
414         me=New()
415         for c in range(pts):
416                 alfa=(ang1+angul*c/(pts-1))*pi/180
417                 if (c): v1=v
418                 v=Vert()
419                 v.co[0]=R*cos(alfa)
420                 v.co[1]=R*sin(alfa)
421                 v.co[2]=0
422                 me.verts.append(v)
423                 if (c):
424                         f=Face()
425                         f.v=[v1]
426                         f.v.append(v)
427                         me.faces.append(f)
428                 if (c==0): v0=v
429         if(ctro):
430                 v1=v
431                 v=Vert()
432                 me.verts.append(v)
433                 f=Face()
434                 f.v=[v1,v]
435                 me.faces.append(f)
436                 f=Face()
437                 f.v=[v,v0]
438                 me.faces.append(f)
439         return me
440
441 def Exec_align(os,c,ali,eqi,sep):
442         coor=os[0].loc[c]
443         ademas=0
444         flag=0
445         for o in os:
446                 m=o.data     #Maya del objeto
447                 ctr=[0,0,0]
448                 if(eqi[c]==2):
449                         for d in range(3): ctr[d]=Extremo(m.verts,o.matrix)[1][d]
450                 elif(eqi[c]==3 or eqi[c]==6):
451                         ctr=Extremo(m.verts,o.matrix)[0]
452                 elif(eqi[c]==4):
453                         ctr=Extremo(m.verts,o.matrix)[2]
454                 elif(eqi[c]==5):
455                         ctr=Media(m.verts)
456                         ctr=GloVrt(ctr,o.matrix)
457
458                 if (eqi[c]>1): ademas=ctr[c]-o.loc[c]
459                 if (flag):      coor=coor-ademas
460                 else:   flag=1
461                 SituaL(o,c,coor)
462                 if(eqi[c]==6): ademas=Extremo(m.verts,o.matrix)[2][c]-o.loc[c]
463                 coor=coor+sep[c]+ademas
464
465 def event(evt,val):
466         if (evt==ESCKEY and not val):
467                 print "Bye..."
468                 Exit()
469
470 #@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@22#
471
472 def bevent(evt):
473         global me
474         global BfL,BfR,BfS
475         #print "Event:",evt
476         os=Blender.Object.GetSelected()
477         if os:
478                 oa=os[0]
479
480         ##########MENU BUFFER
481         if evt==80:
482                 copi=toBuffer.val
483                 if copi==1:
484                         if len(os)==2:
485                                 for c in range(3): BfL[c]=os[1].loc[c]-os[0].loc[c]
486                                 print "Copied to Loc buffer:",BfL
487                         else:
488                                 PupMenu('ERROR%t|Selecciona 2 objetos|Just select 2 objects!')
489                 if copi==2:
490                         if len(os)==2:
491                                 BfL=3*[sqrt((os[1].loc[0]-os[0].loc[0])**2+
492 (os[1].loc[1]-oa.loc[1])**2+
493 (os[1].loc[2]-oa.loc[2])**2)]
494                                 print "Copied to Loc buffer:",BfL
495                         else:
496                                 PupMenu('ERROR%t|Selecciona 2 objetos|Just select 2 objects!')
497                 if copi==3:
498                         if len(os)==2:
499                                 for c in range(3): BfR[c]=os[1].rot[c]-oa.rot[c]
500                                 print "Copied to Rot buffer:",BfR
501                         else:
502                                 PupMenu('ERROR%t|Selecciona 2 objetos|Just select 2 objects!')
503                 if copi==4 and len(os):
504                         lista=[]
505                         for o in os: lista.append(o.loc)
506                         BfL=Media(lista)
507                         print "Copied to Loc buffer:",BfL
508                         lista=[]
509                         for o in os: lista.append(o.rot)
510                         BfR=Media(lista)
511                         for c in range(3): BfR[c]*=180/pi
512                         print "Copied to Rot buffer:",BfR
513                         lista=[]
514                         for o in os: lista.append(o.size)
515                         BfS=Media(lista)
516                         print "Copiado to Size buffer:",BfS
517                 if copi==5:
518                         print"\n_Buffer contents_\n"
519                         for c in range(3): print "Loc",coo[c]," :",BfL[c]
520                         for c in range(3): print "Rot",coo[c]," :",BfR[c]
521                         for c in range(3): print "Size",coo[c],":",BfS[c]
522                         print "______________________"
523
524         ##########COPY
525         if evt==81:
526                 X       =[sepX.val,sepY.val,sepZ.val]
527                 ali     =[aliX.val,aliY.val,aliZ.val]
528                 for c in range(3):
529                         if ali[c]:
530                                 BfL[c]=X[c]
531                                 print"Copying to Loc buffer",coo[c],"value", BfL[c]
532
533         if evt==83:
534                 X       =[incX.val,incY.val,incZ.val]
535                 ali     =[girX.val,girY.val,girZ.val]
536                 for c in range(3):
537                         if ali[c]:
538                                 BfR[c]=X[c]
539                                 print"Copying to Rot buffer",coo[c],"value", BfR[c]
540
541         if evt==85:
542                 X       =[iScaX.val,iScaY.val,iScaZ.val]
543                 ali     =[scaX.val,scaY.val,scaZ.val]
544                 for c in range(3):
545                         if ali[c]:
546                                 BfS[c]=X[c]
547                                 print"Copying to Size buffer",coo[c],"value", BfS[c]
548
549         ##########PASTE
550         if evt==82:
551                 X       =[sepX.val,sepY.val,sepZ.val]
552                 ali     =[aliX.val,aliY.val,aliZ.val]
553                 for c in range(3):
554                         if ali[c]:
555                                 X[c]=BfL[c]
556                                 print"Retrieving from buffer Loc",coo[c],"value", BfL[c]
557                 sepX.val,sepY.val,sepZ.val=X
558
559         if evt==84:
560                 X       =[incX.val,incY.val,incZ.val]
561                 ali     =[girX.val,girY.val,girZ.val]
562                 for c in range(3):
563                         if ali[c]:
564                                 X[c]=BfR[c]
565                                 print"Retrieving from buffer Rot",coo[c],"value", BfR[c]
566                 incX.val,incY.val,incZ.val=X
567
568         if evt==86:
569                 X       =[iScaX.val,iScaY.val,iScaZ.val]
570                 ali     =[scaX.val,scaY.val,scaZ.val]
571                 for c in range(3):
572                         if ali[c]:
573                                 X[c]=BfS[c]
574                                 print"Retrieving from buffer Size",coo[c],"value", BfS[c]
575                 iScaX.val,iScaY.val,iScaZ.val=X
576
577         if evt==87:
578                 print"Retrieving <ApplyLocRotSize> matrix"
579                 nLocX.val,nLocY.val,nLocZ.val=BfL
580                 nRotX.val,nRotY.val,nRotZ.val=BfR
581                 nSizX.val,nSizY.val,nSizZ.val=BfS
582
583         if evt==88:
584                 print"Retrieving float number to button"
585                 x=[BfL[0],BfL[1],BfL[2],
586 sqrt(BfL[0]**2+BfL[1]**2+BfL[2]**2),
587 BfR[0],BfR[1],BfR[2],
588 BfS[0],BfS[1],BfS[2]]
589                 numReal.val = x[menuPaste.val-1]
590
591         if evt==50:
592                 if moveke.val==1:                               #               MOVER HACIA EL O.ACTIVO
593                         oaV=Vector(list(oa.loc))
594                         for o in os[1:]:
595                                 oV=Vector(list(o.loc))
596                                 v=oV-oaV                                                                        #Calculo del vector final
597                                 d=sqrt(DotVecs(v,v))
598                                 v1=v*numReal.val
599                                 if movekomo.val==1:
600                                         v1 = oaV+v1/d
601                                 elif movekomo.val==2:
602                                         v1 = oV+v1/d
603                                 elif movekomo.val==3:
604                                         v1 = oaV+v1
605                                 o.setLocation(v1)
606
607                 if moveke.val==2:                                       #       MOVER AL AZAR
608                         n=numReal.val
609                         if menuLRS.val==2:
610                                 n=n*pi/180
611                         for o in os:
612                                 v1=Vector([
613 aliX.val * (2*n*random() - n),
614 aliY.val * (2*n*random() - n),
615 aliZ.val * (2*n*random() - n)])
616
617                                 if menuLRS.val==1:                                                      #Uso del vector final
618                                         oV=Vector(list(o.loc))
619                                         o.setLocation(oV+v1)
620                                 elif menuLRS.val==2:
621                                         v=Vector(list(o.getEuler()))+v1
622                                         o.setEuler(v)
623                                 else:
624                                         oV=Vector(list(o.size))
625                                         o.setSize(oV+v1)
626
627         if evt==40:                             #                                       PROYECTA EN PLANOS
628                 n=Vector([0.,0.,0.])
629
630                 numObjs=-len(os)+1
631                 if akeplano.val==1:     #plano global
632                         n[akeplanoXYZ.val-1]=1.
633                         numObjs=-len(os)
634                 elif akeplano.val==2: n=oa.matrix[akeplanoXYZ.val-1] #plano local
635                 n.resize3D()
636
637                 d=Vector([0.,0.,0.])
638                 if enkedir.val<4: d[enkedir.val-1]=1.   #direccion global
639                 elif enkedir.val==4: d=n                                #direc. ortog. al plano
640
641                 n1 = Vector([-n[2],n[0],n[1]])
642                 N1 = CrossVecs (n,n1)
643                 N2 = CrossVecs (n,N1)   
644                 n.normalize(), N1.normalize(), N2.normalize()
645                 #print"productos escalares (deben ser 0):",DotVecs(N1,n),DotVecs(N2,n),DotVecs(N2,N1)
646                 p=oa.loc
647                 N=Matrix(
648         [N1[0],N1[1],N1[2],0],
649         [N2[0],N2[1],N2[2],0],
650         [ n[0], n[1], n[2],0],
651         [ p[0], p[1], p[2],1])
652                 NI=CopyMat(N)
653                 NI.invert()
654
655                 dN=VecMultMat(d,NI.rotationPart())
656                 if dN[2]==0:
657                         PupMenu('Error%t|Operacion no permitida: la direccion esta en el plano%x1|Illegal operation: plane contains direction%x2')
658                         return
659                 print"Absolute direction:",d,"\nPlane normal:",n,"\nRelative direction:",dN
660                 for o in filter(mallaScan,os[numObjs:]):
661                         me=Blender.NMesh.GetRawFromObject(o.name)
662                         M=o.matrix
663                         for v in me.verts:
664                                 v0=Vector([v[0],v[1],v[2]])
665                                 v0=GloVrt(v0,M)
666                                 v0.resize4D()
667                                 v0=VecMultMat(v0,NI)
668                                 v[0] = v0[0] - v0[2]/dN[2] * dN[0]
669                                 v[1] = v0[1] - v0[2]/dN[2] * dN[1]
670                                 v[2] = 0
671                         oo=Blender.Object.New('Mesh')
672                         oo.setName(o.name+'Proy')
673                         oo.link(me)
674                         sc.link(oo)
675                         oo.setMatrix(N)
676
677         if(evt>9 and evt<17):   #       HACE ARCO INTERACTIV.
678                 GetRaw(meTx.val)
679                 if(evt==11): angulo.val=ang2.val-ang1.val
680                 if(evt==12 or evt==10): ang2.val=(ang1.val+angulo.val)
681                 m=HaceArco(pts.val,ang1.val,angulo.val,ctro.val,radio.val)
682                 PutRaw(m,meTx.val)
683
684         if evt in [17,18]:      # HACE ARCO CON 3 PTS.
685                 o=oa
686                 m=o.getData()
687                 M=o.matrix
688                 if arcorden.val==1 or evt==18:
689                         p1,p2,p3=m.verts[0],m.verts[1],m.verts[2]
690                 elif arcorden.val==2:
691                         p1,p2,p3=m.verts[1],m.verts[2],m.verts[0]
692                 elif arcorden.val==3:
693                         p1,p2,p3=m.verts[2],m.verts[0],m.verts[1]
694
695                 p1=GloVrt(p1,M)
696                 p2=GloVrt(p2,M)
697                 p3=GloVrt(p3,M)
698
699                 print "Points:\n ",p1,p2,p3
700                 
701                 p21=p2-p1
702                 p31=p3-p1
703
704                 M1=CrossVecs(p21,p31)
705
706                 D=DotVecs(M1,p1)
707                 D2=DotVecs(p21,p1+p2)/2
708                 D3=DotVecs(p31,p1+p3)/2
709
710                 SS=Matrix(
711                 [M1[0],M1[1],M1[2]],
712                 [p21[0],p21[1],p21[2]],
713                 [p31[0],p31[1],p31[2]],
714                 )
715                 O=Sistema(SS,Vector([D,D2,D3]))
716                 
717                 v2=p2-O
718                 b3=p3-O
719                 vN=CrossVecs(v2,b3)
720                 v3=CrossVecs(v2,vN)
721
722                         #Terna ortogonal: v2,vN,v3
723                 R=sqrt(DotVecs(v2,v2))
724                 RN=sqrt(DotVecs(vN,vN))
725                 R3=sqrt(DotVecs(v3,v3))
726
727                 if evt==18: angul=2*pi
728                 else: angul=AngleBetweenVecs(v2,b3)
729
730                 print "Center:",O
731                 print "Radius :",R,"RN:",RN,"R3:",R3
732                 print "Angle:",angul
733                 M2=Matrix([v2[0]/R,v2[1]/R,v2[2]/R],[-v3[0]/R3,-v3[1]/R3,-v3[2]/R3],[vN[0]/RN,vN[1]/RN,vN[2]/RN])
734
735                 arco=HaceArco(pts.val,0,angul,ctro.val,1)
736                 oo=CreaObjDeMalla(arco,o,meTx.val+'.In')
737                 EU=M2.toEuler()
738                 EU=EU[0]*pi/180,EU[1]*pi/180,EU[2]*pi/180
739                 oo.RotX,oo.RotY,oo.RotZ=EU[0],EU[1],EU[2]
740                 oo.setLocation(O)
741                 oo.setSize(R,R,R)
742
743                 if evt==17:
744                         angul=angul-360
745                         arco=HaceArco(pts.val,0,angul,ctro.val,1)
746                         oo=CreaObjDeMalla(arco,o,meTx.val+'.Ex')
747                         oo.RotX,oo.RotY,oo.RotZ=EU[0],EU[1],EU[2]
748                         oo.setLocation(O)
749                         oo.setSize(R,R,R)
750
751         if(evt==20): # SUBDIVIDE
752                 for o in filter(mallaScan,os):
753                         m=o.data
754                         mm=New()
755                         mm.name=m.name+'.Subdiv'
756                         for v in m.verts: mm.verts.append(v)
757                         N=ndiv.val
758                         NV=len(m.verts)-1
759                         for k1 in range(NV+1):
760                                 v1=m.verts[k1]
761                                 for k2 in range(NV-k1):
762                                         v2=m.verts[NV-k2]
763                                         for f in m.faces:
764                                                 if (v1 in f.v) and (v2 in f.v): #       SI...
765                                                         dif=abs(f.v.index(v2)-f.v.index(v1))
766                                                         if dif==1 or dif==len(f.v)-1: #...entonces f contiene la arista v1-v2
767                                                                 v=v1
768                                                                 for K in range(N):
769                                                                         cara=Face()
770                                                                         cara.v.append(v)
771                                                                         if K<N-1:
772                                                                                 v=Vert()
773                                                                                 for c in range(3): v.co[c]=(v1.co[c]*(N-K-1)+v2.co[c]*(K+1))/N
774                                                                                 mm.verts.append(v)
775                                                                         elif K==N-1: v=v2
776                                                                         cara.v.append(v)
777                                                                         mm.faces.append(cara)
778                                                         break #para que no se repitan aristas comunes a varias caras
779                         CreaObjDeMalla(mm,o,o.name+'.Subdiv')
780
781         if evt==30:                                             # APLICA LOC.ROT.SIZE
782                 for o in filter(mallaScan,os):
783                         M=o.matrix
784
785                         eu=Euler([nRotX.val,nRotY.val,nRotZ.val])
786                         Mr=eu.toMatrix()
787                         Mr.resize4x4()
788                         Mt=TranslationMatrix(Vector([nLocX.val,nLocY.val,nLocZ.val]))
789
790                         o.setMatrix(Mr*Mt)
791                         o.setSize(nSizX.val,nSizY.val,nSizZ.val)
792                         MI=o.getInverseMatrix()
793                         P=M*MI
794                         maya=o.getData()
795                         for v in maya.verts:
796                                 w=list(VecMultMat(Vector([v[0],v[1],v[2],1]),P))
797                                 for c in range(3):      v[c]=w[c]/o.size[c]
798
799                         maya.update()
800                         if other.val:
801                                 P.invert()
802                                 for oo in Blender.Object.Get():
803                                         if oo.data.name==maya.name and o!=oo:
804                                                 N=oo.getMatrix()
805                                                 oo.setMatrix(P*N)
806                                                 oo.setSize(oo.SizeX*nSizX.val,oo.SizeY*nSizY.val,oo.SizeZ*nSizZ.val)
807
808         if((evt==5 or evt==6) and len(os)):   #  ENCAJA-ABARCA
809                 enc=[encX.val,encY.val,encZ.val]
810                 en2=[en2X.val,en2Y.val,en2Z.val]
811                 me=GetRaw(oa.data.name)
812                 meVs=me.verts
813                 for v in meVs:
814                         w=GloVrt(v,oa.matrix)
815                         for c in range(3):
816                                 v[c]=w[c]
817                 for c in range(3):
818                         if en2[c] or enc[c]:
819                                 if (len(os)>1):
820                                         n1=Extremo(os[1].data.verts,os[1].matrix)[0][c]
821                                         m1=Extremo(os[1].data.verts,os[1].matrix)[2][c]
822                                 if (len(os)>2):
823                                         n2=Extremo(os[2].data.verts,os[2].matrix)[0][c]
824                                         m2=Extremo(os[2].data.verts,os[2].matrix)[2][c]
825                                 n0=Extremo(meVs)[0] [c]
826                                 m0=Extremo(meVs)[2] [c]
827                                 ancho=m0-n0
828                                 pm=(m0+n0)/2
829                                 if (len(os)==1): n1,m1=n0,m0
830                                 if (len(os)<3): n2,m2=n1,m1
831                                 if (n2<n1):
832                                         t,s=n2,m2
833                                         n2,n1,m2,m1=n1,t,m1,s
834                                 print coo[c],n0,m0,n1,m1,n2,m2
835                         for v in meVs:
836                                 A , factor = 0. , 1.
837                                 if enc[c]:
838                                         if evt==5:      pm2=(n2+m1)/2
839                                         else:           pm2=(m2+n1)/2
840                                         v[c]+= pm2-pm
841                                 if en2[c] and ancho:
842                                         if evt==5:      factor=(n2-m1)/ancho
843                                         else:           factor=(m2-n1)/ancho
844                                         v[c]=pm2+(v[c]-pm2)*factor
845                 PutRaw(me)
846
847         elif (evt==1 and len(os)):
848                 ali=[aliX.val,aliY.val,aliZ.val]
849                 eqi=[menueqiX.val,menueqiY.val,menueqiZ.val]
850                 sep=[sepX.val,sepY.val,sepZ.val]
851                 if CreaNew.val:
852                         for o in filter(mallaScan,os):
853                                 newos=[o]
854                                 for i in range(Cuantos.val):
855                                         newo=CreaObjDeMalla(o.getData(),o,o.name)
856                                         newos.append(newo)
857                                         for c in range(3):
858                                                 if (ali[c]):
859                                                         Exec_align(newos,c,ali,eqi,sep)
860                 else:
861                         for c in range(3):
862                                 if (ali[c]):
863                                         Exec_align(filter(mallaScan,os),c,ali,eqi,sep)
864         elif (evt==2 and len(os)):
865                 gir=[girX.val,girY.val,girZ.val]
866                 inc=[incX.val,incY.val,incZ.val]
867                 if CreaNew.val:
868                         for o in filter(mallaScan,os):
869                                 newos=[o]
870                                 for i in range(Cuantos.val):
871                                         newo=CreaObjDeMalla(o.getData(),o,o.name)
872                                         newos.append(newo)
873                                         for c in range(3):
874                                                 valor=o.rot[c]
875                                                 for oo in newos:
876                                                         if (gir[c]):
877                                                                 SituaR(oo,c,valor)
878                                                                 valor=valor+inc[c]*pi/180
879                 else:
880                         for c in range(3):
881                                 if (gir[c]):
882                                         valor=oa.rot[c]
883                                         for o in os:
884                                                 SituaR(o,c,valor)
885                                                 valor=valor+inc[c]*pi/180
886                                 
887         elif evt in [3,7] and len(os):
888                 sca=[scaX.val,scaY.val,scaZ.val]
889                 inc=[iScaX.val,iScaY.val,iScaZ.val]
890                 if CreaNew.val:
891                         for o in filter(mallaScan,os):
892                                 newos=[o]
893                                 for i in range(Cuantos.val):
894                                         newo=CreaObjDeMalla(o.getData(),o,o.name)
895                                         newos.append(newo)
896                                         for c in range(3):
897                                                 valor=o.size[c]
898                                                 for oo in newos:
899                                                         if (sca[c]):
900                                                                 SituaS(oo,c,valor)
901                                                                 if evt==3: valor=valor+inc[c]
902                                                                 if evt==7: valor=valor*inc[c]
903                 else:
904                         for c in range(3):
905                                 if (sca[c]):
906                                         valor=oa.size[c]
907                                         for o in os:
908                                                 SituaS(o,c,valor)
909                                                 if evt==3: valor=valor+inc[c]
910                                                 if evt==7: valor=valor*inc[c]
911
912         elif (evt==99): Exit()
913         Blender.Redraw()
914
915 #file=open(path+dirsep+'KUlang.txt','r')
916 #file=open(dirsep.join(path)+dirsep+'KUlang.txt','r')
917
918 file=open(join(path,'KUlang.txt'),'r')
919 fich=file.readlines()
920 print "\n\nKloputils",fich[0]
921 for i in range(len(fich)):
922         if fich[i]==Lang+'\012': break
923 print "Language:",fich[i]
924 menuExit=fich[i+1]#Sale prog.
925 menu0=fich[i+2]#Pral.
926 J=int(fich[i+3])#Alinea:botones
927 menu1a=[]
928 for j in range(J): menu1a.append(fich[i+j+4])
929 i=i+J
930 menu1b=fich[i+4]#Alinea:menu separa
931 menu2=str(fich[i+5])#Crea:menu
932 J=int(fich[i+6])#Crea:botones
933 menu2a=[]
934 for j in range(J): menu2a.append(fich[i+j+7])
935 i=i+J
936 menu3=fich[i+7]#Modif:menu
937 menu3a=fich[i+8]#Modif:"partes
938 menu3b=fich[i+9]#Modif:menu plano
939 menu3c=fich[i+10]#Modf:"Actua...
940 menu3d=fich[i+11]#Modif:menu dire
941 menu3e=fich[i+12]#Modf:"Captura
942 menu4=fich[i+13]#Buffer
943 menu5=fich[i+14]#ModifObjs
944 menu5a=fich[i+15]
945
946 file.close
947
948 Register(draw,event,bevent)