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