c7f4de25c7ecaf68f86de90b1fbb399c26680b0c
[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);
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);
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);
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                                 base->object->recalc |= OB_RECALC_OB|OB_RECALC_DATA;
1824                         }
1825                 }
1826                 base= base->next;
1827         }
1828
1829         allqueue(REDRAWVIEW3D, 0);
1830         DAG_scene_flush_update(G.scene);
1831         BIF_undo_push("Do Centre");     
1832 }
1833
1834 void docentre_new(void)
1835 {
1836         if(G.scene->id.lib) return;
1837
1838         if(G.obedit) {
1839                 error("Unable to center new in Edit Mode");
1840         }
1841         else {
1842                 docentre(1);
1843         }
1844 }
1845
1846 void docentre_cursor(void)
1847 {
1848         if(G.scene->id.lib) return;
1849
1850         if(G.obedit) {
1851                 error("Unable to center cursor in Edit Mode");
1852         }
1853         else {
1854                 docentre(2);
1855         }
1856 }
1857
1858 void movetolayer(void)
1859 {
1860         Base *base;
1861         unsigned int lay= 0, local;
1862         int islamp= 0;
1863         
1864         if(G.scene->id.lib) return;
1865
1866         base= FIRSTBASE;
1867         while(base) {
1868                 if TESTBASE(base) lay |= base->lay;
1869                 base= base->next;
1870         }
1871         if(lay==0) return;
1872         lay &= 0xFFFFFF;
1873         
1874         if( movetolayer_buts(&lay)==0 ) return;
1875         if(lay==0) return;
1876
1877         base= FIRSTBASE;
1878         while(base) {
1879                 if TESTBASE(base) {
1880                         /* upper byte is used for local view */
1881                         local= base->lay & 0xFF000000;  
1882                         base->lay= lay + local;
1883                         base->object->lay= lay;
1884                         if(base->object->type==OB_LAMP) islamp= 1;
1885                 }
1886                 base= base->next;
1887         }
1888         
1889         if(islamp && G.vd->drawtype == OB_SHADED) reshadeall_displist();
1890
1891         countall();
1892         allqueue(REDRAWBUTSEDIT, 0);
1893         allqueue(REDRAWVIEW3D, 0);
1894         allqueue(REDRAWOOPS, 0);
1895         allqueue(REDRAWINFO, 0);
1896         
1897         BIF_undo_push("Move to layer");
1898 }
1899
1900 void split_font()
1901 {
1902         Object *ob = OBACT;
1903         Base *oldbase = BASACT;
1904         Curve *cu= ob->data;
1905         char *p= cu->str;
1906         int slen= strlen(p);
1907         int i;
1908
1909         for (i = 0; i<=slen; p++, i++) {
1910                 adduplicate(1);
1911                 cu= OBACT->data;
1912                 cu->sepchar = i+1;
1913                 text_to_curve(OBACT, 0);        // pass 1: only one letter, adapt position
1914                 text_to_curve(OBACT, 0);        // pass 2: remake
1915                 freedisplist(&OBACT->disp);
1916                 makeDispListCurveTypes(OBACT, 0);
1917                 
1918                 OBACT->flag &= ~SELECT;
1919                 BASACT->flag &= ~SELECT;
1920                 oldbase->flag |= SELECT;
1921                 oldbase->object->flag |= SELECT;
1922                 set_active_base(oldbase);               
1923         }
1924 }
1925
1926 void special_editmenu(void)
1927 {
1928         Object *ob= OBACT;
1929         float fac;
1930         int nr,ret;
1931         short randfac,numcuts;
1932         
1933         if(ob==NULL) return;
1934         
1935         if(G.obedit==NULL) {
1936                 
1937                 if(ob->flag & OB_POSEMODE) {
1938                         pose_special_editmenu();
1939                 }
1940                 else if(G.f & G_FACESELECT) {
1941                         Mesh *me= get_mesh(ob);
1942                         TFace *tface;
1943                         int a;
1944                         
1945                         if(me==0 || me->tface==0) return;
1946                         
1947                         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");
1948         
1949                         for(a=me->totface, tface= me->tface; a>0; a--, tface++) {
1950                                 if(tface->flag & SELECT) {
1951                                         switch(nr) {
1952                                         case 1:
1953                                                 tface->mode |= TF_TEX; break;
1954                                         case 2:
1955                                                 tface->mode |= TF_SHAREDCOL; break;
1956                                         case 3:
1957                                                 tface->mode |= TF_LIGHT; break; 
1958                                         case 4:
1959                                                 tface->mode |= TF_INVISIBLE; break;
1960                                         case 5:
1961                                                 tface->mode |= TF_DYNAMIC; break;
1962                                         case 6:
1963                                                 tface->mode &= ~TF_TEX;
1964                                                 tface->tpage= 0;
1965                                                 break;
1966                                         case 7:
1967                                                 tface->mode &= ~TF_SHAREDCOL; break;
1968                                         case 8:
1969                                                 tface->mode &= ~TF_LIGHT; break;
1970                                         case 9:
1971                                                 tface->mode &= ~TF_INVISIBLE; break;
1972                                         case 10:
1973                                                 tface->mode &= ~TF_DYNAMIC; break;
1974                                         }
1975                                 }
1976                         }
1977                         DAG_object_flush_update(G.scene, ob, OB_RECALC_DATA);
1978                         allqueue(REDRAWVIEW3D, 0);
1979                         allqueue(REDRAWBUTSEDIT, 0);
1980                         BIF_undo_push("Change texture face");
1981                 }
1982                 else if(G.f & G_VERTEXPAINT) {
1983                         Mesh *me= get_mesh(ob);
1984                         
1985                         if(me==0 || (me->mcol==NULL && me->tface==NULL) ) return;
1986                         
1987                         nr= pupmenu("Specials%t|Shared VertexCol%x1");
1988                         if(nr==1) {
1989                                 
1990                                 if(me->tface) tface_to_mcol(me);
1991                                 
1992                                 copy_vpaint_undo( (unsigned int *)me->mcol, me->totface);
1993                                 do_shared_vertexcol(me);
1994                                 
1995                                 if(me->tface) mcol_to_tface(me, 1);
1996                                 BIF_undo_push("Shared VertexCol");
1997
1998                                 DAG_object_flush_update(G.scene, ob, OB_RECALC_DATA);
1999                         }
2000                 }
2001                 else if(G.f & G_WEIGHTPAINT) {
2002                         if(ob->parent && (ob->parent->flag & OB_POSEMODE)) {
2003                                 nr= pupmenu("Specials%t|Apply Bone Envelopes to VertexGroups %x1");
2004                                 if(nr==1) {
2005                                         Mesh *me= ob->data;
2006                                         if(me->dvert) copy_wpaint_undo(me->dvert, me->totvert);
2007                                         pose_adds_vgroups(ob);
2008                                 }
2009                         }
2010                 }
2011                 else {
2012                         Base *base, *base_select= NULL;
2013                         
2014                         // Get the active object mesh.
2015                         Mesh *me= get_mesh(ob);
2016
2017                         // Booleans, if the active object is a mesh...
2018                         if (me && ob->id.lib==NULL) {
2019                                 
2020                                 // Bring up a little menu with the boolean operation choices on.
2021                                 nr= pupmenu("Boolean Tools%t|Intersect%x1|Union%x2|Difference%x3|Add Intersect Modifier%x4|Add Union Modifier%x5|Add Difference Modifier%x6");
2022
2023                                 if (nr > 0) {
2024                                         // user has made a choice of a menu element.
2025                                         // All of the boolean functions require 2 mesh objects 
2026                                         // we search through the object list to find the other 
2027                                         // selected item and make sure it is distinct and a mesh.
2028
2029                                         for(base= FIRSTBASE; base; base= base->next) {
2030                                                 if TESTBASELIB(base) {
2031                                                         if(base->object != ob) base_select= base;
2032                                                 }
2033                                         }
2034
2035                                         if (base_select) {
2036                                                 if (get_mesh(base_select->object)) {
2037                                                         if(nr <= 3){
2038                                                                 waitcursor(1);
2039                                                                 ret = NewBooleanMesh(BASACT,base_select,nr);
2040                                                                 if (ret==0) {
2041                                                                         error("An internal error occurred -- sorry!");
2042                                                                 } else if(ret==-1) {
2043                                                                         error("Selected meshes must have faces to perform boolean operations");
2044                                                                 }
2045                                                                 else BIF_undo_push("Boolean");
2046                                                                 waitcursor(0);
2047                                                         } else {
2048                                                                 BooleanModifierData *bmd = NULL;
2049                                                                 bmd = (BooleanModifierData *)modifier_new(eModifierType_Boolean);
2050                                                                 BLI_addtail(&ob->modifiers, bmd);
2051                                                                 bmd->object = base_select->object;
2052                                                                 bmd->modifier.mode |= eModifierMode_Realtime;
2053                                                                 switch(nr){
2054                                                                         case 4: bmd->operation = eBooleanModifierOp_Intersect; break;
2055                                                                         case 5: bmd->operation = eBooleanModifierOp_Union; break;
2056                                                                         case 6: bmd->operation = eBooleanModifierOp_Difference; break;
2057                                                                 }
2058                                                                 do_common_editbuts(B_CHANGEDEP);
2059                                                                 BIF_undo_push("Add Boolean modifier");                                                          
2060                                                         }                                                               
2061                                                 } else {
2062                                                         error("Please select 2 meshes");
2063                                                 }
2064                                         } else {
2065                                                 error("Please select 2 meshes");
2066                                         }
2067                                 }
2068
2069                                 allqueue(REDRAWVIEW3D, 0);
2070                         }
2071                         else if (ob->type == OB_FONT) {
2072                                 nr= pupmenu("Split %t|Characters%x1");
2073                                 if (nr > 0) {
2074                                         switch(nr) {
2075                                                 case 1: split_font();
2076                                         }
2077                                 }
2078                         }                       
2079                 }
2080         }
2081         else if(G.obedit->type==OB_MESH) {
2082
2083                 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");
2084                 
2085                 switch(nr) {
2086                 case 1:
2087                         numcuts = 1;
2088                         waitcursor(1);
2089                         esubdivideflag(1, 0.0, G.scene->toolsettings->editbutflag,numcuts,0);
2090                         
2091                         BIF_undo_push("ESubdivide Single");            
2092                         break;
2093                 case 2:
2094                 numcuts = 2;
2095                         if(button(&numcuts, 1, 128, "Number of Cuts:")==0) return;
2096                         waitcursor(1);
2097                         esubdivideflag(1, 0.0, G.scene->toolsettings->editbutflag,numcuts,0);
2098                         BIF_undo_push("ESubdivide");
2099                         break;
2100                 case 3:
2101                         numcuts = 2;
2102                         if(button(&numcuts, 1, 128, "Number of Cuts:")==0) return;
2103                         waitcursor(1);
2104                         randfac= 10;
2105                         if(button(&randfac, 1, 100, "Rand fac:")==0) return;
2106                         fac= -( (float)randfac )/100;
2107                         esubdivideflag(1, fac, G.scene->toolsettings->editbutflag,numcuts,0);
2108                         BIF_undo_push("Subdivide Fractal");
2109                         break;
2110
2111                 case 4:
2112                         mergemenu();
2113                         break;
2114                 case 5:
2115                         notice("Removed %d Vertices", removedoublesflag(1, G.scene->toolsettings->doublimit));
2116                         BIF_undo_push("Remove Doubles");
2117                         break;
2118                 case 6:
2119                         hide_mesh(0);
2120                         break;
2121                 case 7:
2122                         reveal_mesh();
2123                         break;
2124                 case 8:
2125                         selectswap_mesh();
2126                         break;
2127                 case 9:
2128                         flip_editnormals();
2129                         BIF_undo_push("Flip Normals");
2130                         break;
2131                 case 10:
2132                         vertexsmooth();
2133                         break;
2134                 case 11:
2135                         bevel_menu();
2136                         break;
2137                 case 12:
2138                         numcuts = 2;
2139                         if(button(&numcuts, 1, 128, "Number of Cuts:")==0) return;
2140                         waitcursor(1);
2141                         esubdivideflag(1, 0.0, G.scene->toolsettings->editbutflag | B_SMOOTH,numcuts,0);
2142                         BIF_undo_push("Subdivide Smooth");
2143                         break;          
2144                 case 13:
2145                         waitcursor(1);
2146                         subdivideflag(1, 0.0, G.scene->toolsettings->editbutflag | B_SMOOTH);
2147                         BIF_undo_push("Subdivide Smooth");
2148                         break;
2149                 case 14:
2150                         mesh_set_smooth_faces(1);
2151                         break;
2152                 case 15: 
2153                         mesh_set_smooth_faces(0);
2154                         break;
2155                 }
2156                 
2157                 DAG_object_flush_update(G.scene, G.obedit, OB_RECALC_DATA);
2158                 
2159                 if(nr>0) waitcursor(0);
2160                 
2161         }
2162         else if ELEM(G.obedit->type, OB_CURVE, OB_SURF) {
2163
2164                 nr= pupmenu("Specials%t|Subdivide%x1|Switch Direction%x2");
2165                 
2166                 switch(nr) {
2167                 case 1:
2168                         subdivideNurb();
2169                         break;
2170                 case 2:
2171                         switchdirectionNurb2();
2172                         break;
2173                 }
2174                 
2175                 DAG_object_flush_update(G.scene, G.obedit, OB_RECALC_DATA);
2176         }
2177         else if(G.obedit->type==OB_ARMATURE) {
2178                 nr= pupmenu("Specials%t|Subdivide %x1|Flip Left-Right Names%x2");
2179                 if(nr==1)
2180                         subdivide_armature();
2181                 else if(nr==2)
2182                         armature_flip_names();
2183         }
2184
2185         countall();
2186         allqueue(REDRAWVIEW3D, 0);
2187         
2188 }
2189
2190 void convertmenu(void)
2191 {
2192         Base *base, *basen, *basact, *basedel=NULL;
2193         Object *ob, *ob1;
2194         Curve *cu;
2195         MetaBall *mb;
2196         Mesh *me;
2197         DispList *dl;
2198         int ok=0, nr = 0, a;
2199         
2200         if(G.scene->id.lib) return;
2201
2202         ob= OBACT;
2203         if(ob==0) return;
2204         if(G.obedit) return;
2205         
2206         basact= BASACT; /* will be restored */
2207                 
2208         if(ob->type==OB_FONT) {
2209                 nr= pupmenu("Convert Font to%t|Curve");
2210                 if(nr>0) ok= 1;
2211         }
2212         else if(ob->type==OB_MBALL) {
2213                 nr= pupmenu("Convert Metaball to%t|Mesh (keep original)%x1|Mesh (Delete Original)%x2");
2214                 if(nr>0) ok= 1;
2215         }
2216         else if(ob->type==OB_CURVE) {
2217                 nr= pupmenu("Convert Curve to%t|Mesh");
2218                 if(nr>0) ok= 1;
2219         }
2220         else if(ob->type==OB_SURF) {
2221                 nr= pupmenu("Convert Nurbs Surface to%t|Mesh");
2222                 if(nr>0) ok= 1;
2223         }
2224         else if(ob->type==OB_MESH) {
2225                 nr= pupmenu("Convert Modifiers to%t|Mesh (Keep Original)%x1|Mesh (Delete Original)%x2");
2226                 if(nr>0) ok= 1;
2227         }
2228         if(ok==0) return;
2229
2230         /* don't forget multiple users! */
2231
2232         /* reset flags */
2233         base= FIRSTBASE;
2234         while(base) {
2235                 if TESTBASELIB(base) {
2236                         base->object->flag &= ~OB_DONE;
2237                 }
2238                 base= base->next;
2239         }
2240
2241         base= FIRSTBASE;
2242         while(base) {
2243                 if TESTBASELIB(base) {
2244                         
2245                         ob= base->object;
2246                         
2247                         if(ob->flag & OB_DONE);
2248                         else if(ob->type==OB_MESH) {
2249                                 Mesh *oldme= ob->data;
2250                                 DispListMesh *dlm;
2251                                 DerivedMesh *dm;
2252
2253                                 basedel = base;
2254
2255                                 ob->flag |= OB_DONE;
2256
2257                                 ob1= copy_object(ob);
2258                                 ob1->recalc |= OB_RECALC;
2259                                 object_free_modifiers(ob1);
2260
2261                                 basen= MEM_mallocN(sizeof(Base), "duplibase");
2262                                 *basen= *base;
2263                                 BLI_addhead(&G.scene->base, basen);     /* addhead: otherwise eternal loop */
2264                                 basen->object= ob1;
2265                                 basen->flag &= ~SELECT;
2266                                         
2267                                 me= ob1->data;
2268                                 me->id.us--;
2269                                         
2270                                 ob1->data= add_mesh();
2271                                 G.totmesh++;
2272                                 ob1->type= OB_MESH;
2273
2274                                 me= ob1->data;
2275                                 me->totcol= oldme->totcol;
2276                                 if(ob1->totcol) {
2277                                         me->mat= MEM_dupallocN(oldme->mat);
2278                                         for(a=0; a<ob1->totcol; a++) id_us_plus((ID *)me->mat[a]);
2279                                 }
2280
2281                                 dm= mesh_create_derived_no_deform(ob, NULL);
2282                                 dlm= dm->convertToDispListMesh(dm, 0);
2283                                 displistmesh_to_mesh(dlm, ob1->data);
2284                                 dm->release(dm);
2285                         }
2286                         else if(ob->type==OB_FONT) {
2287                                 if(nr==1) {
2288                                 
2289                                         ob->flag |= OB_DONE;
2290                                 
2291                                         ob->type= OB_CURVE;
2292                                         cu= ob->data;
2293                                         
2294                                         if(cu->vfont) {
2295                                                 cu->vfont->id.us--;
2296                                                 cu->vfont= 0;
2297                                         }
2298                                         if(cu->vfontb) {
2299                                                 cu->vfontb->id.us--;
2300                                                 cu->vfontb= 0;
2301                                         }
2302                                         if(cu->vfonti) {
2303                                                 cu->vfonti->id.us--;
2304                                                 cu->vfonti= 0;
2305                                         }
2306                                         if(cu->vfontbi) {
2307                                                 cu->vfontbi->id.us--;
2308                                                 cu->vfontbi= 0;
2309                                         }                                       
2310                                         /* other users */
2311                                         if(cu->id.us>1) {
2312                                                 ob1= G.main->object.first;
2313                                                 while(ob1) {
2314                                                         if(ob1->data==cu) {
2315                                                                 ob1->type= OB_CURVE;
2316                                                                 ob1->recalc |= OB_RECALC;
2317                                                         }
2318                                                         ob1= ob1->id.next;
2319                                                 }
2320                                         }
2321                                 }
2322                         }
2323                         else if ELEM(ob->type, OB_CURVE, OB_SURF) {
2324                                 if(nr==1) {
2325
2326                                         ob->flag |= OB_DONE;
2327                                         cu= ob->data;
2328                                         
2329                                         dl= cu->disp.first;
2330                                         if(dl==0) makeDispListCurveTypes(ob, 0);                // force creation
2331
2332                                         nurbs_to_mesh(ob); /* also does users */
2333
2334                                         /* texspace and normals */
2335                                         BASACT= base;
2336                                         enter_editmode();
2337                                         exit_editmode(1); // freedata, but no undo
2338                                         BASACT= basact;
2339                                 }
2340                         }
2341                         else if(ob->type==OB_MBALL) {
2342                         
2343                                 if(nr==1 || nr == 2) {
2344                                         ob= find_basis_mball(ob);
2345                                         
2346                                         if(ob->disp.first && !(ob->flag&OB_DONE)) {
2347                                                 basedel = base;
2348                                         
2349                                                 ob->flag |= OB_DONE;
2350
2351                                                 ob1= copy_object(ob);
2352                                                 ob1->recalc |= OB_RECALC;
2353
2354                                                 basen= MEM_mallocN(sizeof(Base), "duplibase");
2355                                                 *basen= *base;
2356                                                 BLI_addhead(&G.scene->base, basen);     /* addhead: othwise eternal loop */
2357                                                 basen->object= ob1;
2358                                                 basen->flag &= ~SELECT;
2359                                                 
2360                                                 mb= ob1->data;
2361                                                 mb->id.us--;
2362                                                 
2363                                                 ob1->data= add_mesh();
2364                                                 G.totmesh++;
2365                                                 ob1->type= OB_MESH;
2366                                                 
2367                                                 me= ob1->data;
2368                                                 me->totcol= mb->totcol;
2369                                                 if(ob1->totcol) {
2370                                                         me->mat= MEM_dupallocN(mb->mat);
2371                                                         for(a=0; a<ob1->totcol; a++) id_us_plus((ID *)me->mat[a]);
2372                                                 }
2373                                                 
2374                                                 mball_to_mesh(&ob->disp, ob1->data);
2375                                         }
2376                                 }
2377                         }
2378                 }
2379                 base= base->next;
2380                 if(basedel != NULL && nr == 2)
2381                         free_and_unlink_base(basedel);  
2382                 basedel = NULL;                         
2383         }
2384
2385         countall();
2386         allqueue(REDRAWVIEW3D, 0);
2387         allqueue(REDRAWOOPS, 0);
2388         allqueue(REDRAWBUTSEDIT, 0);
2389         BIF_undo_push("Convert Object");
2390
2391         DAG_scene_sort(G.scene);
2392 }
2393
2394         /* Change subdivision properties of mesh object ob, if
2395          * level==-1 then toggle subsurf, else set to level.
2396          */
2397 void flip_subdivison(Object *ob, int level)
2398 {
2399         ModifierData *md = modifiers_findByType(ob, eModifierType_Subsurf);
2400
2401         if (md) {
2402                 SubsurfModifierData *smd = (SubsurfModifierData*) md;
2403
2404                 if (level == -1) {
2405                         if (smd->modifier.mode&(eModifierMode_Render|eModifierMode_Realtime)) {
2406                                 smd->modifier.mode &= ~(eModifierMode_Render|eModifierMode_Realtime);
2407                         } else {
2408                                 smd->modifier.mode |= (eModifierMode_Render|eModifierMode_Realtime);
2409                         }
2410                 } else {
2411                         smd->levels = level;
2412                 }
2413         } else {
2414                 SubsurfModifierData *smd = (SubsurfModifierData*) modifier_new(eModifierType_Subsurf);
2415
2416                 BLI_addtail(&ob->modifiers, smd);
2417
2418                 if (level!=-1) {
2419                         smd->levels = level;
2420                 }
2421         }
2422
2423         countall();
2424         allqueue(REDRAWVIEW3D, 0);
2425         allqueue(REDRAWOOPS, 0);
2426         allqueue(REDRAWBUTSEDIT, 0);
2427         allqueue(REDRAWBUTSOBJECT, 0);
2428         DAG_object_flush_update(G.scene, ob, OB_RECALC_DATA);
2429         
2430         BIF_undo_push("Switch subsurf on/off");
2431 }
2432  
2433 static void copymenu_properties(Object *ob)
2434 {       
2435         bProperty *prop, *propn, *propc;
2436         Base *base;
2437         int nr, tot=0;
2438         char *str;
2439         
2440         prop= ob->prop.first;
2441         while(prop) {
2442                 tot++;
2443                 prop= prop->next;
2444         }
2445         
2446         if(tot==0) {
2447                 error("No properties in the active object to copy");
2448                 return;
2449         }
2450         
2451         str= MEM_callocN(24+32*tot, "copymenu prop");
2452         
2453         strcpy(str, "Copy Property %t");
2454         
2455         tot= 0; 
2456         prop= ob->prop.first;
2457         while(prop) {
2458                 tot++;
2459                 strcat(str, " |");
2460                 strcat(str, prop->name);
2461                 prop= prop->next;
2462         }
2463
2464         nr= pupmenu(str);
2465         if(nr>0) {
2466                 tot= 0;
2467                 prop= ob->prop.first;
2468                 while(prop) {
2469                         tot++;
2470                         if(tot==nr) break;
2471                         prop= prop->next;
2472                 }
2473                 if(prop) {
2474                         propc= prop;
2475                         
2476                         base= FIRSTBASE;
2477                         while(base) {
2478                                 if(base != BASACT) {
2479                                         if(TESTBASELIB(base)) {
2480                                                 prop= get_property(base->object, propc->name);
2481                                                 if(prop) {
2482                                                         free_property(prop);
2483                                                         BLI_remlink(&base->object->prop, prop);
2484                                                 }
2485                                                 propn= copy_property(propc);
2486                                                 BLI_addtail(&base->object->prop, propn);
2487                                         }
2488                                 }
2489                                 base= base->next;
2490                         }
2491                 }
2492         }
2493         MEM_freeN(str);
2494         allqueue(REDRAWVIEW3D, 0);
2495         
2496         BIF_undo_push("Copy properties");
2497 }
2498
2499 static void copymenu_logicbricks(Object *ob)
2500 {
2501         Base *base;
2502         
2503         base= FIRSTBASE;
2504         while(base) {
2505                 if(base->object != ob) {
2506                         if(TESTBASELIB(base)) {
2507                                 
2508                                 /* first: free all logic */
2509                                 free_sensors(&base->object->sensors);                           
2510                                 unlink_controllers(&base->object->controllers);
2511                                 free_controllers(&base->object->controllers);
2512                                 unlink_actuators(&base->object->actuators);
2513                                 free_actuators(&base->object->actuators);
2514                                 
2515                                 /* now copy it, this also works without logicbricks! */
2516                                 clear_sca_new_poins_ob(ob);
2517                                 copy_sensors(&base->object->sensors, &ob->sensors);
2518                                 copy_controllers(&base->object->controllers, &ob->controllers);
2519                                 copy_actuators(&base->object->actuators, &ob->actuators);
2520                                 set_sca_new_poins_ob(base->object);
2521                                 
2522                                 /* some menu settings */
2523                                 base->object->scavisflag= ob->scavisflag;
2524                                 base->object->scaflag= ob->scaflag;
2525                                 
2526                         }
2527                 }
2528                 base= base->next;
2529         }
2530         BIF_undo_push("Copy logic");
2531 }
2532
2533 static void copymenu_modifiers(Object *ob)
2534 {
2535         char str[512];
2536         int i, event;
2537         Base *base;
2538
2539         strcpy(str, "Copy Modifiers %t");
2540
2541         sprintf(str+strlen(str), "|All%%x%d|%%l", NUM_MODIFIER_TYPES);
2542
2543         for (i=eModifierType_None+1; i<NUM_MODIFIER_TYPES; i++) {
2544                 ModifierTypeInfo *mti = modifierType_getInfo(i);
2545
2546                 if (ELEM(i, eModifierType_Hook, eModifierType_Softbody)) continue;
2547
2548                 if (    (mti->flags&eModifierTypeFlag_AcceptsCVs) || 
2549                                 (ob->type==OB_MESH && (mti->flags&eModifierTypeFlag_AcceptsMesh))) {
2550                         sprintf(str+strlen(str), "|%s%%x%d", mti->name, i);
2551                 }
2552         }
2553
2554         event = pupmenu(str);
2555         if(event<=0) return;
2556
2557         for (base= FIRSTBASE; base; base= base->next) {
2558                 if(base->object != ob) {
2559                         if(TESTBASELIB(base)) {
2560                                 ModifierData *md;
2561
2562                                 base->object->recalc |= OB_RECALC_OB|OB_RECALC_DATA;
2563
2564                                 if (base->object->type==OB_MESH) {
2565                                         if (event==NUM_MODIFIER_TYPES) {
2566                                                 object_free_modifiers(base->object);
2567
2568                                                 for (md=ob->modifiers.first; md; md=md->next) {
2569                                                         if (md->type!=eModifierType_Hook) {
2570                                                                 ModifierData *nmd = modifier_new(md->type);
2571                                                                 modifier_copyData(md, nmd);
2572                                                                 BLI_addtail(&base->object->modifiers, nmd);
2573                                                         }
2574                                                 }
2575                                         } else {
2576                                                 ModifierData *md = modifiers_findByType(ob, event);
2577
2578                                                 if (md) {
2579                                                         ModifierData *tmd = modifiers_findByType(base->object, event);
2580
2581                                                         if (!tmd) {
2582                                                                 tmd = modifier_new(event);
2583                                                                 BLI_addtail(&base->object->modifiers, tmd);
2584                                                         }
2585
2586                                                         modifier_copyData(md, tmd);
2587                                                 }
2588                                         }
2589                                 }
2590                         }
2591                 }
2592         }
2593                                 
2594         allqueue(REDRAWVIEW3D, 0);
2595         allqueue(REDRAWBUTSOBJECT, 0);
2596         DAG_scene_sort(G.scene);
2597         
2598         BIF_undo_push("Copy modifiers");
2599 }
2600
2601 void copy_attr_menu()
2602 {
2603         Object *ob;
2604         short event;
2605         char str[512];
2606
2607         /* If you change this menu, don't forget to update the menu in header_view3d.c
2608          * view3d_edit_object_copyattrmenu() and in toolbox.c
2609          */
2610         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");
2611
2612         if(!(ob=OBACT)) return;
2613         
2614         strcat (str, "|Object Constraints%x22");
2615         
2616         if ELEM5(ob->type, OB_MESH, OB_CURVE, OB_SURF, OB_FONT, OB_MBALL) {
2617                 strcat(str, "|Texture Space%x17");
2618         }       
2619         
2620         if(ob->type == OB_FONT) strcat(str, "|Font Settings%x18|Bevel Settings%x19");
2621         if(ob->type == OB_CURVE) strcat(str, "|Bevel Settings%x19");
2622
2623         if(ob->type==OB_MESH){
2624                 strcat(str, "|Subdiv%x21");
2625         }
2626
2627         if( give_parteff(ob) ) strcat(str, "|Particle Settings%x20");
2628
2629         if(ob->soft) strcat(str, "|Soft Body Settings%x23");
2630         
2631         if(ob->type==OB_MESH){
2632                 strcat(str, "|Modifiers ...%x24");
2633         }
2634
2635         event= pupmenu(str);
2636         if(event<= 0) return;
2637         
2638         copy_attr(event);
2639 }
2640
2641 void copy_attr(short event)
2642 {
2643         Object *ob, *obt;
2644         Base *base;
2645         Curve *cu, *cu1;
2646         void *poin1, *poin2=0;
2647         
2648         if(G.scene->id.lib) return;
2649
2650         if(!(ob=OBACT)) return;
2651         
2652         if(G.obedit) {
2653                 /* obedit_copymenu(); */
2654                 return;
2655         }
2656         
2657         if ELEM5(ob->type, OB_MESH, OB_CURVE, OB_SURF, OB_FONT, OB_MBALL) {
2658                 if(ob->type==OB_MESH) poin2= &(((Mesh *)ob->data)->texflag);
2659                 else if ELEM3(ob->type, OB_CURVE, OB_SURF, OB_FONT) poin2= &(((Curve *)ob->data)->texflag);
2660                 else if(ob->type==OB_MBALL) poin2= &(((MetaBall *)ob->data)->texflag);
2661         }       
2662
2663         if(event==9) {
2664                 copymenu_properties(ob);
2665                 return;
2666         }
2667         else if(event==10) {
2668                 copymenu_logicbricks(ob);
2669                 return;
2670         }
2671         else if(event==24) {
2672                 copymenu_modifiers(ob);
2673                 return;
2674         }
2675
2676         base= FIRSTBASE;
2677         while(base) {
2678                 if(base != BASACT) {
2679                         if(TESTBASELIB(base)) {
2680                                 base->object->recalc |= OB_RECALC_OB;
2681                                 
2682                                 if(event==1) {  /* loc */
2683                                         VECCOPY(base->object->loc, ob->loc);
2684                                         VECCOPY(base->object->dloc, ob->dloc);
2685                                 }
2686                                 else if(event==2) {  /* rot */
2687                                         VECCOPY(base->object->rot, ob->rot);
2688                                         VECCOPY(base->object->drot, ob->drot);
2689                                         VECCOPY(base->object->quat, ob->quat);
2690                                         VECCOPY(base->object->dquat, ob->dquat);
2691                                 }
2692                                 else if(event==3) {  /* size */
2693                                         VECCOPY(base->object->size, ob->size);
2694                                         VECCOPY(base->object->dsize, ob->dsize);
2695                                 }
2696                                 else if(event==4) {  /* drawtype */
2697                                         base->object->dt= ob->dt;
2698                                         base->object->dtx= ob->dtx;
2699                                 }
2700                                 else if(event==5) {  /* time offs */
2701                                         base->object->sf= ob->sf;
2702                                 }
2703                                 else if(event==6) {  /* dupli */
2704                                         base->object->dupon= ob->dupon;
2705                                         base->object->dupoff= ob->dupoff;
2706                                         base->object->dupsta= ob->dupsta;
2707                                         base->object->dupend= ob->dupend;
2708                                         
2709                                         base->object->transflag &= ~OB_DUPLI;
2710                                         base->object->transflag |= (ob->transflag & OB_DUPLI);
2711                                 }
2712                                 else if(event==7) {     /* mass */
2713                                         base->object->mass= ob->mass;
2714                                 }
2715                                 else if(event==8) {     /* damping */
2716                                         base->object->damping= ob->damping;
2717                                         base->object->rdamping= ob->rdamping;
2718                                 }
2719                                 else if(event==17) {    /* tex space */
2720                                         obt= base->object;
2721                                         poin1= 0;
2722                                         if(obt->type==OB_MESH) poin1= &(((Mesh *)obt->data)->texflag);
2723                                         else if ELEM3(obt->type, OB_CURVE, OB_SURF, OB_FONT) poin1= &(((Curve *)obt->data)->texflag);
2724                                         else if(obt->type==OB_MBALL) poin1= &(((MetaBall *)obt->data)->texflag);                                        
2725                                         
2726                                         if(poin1) {
2727                                                 memcpy(poin1, poin2, 4+12+12+12);
2728                                         
2729                                                 if(obt->type==OB_MESH) ;
2730                                                 else if(obt->type==OB_MBALL) tex_space_mball(obt);
2731                                                 else tex_space_curve(obt->data);
2732                                         }
2733                                 }
2734                                 else if(event==18) {    /* font settings */
2735                                         
2736                                         if(base->object->type==ob->type) {
2737                                                 cu= ob->data;
2738                                                 cu1= base->object->data;
2739                                                 
2740                                                 cu1->spacemode= cu->spacemode;
2741                                                 cu1->spacing= cu->spacing;
2742                                                 cu1->linedist= cu->linedist;
2743                                                 cu1->shear= cu->shear;
2744                                                 cu1->fsize= cu->fsize;
2745                                                 cu1->xof= cu->xof;
2746                                                 cu1->yof= cu->yof;
2747                                                 cu1->textoncurve= cu->textoncurve;
2748                                                 if(cu1->vfont) cu1->vfont->id.us--;
2749                                                 cu1->vfont= cu->vfont;
2750                                                 id_us_plus((ID *)cu1->vfont);
2751                                                 if(cu1->vfontb) cu1->vfontb->id.us--;
2752                                                 cu1->vfontb= cu->vfontb;
2753                                                 id_us_plus((ID *)cu1->vfontb);
2754                                                 if(cu1->vfonti) cu1->vfonti->id.us--;
2755                                                 cu1->vfonti= cu->vfonti;
2756                                                 id_us_plus((ID *)cu1->vfonti);
2757                                                 if(cu1->vfontbi) cu1->vfontbi->id.us--;
2758                                                 cu1->vfontbi= cu->vfontbi;
2759                                                 id_us_plus((ID *)cu1->vfontbi);                                         
2760
2761                                                 text_to_curve(base->object, 0);         // needed?
2762
2763                                                 
2764                                                 strcpy(cu1->family, cu->family);
2765                                                 
2766                                                 base->object->recalc |= OB_RECALC_DATA;
2767                                         }
2768                                 }
2769                                 else if(event==19) {    /* bevel settings */
2770                                         
2771                                         if ELEM(base->object->type, OB_CURVE, OB_FONT) {
2772                                                 cu= ob->data;
2773                                                 cu1= base->object->data;
2774                                                 
2775                                                 cu1->bevobj= cu->bevobj;
2776                                                 cu1->taperobj= cu->taperobj;
2777                                                 cu1->width= cu->width;
2778                                                 cu1->bevresol= cu->bevresol;
2779                                                 cu1->ext1= cu->ext1;
2780                                                 cu1->ext2= cu->ext2;
2781                                                 
2782                                                 base->object->recalc |= OB_RECALC_DATA;
2783                                         }
2784                                 }
2785                                 else if(event==20) {    /* particle settings */
2786                                         PartEff *pa1, *pa2;
2787                                         char *p1, *p2;
2788                                         
2789                                         pa1= give_parteff(ob);
2790                                         pa2= give_parteff(base->object);
2791
2792                                         if(pa1==0 && pa2) {
2793                                                 BLI_remlink( &(base->object->effect), pa2);
2794                                                 free_effect( (Effect *) pa2);
2795                                         }
2796                                         else if(pa1 && pa2==0) {
2797                                                 free_effects(&(base->object->effect));
2798                                                 copy_effects(&(base->object->effect), &ob->effect);
2799                                                 build_particle_system(base->object);
2800                                         }
2801                                         else if(pa1 && pa2) {
2802                                                 if(pa2->keys) MEM_freeN(pa2->keys);
2803                                                 
2804                                                 p1= (char *)pa1; p2= (char *)pa2;
2805                                                 memcpy( p2+8, p1+8, sizeof(PartEff) - 8);
2806                                                 pa2->keys= 0;
2807                                                 
2808                                                 build_particle_system(base->object);
2809                                         }
2810                                 }
2811                                 else if(event==21){
2812                                         if (base->object->type==OB_MESH) {
2813                                                 ModifierData *md = modifiers_findByType(ob, eModifierType_Subsurf);
2814
2815                                                 if (md) {
2816                                                         ModifierData *tmd = modifiers_findByType(base->object, eModifierType_Subsurf);
2817
2818                                                         if (!tmd) {
2819                                                                 tmd = modifier_new(eModifierType_Subsurf);
2820                                                                 BLI_addtail(&base->object->modifiers, tmd);
2821                                                         }
2822
2823                                                         modifier_copyData(md, tmd);
2824                                                         base->object->recalc |= OB_RECALC_DATA;
2825                                                 }
2826                                         }
2827                                 }
2828                                 else if(event==22) {
2829                                         /* Clear the constraints on the target */
2830                                         free_constraints(&base->object->constraints);
2831                                         free_constraint_channels(&base->object->constraintChannels);
2832
2833                                         /* Copy the constraint channels over */
2834                                         copy_constraints(&base->object->constraints, &ob->constraints);
2835                                         if (U.dupflag& USER_DUP_IPO)
2836                                                 copy_constraint_channels(&base->object->constraintChannels, &ob->constraintChannels);
2837                                         else
2838                                                 clone_constraint_channels (&base->object->constraintChannels, &ob->constraintChannels);
2839                                 }
2840                                 else if(event==23) {
2841                                         base->object->softflag= ob->softflag;
2842                                         if(base->object->soft) sbFree(base->object->soft);
2843                                         
2844                                         base->object->soft= copy_softbody(ob->soft);
2845
2846                                         if (!modifiers_findByType(base->object, eModifierType_Softbody)) {
2847                                                 BLI_addhead(&base->object->modifiers, modifier_new(eModifierType_Softbody));
2848                                         }
2849                                 }
2850                         }
2851                 }
2852                 base= base->next;
2853         }
2854         
2855         allqueue(REDRAWVIEW3D, 0);
2856         DAG_scene_flush_update(G.scene);
2857
2858         if(event==20) {
2859                 allqueue(REDRAWBUTSOBJECT, 0);
2860         }
2861         
2862         BIF_undo_push("Copy attributes");
2863 }
2864
2865 void link_to_scene(unsigned short nr)
2866 {       
2867         Scene *sce= (Scene*) BLI_findlink(&G.main->scene, G.curscreen->scenenr-1);
2868         Base *base, *nbase;
2869         
2870         if(sce==0) return;
2871         if(sce->id.lib) return;
2872         
2873         base= FIRSTBASE;
2874         while(base) {
2875                 if(TESTBASE(base)) {
2876                         
2877                         nbase= MEM_mallocN( sizeof(Base), "newbase");
2878                         *nbase= *base;
2879                         BLI_addhead( &(sce->base), nbase);
2880                         id_us_plus((ID *)base->object);
2881                 }
2882                 base= base->next;
2883         }
2884 }
2885
2886 void make_links_menu()
2887 {
2888         Object *ob;
2889         short event=0;
2890         char str[140];
2891         
2892         if(!(ob=OBACT)) return;
2893         
2894         strcpy(str, "Make Links %t|To Scene...%x1|%l|Object Ipo%x4");
2895         
2896         if(ob->type==OB_MESH)
2897                 strcat(str, "|Mesh Data%x2|Materials%x3");
2898         else if(ob->type==OB_CURVE)
2899                 strcat(str, "|Curve Data%x2|Materials%x3");
2900  &