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