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