6db681d9d9fe55f596d4aa541ca526e943934673
[blender.git] / source / blender / src / editobject.c
1 /**
2  * $Id$
3  *
4  * ***** BEGIN GPL/BL DUAL LICENSE BLOCK *****
5  *
6  * This program is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public License
8  * as published by the Free Software Foundation; either version 2
9  * of the License, or (at your option) any later version. The Blender
10  * Foundation also sells licenses for use in proprietary software under
11  * the Blender License.  See http://www.blender.org/BL/ for information
12  * about this.
13  *
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17  * GNU General Public License for more details.
18  *
19  * You should have received a copy of the GNU General Public License
20  * along with this program; if not, write to the Free Software Foundation,
21  * Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
22  *
23  * The Original Code is Copyright (C) 2001-2002 by NaN Holding BV.
24  * All rights reserved.
25  *
26  * The Original Code is: all of this file.
27  *
28  * Contributor(s): none yet.
29  *
30  * ***** END GPL/BL DUAL LICENSE BLOCK *****
31  */
32
33 /** 
34  * Theorie: (matrices) A x B x C == A x ( B x C x Binv) x B
35  * ofwel: OB x PAR x EDIT = OB x (PAR x EDIT x PARinv) x PAR
36  */
37
38 #include <stdlib.h>
39 #include <string.h>
40 #include <math.h>
41
42 #ifdef HAVE_CONFIG_H
43 #include <config.h>
44 #endif
45
46 #ifndef WIN32
47 #include <unistd.h>
48 #else
49 #include <io.h>
50 #include "BLI_winstuff.h"
51 #endif
52 #include "MEM_guardedalloc.h"
53 #include "PIL_time.h"
54
55 #include "BMF_Api.h"
56
57
58 #include "IMB_imbuf_types.h"
59
60 #include "DNA_camera_types.h"
61 #include "DNA_curve_types.h"
62 #include "DNA_effect_types.h"
63 #include "DNA_ika_types.h"
64 #include "DNA_image_types.h"
65 #include "DNA_ipo_types.h"
66 #include "DNA_key_types.h"
67 #include "DNA_lamp_types.h"
68 #include "DNA_lattice_types.h"
69 #include "DNA_mesh_types.h"
70 #include "DNA_meta_types.h"
71 #include "DNA_object_types.h"
72 #include "DNA_scene_types.h"
73 #include "DNA_screen_types.h"
74 #include "DNA_texture_types.h"
75 #include "DNA_view3d_types.h"
76 #include "DNA_world_types.h"
77 #include "DNA_userdef_types.h"
78 #include "DNA_property_types.h"
79 #include "DNA_vfont_types.h"
80 #include "DNA_constraint_types.h"
81
82 #include "BLI_blenlib.h"
83 #include "BLI_arithb.h"
84 #include "BLI_editVert.h"
85 #include "BLI_ghash.h"
86
87 #include "BKE_nla.h"
88 #include "BKE_utildefines.h"
89 #include "BKE_anim.h"
90 #include "BKE_blender.h"
91 #include "BKE_curve.h"
92 #include "BKE_displist.h"
93 #include "BKE_effect.h"
94 #include "BKE_font.h"
95 #include "BKE_global.h"
96 #include "BKE_ika.h"
97 #include "BKE_ipo.h"
98 #include "BKE_key.h"
99 #include "BKE_lattice.h"
100 #include "BKE_library.h"
101 #include "BKE_main.h"
102 #include "BKE_material.h"
103 #include "BKE_mball.h"
104 #include "BKE_mesh.h"
105 #include "BKE_object.h"
106 #include "BKE_property.h"
107 #include "BKE_sca.h"
108 #include "BKE_scene.h"
109 #include "BKE_subsurf.h"
110 #include "BKE_texture.h"
111 #include "BKE_booleanops.h"
112
113 #include "BIF_gl.h"
114 #include "BIF_graphics.h"
115 #include "BIF_interface.h"
116 #include "BIF_mywindow.h"
117 #include "BIF_toolbox.h"
118 #include "BIF_screen.h"
119 #include "BIF_space.h"
120 #include "BIF_toets.h"
121 #include "BIF_butspace.h"
122 #include "BIF_editdeform.h"
123 #include "BIF_editfont.h"
124 #include "BIF_editika.h"
125 #include "BIF_editlattice.h"
126 #include "BIF_editmesh.h"
127 #include "BIF_editoops.h"
128 #include "BIF_editview.h"
129 #include "BIF_editarmature.h"
130 #include "BIF_resources.h"
131
132 #include "BSE_edit.h"
133 #include "BSE_editaction.h"
134 #include "BSE_editipo.h"
135 #include "BSE_filesel.h"        /* For activate_databrowse() */
136 #include "BSE_view.h"
137 #include "BSE_trans_types.h"
138 #include "BSE_editipo_types.h"
139
140 #include "BDR_vpaint.h"
141 #include "BDR_editmball.h"
142 #include "BDR_editobject.h"
143 #include "BDR_drawobject.h"
144 #include "BDR_editcurve.h"
145
146 #include "render.h"
147 #include <time.h>
148 #include "mydevice.h"
149 #include "nla.h"
150
151 #include "blendef.h"
152
153 #include "BKE_constraint.h"
154 #include "BIF_editconstraint.h"
155
156 #include "BKE_action.h"
157 #include "DNA_action_types.h"
158 #include "BKE_armature.h"
159 #include "DNA_armature_types.h"
160 #include "BIF_poseobject.h"
161
162 /*  extern Lattice *copy_lattice(Lattice *lt); */
163 extern ListBase editNurb;
164 extern ListBase editelems;
165
166 TransOb *transmain= 0;
167 TransVert *transvmain= 0;
168 int tottrans=0, transmode=0;    /* 1: texspace */
169
170 float prop_size= 1.0;
171 int prop_mode= 0;
172 float prop_cent[3];
173
174 /* used in editipo, editcurve and here */
175 #define BEZSELECTED(bezt)   (((bezt)->f1 & 1) || ((bezt)->f2 & 1) || ((bezt)->f3 & 1))
176
177 #define TRANS_TEX       1
178
179 #define KEYFLAG_ROT             0x00000001
180 #define KEYFLAG_LOC             0x00000002
181 #define KEYFLAG_SIZE    0x00000004
182
183 float centre[3], centroid[3];
184
185 void mirrormenu(void);
186
187 void add_object_draw(int type)  /* for toolbox */
188 {
189         Object *ob;
190         
191         G.f &= ~(G_VERTEXPAINT+G_FACESELECT+G_TEXTUREPAINT+G_WEIGHTPAINT);
192         setcursor_space(SPACE_VIEW3D, CURSOR_STD);
193
194         if ELEM3(curarea->spacetype, SPACE_VIEW3D, SPACE_BUTS, SPACE_INFO) {
195                 if (G.obedit) exit_editmode(1);
196                 ob= add_object(type);
197                 base_init_from_view3d(BASACT, G.vd);
198
199                 allqueue(REDRAWVIEW3D, 0);
200         }
201
202         redraw_test_buttons(BASACT);
203
204         allqueue(REDRAWALL, 0);
205
206         deselect_all_area_oops();
207         set_select_flag_oops();
208         allqueue(REDRAWINFO, 1);        /* 1, because header->win==0! */
209
210 }
211
212 void free_and_unlink_base(Base *base)
213 {
214         if (base==BASACT)
215                 BASACT= NULL;
216         
217         BLI_remlink(&G.scene->base, base);
218         free_libblock_us(&G.main->object, base->object);
219         MEM_freeN(base);
220 }
221
222 void delete_obj(int ok)
223 {
224         Base *base;
225 extern int undo_push(char *);
226         
227         if(G.obpose) return;
228         if(G.obedit) return;
229         if(G.scene->id.lib) return;
230         
231 //if (undo_push("Erase")) return;       
232         
233         base= FIRSTBASE;
234         while(base) {
235                 Base *nbase= base->next;
236
237                 if TESTBASE(base) {
238                         if(ok==0 &&  (ok=okee("ERASE SELECTED"))==0) return;
239                         
240                         free_and_unlink_base(base);
241                 }
242                 
243                 base= nbase;
244         }
245         countall();
246
247         G.f &= ~(G_VERTEXPAINT+G_FACESELECT+G_TEXTUREPAINT+G_WEIGHTPAINT);
248         setcursor_space(SPACE_VIEW3D, CURSOR_STD);
249         
250         test_scene_constraints();
251         allqueue(REDRAWVIEW3D, 0);
252         redraw_test_buttons(BASACT);
253         allqueue (REDRAWACTION, 0);
254         allqueue(REDRAWIPO, 0);
255         allqueue(REDRAWDATASELECT, 0);
256         allqueue(REDRAWOOPS, 0);
257         allqueue(REDRAWACTION, 0);
258         allqueue(REDRAWNLA, 0);
259 }
260
261
262 void make_track(void)
263 {
264         Base *base;
265         short mode=0;
266         
267         if(G.scene->id.lib) return;
268         if(G.obedit) {
269                 return;
270         }
271         if(BASACT==0) return;
272
273         mode= pupmenu("Make Track %t|Constraint %x1|Old Track %x2");
274         if (mode == 0){
275                 return;
276         }
277         else if (mode == 1){
278                 bConstraint *con;
279                 bTrackToConstraint *data;
280
281                 base= FIRSTBASE;
282                 while(base) {
283                         if TESTBASELIB(base) {
284                                 if(base!=BASACT) {
285                                         con = add_new_constraint(CONSTRAINT_TYPE_TRACKTO);
286                                         strcpy (con->name, "AutoTrack");
287
288                                         data = con->data;
289                                         data->tar = BASACT->object;
290
291                                         add_constraint_to_object(con, base->object);
292                                 }
293                         }
294                         base= base->next;
295                 }
296
297                 test_scene_constraints();
298                 allqueue(REDRAWVIEW3D, 0);
299                 sort_baselist(G.scene);
300         }
301         else if (mode == 2){
302                 base= FIRSTBASE;
303                 while(base) {
304                         if TESTBASELIB(base) {
305                                 if(base!=BASACT) {
306
307                                         base->object->track= BASACT->object;
308                                 }
309                         }
310                         base= base->next;
311                 }
312
313                 test_scene_constraints();
314                 allqueue(REDRAWVIEW3D, 0);
315                 allqueue(REDRAWOOPS, 0);
316                 sort_baselist(G.scene);
317         }
318 }
319
320 void apply_obmat(Object *ob)
321 {
322         float mat[3][3], imat[3][3], tmat[3][3];
323         
324         /* from obmat to loc rot size */
325         
326         if(ob==0) return;
327         Mat3CpyMat4(mat, ob->obmat);
328         
329         VECCOPY(ob->loc, ob->obmat[3]);
330         
331         if(ob->transflag & OB_QUAT) {
332                 Mat3ToQuat(mat, ob->quat);
333                 QuatToMat3(ob->quat, tmat);
334         }
335         else {
336                 Mat3ToEul(mat, ob->rot);
337                 EulToMat3(ob->rot, tmat);
338         }
339         Mat3Inv(imat, tmat);
340         
341         Mat3MulMat3(tmat, imat, mat);
342         
343         ob->size[0]= tmat[0][0];
344         ob->size[1]= tmat[1][1];
345         ob->size[2]= tmat[2][2];
346
347 }
348
349 void clear_parent(void)
350 {
351         Object *par;
352         Base *base;
353         int mode;
354         
355         if(G.obedit) return;
356         if(G.scene->id.lib) return;
357
358         mode= pupmenu("OK? %t|Clear Parent %x1| ... and keep transform (clr track) %x2|Clear parent inverse %x3");
359         
360         if(mode<1) return;
361
362         base= FIRSTBASE;
363         while(base) {
364                 if TESTBASELIB(base) {
365                         par= 0;
366                         if(mode==1 || mode==2) {
367                                 if(base->object->type==OB_IKA) {
368                                         Ika *ika= base->object->data;
369                                         ika->parent= 0;
370                                 }
371                                 par= base->object->parent;
372                                 base->object->parent= 0;
373                         
374                                 if(mode==2) {
375                                         base->object->track= 0;
376                                         apply_obmat(base->object);
377                                 }
378                         }
379                         else if(mode==3) {
380                                 Mat4One(base->object->parentinv);
381                         }
382                         
383                         if(par) {
384                                 if(par->type==OB_LATTICE) makeDispList(base->object);
385                                 if(par->type==OB_IKA) makeDispList(base->object);
386                                 if(par->type==OB_ARMATURE) makeDispList(base->object);
387                         }
388                 }
389                 base= base->next;
390         }
391
392         test_scene_constraints();
393         allqueue(REDRAWVIEW3D, 0);
394         allqueue(REDRAWOOPS, 0);
395 }
396
397 void clear_track(void)
398 {
399         Base *base;
400         int mode;
401         
402         if(G.obedit) return;
403         if(G.scene->id.lib) return;
404
405         mode= pupmenu("OK? %t|Clear Track %x1| ... and keep transform %x2");
406
407         if(mode<1) return;
408
409         base= FIRSTBASE;
410         while(base) {
411                 if TESTBASELIB(base) {
412                         base->object->track= 0;
413
414                         if(mode==2) {
415                                 apply_obmat(base->object);
416                         }                       
417                 }
418                 base= base->next;
419         }
420         test_scene_constraints();
421         allqueue(REDRAWVIEW3D, 0);
422         allqueue(REDRAWOOPS, 0);
423 }
424
425 void clear_object(char mode)
426 {
427         Base *base;
428         float *v1, *v3, mat[3][3];
429         
430         if(G.obedit) return;
431         if(G.scene->id.lib) return;
432         
433         if(mode=='r' && okee("Clear rotation")==0) return;
434         else if(mode=='g' && okee("Clear location")==0) return;
435         else if(mode=='s' && okee("Clear size")==0) return;
436         else if(mode=='o' && okee("Clear origin")==0) return;
437         
438         if (G.obpose){
439
440                 switch (G.obpose->type){
441                 case OB_ARMATURE:
442                         clear_armature (G.obpose, mode);
443 #if 1
444                         make_displists_by_armature (G.obpose);
445 #endif
446                         break;
447                 }
448
449                 allqueue(REDRAWVIEW3D, 0);
450                 return;
451         }
452
453         base= FIRSTBASE;
454         while(base) {
455                 if TESTBASELIB(base) {
456                         if(mode=='r') {
457                                 memset(base->object->rot, 0, 3*sizeof(float));
458                                 memset(base->object->drot, 0, 3*sizeof(float));
459                                 QuatOne(base->object->quat);
460                                 QuatOne(base->object->dquat);
461                         }
462                         else if(mode=='g') {
463                                 memset(base->object->loc, 0, 3*sizeof(float));
464                                 memset(base->object->dloc, 0, 3*sizeof(float));
465                         }
466                         else if(mode=='s') {
467                                 memset(base->object->dsize, 0, 3*sizeof(float));
468                                 base->object->size[0]= 1.0;
469                                 base->object->size[1]= 1.0;
470                                 base->object->size[2]= 1.0;
471                         }
472                         else if(mode=='o') {
473                                 if(base->object->parent) {
474                                         v1= base->object->loc;
475                                         v3= base->object->parentinv[3];
476                                         
477                                         Mat3CpyMat4(mat, base->object->parentinv);
478                                         VECCOPY(v3, v1);
479                                         v3[0]= -v3[0];
480                                         v3[1]= -v3[1];
481                                         v3[2]= -v3[2];
482                                         Mat3MulVecfl(mat, v3);
483                                 }
484                         }
485                 }
486                 base= base->next;
487         }
488         
489         allqueue(REDRAWVIEW3D, 0);
490 }
491
492 void reset_slowparents(void)
493 {
494         /* back to original locations */
495         Base *base;
496         
497         base= FIRSTBASE;
498         while(base) {
499                 if(base->object->parent) {
500                         if(base->object->partype & PARSLOW) {
501                                 base->object->partype -= PARSLOW;
502                                 where_is_object(base->object);
503                                 base->object->partype |= PARSLOW;
504                         }
505                 }
506                 base= base->next;
507         }
508 }
509
510 void set_slowparent(void)
511 {
512         Base *base;
513
514         if( okee("Set Slow parent")==0 ) return;
515
516         base= FIRSTBASE;
517         while(base) {
518                 if TESTBASELIB(base) {
519                         if(base->object->parent) base->object->partype |= PARSLOW;
520                 }
521                 base= base->next;
522         }
523         
524 }
525
526 void make_vertex_parent(void)
527 {
528         EditVert *eve;
529         Base *base;
530         Nurb *nu;
531         BezTriple *bezt;
532         BPoint *bp;
533         Object *par, *ob;
534         int a, v1=0, v2=0, v3=0, nr=1;
535         
536         /* we need 1 ot 3 selected vertices */
537         
538         if(G.obedit->type==OB_MESH) {
539                 eve= G.edve.first;
540                 while(eve) {
541                         if(eve->f & 1) {
542                                 if(v1==0) v1= nr;
543                                 else if(v2==0) v2= nr;
544                                 else if(v3==0) v3= nr;
545                                 else break;
546                         }
547                         nr++;
548                         eve= eve->next;
549                 }
550         }
551         else if ELEM(G.obedit->type, OB_SURF, OB_CURVE) {
552                 nu= editNurb.first;
553                 while(nu) {
554                         if((nu->type & 7)==CU_BEZIER) {
555                                 bezt= nu->bezt;
556                                 a= nu->pntsu;
557                                 while(a--) {
558                                         if(BEZSELECTED(bezt)) {
559                                                 if(v1==0) v1= nr;
560                                                 else if(v2==0) v2= nr;
561                                                 else if(v3==0) v3= nr;
562                                                 else break;
563                                         }
564                                         nr++;
565                                         bezt++;
566                                 }
567                         }
568                         else {
569                                 bp= nu->bp;
570                                 a= nu->pntsu*nu->pntsv;
571                                 while(a--) {
572                                         if(bp->f1 & SELECT) {
573                                                 if(v1==0) v1= nr;
574                                                 else if(v2==0) v2= nr;
575                                                 else if(v3==0) v3= nr;
576                                                 else break;
577                                         }
578                                         nr++;
579                                         bp++;
580                                 }
581                         }
582                         nu= nu->next;
583                 }
584                 
585         }
586         
587         if( !(v1 && v2==0 && v3==0) && !(v1 && v2 && v3) ) {
588                 error("select 1 or 3 vertices");
589                 return;
590         }
591         
592         if(okee("Make vertex-parent")==0) return;
593         
594         base= FIRSTBASE;
595         while(base) {
596                 if TESTBASELIB(base) {
597                         if(base!=BASACT) {
598                                 ob= base->object;
599                                 par= BASACT->object->parent;
600                                 
601                                 while(par) {
602                                         if(par==ob) break;
603                                         par= par->parent;
604                                 }
605                                 if(par) {
606                                         error("Loop in parents");
607                                 }
608                                 else {
609                                         ob->parent= BASACT->object;
610                                         if(v3) {
611                                                 ob->partype= PARVERT3;
612                                                 ob->par1= v1-1;
613                                                 ob->par2= v2-1;
614                                                 ob->par3= v3-1;
615
616                                                 /* inverse parent matrix */
617                                                 what_does_parent(ob);
618                                                 Mat4Invert(ob->parentinv, workob.obmat);
619                                                 clear_workob();
620                                         }
621                                         else {
622                                                 ob->partype= PARVERT1;
623                                                 ob->par1= v1-1;
624
625                                                 /* inverse parent matrix */
626                                                 what_does_parent(ob);
627                                                 Mat4Invert(ob->parentinv, workob.obmat);
628                                                 clear_workob();
629                                         }
630                                 }
631                         }
632                 }
633                 base= base->next;
634         }
635         allqueue(REDRAWVIEW3D, 0);
636         
637 }
638
639 int test_parent_loop(Object *par, Object *ob)
640 {
641         /* test if 'ob' is a parent somewhere in par's parents */
642         
643         if(par==0) return 0;
644         if(ob == par) return 1;
645         
646         if(par->type==OB_IKA) {
647                 Ika *ika= par->data;
648                 
649                 if( ob == ika->parent ) return 1;
650                 if( test_parent_loop(ika->parent, ob) ) return 1;
651         }
652
653         return test_parent_loop(par->parent, ob);
654
655 }
656
657 void make_parent(void)
658 {
659         Base *base;
660         Object *par;
661         Ika *ika;
662         short qual, ok, mode=0, limbnr=0, effchild=0;
663         char *bonestr=NULL;
664         Bone    *bone=NULL;
665         int     bonenr;
666
667         if(G.scene->id.lib) return;
668         if(G.obedit) {
669                 if ELEM3(G.obedit->type, OB_MESH, OB_CURVE, OB_SURF) make_vertex_parent();
670                 else if (G.obedit->type==OB_ARMATURE) make_bone_parent();
671                 return;
672         }
673         if(BASACT==0) return;
674         
675         qual= G.qual;
676         par= BASACT->object;
677
678         if(par->type==OB_IKA) {
679                 
680                 if(qual & LR_SHIFTKEY)
681                         mode= pupmenu("Make Parent without inverse%t|Use Vertex %x1|Use Limb %x2|Use Skeleton %x3");
682                 else 
683                         mode= pupmenu("Make Parent %t|Use Vertex %x1|Use Limb %x2|Use Skeleton %x3");
684                 
685                 if(mode==1) {
686                         draw_ika_nrs(par, 0);
687                         if(button(&limbnr, 0, 99, "Vertex: ")==0) {
688                                 allqueue(REDRAWVIEW3D, 0);
689                                 return;
690                         }
691                 }
692                 else if(mode==2) {
693                         draw_ika_nrs(par, 1);
694                         if(button(&limbnr, 0, 99, "Limb: ")==0) {
695                                 allqueue(REDRAWVIEW3D, 0);
696                                 return;
697                         }
698                 }
699                 else if(mode==3) {
700                         ika= par->data;
701                         if(ika->def==0) {
702                                 error("No skeleton available: use CTRL K");
703                                 return;
704                         }
705                 }
706                 else return;
707
708                 if(mode==1) mode= PARVERT1;
709                 else if(mode==2) mode= PARLIMB;
710                 else if(mode==3) mode= PARSKEL;
711
712                 /* test effchild */
713                 base= FIRSTBASE;
714                 while(base) {
715                         if TESTBASELIB(base) {
716                                 if(base->object->type==OB_IKA && base->object!=par && mode==PARVERT1 ) {
717                                         if(effchild==0) {
718                                                 if(okee("Effector as Child")) effchild= 1;
719                                                 else effchild= 2;
720                                         }
721                                 }
722                         }
723                         if(effchild) break;
724                         base= base->next;
725                 }
726         }
727         else if(par->type == OB_CURVE){
728                 bConstraint *con;
729                 bFollowPathConstraint *data;
730
731                 mode= pupmenu("Make Parent %t|Normal Parent %x1|Follow Path %x2");
732                 if (mode == 0){
733                         return;
734                 }
735                 else if (mode == 2){
736
737                         base= FIRSTBASE;
738                         while(base) {
739                                 if TESTBASELIB(base) {
740                                         if(base!=BASACT) {
741                                                 float cmat[4][4], vec[3], size[3];
742
743                                                 con = add_new_constraint(CONSTRAINT_TYPE_FOLLOWPATH);
744                                                 strcpy (con->name, "AutoPath");
745
746                                                 data = con->data;
747                                                 data->tar = BASACT->object;
748
749                                                 add_constraint_to_object(con, base->object);
750
751                                                 get_constraint_target(con, TARGET_OBJECT, NULL, cmat, size, G.scene->r.cfra - base->object->sf);
752                                                 VecSubf(vec, base->object->obmat[3], cmat[3]);
753
754                                                 base->object->loc[0] = vec[0];
755                                                 base->object->loc[1] = vec[1];
756                                                 base->object->loc[2] = vec[2];
757                                         }
758                                 }
759                                 base= base->next;
760                         }
761
762                         test_scene_constraints();
763                         allqueue(REDRAWVIEW3D, 0);
764                         sort_baselist(G.scene);
765                         return;
766                 }
767         }
768         else if(par->type == OB_ARMATURE){
769                         mode= pupmenu("Make Parent %t|Use Bone %x1|Use Armature %x2|Use Object %x3");
770                         switch (mode){
771                         case 1:
772                                 mode=PARBONE;
773                                 /* Make bone popup menu */
774
775                                 bonestr = make_bone_menu(get_armature(par));
776                 //              if(mbutton(&bone, bonestr, 1, 24, "Bone: ")==0) {
777
778                                 bonenr= pupmenu_col(bonestr, 20);
779                                 if (bonestr)
780                                         MEM_freeN (bonestr);
781                                 
782                                 if (bonenr==-1){
783                                         allqueue(REDRAWVIEW3D, 0);
784                                         return;
785                                 }
786
787                                 apply_pose_armature(get_armature(par), par->pose, 0);
788                                 bone=get_indexed_bone(get_armature(par), bonenr); 
789                                 if (!bone){
790                 //                      error ("Invalid bone!");
791                                         allqueue(REDRAWVIEW3D, 0);
792                                         return;
793                                 }
794
795                                 break;
796                         case 2:
797                                 mode=PARSKEL;
798                                 break;
799                         case 3:
800                                 mode=PAROBJECT;
801                                 break;
802                         default:
803                                 return;
804                         }
805         }
806                 else {
807                 if(qual & LR_SHIFTKEY) {
808                         if(okee("Make Parent without inverse")==0) return;
809                 }
810                 else {
811                         if(qual & LR_ALTKEY) {
812                                 if(okee("Make VertexParent")==0) return;
813                         }
814                         else if(okee("Make Parent")==0) return;
815
816                         /* test effchild */
817                         base= FIRSTBASE;
818                         while(base) {
819                                 if TESTBASELIB(base) {
820                                         if(base->object->type==OB_IKA && base->object != par) {
821                                                 if(effchild==0) {
822                                                         if(okee("Effector as Child")) effchild= 1;
823                                                         else effchild= 2;
824                                                 }
825                                         }
826                                 }
827                                 if(effchild) break;
828                                 base= base->next;
829                         }
830                         
831                         /* now we'll clearparentandkeeptransform all objects */
832                         base= FIRSTBASE;
833                         while(base) {
834                                 if TESTBASELIB(base) {
835                                         if(base!=BASACT && base->object->parent) {
836                                                 if(base->object->type==OB_IKA && effchild==1);
837                                                 else {
838                                                         base->object->parent= 0;
839                                                         apply_obmat(base->object);
840                                                 }
841                                         }
842                                 }
843                                 base= base->next;
844                         }
845                 }
846         }
847         
848         
849         base= FIRSTBASE;
850         while(base) {
851                 if TESTBASELIB(base) {
852                         if(base!=BASACT) {
853                                 
854                                 ok= 1;
855                                 if(base->object->type==OB_IKA) {
856                                 
857                                         if(effchild==1) {
858                                         
859                                                 if( test_parent_loop(par, base->object)==0 ) {
860                                                 
861                                                         Ika *ika= base->object->data;
862                                                         
863                                                         ika->parent= par;
864                                                         ika->par1= limbnr;
865                                                         ika->partype= mode;
866                                                         itterate_ika(base->object);
867                                                         ok= 0;
868                                                 }
869                                                 else {
870                                                         ok= 0;
871                                                         error("Loop in parents");
872                                                 }
873                                         }
874                                 }
875                                 
876                                 if(ok) {
877                                         if( test_parent_loop(par, base->object) ) {
878                                                 error("Loop in parents");
879                                         }
880                                         else {
881                                                 
882                                                 if(par->type==OB_IKA){
883                                                         base->object->partype= mode;
884                                                         base->object->par1= limbnr;
885                                                 }
886                                                 else if (par->type==OB_ARMATURE){
887                                                         base->object->partype= mode;
888                                                         if (bone)
889                                                                 strcpy (base->object->parsubstr, bone->name);
890                                                         else
891                                                                 base->object->parsubstr[0]=0;
892                                                 }
893                                                 
894                                                 else if(qual & LR_ALTKEY) {
895                                                         base->object->partype= PARVERT1;
896                                                 }
897                                                 else {
898                                                         base->object->partype= PAROBJECT;
899                                                 }
900                                                 
901                                                 base->object->parent= par;
902                                                 
903                                                 /* calculate inverse parent matrix? */
904                                                 if( (qual & LR_SHIFTKEY) ) {
905                                                         /* not... */
906                                                         Mat4One(base->object->parentinv);
907                                                         memset(base->object->loc, 0, 3*sizeof(float));
908                                                 }
909                                                 else {
910                                                         if(mode==PARSKEL && par->type == OB_ARMATURE) {
911                                                                 /* Prompt the user as to whether he wants to
912                                                                  * add some vertex groups based on the bones
913                                                                  * in the parent armature.
914                                                                  */
915                                                                 create_vgroups_from_armature(base->object, 
916                                                                                                                          par);
917
918                                                                 base->object->partype= PAROBJECT;
919                                                                 what_does_parent(base->object);
920                                                                 Mat4One (base->object->parentinv);
921                                                                 base->object->partype= mode;
922                                                         }
923                                                         else
924                                                                 what_does_parent(base->object);
925                                                         Mat4Invert(base->object->parentinv, workob.obmat);
926                                                 }
927                                                 
928                                                 if(par->type==OB_LATTICE) makeDispList(base->object);
929                                                 if(par->type==OB_IKA && mode==PARSKEL) makeDispList(base->object);
930                                                 if(par->type==OB_ARMATURE && mode == PARSKEL){
931                                                         verify_defgroups(base->object);
932                                                         makeDispList(base->object);
933                                                 }
934                                         }
935                                 }
936                         }
937                 }
938                 base= base->next;
939         }
940         allqueue(REDRAWVIEW3D, 0);
941         allqueue(REDRAWOOPS, 0);
942         
943         test_scene_constraints();
944         sort_baselist(G.scene);
945 }
946
947
948 void enter_editmode(void)
949 {
950         Base *base;
951         Object *ob;
952         Ika *ika;
953         ID *id;
954         Mesh *me;
955         int ok= 0;
956         bArmature *arm;
957         
958         if(G.scene->id.lib) return;
959         base= BASACT;
960         if(base==0) return;
961         if((base->lay & G.vd->lay)==0) return;
962         
963         ob= base->object;
964         if(ob->data==0) return;
965         
966         id= ob->data;
967         if(id->lib) {
968                 error("Can't edit libdata");
969                 return;
970         }
971         
972         if(ob->type==OB_MESH) {
973                 me= get_mesh(ob);
974                 if( me==0 ) return;
975                 if(me->id.lib) {
976                         error("Can't edit libdata");
977                         return;
978                 }
979                 ok= 1;
980                 G.obedit= ob;
981                 make_editMesh();
982                 allqueue(REDRAWBUTSLOGIC, 0);
983         }
984         if (ob->type==OB_ARMATURE){
985                 arm=base->object->data;
986                 if (!arm) return;
987                 if (arm->id.lib){
988                         error("Can't edit libdata");
989                         return;
990                 }
991                 ok=1;
992                 G.obedit=ob;
993                 make_editArmature();
994                 allqueue (REDRAWVIEW3D,0);
995         }
996         else if(ob->type==OB_IKA) {     /* grab type */
997                 base= FIRSTBASE;
998                 while(base) {
999                         if TESTBASE(base) {
1000                                 if(base->object->type==OB_IKA) {
1001                                         ika= base->object->data;
1002                                         if(ika->flag & IK_GRABEFF) ika->flag &= ~IK_GRABEFF;
1003                                         else ika->flag |= IK_GRABEFF;
1004                                 }
1005                         }
1006                         base= base->next;
1007                 }
1008                 allqueue(REDRAWVIEW3D, 0);
1009         }
1010         else if(ob->type==OB_FONT) {
1011                 G.obedit= ob;
1012                 ok= 1;
1013                 make_editText();
1014         }
1015         else if(ob->type==OB_MBALL) {
1016                 G.obedit= ob;
1017                 ok= 1;
1018                 make_editMball();
1019         }
1020         else if(ob->type==OB_LATTICE) {
1021                 G.obedit= ob;
1022                 ok= 1;
1023                 make_editLatt();
1024         }
1025         else if(ob->type==OB_SURF || ob->type==OB_CURVE) {
1026                 ok= 1;
1027                 G.obedit= ob;
1028                 make_editNurb();
1029         }
1030         allqueue(REDRAWBUTSEDIT, 0);
1031         countall();
1032         
1033         if(ok) {
1034                 setcursor_space(SPACE_VIEW3D, CURSOR_EDIT);
1035         
1036                 allqueue(REDRAWVIEW3D, 0);
1037         }
1038         else G.obedit= 0;
1039
1040         if (G.obpose)
1041                 exit_posemode (1);
1042         scrarea_queue_headredraw(curarea);
1043 }
1044
1045 void make_displists_by_parent(Object *ob) {
1046         Base *base;
1047         
1048         for (base= FIRSTBASE; base; base= base->next)
1049                 if (ob==base->object->parent)
1050                         makeDispList(base->object);
1051 }
1052
1053 void exit_editmode(int freedata)        /* freedata==0 at render */
1054 {
1055         Base *base;
1056         Object *ob;
1057         Curve *cu;
1058
1059         if(G.obedit==0) return;
1060
1061         if(G.obedit->type==OB_MESH) {
1062
1063                 /* temporal */
1064                 countall();
1065
1066                 if(G.totvert>2000000000L) {
1067                         error("too many vertices");
1068                         return;
1069                 }
1070                 load_editMesh();        /* makes new displist */
1071
1072                 if(freedata) free_editMesh();
1073                 
1074                 if(G.f & G_FACESELECT) allqueue(REDRAWIMAGE, 0);
1075
1076                 build_particle_system(G.obedit);
1077         }
1078         else if (G.obedit->type==OB_ARMATURE){  
1079                 load_editArmature();
1080                 if (freedata) free_editArmature();
1081         }
1082         else if ELEM(G.obedit->type, OB_CURVE, OB_SURF) {
1083                 load_editNurb();
1084                 if(freedata) freeNurblist(&editNurb);
1085         }
1086         else if(G.obedit->type==OB_FONT && freedata==1) {
1087                 load_editText();
1088         }
1089         else if(G.obedit->type==OB_LATTICE) {
1090                 load_editLatt();
1091                 if(freedata) free_editLatt();
1092         }
1093         else if(G.obedit->type==OB_MBALL) {
1094                 load_editMball();
1095                 if(freedata) BLI_freelistN(&editelems);
1096         }
1097
1098         ob= G.obedit;
1099         
1100         /* displist make is different in editmode */
1101         if(freedata) G.obedit= NULL;
1102         makeDispList(ob);
1103
1104         /* has this influence at other objects? */
1105         if(ob->type==OB_CURVE) {
1106
1107                 /* test if ob is use as bevelcurve r textoncurve */
1108                 base= FIRSTBASE;
1109                 while(base) {
1110                         if ELEM(base->object->type, OB_CURVE, OB_FONT) {
1111                                 cu= base->object->data;
1112                                 
1113                                 if(cu->textoncurve==ob) {
1114                                         text_to_curve(base->object, 0);
1115                                         makeDispList(base->object);
1116                                 }
1117                                 if(cu->bevobj== ob) {
1118                                         makeDispList(base->object);
1119                                 }
1120                         }
1121                         base= base->next;
1122                 }
1123                 
1124         }
1125         else if(ob->type==OB_LATTICE) {
1126                 make_displists_by_parent(ob);
1127         }
1128
1129         if(freedata) {
1130                 setcursor_space(SPACE_VIEW3D, CURSOR_STD);
1131         
1132                 countall();
1133                 allqueue(REDRAWVIEW3D, 0);
1134                 allqueue(REDRAWBUTSEDIT, 0);
1135                 allqueue(REDRAWBUTSLOGIC, 0);
1136         }
1137         scrarea_queue_headredraw(curarea);
1138
1139 }
1140
1141 void check_editmode(int type)
1142 {
1143         
1144         if (G.obedit==0 || G.obedit->type==type) return;
1145
1146         exit_editmode(1);
1147 }
1148
1149 static int centremode= 0; /* 0 == do centre, 1 == centre new, 2 == centre cursor */
1150
1151 void docentre(void)
1152 {
1153         Base *base;
1154         Object *ob;
1155         Mesh *me, *tme;
1156         Curve *cu;
1157         MVert *mvert;
1158 //      BezTriple *bezt;
1159 //      BPoint *bp;
1160         Nurb *nu, *nu1;
1161         EditVert *eve;
1162         float cent[3], centn[3], min[3], max[3], omat[3][3];
1163         int a;
1164
1165         if(G.scene->id.lib) return;
1166
1167         if(G.obedit) {
1168
1169                 INIT_MINMAX(min, max);
1170         
1171                 if(G.obedit->type==OB_MESH) {
1172                         eve= G.edve.first;
1173                         while(eve) {
1174                                 DO_MINMAX(eve->co, min, max);
1175                                 eve= eve->next;
1176                         }
1177                         cent[0]= (min[0]+max[0])/2.0;
1178                         cent[1]= (min[1]+max[1])/2.0;
1179                         cent[2]= (min[2]+max[2])/2.0;
1180                         
1181                         eve= G.edve.first;
1182                         while(eve) {
1183                                 VecSubf(eve->co, eve->co, cent);                        
1184                                 eve= eve->next;
1185                         }
1186                 }
1187         }
1188         
1189         /* reset flags */
1190         base= FIRSTBASE;
1191         while(base) {
1192                 if TESTBASELIB(base) {
1193                         base->object->flag &= ~OB_DONE;
1194                 }
1195                 base= base->next;
1196         }
1197         me= G.main->mesh.first;
1198         while(me) {
1199                 me->flag &= ~ME_ISDONE;
1200                 me= me->id.next;
1201         }
1202         
1203         base= FIRSTBASE;
1204         while(base) {
1205                 
1206                 if TESTBASELIB(base) {
1207                         if((base->object->flag & OB_DONE)==0) {
1208                                 
1209                                 base->object->flag |= OB_DONE;
1210                                 
1211                                 if(G.obedit==0 && (me=get_mesh(base->object)) ) {
1212                                         
1213                                         if(me->key) {
1214                                                 error("Mesh with vertexkey!");
1215                                                 return;
1216                                         }
1217                                         
1218                                         if(centremode==2) {
1219                                                 VECCOPY(cent, give_cursor());
1220                                                 Mat4Invert(base->object->imat, base->object->obmat);
1221                                                 Mat4MulVecfl(base->object->imat, cent);
1222                                         } else {
1223                                                 INIT_MINMAX(min, max);
1224                 
1225                                                 mvert= me->mvert;
1226                                                 for(a=0; a<me->totvert; a++, mvert++) {
1227                                                         DO_MINMAX(mvert->co, min, max);
1228                                                 }
1229                                 
1230                                                 cent[0]= (min[0]+max[0])/2.0;
1231                                                 cent[1]= (min[1]+max[1])/2.0;
1232                                                 cent[2]= (min[2]+max[2])/2.0;
1233                                         }
1234                                                 
1235                                         mvert= me->mvert;
1236                                         for(a=0; a<me->totvert; a++, mvert++) {
1237                                                 VecSubf(mvert->co, mvert->co, cent);
1238                                         }
1239                                         me->flag |= ME_ISDONE;
1240                                         
1241                                         if(centremode) {
1242                                                 Mat3CpyMat4(omat, base->object->obmat);
1243                                                 
1244                                                 VECCOPY(centn, cent);
1245                                                 Mat3MulVecfl(omat, centn);
1246                                                 base->object->loc[0]+= centn[0];
1247                                                 base->object->loc[1]+= centn[1];
1248                                                 base->object->loc[2]+= centn[2];
1249                                                 
1250                                                 /* other users? */
1251                                                 ob= G.main->object.first;
1252                                                 while(ob) {
1253                                                         if((ob->flag & OB_DONE)==0) {
1254                                                                 tme= get_mesh(ob);
1255                                                                 
1256                                                                 if(tme==me) {
1257                                                                         
1258                                                                         ob->flag |= OB_DONE;
1259
1260                                                                         Mat3CpyMat4(omat, ob->obmat);
1261                                                                         VECCOPY(centn, cent);
1262                                                                         Mat3MulVecfl(omat, centn);
1263                                                                         ob->loc[0]+= centn[0];
1264                                                                         ob->loc[1]+= centn[1];
1265                                                                         ob->loc[2]+= centn[2];
1266                                                                         
1267                                                                         if(tme && (tme->flag & ME_ISDONE)==0) {
1268                                                                                 mvert= tme->mvert;
1269                                                                                 for(a=0; a<tme->totvert; a++, mvert++) {
1270                                                                                         VecSubf(mvert->co, mvert->co, cent);
1271                                                                                 }
1272                                                                                 tme->flag |= ME_ISDONE;
1273                                                                         }
1274                                                                 }
1275                                                         }
1276                                                         
1277                                                         ob= ob->id.next;
1278                                                 }
1279                                         }
1280                                 
1281                                         /* displist of all users, also this one */
1282                                         makeDispList(base->object);
1283                                         
1284                                         /* DO: check all users... */
1285                                         tex_space_mesh(me);
1286                 
1287                                 }
1288                                 else if ELEM(base->object->type, OB_CURVE, OB_SURF) {
1289                                                                         
1290                                         if(G.obedit) {
1291                                                 nu1= editNurb.first;
1292                                         }
1293                                         else {
1294                                                 cu= base->object->data;
1295                                                 nu1= cu->nurb.first;
1296                                         }
1297                                         
1298                                         if(centremode==2) {
1299                                                 VECCOPY(cent, give_cursor());
1300                                                 Mat4Invert(base->object->imat, base->object->obmat);
1301                                                 Mat4MulVecfl(base->object->imat, cent);
1302                                                         
1303                                                         /* Curves need to be 2d, never offset in
1304                                                          * Z. Is a somewhat arbitrary restriction, 
1305                                                          * would probably be nice to remove.
1306                                                          */
1307                                                 cent[2]= 0.0;
1308                                         } else {
1309                                                 INIT_MINMAX(min, max);
1310         
1311                                                 nu= nu1;
1312                                                 while(nu) {
1313                                                         minmaxNurb(nu, min, max);
1314                                                         nu= nu->next;
1315                                                 }
1316                                                 
1317                                                 cent[0]= (min[0]+max[0])/2.0;
1318                                                 cent[1]= (min[1]+max[1])/2.0;
1319                                                 cent[2]= (min[2]+max[2])/2.0;
1320                                         }
1321                                         
1322                                         nu= nu1;
1323                                         while(nu) {
1324                                                 if( (nu->type & 7)==1) {
1325                                                         a= nu->pntsu;
1326                                                         while (a--) {
1327                                                                 VecSubf(nu->bezt[a].vec[0], nu->bezt[a].vec[0], cent);
1328                                                                 VecSubf(nu->bezt[a].vec[1], nu->bezt[a].vec[1], cent);
1329                                                                 VecSubf(nu->bezt[a].vec[2], nu->bezt[a].vec[2], cent);
1330                                                         }
1331                                                 }
1332                                                 else {
1333                                                         a= nu->pntsu*nu->pntsv;
1334                                                         while (a--)
1335                                                                 VecSubf(nu->bp[a].vec, nu->bp[a].vec, cent);
1336                                                 }
1337                                                 nu= nu->next;
1338                                         }
1339                         
1340                                         if(centremode && G.obedit==0) {
1341                                                 Mat3CpyMat4(omat, base->object->obmat);
1342                                                 
1343                                                 Mat3MulVecfl(omat, cent);
1344                                                 base->object->loc[0]+= cent[0];
1345                                                 base->object->loc[1]+= cent[1];
1346                                                 base->object->loc[2]+= cent[2];
1347                                         }
1348                         
1349                                         if(G.obedit) {
1350                                                 makeDispList(G.obedit);
1351                                                 break;
1352                                         }
1353                                         else makeDispList(base->object);
1354         
1355                                 }
1356                                 else if(base->object->type==OB_FONT) {
1357                                         /* get from bb */
1358                                         
1359                                         cu= base->object->data;
1360                                         if(cu->bb==0) return;
1361                                         
1362                                         cu->xof= -0.5*( cu->bb->vec[4][0] - cu->bb->vec[0][0]);
1363                                         cu->yof= -0.5 -0.5*( cu->bb->vec[0][1] - cu->bb->vec[2][1]);    /* extra 0.5 is the height of above line */
1364                                         
1365                                         /* not really ok, do this better once! */
1366                                         cu->xof /= cu->fsize;
1367                                         cu->yof /= cu->fsize;
1368                                         
1369                                         text_to_curve(base->object, 0);
1370                                         makeDispList(base->object);
1371                                         
1372                                         allqueue(REDRAWBUTSEDIT, 0);
1373                                 }
1374                         }
1375                 }
1376                 base= base->next;
1377         }
1378
1379         allqueue(REDRAWVIEW3D, 0);
1380 }
1381
1382 void docentre_new(void)
1383 {
1384         if(G.scene->id.lib) return;
1385
1386         if(G.obedit) {
1387                 error("Unable to perform function in EditMode");
1388         }
1389         else {
1390                 centremode= 1;
1391                 docentre();
1392                 centremode= 0;
1393         }
1394 }
1395
1396 void docentre_cursor(void)
1397 {
1398         if(G.scene->id.lib) return;
1399
1400         if(G.obedit) {
1401                 error("Unable to perform function in EditMode");
1402         }
1403         else {
1404                 centremode= 2;
1405                 docentre();
1406                 centremode= 0;
1407         }
1408 }
1409
1410 void movetolayer(void)
1411 {
1412         Base *base;
1413         unsigned int lay= 0, local;
1414
1415         if(G.scene->id.lib) return;
1416
1417         base= FIRSTBASE;
1418         while(base) {
1419                 if TESTBASE(base) lay |= base->lay;
1420                 base= base->next;
1421         }
1422         if(lay==0) return;
1423         lay &= 0xFFFFFF;
1424         
1425         if( movetolayer_buts(&lay)==0 ) return;
1426         if(lay==0) return;
1427
1428         base= FIRSTBASE;
1429         while(base) {
1430                 if TESTBASE(base) {
1431                         local= base->lay & 0xFF000000;
1432                         base->lay= lay + local;
1433
1434                         base->object->lay= lay;
1435                 }
1436                 base= base->next;
1437         }
1438         countall();
1439         allqueue(REDRAWBUTSEDIT, 0);
1440         allqueue(REDRAWVIEW3D, 0);
1441         allqueue(REDRAWOOPS, 0);
1442         allqueue(REDRAWINFO, 0);
1443 }
1444
1445
1446 void special_editmenu(void)
1447 {
1448         extern short editbutflag;
1449         extern float doublimit;
1450         float fac;
1451         int nr;
1452         short randfac;
1453         
1454         if(G.obedit==0) {
1455                 if(G.f & G_FACESELECT) {
1456                         Mesh *me= get_mesh(OBACT);
1457                         TFace *tface;
1458                         int a;
1459                         
1460                         if(me==0 || me->tface==0) return;
1461                         
1462                         nr= pupmenu("Specials%t|Set     Tex%x1|         Shared%x2|         Light%x3|         Invisible%x4|         Collision%x5|Clr     Tex%x6|         Shared%x7|         Light%x8|         Invisible%x9|         Collision%x10");
1463         
1464                         for(a=me->totface, tface= me->tface; a>0; a--, tface++) {
1465                                 if(tface->flag & SELECT) {
1466                                         switch(nr) {
1467                                         case 1:
1468                                                 tface->mode |= TF_TEX; break;
1469                                         case 2:
1470                                                 tface->mode |= TF_SHAREDCOL; break;
1471                                         case 3:
1472                                                 tface->mode |= TF_LIGHT; break; 
1473                                         case 4:
1474                                                 tface->mode |= TF_INVISIBLE; break;
1475                                         case 5:
1476                                                 tface->mode |= TF_DYNAMIC; break;
1477                                         case 6:
1478                                                 tface->mode &= ~TF_TEX;
1479                                                 tface->tpage= 0;
1480                                                 break;
1481                                         case 7:
1482                                                 tface->mode &= ~TF_SHAREDCOL; break;
1483                                         case 8:
1484                                                 tface->mode &= ~TF_LIGHT; break;
1485                                         case 9:
1486                                                 tface->mode &= ~TF_INVISIBLE; break;
1487                                         case 10:
1488                                                 tface->mode &= ~TF_DYNAMIC; break;
1489                                         }
1490                                 }
1491                         }
1492                         allqueue(REDRAWVIEW3D, 0);
1493                         allqueue(REDRAWBUTSLOGIC, 0);
1494                 }
1495                 else if(G.f & G_VERTEXPAINT) {
1496                         Mesh *me= get_mesh(OBACT);
1497                         
1498                         if(me==0 || (me->mcol==NULL && me->tface==NULL) ) return;
1499                         
1500                         nr= pupmenu("Specials%t|Shared VertexCol%x1");
1501                         if(nr==1) {
1502                                 
1503                                 if(me->tface) tface_to_mcol(me);
1504                                 
1505                                 copy_vpaint_undo( (unsigned int *)me->mcol, me->totface);
1506                                 do_shared_vertexcol(me);
1507                                 
1508                                 if(me->tface) mcol_to_tface(me, 1);
1509                         }
1510                 }
1511                 else {
1512                         Base *base, *base_select= NULL;
1513
1514                         // Get the active object mesh.
1515                         Mesh *me= get_mesh(OBACT);
1516
1517                         // If the active object is a mesh...
1518                         if (me) {
1519                                 // Bring up a little menu with the boolean operation choices on.
1520                                 nr= pupmenu("Boolean %t|Intersect%x1|Union%x2|Difference%x3");
1521
1522                                 if (nr > 0) {
1523                                         // user has made a choice of a menu element.
1524                                         // All of the boolean functions require 2 mesh objects 
1525                                         // we search through the object list to find the other 
1526                                         // selected item and make sure it is distinct and a mesh.
1527
1528                                         base= FIRSTBASE;
1529                                         while(base) {
1530                                                 if(base->flag & SELECT) {
1531                                                         if(base->object != OBACT) base_select= base;
1532                                                 }
1533
1534                                                 base= base->next;
1535                                         }
1536
1537                                         if (base_select) {
1538                                                 if (get_mesh(base_select->object)) {
1539                                                         waitcursor(1);
1540
1541                                                         if (!NewBooleanMesh(BASACT,base_select,nr)) {
1542                                                                 error("An internal error occurred -- sorry!");
1543                                                         }
1544
1545                                                         waitcursor(0);
1546                                                 } else {
1547                                                         error("Please select 2 meshes");
1548                                                 }
1549                                         } else {
1550                                                 error("Please select 2 meshes");
1551                                         }
1552                                 }
1553
1554                                 allqueue(REDRAWVIEW3D, 0);
1555                         }
1556                 }
1557         }
1558         else if(G.obedit->type==OB_MESH) {
1559
1560                 nr= pupmenu("Specials%t|Subdivide%x1|Subdivide Fractal%x2|Subdivide Smooth%x3|Merge%x4|Remove Doubles%x5|Hide%x6|Reveal%x7|Select swap%x8|Flip Normals %x9|Smooth %x10|Bevel %x11");
1561                 if(nr>0) waitcursor(1);
1562                 
1563                 switch(nr) {
1564                 case 1:
1565                         undo_push_mesh("Subdivide");
1566                         subdivideflag(1, 0.0, editbutflag);
1567                         break;
1568                 case 2:
1569                         randfac= 10;
1570                         if(button(&randfac, 1, 100, "Rand fac:")==0) return;
1571                         fac= -( (float)randfac )/100;
1572                         undo_push_mesh("Subdivide Fractal");
1573                         subdivideflag(1, fac, editbutflag);
1574                         break;
1575                 case 3:
1576                         undo_push_mesh("Subdivide Smooth");
1577                         subdivideflag(1, 0.0, editbutflag | B_SMOOTH);
1578                         break;
1579                 case 4:
1580                         mergemenu();
1581                         break;
1582                 case 5:
1583                         undo_push_mesh("Rem Doubles");
1584                         notice("Removed: %d", removedoublesflag(1, doublimit));
1585                         break;
1586                 case 6:
1587                         hide_mesh(0);
1588                         break;
1589                 case 7:
1590                         reveal_mesh();
1591                         break;
1592                 case 8:
1593                         selectswap_mesh();
1594                         break;
1595                 case 9:
1596                         undo_push_mesh("Flip Normals");
1597                         flip_editnormals();
1598                         break;
1599                 case 10:
1600                         undo_push_mesh("Smooth");
1601                         vertexsmooth();
1602                         break;
1603                 case 11:
1604                         bevel_menu();
1605                         break;
1606                 }               
1607                 
1608                 makeDispList(G.obedit);
1609                 
1610                 if(nr>0) waitcursor(0);
1611                 
1612         }
1613         else if ELEM(G.obedit->type, OB_CURVE, OB_SURF) {
1614
1615                 nr= pupmenu("Specials%t|Subdivide%x1|Switch Direction%x2");
1616                 
1617                 switch(nr) {
1618                 case 1:
1619                         subdivideNurb();
1620                         break;
1621                 case 2:
1622                         switchdirectionNurb2();
1623                         break;
1624                 }
1625         }
1626
1627         countall();
1628         allqueue(REDRAWVIEW3D, 0);
1629         
1630 }
1631
1632 void convertmenu(void)
1633 {
1634         Base *base, *basen, *basact;
1635         Object *ob, *ob1;
1636         Curve *cu;
1637         MetaBall *mb;
1638         Mesh *me;
1639         DispList *dl;
1640         int ok=0, nr = 0, a;
1641         
1642         if(G.scene->id.lib) return;
1643
1644         ob= OBACT;
1645         if(ob==0) return;
1646         if(G.obedit) return;
1647         
1648         basact= BASACT; /* will be restored */
1649                 
1650         if(ob->type==OB_FONT) {
1651                 nr= pupmenu("Convert Font to%t|Curve");
1652                 if(nr>0) ok= 1;
1653         }
1654         else if(ob->type==OB_MBALL) {
1655                 nr= pupmenu("Convert MetaBall to%t|Mesh (keep original)");
1656                 if(nr>0) ok= 1;
1657         }
1658         else if(ob->type==OB_CURVE) {
1659                 nr= pupmenu("Convert Curve to%t|Mesh");
1660                 if(nr>0) ok= 1;
1661         }
1662         else if(ob->type==OB_SURF) {
1663                 nr= pupmenu("Convert Nurbs Surf to%t|Mesh");
1664                 if(nr>0) ok= 1;
1665         }
1666         else if(ob->type==OB_MESH && ((Mesh*) ob->data)->flag&ME_SUBSURF) {
1667                 nr= pupmenu("Convert SubSurf to%t|Mesh (keep original)");
1668                 if(nr>0) ok= 1;
1669         }
1670         if(ok==0) return;
1671
1672         /* don't forget multiple users! */
1673
1674         /* reset flags */
1675         base= FIRSTBASE;
1676         while(base) {
1677                 if TESTBASELIB(base) {
1678                         base->object->flag &= ~OB_DONE;
1679                 }
1680                 base= base->next;
1681         }
1682
1683         base= FIRSTBASE;
1684         while(base) {
1685                 if TESTBASELIB(base) {
1686                         
1687                         ob= base->object;
1688                         
1689                         if(ob->flag & OB_DONE);
1690                         else if(ob->type==OB_MESH) {
1691                                 Mesh *oldme= ob->data;
1692                                 
1693                                 if( oldme->subdiv==0 ) {
1694                                         error("No subdivision Mesh to convert available");
1695                                 }
1696                                 else if (oldme->flag&ME_SUBSURF) {
1697                                         ob->flag |= OB_DONE;
1698
1699                                         ob1= copy_object(ob);
1700
1701                                         basen= MEM_mallocN(sizeof(Base), "duplibase");
1702                                         *basen= *base;
1703                                         BLI_addhead(&G.scene->base, basen);     /* addhead: otherwise eternal loop */
1704                                         basen->object= ob1;
1705                                         basen->flag &= ~SELECT;
1706                                                 
1707                                         me= ob1->data;
1708                                         me->id.us--;
1709                                                 
1710                                         ob1->data= add_mesh();
1711                                         G.totmesh++;
1712                                         ob1->type= OB_MESH;
1713
1714                                         me= ob1->data;
1715                                         me->totcol= oldme->totcol;
1716                                         if(ob1->totcol) {
1717                                                 me->mat= MEM_dupallocN(oldme->mat);
1718                                                 for(a=0; a<ob1->totcol; a++) id_us_plus((ID *)me->mat[a]);
1719                                         }
1720                                                 
1721                                         subsurf_to_mesh(ob, ob1->data);
1722         
1723                                         tex_space_mesh(me);
1724                                 }
1725                         }
1726                         else if(ob->type==OB_FONT) {
1727                                 if(nr==1) {
1728                                 
1729                                         ob->flag |= OB_DONE;
1730                                 
1731                                         ob->type= OB_CURVE;
1732                                         cu= ob->data;
1733                                         
1734                                         if(cu->vfont) {
1735                                                 cu->vfont->id.us--;
1736                                                 cu->vfont= 0;
1737                                         }
1738                                         /* other users */
1739                                         if(cu->id.us>1) {
1740                                                 ob1= G.main->object.first;
1741                                                 while(ob1) {
1742                                                         if(ob1->data==cu) ob1->type= OB_CURVE;
1743                                                         ob1= ob1->id.next;
1744                                                 }
1745                                         }
1746                                 }
1747                         }
1748                         else if ELEM(ob->type, OB_CURVE, OB_SURF) {
1749                                 if(nr==1) {
1750
1751                                         ob->flag |= OB_DONE;
1752                                         cu= ob->data;
1753                                         
1754                                         dl= cu->disp.first;
1755                                         if(dl==0) makeDispList(ob);
1756
1757                                         nurbs_to_mesh(ob); /* also does users */
1758
1759                                         /* texspace and normals */
1760                                         BASACT= base;
1761                                         enter_editmode();
1762                                         exit_editmode(1);
1763                                         BASACT= basact;
1764                                 }
1765                         }
1766                         else if(ob->type==OB_MBALL) {
1767                         
1768                                 if(nr==1) {
1769                                         ob= find_basis_mball(ob);
1770                                         
1771                                         if(ob->disp.first && !(ob->flag&OB_DONE)) {
1772                                         
1773                                                 ob->flag |= OB_DONE;
1774
1775                                                 ob1= copy_object(ob);
1776
1777                                                 basen= MEM_mallocN(sizeof(Base), "duplibase");
1778                                                 *basen= *base;
1779                                                 BLI_addhead(&G.scene->base, basen);     /* addhead: othwise eternal loop */
1780                                                 basen->object= ob1;
1781                                                 basen->flag &= ~SELECT;
1782                                                 
1783                                                 mb= ob1->data;
1784                                                 mb->id.us--;
1785                                                 
1786                                                 ob1->data= add_mesh();
1787                                                 G.totmesh++;
1788                                                 ob1->type= OB_MESH;
1789                                                 
1790                                                 me= ob1->data;
1791                                                 me->totcol= mb->totcol;
1792                                                 if(ob1->totcol) {
1793                                                         me->mat= MEM_dupallocN(mb->mat);
1794                                                         for(a=0; a<ob1->totcol; a++) id_us_plus((ID *)me->mat[a]);
1795                                                 }
1796                                                 
1797                                                 mball_to_mesh(&ob->disp, ob1->data);
1798                                                 tex_space_mesh(me);
1799                                         }
1800                                 }
1801                         }
1802                 }
1803                 base= base->next;
1804         }
1805         
1806         allqueue(REDRAWVIEW3D, 0);
1807         allqueue(REDRAWOOPS, 0);
1808         allqueue(REDRAWBUTSEDIT, 0);
1809 }
1810
1811 void flip_subdivison(Object *ob, int level)
1812 {
1813         Mesh *me;
1814
1815         me = ob->data;
1816
1817         if (level == 0)
1818         {
1819                 if(me->flag & ME_SUBSURF)
1820                         me->flag &= ~ME_SUBSURF;
1821                 else
1822                         me->flag |= ME_SUBSURF;
1823         } else {
1824                 me->subdiv = level;
1825         }
1826
1827         allqueue(REDRAWVIEW3D, 0);
1828         allqueue(REDRAWOOPS, 0);
1829         allqueue(REDRAWBUTSEDIT, 0);
1830         makeDispList(ob);
1831 }
1832  
1833 void copymenu_properties(Object *ob)
1834 {       
1835         bProperty *prop, *propn, *propc;
1836         Base *base;
1837         int nr, tot=0;
1838         char *str;
1839         
1840         prop= ob->prop.first;
1841         while(prop) {
1842                 tot++;
1843                 prop= prop->next;
1844         }
1845         
1846         if(tot==0) {
1847                 error("No properties in Object");
1848                 return;
1849         }
1850         
1851         str= MEM_callocN(24+32*tot, "copymenu prop");
1852         
1853         strcpy(str, "Copy Property %t");
1854         
1855         tot= 0; 
1856         prop= ob->prop.first;
1857         while(prop) {
1858                 tot++;
1859                 strcat(str, " |");
1860                 strcat(str, prop->name);
1861                 prop= prop->next;
1862         }
1863
1864         nr= pupmenu(str);
1865         if(nr>0) {
1866                 tot= 0;
1867                 prop= ob->prop.first;
1868                 while(prop) {
1869                         tot++;
1870                         if(tot==nr) break;
1871                         prop= prop->next;
1872                 }
1873                 if(prop) {
1874                         propc= prop;
1875                         
1876                         base= FIRSTBASE;
1877                         while(base) {
1878                                 if(base != BASACT) {
1879                                         if(TESTBASELIB(base)) {
1880                                                 prop= get_property(base->object, propc->name);
1881                                                 if(prop) {
1882                                                         free_property(prop);
1883                                                         BLI_remlink(&base->object->prop, prop);
1884                                                 }
1885                                                 propn= copy_property(propc);
1886                                                 BLI_addtail(&base->object->prop, propn);
1887                                         }
1888                                 }
1889                                 base= base->next;
1890                         }
1891                 }
1892         }
1893         MEM_freeN(str);
1894         allqueue(REDRAWVIEW3D, 0);
1895 }
1896
1897 void copymenu_logicbricks(Object *ob)
1898 {
1899         Base *base;
1900         
1901         base= FIRSTBASE;
1902         while(base) {
1903                 if(base->object != ob) {
1904                         if(TESTBASELIB(base)) {
1905                                 
1906                                 /* first: free all logic */
1907                                 free_sensors(&base->object->sensors);                           
1908                                 unlink_controllers(&base->object->controllers);
1909                                 free_controllers(&base->object->controllers);
1910                                 unlink_actuators(&base->object->actuators);
1911                                 free_actuators(&base->object->actuators);
1912                                 
1913                                 /* now copy it, this also works without logicbricks! */
1914                                 clear_sca_new_poins_ob(ob);
1915                                 copy_sensors(&base->object->sensors, &ob->sensors);
1916                                 copy_controllers(&base->object->controllers, &ob->controllers);
1917                                 copy_actuators(&base->object->actuators, &ob->actuators);
1918                                 set_sca_new_poins_ob(base->object);
1919                                 
1920                                 /* some menu settings */
1921                                 base->object->scavisflag= ob->scavisflag;
1922                                 base->object->scaflag= ob->scaflag;
1923                                 
1924                         }
1925                 }
1926                 base= base->next;
1927         }
1928 }
1929
1930 void copymenu()
1931 {
1932         Object *ob, *obt;
1933         Base *base;
1934         Curve *cu, *cu1;
1935         void *poin1, *poin2=0;
1936         short event;
1937         char str[256];
1938         
1939         if(G.scene->id.lib) return;
1940
1941         if(OBACT==0) return;
1942         if(G.obedit) {
1943                 /* obedit_copymenu(); */
1944                 return;
1945         }
1946         
1947         strcpy(str, "COPY %t|Loc%x1|Rot%x2|Size%x3|Drawtype%x4|TimeOffs%x5|Dupli%x6|%l|Mass%x7|Damping%x8|Properties%x9|Logic Bricks%x10");
1948
1949         ob= OBACT;
1950         
1951         if ELEM5(ob->type, OB_MESH, OB_CURVE, OB_SURF, OB_FONT, OB_MBALL) {
1952                 strcat(str, "|Tex Space%x17");
1953                 if(ob->type==OB_MESH) poin2= &(((Mesh *)ob->data)->texflag);
1954                 else if ELEM3(ob->type, OB_CURVE, OB_SURF, OB_FONT) poin2= &(((Curve *)ob->data)->texflag);
1955                 else if(ob->type==OB_MBALL) poin2= &(((MetaBall *)ob->data)->texflag);
1956         }       
1957         
1958         if(ob->type == OB_FONT) strcat(str, "|Font Settings%x18|Bevel Settings%x19");
1959         if(ob->type == OB_CURVE) strcat(str, "|Bevel Settings%x19");
1960
1961         if(ob->type==OB_MESH){
1962                 strcat(str, "|Subdiv%x21");
1963         }
1964
1965         if( give_parteff(ob) ) strcat(str, "|Particle Settings%x20");
1966
1967         strcat (str, "|Object Constraints%x22");
1968
1969         event= pupmenu(str);
1970         if(event<= 0) return;
1971
1972         if(event==9) {
1973                 copymenu_properties(ob);
1974                 return;
1975         }
1976         else if(event==10) {
1977                 copymenu_logicbricks(ob);
1978                 return;
1979         }
1980
1981         base= FIRSTBASE;
1982         while(base) {
1983                 if(base != BASACT) {
1984                         if(TESTBASELIB(base)) {
1985                                 
1986                                 if(event==1) {  /* loc */
1987                                         VECCOPY(base->object->loc, ob->loc);
1988                                         VECCOPY(base->object->dloc, ob->dloc);
1989                                 }
1990                                 else if(event==2) {  /* rot */
1991                                         VECCOPY(base->object->rot, ob->rot);
1992                                         VECCOPY(base->object->drot, ob->drot);
1993                                         VECCOPY(base->object->quat, ob->quat);
1994                                         VECCOPY(base->object->dquat, ob->dquat);
1995                                 }
1996                                 else if(event==3) {  /* size */
1997                                         VECCOPY(base->object->size, ob->size);
1998                                         VECCOPY(base->object->dsize, ob->dsize);
1999                                 }
2000                                 else if(event==4) {  /* drawtype */
2001                                         base->object->dt= ob->dt;
2002                                         base->object->dtx= ob->dtx;
2003                                         }
2004                                 else if(event==5) {  /* time offs */
2005                                         base->object->sf= ob->sf;
2006                                 }
2007                                 else if(event==6) {  /* dupli */
2008                                         base->object->dupon= ob->dupon;
2009                                         base->object->dupoff= ob->dupoff;
2010                                         base->object->dupsta= ob->dupsta;
2011                                         base->object->dupend= ob->dupend;
2012                                         
2013                                         base->object->transflag &= ~OB_DUPLI;
2014                                         base->object->transflag |= (ob->transflag & OB_DUPLI);
2015                                 }
2016                                 else if(event==7) {     /* mass */
2017                                         base->object->mass= ob->mass;
2018                                 }
2019                                 else if(event==8) {     /* damping */
2020                                         base->object->damping= ob->damping;
2021                                         base->object->rdamping= ob->rdamping;
2022                                 }
2023                                 else if(event==17) {    /* tex space */
2024                                         obt= base->object;
2025                                         poin1= 0;
2026                                         if(obt->type==OB_MESH) poin1= &(((Mesh *)obt->data)->texflag);
2027                                         else if ELEM3(obt->type, OB_CURVE, OB_SURF, OB_FONT) poin1= &(((Curve *)obt->data)->texflag);
2028                                         else if(obt->type==OB_MBALL) poin1= &(((MetaBall *)obt->data)->texflag);                                        
2029                                         
2030                                         if(poin1) {
2031                                                 memcpy(poin1, poin2, 4+12+12+12);
2032                                         
2033                                                 if(obt->type==OB_MESH) tex_space_mesh(obt->data);
2034                                                 else if(obt->type==OB_MBALL) tex_space_mball(obt);
2035                                                 else tex_space_curve(obt->data);
2036                                         }
2037                                 }
2038                                 else if(event==18) {    /* font settings */
2039                                         
2040                                         if(base->object->type==ob->type) {
2041                                                 cu= ob->data;
2042                                                 cu1= base->object->data;
2043                                                 
2044                                                 cu1->spacemode= cu->spacemode;
2045                                                 cu1->spacing= cu->spacing;
2046                                                 cu1->linedist= cu->linedist;
2047                                                 cu1->shear= cu->shear;
2048                                                 cu1->fsize= cu->fsize;
2049                                                 cu1->xof= cu->xof;
2050                                                 cu1->yof= cu->yof;
2051                                                 cu1->textoncurve= cu->textoncurve;
2052                                                 if(cu1->vfont) cu1->vfont->id.us--;
2053                                                 cu1->vfont= cu->vfont;
2054                                                 id_us_plus((ID *)cu1->vfont);
2055                                                 text_to_curve(base->object, 0);
2056                                                 
2057                                                 strcpy(cu1->family, cu->family);
2058                                                 
2059                                                 makeDispList(base->object);
2060                                         }
2061                                 }
2062                                 else if(event==19) {    /* bevel settings */
2063                                         
2064                                         if ELEM(base->object->type, OB_CURVE, OB_FONT) {
2065                                                 cu= ob->data;
2066                                                 cu1= base->object->data;
2067                                                 
2068                                                 cu1->bevobj= cu->bevobj;
2069                                                 cu1->width= cu->width;
2070                                                 cu1->bevresol= cu->bevresol;
2071                                                 cu1->ext1= cu->ext1;
2072                                                 cu1->ext2= cu->ext2;
2073                                                 
2074                                                 makeDispList(base->object);
2075                                         }
2076                                 }
2077                                 else if(event==20) {    /* particle settings */
2078                                         PartEff *pa1, *pa2;
2079                                         char *p1, *p2;
2080                                         
2081                                         pa1= give_parteff(ob);
2082                                         pa2= give_parteff(base->object);
2083
2084                                         if(pa1==0 && pa2) {
2085                                                 BLI_remlink( &(base->object->effect), pa2);
2086                                                 free_effect( (Effect *) pa2);
2087                                         }
2088                                         else if(pa1 && pa2==0) {
2089                                                 free_effects(&(base->object->effect));
2090                                                 copy_effects(&(base->object->effect), &ob->effect);
2091                                                 build_particle_system(base->object);
2092                                         }
2093                                         else if(pa1 && pa2) {
2094                                                 if(pa2->keys) MEM_freeN(pa2->keys);
2095                                                 
2096                                                 p1= (char *)pa1; p2= (char *)pa2;
2097                                                 memcpy( p2+8, p1+8, sizeof(PartEff) - 8);
2098                                                 pa2->keys= 0;
2099                                                 
2100                                                 build_particle_system(base->object);
2101                                         }
2102                                 }
2103                                 else if(event==21){
2104                                         if (base->object->type==OB_MESH) {
2105                                                 Mesh *targetme= base->object->data;
2106                                                 Mesh *sourceme= ob->data;
2107
2108                                                 targetme->flag= (targetme->flag&~ME_SUBSURF) | (sourceme->flag&ME_SUBSURF);
2109                                                 targetme->subdiv= sourceme->subdiv;
2110                                                 targetme->subdivr= sourceme->subdivr;
2111                                                 makeDispList(base->object);
2112                                         }
2113                                 }
2114                                 else if(event==22){
2115                                         /* Clear the constraints on the target */
2116                                         free_constraints(&base->object->constraints);
2117                                         free_constraint_channels(&base->object->constraintChannels);
2118
2119                                         /* Copy the constraint channels over */
2120                                         copy_constraints(&base->object->constraints, &ob->constraints);
2121                                         if (U.dupflag&DUPIPO)
2122                                                 copy_constraint_channels(&base->object->constraintChannels, &ob->constraintChannels);
2123                                         else
2124                                                 clone_constraint_channels (&base->object->constraintChannels, &ob->constraintChannels, NULL);
2125
2126                                         base->object->activecon = NULL;
2127                                 }
2128                         }
2129                 }
2130                 base= base->next;
2131         }
2132         
2133         allqueue(REDRAWVIEW3D, 0);
2134         if(event==20) {
2135                 allqueue(REDRAWBUTSOBJECT, 0);
2136         }
2137         
2138 }
2139
2140 void link_to_scene(unsigned short nr)
2141 {       
2142         Scene *sce= (Scene*) BLI_findlink(&G.main->scene, G.curscreen->scenenr-1);
2143         Base *base, *nbase;
2144         
2145         if(sce==0) return;
2146         if(sce->id.lib) return;
2147         
2148         base= FIRSTBASE;
2149         while(base) {
2150                 if(TESTBASE(base)) {
2151                         
2152                         nbase= MEM_mallocN( sizeof(Base), "newbase");
2153                         *nbase= *base;
2154                         BLI_addhead( &(sce->base), nbase);
2155                         id_us_plus((ID *)base->object);
2156                 }
2157                 base= base->next;
2158         }
2159 }
2160
2161 void linkmenu()
2162 {
2163         Object *ob, *obt;
2164         Base *base, *nbase, *sbase;
2165         Scene *sce = NULL;
2166         ID *id;
2167         Material ***matarar, ***obmatarar, **matar1, **matar2;
2168         int a;
2169         short event, *totcolp, nr;
2170         char str[140], *strp;
2171         
2172
2173         if(OBACT==0) return;
2174         ob= OBACT;
2175         
2176         strcpy(str, "MAKE LINKS %t|To scene...%x1|Object Ipo%x4");
2177         
2178         if(ob->type==OB_MESH)
2179                 strcat(str, "|Mesh data%x2|Materials%x3");
2180         else if(ob->type==OB_CURVE)
2181                 strcat(str, "|Curve data%x2|Materials%x3");
2182         else if(ob->type==OB_FONT)
2183                 strcat(str, "|Font data%x2|Materials%x3");
2184         else if(ob->type==OB_SURF)
2185                 strcat(str, "|Surf data%x2|Materials%x3");
2186         else if(ob->type==OB_MBALL)
2187                 strcat(str, "|Materials%x3");
2188         else if(ob->type==OB_CAMERA)
2189                 strcat(str, "|Camera data%x2");
2190         else if(ob->type==OB_LAMP)
2191                 strcat(str, "|Lamp data%x2");
2192         else if(ob->type==OB_LATTICE)
2193                 strcat(str, "|Lattice data%x2");
2194         else if(ob->type==OB_ARMATURE)
2195                 strcat(str, "|Armature data%x2");
2196         event= pupmenu(str);
2197         if(event<= 0) return;
2198
2199         if(event==1) {
2200                 IDnames_to_pupstring(&strp, NULL, NULL, &(G.main->scene), 0, &nr);
2201                 
2202                 if(strncmp(strp, "DataBrow", 8)==0) {
2203                         MEM_freeN(strp);
2204
2205                         activate_databrowse((ID *)G.scene, ID_SCE, 0, B_INFOSCE, &(G.curscreen->scenenr), link_to_scene );
2206                         
2207                         return;                 
2208                 }
2209                 else {
2210                         event= pupmenu(strp);
2211                         MEM_freeN(strp);
2212                 
2213                         if(event<= 0) return;
2214                 
2215                         nr= 1;
2216                         sce= G.main->scene.first;
2217                         while(sce) {
2218                                 if(nr==event) break;
2219                                 nr++;
2220                                 sce= sce->id.next;
2221                         }
2222                         if(sce==G.scene) {
2223                                 error("This is current scene");
2224                                 return;
2225                         }
2226                         if(sce==0 || sce->id.lib) return;
2227                         
2228                         /* remember: is needed below */
2229                         event= 1;
2230                 }
2231         }
2232
2233         base= FIRSTBASE;
2234         while(base) {
2235                 if(event==1 || base != BASACT) {
2236                         
2237                         obt= base->object;
2238
2239                         if(TESTBASE(base)) {
2240                                 
2241                                 if(event==1) {          /* to scene */
2242                                         
2243                                         /* test if already linked */
2244                                         sbase= sce->base.first;
2245                                         while(sbase) {
2246                                                 if(sbase->object==base->object) break;
2247                                                 sbase= sbase->next;
2248                                         }
2249                                         if(sbase) {     /* remove */
2250                                                 base= base->next;
2251                                                 continue;
2252                                         }
2253                                         
2254                                         nbase= MEM_mallocN( sizeof(Base), "newbase");
2255                                         *nbase= *base;
2256                                         BLI_addhead( &(sce->base), nbase);
2257                                         id_us_plus((ID *)base->object);
2258                                 }
2259                         }
2260                         if(TESTBASELIB(base)) {
2261                                 if(event==2 || event==5) {  /* obdata */
2262                                         if(ob->type==obt->type) {
2263                                                 
2264                                                         id= obt->data;
2265                                                         id->us--;
2266                                                         
2267                                                         id= ob->data;
2268                                                         id_us_plus(id);
2269                                                         obt->data= id;
2270                                                         
2271                                                         /* if amount of material indices changed: */
2272                                                         test_object_materials(obt->data);
2273                                                 }
2274                                         }
2275                                 else if(event==4) {  /* ob ipo */
2276                                         if(obt->ipo) obt->ipo->id.us--;
2277                                         obt->ipo= ob->ipo;
2278                                         if(obt->ipo) {
2279                                                 id_us_plus((ID *)obt->ipo);
2280                                                 do_ob_ipo(obt);
2281                                         }
2282                                 }
2283                                 else if(event==3) {  /* materials */
2284                                         
2285                                         /* only if obt has no material: make arrays */
2286                                         /* from ob to obt! */
2287                                         
2288                                         obmatarar= give_matarar(ob);
2289                                         matarar= give_matarar(obt);
2290                                         totcolp= give_totcolp(obt);
2291
2292                                         /* if one of the two is zero: no render-able object */                                          
2293                                         if( matarar && obmatarar) {
2294                                                 
2295                                                 /* take care of users! so first a copy of original: */
2296
2297                                                 if(ob->totcol) {
2298                                                         matar1= MEM_dupallocN(ob->mat);
2299                                                         matar2= MEM_dupallocN(*obmatarar);
2300                                                 }
2301                                                 else {
2302                                                         matar1= matar2= 0;
2303                                                 }
2304                                                 
2305                                                 /* remove links from obt */
2306                                                 for(a=0; a<obt->totcol; a++) {
2307                                                         if(obt->mat[a]) obt->mat[a]->id.us--;
2308                                                         if( (*matarar)[a]) (*matarar)[a]->id.us--;
2309                                                 }
2310                                                 
2311                                                 /* free */
2312                                                 if(obt->mat) MEM_freeN(obt->mat);
2313                                                 if(*matarar) MEM_freeN(*matarar);
2314                                                 
2315                                                 /* connect a copy */
2316                                                 obt->mat= matar1;
2317                                                 *matarar= matar2;
2318                                                 obt->totcol= ob->totcol;
2319                                                 *totcolp= ob->totcol;
2320                                         
2321                                                 /* increase users */
2322                                                 for(a=0; a<obt->totcol; a++) {
2323                                                         if(obt->mat[a]) id_us_plus((ID *)obt->mat[a]);
2324                                                         if( (*matarar)[a]) id_us_plus((ID *)(*matarar)[a]);
2325                                                 }
2326
2327                                                 obt->colbits= ob->colbits;
2328                                                 
2329                                                 /* if amount of material indices changed: */
2330                                                 test_object_materials(obt->data);
2331                                         }
2332                                 }
2333                         }
2334                 }
2335                 base= base->next;
2336         }
2337         
2338         allqueue(REDRAWVIEW3D, 0);
2339         allqueue(REDRAWOOPS, 0);
2340         allqueue(REDRAWBUTSHEAD, 0);
2341 }
2342
2343 void make_duplilist_real()
2344 {
2345         Base *base, *basen;
2346         Object *ob;
2347         extern ListBase duplilist;
2348         
2349         if(okee("Make dupli's real")==0) return;
2350         
2351         base= FIRSTBASE;
2352         while(base) {
2353                 if TESTBASELIB(base) {
2354
2355                         if(base->object->transflag & OB_DUPLI) {
2356                                 
2357                                 make_duplilist(G.scene, base->object);
2358                                 ob= duplilist.first;
2359                                 while(ob) {
2360                                         
2361                                         /* font duplis can have a totcol without material, we get them from parent
2362                                          * should be implemented better...
2363                                          */
2364                                         if(ob->mat==0) ob->totcol= 0;
2365                                         
2366                                         basen= MEM_dupallocN(base);
2367                                         basen->flag &= ~OB_FROMDUPLI;
2368                                         BLI_addhead(&G.scene->base, basen);     /* addhead: othwise eternal loop */
2369                                         ob->ipo= 0;             /* make sure apply works */
2370                                         ob->parent= ob->track= 0;
2371                                         ob->disp.first= ob->disp.last= 0;
2372                                         ob->transflag &= ~OB_DUPLI;
2373                                         basen->object= copy_object(ob);
2374                                         
2375                                         apply_obmat(basen->object);
2376                                         ob= ob->id.next;
2377                                 }
2378                                 
2379                                 free_duplilist();
2380                                 
2381                                 base->object->transflag &= ~OB_DUPLI;   
2382                         }
2383                 }
2384                 base= base->next;
2385         }
2386         
2387         allqueue(REDRAWVIEW3D, 0);
2388         allqueue(REDRAWOOPS, 0);
2389 }
2390
2391 void apply_object()
2392 {
2393         Base *base, *basact;
2394         Object *ob;
2395         Mesh *me;
2396         Curve *cu;
2397         Nurb *nu;
2398         BPoint *bp;
2399         BezTriple *bezt;
2400         MVert *mvert;
2401         float mat[3][3];
2402         int a;
2403
2404         if(G.scene->id.lib) return;
2405         if(G.obedit) return;
2406         basact= BASACT;
2407         
2408         if(G.qual & LR_SHIFTKEY) {
2409                 ob= OBACT;
2410                 if(ob==0) return;
2411                 
2412                 if(ob->transflag & OB_DUPLI) make_duplilist_real();
2413                 else if(ob->parent && ob->parent->type==OB_LATTICE) apply_lattice();
2414                 
2415                 return;
2416         }
2417
2418         if(okee("Apply size/rot")==0) return;
2419
2420         base= FIRSTBASE;
2421         while(base) {
2422                 if TESTBASELIB(base) {
2423                         ob= base->object;
2424         
2425                         if(ob->type==OB_MESH) {
2426                                 object_to_mat3(ob, mat);
2427                                 me= ob->data;
2428                                 
2429                                 if(me->id.us>1) {
2430                                         error("Can't do multi user mesh");
2431                                         return;
2432                                 }
2433                                 if(me->key) {
2434                                         error("Can't do key && mesh");
2435                                         return;
2436                                 }
2437                                 
2438                                 mvert= me->mvert;
2439                                 for(a=0; a<me->totvert; a++, mvert++) {
2440                                         Mat3MulVecfl(mat, mvert->co);
2441                                 }
2442                                 ob->size[0]= ob->size[1]= ob->size[2]= 1.0;
2443                                 ob->rot[0]= ob->rot[1]= ob->rot[2]= 0.0;
2444                                 QuatOne(ob->quat);
2445                                 
2446                                 where_is_object(ob);
2447                                 
2448                                 /* texspace and normals */
2449                                 BASACT= base;
2450                                 enter_editmode();
2451                                 exit_editmode(1);
2452                                 BASACT= basact;                         
2453                                 
2454                         }
2455                         else if (ob->type==OB_ARMATURE){
2456                                 bArmature *arm;
2457
2458                                 object_to_mat3(ob, mat);
2459                                 arm= ob->data;
2460                                 if(arm->id.us>1) {
2461                                         error("Can't do multi user armature");
2462                                         return;
2463                                 }
2464
2465                                 apply_rot_armature (ob, mat);
2466                                 /* Reset the object's transforms */
2467                                 ob->size[0]= ob->size[1]= ob->size[2]= 1.0;
2468                                 ob->rot[0]= ob->rot[1]= ob->rot[2]= 0.0;
2469                                 QuatOne(ob->quat);
2470                                 
2471                                 where_is_object(ob);
2472                         }
2473                         else if ELEM(ob->type, OB_CURVE, OB_SURF) {
2474                                 object_to_mat3(ob, mat);
2475                                 cu= ob->data;
2476                                 
2477                                 if(cu->id.us>1) {
2478                                         error("Can't do multi user curve");
2479                                         return;
2480                                 }
2481                                 if(cu->key) {
2482                                         error("Can't do keys");
2483                                         return;
2484                                 }
2485                                 
2486                                 nu= cu->nurb.first;
2487                                 while(nu) {
2488                                         if( (nu->type & 7)==1) {
2489                                                 a= nu->pntsu;
2490                                                 bezt= nu->bezt;
2491                                                 while(a--) {
2492                                                         Mat3MulVecfl(mat, bezt->vec[0]);
2493                                                         Mat3MulVecfl(mat, bezt->vec[1]);
2494                                                         Mat3MulVecfl(mat, bezt->vec[2]);
2495                                                         bezt++;
2496                                                 }
2497                                         }
2498                                         else {
2499                                                 a= nu->pntsu*nu->pntsv;
2500                                                 bp= nu->bp;
2501                                                 while(a--) {
2502                                                         Mat3MulVecfl(mat, bp->vec);
2503                                                         bp++;
2504                                                 }
2505                                         }
2506                                         nu= nu->next;
2507                                 }
2508                         
2509                                 ob->size[0]= ob->size[1]= ob->size[2]= 1.0;
2510                                 ob->rot[0]= ob->rot[1]= ob->rot[2]= 0.0;
2511                                 QuatOne(ob->quat);
2512                                 
2513                                 where_is_object(ob);
2514                                 
2515                                 /* texspace and normals */
2516                                 BASACT= base;
2517                                 enter_editmode();
2518                                 exit_editmode(1);
2519                                 BASACT= basact;
2520                         }
2521                 }
2522                 base= base->next;
2523         }
2524         
2525         allqueue(REDRAWVIEW3D, 0);
2526 }
2527
2528
2529
2530 /* ************ GENERAL  *************** */
2531
2532 static Object *is_a_parent_selected_int(Object *startob, Object *ob, GHash *done_hash) {
2533         if (ob!=startob && TESTBASE(ob))
2534                 return ob;
2535                 
2536         if (BLI_ghash_haskey(done_hash, ob))
2537                 return NULL;
2538         else
2539                 BLI_ghash_insert(done_hash, ob, NULL);
2540         
2541         if (ob->parent) {
2542                 Object *par= is_a_parent_selected_int(startob, ob->parent, done_hash);
2543                 if (par)
2544                         return par;
2545         }
2546
2547         /* IK is more complex in parents... */
2548         
2549         /* XXX, should we be handling armatures or constraints here? - zr */
2550
2551         if(ob->type==OB_IKA) {
2552                 Ika *ika= ob->data;
2553                 
2554                 if (ika->def) {
2555                         int i;
2556                         
2557                         for (i=0; i<ika->totdef; i++) {
2558                                 Deform *def= &ika->def[i];
2559                                 
2560                                 if (def->ob && ob!=def->ob && def->ob!=startob) {
2561                                         Object *par= is_a_parent_selected_int(startob, def->ob, done_hash);
2562                                         if (par)
2563                                                 return par;
2564                                 }
2565                         }
2566                 }
2567                 
2568                 if (ika->parent) {
2569                         Object *par= is_a_parent_selected_int(startob, ika->parent, done_hash);
2570                         if (par)
2571                                 return par;
2572                 }
2573         }
2574         
2575         return NULL;
2576 }
2577
2578 static Object *is_a_parent_selected(Object *ob) {
2579         GHash *gh= BLI_ghash_new(BLI_ghashutil_ptrhash, BLI_ghashutil_ptrcmp);
2580         Object *res= is_a_parent_selected_int(ob, ob, gh);
2581         BLI_ghash_free(gh, NULL, NULL);
2582         
2583         return res;
2584 }
2585
2586 static void setbaseflags_for_editing(int mode)  /* 0,'g','r','s' */
2587 {
2588         /*
2589                 if base selected and has parent selected:
2590                         base->flag= BA_WASSEL+BA_PARSEL
2591                 if base not selected and parent selected:
2592                         base->flag= BA_PARSEL
2593                                 
2594         */
2595         GHash *object_to_base_hash= NULL; /* built on demand, see below - zr */
2596         Base *base;
2597         
2598         copy_baseflags();
2599
2600         for (base= FIRSTBASE; base; base= base->next) {
2601                 base->flag &= ~(BA_PARSEL+BA_WASSEL);
2602                         
2603                 if( (base->lay & G.vd->lay) && base->object->id.lib==0) {
2604                         Object *ob= base->object;
2605                         Object *parsel= is_a_parent_selected(ob);
2606                         
2607                         /* parentkey here? */
2608
2609                         if(parsel) {
2610                                 if(base->flag & SELECT) {
2611                                         base->flag &= ~SELECT;
2612                                         base->flag |= (BA_PARSEL+BA_WASSEL);
2613                                 }
2614                                 else base->flag |= BA_PARSEL;
2615                         }
2616
2617                         if(mode=='g')  {
2618                                 if(ob->track && TESTBASE(ob->track) && (base->flag & SELECT)==0)  
2619                                         base->flag |= BA_PARSEL;
2620                         }
2621                         
2622                         /* updates? */
2623                         if(ob->type==OB_IKA) {
2624                                 Ika *ika= ob->data;
2625                                 if(ika->parent && parsel) base->flag |= BA_WHERE_UPDATE;
2626                         }
2627                         
2628                         if(base->flag & (SELECT | BA_PARSEL)) {
2629                                 
2630                                 base->flag |= BA_WHERE_UPDATE;
2631                                 
2632                                 if(ob->parent) {
2633                                         if(ob->parent->type==OB_LATTICE) base->flag |= BA_DISP_UPDATE;
2634                                         if(ob->parent->type==OB_IKA && ob->partype==PARSKEL) base->flag |= BA_DISP_UPDATE;
2635                                         if(ob->parent->type==OB_ARMATURE && ob->partype==PARSKEL) base->flag |= BA_DISP_UPDATE;
2636                                 }
2637                                 if(ob->track) {
2638                                         ;
2639                                 }
2640                                 
2641                                 if( give_parteff(ob) ) base->flag |= BA_DISP_UPDATE;
2642                                 
2643                                 if(ob->type==OB_MBALL) {
2644                                         Base *b;
2645                                         
2646                                                 /* Only bother building the object to base
2647                                                  * hash if we are going to be needing it... - zr
2648                                                  */
2649                                         if (!object_to_base_hash) {
2650                                                 object_to_base_hash= BLI_ghash_new(BLI_ghashutil_ptrhash, BLI_ghashutil_ptrcmp);
2651                                                 
2652                                                 for (b= FIRSTBASE; b; b= b->next)
2653                                                         BLI_ghash_insert(object_to_base_hash, b->object, b);
2654                                         }
2655
2656                                         b= BLI_ghash_lookup(object_to_base_hash, find_basis_mball(ob));
2657                                         b->flag |= BA_DISP_UPDATE;
2658                                 }
2659                         }
2660                 }
2661         }
2662         
2663         if (object_to_base_hash)
2664                 BLI_ghash_free(object_to_base_hash, NULL, NULL);
2665 }
2666
2667
2668 void clearbaseflags_for_editing()
2669 {
2670         Base *base;
2671
2672         base= FIRSTBASE;
2673         while(base) {
2674                 if(base->flag & BA_WASSEL) base->flag |= SELECT;
2675                 base->flag &= ~(BA_PARSEL+BA_WASSEL);
2676                 
2677                 base->flag &= ~(BA_DISP_UPDATE+BA_WHERE_UPDATE+BA_DO_IPO);
2678                 
2679                 base= base->next;
2680         }
2681         copy_baseflags();
2682 }
2683
2684 void ob_to_transob(Object *ob, TransOb *tob)
2685 {
2686         float totmat[3][3];
2687         Object *tr;
2688         void *cfirst, *clast;
2689         
2690         tob->ob= ob;
2691         
2692         cfirst = ob->constraints.first;
2693         clast = ob->constraints.last;
2694         
2695         ob->constraints.first=ob->constraints.last=NULL;
2696         
2697         tr= ob->track;
2698         ob->track= 0;
2699         where_is_object(ob);
2700         ob->track= tr;
2701         ob->constraints.first = cfirst;
2702         ob->constraints.last = clast;
2703
2704         
2705         
2706         tob->loc= ob->loc;
2707         VECCOPY(tob->oldloc, tob->loc);
2708         
2709         tob->rot= ob->rot;
2710         VECCOPY(tob->oldrot, ob->rot);
2711         VECCOPY(tob->olddrot, ob->drot);
2712         
2713         tob->quat= ob->quat;
2714         QUATCOPY(tob->oldquat, ob->quat);
2715         QUATCOPY(tob->olddquat, ob->dquat);
2716
2717         tob->size= ob->size;
2718         VECCOPY(tob->oldsize, ob->size);
2719
2720         VECCOPY(tob->olddsize, ob->dsize);
2721
2722         /* only object, not parent */
2723         object_to_mat3(ob, tob->obmat);
2724         Mat3Inv(tob->obinv, tob->obmat);
2725         
2726         Mat3CpyMat4(totmat, ob->obmat);
2727
2728         /* this is totmat without obmat: so a parmat */
2729         Mat3MulMat3(tob->parmat, totmat, tob->obinv);
2730         Mat3Inv(tob->parinv, tob->parmat);
2731
2732         Mat3MulMat3(tob->axismat, tob->parmat, tob->obmat);     // New!
2733         Mat3Ortho(tob->axismat);
2734
2735         VECCOPY(tob->obvec, ob->obmat[3]);
2736
2737         centroid[0]+= tob->obvec[0];
2738         centroid[1]+= tob->obvec[1];
2739         centroid[2]+= tob->obvec[2];
2740
2741         tob->eff= 0;
2742
2743         if(ob->type==OB_IKA) {
2744                 Ika *ika=ob->data;
2745                 
2746                 calc_ika(ika, 0);
2747                 
2748                 ika->effn[0]= ika->eff[0];
2749                 ika->effn[1]= ika->eff[1];
2750                 ika->effn[2]= 0.0;      
2751                 
2752                 VecMat4MulVecfl(ika->effg, ob->obmat, ika->effn);
2753                 
2754                 if(ika->flag & IK_GRABEFF) {
2755
2756                         tob->eff= ika->effg;
2757                         VECCOPY(tob->oldeff, tob->eff);
2758                         tob->flag |= TOB_IKA;
2759
2760                         tob->loc= 0;
2761                 }
2762                 
2763         }
2764 }
2765
2766 void ob_to_tex_transob(Object *ob, TransOb *tob)
2767 {
2768         Mesh *me;
2769         Curve *cu;
2770         MetaBall *mb;
2771         ID *id;
2772         
2773         ob_to_transob(ob, tob);
2774         
2775         id= ob->data;
2776         if(id==0);
2777         else if( GS(id->name)==ID_ME) {
2778                 me= ob->data;
2779                 me->texflag &= ~AUTOSPACE;
2780                 tob->loc= me->loc;
2781                 tob->rot= me->rot;
2782                 tob->size= me->size;
2783         }
2784         else if( GS(id->name)==ID_CU) {
2785                 cu= ob->data;
2786                 cu->texflag &= ~AUTOSPACE;
2787                 tob->loc= cu->loc;
2788                 tob->rot= cu->rot;
2789                 tob->size= cu->size;
2790         }
2791         else if( GS(id->name)==ID_MB) {
2792                 mb= ob->data;
2793                 mb->texflag &= ~AUTOSPACE;
2794                 tob->loc= mb->loc;
2795                 tob->rot= mb->rot;
2796                 tob->size= mb->size;
2797         }
2798         
2799         VECCOPY(tob->oldloc, tob->loc);
2800         VECCOPY(tob->oldrot, tob->rot);
2801         VECCOPY(tob->oldsize, tob->size);
2802 }
2803
2804 void make_trans_objects()
2805 {
2806         Base *base;
2807         Object *ob;
2808         TransOb *tob = NULL;
2809         ListBase elems;
2810         IpoKey *ik;
2811         float cfraont, min[3], max[3];
2812         int ipoflag;
2813         
2814         tottrans= 0;
2815         
2816         INIT_MINMAX(min, max);
2817         centroid[0]=centroid[1]=centroid[2]= 0.0;
2818         
2819         /* count */     
2820         base= FIRSTBASE;
2821         while(base) {
2822                 if TESTBASELIB(base) {
2823                         ob= base->object;
2824                         
2825                         if(transmode==TRANS_TEX) {
2826                                 if(ob->dtx & OB_TEXSPACE) tottrans++;
2827                         }
2828                         else {
2829                                 if(ob->ipo && ob->ipo->showkey && (ob->ipoflag & OB_DRAWKEY)) {
2830                                         elems.first= elems.last= 0;
2831                                         make_ipokey_transform(ob, &elems, 1); /* '1' only selected keys */
2832                                         
2833                                         pushdata(&elems, sizeof(ListBase));
2834                                         
2835                                         ik= elems.first;
2836                                         while(ik) {
2837                                                 tottrans++;
2838                                                 ik= ik->next;
2839                                         }
2840                                         if(elems.first==0) tottrans++;
2841                                 }
2842                                 else tottrans++;
2843                         }
2844                 }
2845                 base= base->next;
2846         }
2847
2848         if(tottrans) tob= transmain= MEM_mallocN(tottrans*sizeof(TransOb), "transmain");
2849         
2850         reset_slowparents();
2851
2852
2853         /*also do this below when tottrans==0, because of freeing pushpop and ipokeys */
2854         
2855         base= FIRSTBASE;
2856         while(base) {
2857                 if TESTBASELIB(base) {
2858                         ob= base->object;
2859                         
2860                         if(transmode==TRANS_TEX) {
2861                                 if(ob->dtx & OB_TEXSPACE) {
2862                                         tob->flag= 0;
2863