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