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