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