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