Orange branch: Revived hidden treasure, the Groups!
[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 size";
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                         set_seamtface();
1509                         allqueue(REDRAWIMAGE, 0);
1510                 }
1511                 if(G.f & G_WEIGHTPAINT)
1512                         mesh_octree_table(G.obedit, NULL, 'e');
1513         }
1514         else if (G.obedit->type==OB_ARMATURE){  
1515                 load_editArmature();
1516                 if (freedata) free_editArmature();
1517         }
1518         else if ELEM(G.obedit->type, OB_CURVE, OB_SURF) {
1519                 extern ListBase editNurb;
1520                 load_editNurb();
1521                 if(freedata) freeNurblist(&editNurb);
1522         }
1523         else if(G.obedit->type==OB_FONT && freedata) {
1524                 load_editText();
1525         }
1526         else if(G.obedit->type==OB_LATTICE) {
1527                 load_editLatt();
1528                 if(freedata) free_editLatt();
1529         }
1530         else if(G.obedit->type==OB_MBALL) {
1531                 extern ListBase editelems;
1532                 load_editMball();
1533                 if(freedata) BLI_freelistN(&editelems);
1534         }
1535
1536         ob= G.obedit;
1537         
1538         /* for example; displist make is different in editmode */
1539         if(freedata) G.obedit= NULL;
1540
1541         /* total remake of softbody data */
1542         if(modifiers_isSoftbodyEnabled(ob)) {
1543                 if (ob->soft && ob->soft->keys) {
1544                         notice("Erased Baked SoftBody");
1545                 }
1546
1547                 sbObjectToSoftbody(ob);
1548         }
1549         /* also flush ob recalc, doesn't take much overhead, but used for particles */
1550         DAG_object_flush_update(G.scene, ob, OB_RECALC_OB|OB_RECALC_DATA);
1551
1552         if(freedata) {
1553                 setcursor_space(SPACE_VIEW3D, CURSOR_STD);
1554         }
1555         
1556         countall();
1557         allqueue(REDRAWVIEW3D, 1);
1558         allqueue(REDRAWBUTSALL, 0);
1559         allqueue(REDRAWACTION, 0);
1560         allqueue(REDRAWNLA, 0);
1561         allqueue(REDRAWIPO, 0);
1562         allqueue(REDRAWOOPS, 0);
1563
1564         scrarea_queue_headredraw(curarea);
1565         
1566         if(G.obedit==NULL && freedata==2) 
1567                 BIF_undo_push("Editmode");
1568 }
1569
1570 void check_editmode(int type)
1571 {
1572         
1573         if (G.obedit==0 || G.obedit->type==type) return;
1574
1575         exit_editmode(2); // freedata, and undo
1576 }
1577
1578 /* 0 == do centre, 1 == centre new, 2 == centre cursor */
1579
1580 void docentre(int centremode)
1581 {
1582         EditMesh *em = G.editMesh;
1583         Base *base;
1584         Object *ob;
1585         Mesh *me, *tme;
1586         Curve *cu;
1587 //      BezTriple *bezt;
1588 //      BPoint *bp;
1589         Nurb *nu, *nu1;
1590         EditVert *eve;
1591         float cent[3], centn[3], min[3], max[3], omat[3][3];
1592         int a, total= 0;
1593         MVert *mvert;
1594
1595         if(G.scene->id.lib) return;
1596         
1597         cent[0]= cent[1]= cent[2]= 0.0;
1598         
1599         if(G.obedit) {
1600
1601                 INIT_MINMAX(min, max);
1602         
1603                 if(G.obedit->type==OB_MESH) {
1604                         for(eve= em->verts.first; eve; eve= eve->next) {
1605                                 if(G.vd->around==V3D_CENTROID) {
1606                                         total++;
1607                                         VECADD(cent, cent, eve->co);
1608                                 }
1609                                 else {
1610                                         DO_MINMAX(eve->co, min, max);
1611                                 }
1612                         }
1613                         
1614                         if(G.vd->around==V3D_CENTROID) {
1615                                 VecMulf(cent, 1.0f/(float)total);
1616                         }
1617                         else {
1618                                 cent[0]= (min[0]+max[0])/2.0f;
1619                                 cent[1]= (min[1]+max[1])/2.0f;
1620                                 cent[2]= (min[2]+max[2])/2.0f;
1621                         }
1622                         
1623                         for(eve= em->verts.first; eve; eve= eve->next) {
1624                                 VecSubf(eve->co, eve->co, cent);                        
1625                         }
1626                         
1627                         recalc_editnormals();
1628                 }
1629         }
1630         
1631         /* reset flags */
1632         base= FIRSTBASE;
1633         while(base) {
1634                 if TESTBASELIB(base) {
1635                         base->object->flag &= ~OB_DONE;
1636                 }
1637                 base= base->next;
1638         }
1639         me= G.main->mesh.first;
1640         while(me) {
1641                 me->flag &= ~ME_ISDONE;
1642                 me= me->id.next;
1643         }
1644         
1645         base= FIRSTBASE;
1646         while(base) {
1647                 
1648                 if TESTBASELIB(base) {
1649                         if((base->object->flag & OB_DONE)==0) {
1650                                 
1651                                 base->object->flag |= OB_DONE;
1652                                 
1653                                 if(G.obedit==0 && (me=get_mesh(base->object)) ) {
1654                                         
1655                                         if(me->key) {
1656                                                 error("Can't change the center of a mesh with vertex keys");
1657                                                 return;
1658                                         }
1659                                         
1660                                         if(centremode==2) {
1661                                                 VECCOPY(cent, give_cursor());
1662                                                 Mat4Invert(base->object->imat, base->object->obmat);
1663                                                 Mat4MulVecfl(base->object->imat, cent);
1664                                         } else {
1665                                                 INIT_MINMAX(min, max);
1666                 
1667                                                 mvert= me->mvert;
1668                                                 for(a=0; a<me->totvert; a++, mvert++) {
1669                                                         DO_MINMAX(mvert->co, min, max);
1670                                                 }
1671                                 
1672                                                 cent[0]= (min[0]+max[0])/2.0f;
1673                                                 cent[1]= (min[1]+max[1])/2.0f;
1674                                                 cent[2]= (min[2]+max[2])/2.0f;
1675                                         }
1676                                                 
1677                                         mvert= me->mvert;
1678                                         for(a=0; a<me->totvert; a++, mvert++) {
1679                                                 VecSubf(mvert->co, mvert->co, cent);
1680                                         }
1681                                         me->flag |= ME_ISDONE;
1682                                         
1683                                         if(centremode) {
1684                                                 Mat3CpyMat4(omat, base->object->obmat);
1685                                                 
1686                                                 VECCOPY(centn, cent);
1687                                                 Mat3MulVecfl(omat, centn);
1688                                                 base->object->loc[0]+= centn[0];
1689                                                 base->object->loc[1]+= centn[1];
1690                                                 base->object->loc[2]+= centn[2];
1691                                                 
1692                                                 /* other users? */
1693                                                 ob= G.main->object.first;
1694                                                 while(ob) {
1695                                                         if((ob->flag & OB_DONE)==0) {
1696                                                                 tme= get_mesh(ob);
1697                                                                 
1698                                                                 if(tme==me) {
1699                                                                         
1700                                                                         ob->flag |= OB_DONE;
1701                                                                         ob->recalc= OB_RECALC_OB|OB_RECALC_DATA;
1702
1703                                                                         Mat3CpyMat4(omat, ob->obmat);
1704                                                                         VECCOPY(centn, cent);
1705                                                                         Mat3MulVecfl(omat, centn);
1706                                                                         ob->loc[0]+= centn[0];
1707                                                                         ob->loc[1]+= centn[1];
1708                                                                         ob->loc[2]+= centn[2];
1709                                                                         
1710                                                                         if(tme && (tme->flag & ME_ISDONE)==0) {
1711                                                                                 mvert= tme->mvert;
1712                                                                                 for(a=0; a<tme->totvert; a++, mvert++) {
1713                                                                                         VecSubf(mvert->co, mvert->co, cent);
1714                                                                                 }
1715                                                                                 tme->flag |= ME_ISDONE;
1716                                                                         }
1717                                                                 }
1718                                                         }
1719                                                         
1720                                                         ob= ob->id.next;
1721                                                 }
1722                                         }
1723                                 }
1724                                 else if ELEM(base->object->type, OB_CURVE, OB_SURF) {
1725                                                                         
1726                                         if(G.obedit) {
1727                                                 extern ListBase editNurb;
1728                                                 nu1= editNurb.first;
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                                                         /* Curves need to be 2d, never offset in
1741                                                          * Z. Is a somewhat arbitrary restriction, 
1742                                                          * would probably be nice to remove.
1743                                                          */
1744                                                 cent[2]= 0.0;
1745                                         } else {
1746                                                 INIT_MINMAX(min, max);
1747         
1748                                                 nu= nu1;
1749                                                 while(nu) {
1750                                                         minmaxNurb(nu, min, max);
1751                                                         nu= nu->next;
1752                                                 }
1753                                                 
1754                                                 cent[0]= (min[0]+max[0])/2.0f;
1755                                                 cent[1]= (min[1]+max[1])/2.0f;
1756                                                 cent[2]= (min[2]+max[2])/2.0f;
1757                                         }
1758                                         
1759                                         nu= nu1;
1760                                         while(nu) {
1761                                                 if( (nu->type & 7)==1) {
1762                                                         a= nu->pntsu;
1763                                                         while (a--) {
1764                                                                 VecSubf(nu->bezt[a].vec[0], nu->bezt[a].vec[0], cent);
1765                                                                 VecSubf(nu->bezt[a].vec[1], nu->bezt[a].vec[1], cent);
1766                                                                 VecSubf(nu->bezt[a].vec[2], nu->bezt[a].vec[2], cent);
1767                                                         }
1768                                                 }
1769                                                 else {
1770                                                         a= nu->pntsu*nu->pntsv;
1771                                                         while (a--)
1772                                                                 VecSubf(nu->bp[a].vec, nu->bp[a].vec, cent);
1773                                                 }
1774                                                 nu= nu->next;
1775                                         }
1776                         
1777                                         if(centremode && G.obedit==0) {
1778                                                 Mat3CpyMat4(omat, base->object->obmat);
1779                                                 
1780                                                 Mat3MulVecfl(omat, cent);
1781                                                 base->object->loc[0]+= cent[0];
1782                                                 base->object->loc[1]+= cent[1];
1783                                                 base->object->loc[2]+= cent[2];
1784                                         }
1785                         
1786                                         if(G.obedit) {
1787                                                 break;
1788                                         }
1789         
1790                                 }
1791                                 else if(base->object->type==OB_FONT) {
1792                                         /* get from bb */
1793                                         
1794                                         cu= base->object->data;
1795                                         if(cu->bb==0) return;
1796                                         
1797                                         cu->xof= -0.5f*( cu->bb->vec[4][0] - cu->bb->vec[0][0]);
1798                                         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 */
1799                                         
1800                                         /* not really ok, do this better once! */
1801                                         cu->xof /= cu->fsize;
1802                                         cu->yof /= cu->fsize;
1803
1804                                         allqueue(REDRAWBUTSEDIT, 0);
1805                                 }
1806                                 base->object->recalc= OB_RECALC_OB|OB_RECALC_DATA;
1807                         }
1808                 }
1809                 base= base->next;
1810         }
1811
1812         DAG_scene_flush_update(G.scene, screen_view3d_layers());
1813         allqueue(REDRAWVIEW3D, 0);
1814         BIF_undo_push("Do Centre");     
1815 }
1816
1817 void docentre_new(void)
1818 {
1819         if(G.scene->id.lib) return;
1820
1821         if(G.obedit) {
1822                 error("Unable to center new in Edit Mode");
1823         }
1824         else {
1825                 docentre(1);
1826         }
1827 }
1828
1829 void docentre_cursor(void)
1830 {
1831         if(G.scene->id.lib) return;
1832
1833         if(G.obedit) {
1834                 error("Unable to center cursor in Edit Mode");
1835         }
1836         else {
1837                 docentre(2);
1838         }
1839 }
1840
1841 void movetolayer(void)
1842 {
1843         Base *base;
1844         unsigned int lay= 0, local;
1845         int islamp= 0;
1846         
1847         if(G.scene->id.lib) return;
1848
1849         base= FIRSTBASE;
1850         while(base) {
1851                 if TESTBASE(base) lay |= base->lay;
1852                 base= base->next;
1853         }
1854         if(lay==0) return;
1855         lay &= 0xFFFFFF;
1856         
1857         if( movetolayer_buts(&lay)==0 ) return;
1858         if(lay==0) return;
1859
1860         base= FIRSTBASE;
1861         while(base) {
1862                 if TESTBASE(base) {
1863                         /* upper byte is used for local view */
1864                         local= base->lay & 0xFF000000;  
1865                         base->lay= lay + local;
1866                         base->object->lay= lay;
1867                         if(base->object->type==OB_LAMP) islamp= 1;
1868                 }
1869                 base= base->next;
1870         }
1871         
1872         if(islamp && G.vd->drawtype == OB_SHADED) reshadeall_displist();
1873
1874         countall();
1875         DAG_scene_sort(G.scene);
1876         
1877         allqueue(REDRAWBUTSEDIT, 0);
1878         allqueue(REDRAWVIEW3D, 0);
1879         allqueue(REDRAWOOPS, 0);
1880         allqueue(REDRAWINFO, 0);
1881         
1882         BIF_undo_push("Move to layer");
1883 }
1884
1885 void split_font()
1886 {
1887         Object *ob = OBACT;
1888         Base *oldbase = BASACT;
1889         Curve *cu= ob->data;
1890         char *p= cu->str;
1891         int slen= strlen(p);
1892         int i;
1893
1894         for (i = 0; i<=slen; p++, i++) {
1895                 adduplicate(1);
1896                 cu= OBACT->data;
1897                 cu->sepchar = i+1;
1898                 text_to_curve(OBACT, 0);        // pass 1: only one letter, adapt position
1899                 text_to_curve(OBACT, 0);        // pass 2: remake
1900                 freedisplist(&OBACT->disp);
1901                 makeDispListCurveTypes(OBACT, 0);
1902                 
1903                 OBACT->flag &= ~SELECT;
1904                 BASACT->flag &= ~SELECT;
1905                 oldbase->flag |= SELECT;
1906                 oldbase->object->flag |= SELECT;
1907                 set_active_base(oldbase);               
1908         }
1909 }
1910
1911 void special_editmenu(void)
1912 {
1913         static short numcuts= 2;
1914         Object *ob= OBACT;
1915         float fac;
1916         int nr,ret;
1917         short randfac;
1918         
1919         if(ob==NULL) return;
1920         
1921         if(G.obedit==NULL) {
1922                 
1923                 if(ob->flag & OB_POSEMODE) {
1924                         pose_special_editmenu();
1925                 }
1926                 else if(G.f & G_FACESELECT) {
1927                         Mesh *me= get_mesh(ob);
1928                         TFace *tface;
1929                         int a;
1930                         
1931                         if(me==0 || me->tface==0) return;
1932                         
1933                         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");
1934         
1935                         for(a=me->totface, tface= me->tface; a>0; a--, tface++) {
1936                                 if(tface->flag & SELECT) {
1937                                         switch(nr) {
1938                                         case 1:
1939                                                 tface->mode |= TF_TEX; break;
1940                                         case 2:
1941                                                 tface->mode |= TF_SHAREDCOL; break;
1942                                         case 3:
1943                                                 tface->mode |= TF_LIGHT; break; 
1944                                         case 4:
1945                                                 tface->mode |= TF_INVISIBLE; break;
1946                                         case 5:
1947                                                 tface->mode |= TF_DYNAMIC; break;
1948                                         case 6:
1949                                                 tface->mode &= ~TF_TEX;
1950                                                 tface->tpage= 0;
1951                                                 break;
1952                                         case 7:
1953                                                 tface->mode &= ~TF_SHAREDCOL; break;
1954                                         case 8:
1955                                                 tface->mode &= ~TF_LIGHT; break;
1956                                         case 9:
1957                                                 tface->mode &= ~TF_INVISIBLE; break;
1958                                         case 10:
1959                                                 tface->mode &= ~TF_DYNAMIC; break;
1960                                         }
1961                                 }
1962                         }
1963                         DAG_object_flush_update(G.scene, ob, OB_RECALC_DATA);
1964                         allqueue(REDRAWVIEW3D, 0);
1965                         allqueue(REDRAWBUTSEDIT, 0);
1966                         BIF_undo_push("Change texture face");
1967                 }
1968                 else if(G.f & G_VERTEXPAINT) {
1969                         Mesh *me= get_mesh(ob);
1970                         
1971                         if(me==0 || (me->mcol==NULL && me->tface==NULL) ) return;
1972                         
1973                         nr= pupmenu("Specials%t|Shared VertexCol%x1");
1974                         if(nr==1) {
1975                                 
1976                                 if(me->tface) tface_to_mcol(me);
1977                                 
1978                                 copy_vpaint_undo( (unsigned int *)me->mcol, me->totface);
1979                                 do_shared_vertexcol(me);
1980                                 
1981                                 if(me->tface) mcol_to_tface(me, 1);
1982                                 BIF_undo_push("Shared VertexCol");
1983
1984                                 DAG_object_flush_update(G.scene, ob, OB_RECALC_DATA);
1985                         }
1986                 }
1987                 else if(G.f & G_WEIGHTPAINT) {
1988                         Object *par= modifiers_isDeformedByArmature(ob);
1989                         if(par && (par->flag & OB_POSEMODE)) {
1990                                 nr= pupmenu("Specials%t|Apply Bone Envelopes to VertexGroups %x1");
1991                                 if(nr==1) {
1992                                         Mesh *me= ob->data;
1993                                         if(me->dvert) copy_wpaint_undo(me->dvert, me->totvert);
1994                                         pose_adds_vgroups(ob);
1995                                 }
1996                         }
1997                 }
1998                 else {
1999                         Base *base, *base_select= NULL;
2000                         
2001                         // Get the active object mesh.
2002                         Mesh *me= get_mesh(ob);
2003
2004                         // Booleans, if the active object is a mesh...
2005                         if (me && ob->id.lib==NULL) {
2006                                 
2007                                 // Bring up a little menu with the boolean operation choices on.
2008                                 nr= pupmenu("Boolean Tools%t|Intersect%x1|Union%x2|Difference%x3|Add Intersect Modifier%x4|Add Union Modifier%x5|Add Difference Modifier%x6");
2009
2010                                 if (nr > 0) {
2011                                         // user has made a choice of a menu element.
2012                                         // All of the boolean functions require 2 mesh objects 
2013                                         // we search through the object list to find the other 
2014                                         // selected item and make sure it is distinct and a mesh.
2015
2016                                         for(base= FIRSTBASE; base; base= base->next) {
2017                                                 if TESTBASELIB(base) {
2018                                                         if(base->object != ob) base_select= base;
2019                                                 }
2020                                         }
2021
2022                                         if (base_select) {
2023                                                 if (get_mesh(base_select->object)) {
2024                                                         if(nr <= 3){
2025                                                                 waitcursor(1);
2026                                                                 ret = NewBooleanMesh(BASACT,base_select,nr);
2027                                                                 if (ret==0) {
2028                                                                         error("An internal error occurred");
2029                                                                 } else if(ret==-1) {
2030                                                                         error("Selected meshes must have faces to perform boolean operations");
2031                                                                 } else if (ret==-2) {
2032                                                                         error("Both meshes must be a closed mesh");
2033                                                                 }
2034                                                                 else BIF_undo_push("Boolean");
2035                                                                 waitcursor(0);
2036                                                         } else {
2037                                                                 BooleanModifierData *bmd = NULL;
2038                                                                 bmd = (BooleanModifierData *)modifier_new(eModifierType_Boolean);
2039                                                                 BLI_addtail(&ob->modifiers, bmd);
2040                                                                 bmd->object = base_select->object;
2041                                                                 bmd->modifier.mode |= eModifierMode_Realtime;
2042                                                                 switch(nr){
2043                                                                         case 4: bmd->operation = eBooleanModifierOp_Intersect; break;
2044                                                                         case 5: bmd->operation = eBooleanModifierOp_Union; break;
2045                                                                         case 6: bmd->operation = eBooleanModifierOp_Difference; break;
2046                                                                 }
2047                                                                 do_common_editbuts(B_CHANGEDEP);
2048                                                                 BIF_undo_push("Add Boolean modifier");                                                          
2049                                                         }                                                               
2050                                                 } else {
2051                                                         error("Please select 2 meshes");
2052                                                 }
2053                                         } else {
2054                                                 error("Please select 2 meshes");
2055                                         }
2056                                 }
2057
2058                                 allqueue(REDRAWVIEW3D, 0);
2059                         }
2060                         else if (ob->type == OB_FONT) {
2061                                 nr= pupmenu("Split %t|Characters%x1");
2062                                 if (nr > 0) {
2063                                         switch(nr) {
2064                                                 case 1: split_font();
2065                                         }
2066                                 }
2067                         }                       
2068                 }
2069         }
2070         else if(G.obedit->type==OB_MESH) {
2071
2072                 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");
2073                 
2074                 switch(nr) {
2075                 case 1:
2076                         waitcursor(1);
2077                         esubdivideflag(1, 0.0, G.scene->toolsettings->editbutflag, 1, 0);
2078                         
2079                         BIF_undo_push("ESubdivide Single");            
2080                         break;
2081                 case 2:
2082                         if(button(&numcuts, 1, 128, "Number of Cuts:")==0) return;
2083                         waitcursor(1);
2084                         esubdivideflag(1, 0.0, G.scene->toolsettings->editbutflag, numcuts, 0);
2085                         BIF_undo_push("ESubdivide");
2086                         break;
2087                 case 3:
2088                         if(button(&numcuts, 1, 128, "Number of Cuts:")==0) return;
2089                         waitcursor(1);
2090                         randfac= 10;
2091                         if(button(&randfac, 1, 100, "Rand fac:")==0) return;
2092                         fac= -( (float)randfac )/100;
2093                         esubdivideflag(1, fac, G.scene->toolsettings->editbutflag, numcuts, 0);
2094                         BIF_undo_push("Subdivide Fractal");
2095                         break;
2096                         
2097                 case 12:        /* smooth */
2098                         //if(button(&numcuts, 1, 128, "Number of Cuts:")==0) return;
2099                         fac= 1.0f;
2100                         if(fbutton(&fac, 0.0f, 5.0f, 10, 10, "Smooth:")==0) return;
2101                                 fac= 0.292f*fac;
2102                         
2103                         waitcursor(1);
2104                         esubdivideflag(1, fac, G.scene->toolsettings->editbutflag | B_SMOOTH, 1, 0);
2105                         BIF_undo_push("Subdivide Smooth");
2106                         break;          
2107                         
2108                 case 4:
2109                         mergemenu();
2110                         break;
2111                 case 5:
2112                         notice("Removed %d Vertices", removedoublesflag(1, G.scene->toolsettings->doublimit));
2113                         BIF_undo_push("Remove Doubles");
2114                         break;
2115                 case 6:
2116                         hide_mesh(0);
2117                         break;
2118                 case 7:
2119                         reveal_mesh();
2120                         break;
2121                 case 8:
2122                         selectswap_mesh();
2123                         break;
2124                 case 9:
2125                         flip_editnormals();
2126                         BIF_undo_push("Flip Normals");
2127                         break;
2128                 case 10:
2129                         vertexsmooth();
2130                         break;
2131                 case 11:
2132                         bevel_menu();
2133                         break;
2134                 case 14:
2135                         mesh_set_smooth_faces(1);
2136                         break;
2137                 case 15: 
2138                         mesh_set_smooth_faces(0);
2139                         break;
2140                 }
2141                 
2142                 DAG_object_flush_update(G.scene, G.obedit, OB_RECALC_DATA);
2143                 
2144                 if(nr>0) waitcursor(0);
2145                 
2146         }
2147         else if ELEM(G.obedit->type, OB_CURVE, OB_SURF) {
2148
2149                 nr= pupmenu("Specials%t|Subdivide%x1|Switch Direction%x2|Set Goal Weight %x3");
2150                 
2151                 switch(nr) {
2152                 case 1:
2153                         subdivideNurb();
2154                         break;
2155                 case 2:
2156                         switchdirectionNurb2();
2157                         break;
2158                 case 3:
2159                         {
2160                                 static float weight= 1.0f;
2161                                 extern ListBase editNurb;
2162                                 Nurb *nu;
2163                                 BezTriple *bezt;
2164                                 BPoint *bp;
2165                                 int a;
2166                                 
2167                                 if(fbutton(&weight, 0.0f, 1.0f, 10, 10, "Set Weight")) {
2168                                         for(nu= editNurb.first; nu; nu= nu->next) {
2169                                                 if(nu->bezt) {
2170                                                         for(bezt=nu->bezt, a=0; a<nu->pntsu; a++, bezt++) {
2171                                                                 if(bezt->f2 & SELECT)
2172                                                                         bezt->weight= weight;
2173                                                         }
2174                                                 }
2175                                                 else if(nu->bp) {
2176                                                         for(bp=nu->bp, a=0; a<nu->pntsu*nu->pntsv; a++, bp++) {
2177                                                                 if(bp->f1 & SELECT)
2178                                                                         bp->weight= weight;
2179                                                         }
2180                                                 }
2181                                         }       
2182                                 }
2183                         }
2184                         break;
2185                 }
2186                 
2187                 DAG_object_flush_update(G.scene, G.obedit, OB_RECALC_DATA);
2188         }
2189         else if(G.obedit->type==OB_ARMATURE) {
2190                 nr= pupmenu("Specials%t|Subdivide %x1|Flip Left-Right Names%x2");
2191                 if(nr==1)
2192                         subdivide_armature();
2193                 else if(nr==2)
2194                         armature_flip_names();
2195         }
2196         else if(G.obedit->type==OB_LATTICE) {
2197                 static float weight= 1.0f;
2198                 if(fbutton(&weight, 0.0f, 1.0f, 10, 10, "Set Weight")) {
2199                         int a= editLatt->pntsu*editLatt->pntsv*editLatt->pntsw;
2200                         BPoint *bp= editLatt->def;
2201                         
2202                         while(a--) {
2203                                 if(bp->f1 & SELECT)
2204                                         bp->weight= weight;
2205                                 bp++;
2206                         }       
2207                 }
2208         }
2209
2210         countall();
2211         allqueue(REDRAWVIEW3D, 0);
2212         
2213 }
2214
2215 void convertmenu(void)
2216 {
2217         Base *base, *basen, *basact, *basedel=NULL;
2218         Object *ob, *ob1;
2219         Curve *cu;
2220         MetaBall *mb;
2221         Mesh *me;
2222         DispList *dl;
2223         int ok=0, nr = 0, a;
2224         
2225         if(G.scene->id.lib) return;
2226
2227         ob= OBACT;
2228         if(ob==0) return;
2229         if(G.obedit) return;
2230         
2231         basact= BASACT; /* will be restored */
2232                 
2233         if(ob->type==OB_FONT) {
2234                 nr= pupmenu("Convert Font to%t|Curve");
2235                 if(nr>0) ok= 1;
2236         }
2237         else if(ob->type==OB_MBALL) {
2238                 nr= pupmenu("Convert Metaball to%t|Mesh (keep original)%x1|Mesh (Delete Original)%x2");
2239                 if(nr>0) ok= 1;
2240         }
2241         else if(ob->type==OB_CURVE) {
2242                 nr= pupmenu("Convert Curve to%t|Mesh");
2243                 if(nr>0) ok= 1;
2244         }
2245         else if(ob->type==OB_SURF) {
2246                 nr= pupmenu("Convert Nurbs Surface to%t|Mesh");
2247                 if(nr>0) ok= 1;
2248         }
2249         else if(ob->type==OB_MESH) {
2250                 if(ob->modifiers.first) 
2251                         nr= pupmenu("Convert Modifiers to%t|Mesh (Keep Original)%x1|Mesh (Delete Original)%x2");
2252                 if(nr>0) ok= 1;
2253         }
2254         if(ok==0) return;
2255
2256         /* don't forget multiple users! */
2257
2258         /* reset flags */
2259         base= FIRSTBASE;
2260         while(base) {
2261                 if TESTBASELIB(base) {
2262                         base->object->flag &= ~OB_DONE;
2263                 }
2264                 base= base->next;
2265         }
2266
2267         base= FIRSTBASE;
2268         while(base) {
2269                 if TESTBASELIB(base) {
2270                         
2271                         ob= base->object;
2272                         
2273                         if(ob->flag & OB_DONE);
2274                         else if(ob->type==OB_MESH) {
2275                                 DispListMesh *dlm;
2276                                 DerivedMesh *dm;
2277
2278                                 basedel = base;
2279
2280                                 ob->flag |= OB_DONE;
2281
2282                                 ob1= copy_object(ob);
2283                                 ob1->recalc |= OB_RECALC;
2284                                 object_free_modifiers(ob1);
2285
2286                                 basen= MEM_mallocN(sizeof(Base), "duplibase");
2287                                 *basen= *base;
2288                                 BLI_addhead(&G.scene->base, basen);     /* addhead: otherwise eternal loop */
2289                                 basen->object= ob1;
2290                                 basen->flag &= ~SELECT;
2291
2292                                 /* decrement original mesh's usage count  */
2293                                 me= ob1->data;
2294                                 me->id.us--;
2295
2296                                 /* make a new copy of the mesh */
2297                                 ob1->data= copy_mesh(me);
2298                                 G.totmesh++;
2299
2300                                 /* make new mesh data from the original copy */
2301                                 dm= mesh_create_derived_no_deform(ob, NULL);
2302                                 dlm= dm->convertToDispListMesh(dm, 0);
2303                                 displistmesh_to_mesh(dlm, ob1->data);
2304                                 dm->release(dm);
2305                         }
2306                         else if(ob->type==OB_FONT) {
2307                                 if(nr==1) {
2308                                 
2309                                         ob->flag |= OB_DONE;
2310                                 
2311                                         ob->type= OB_CURVE;
2312                                         cu= ob->data;
2313                                         
2314                                         if(cu->vfont) {
2315                                                 cu->vfont->id.us--;
2316                                                 cu->vfont= 0;
2317                                         }
2318                                         if(cu->vfontb) {
2319                                                 cu->vfontb->id.us--;
2320                                                 cu->vfontb= 0;
2321                                         }
2322                                         if(cu->vfonti) {
2323                                                 cu->vfonti->id.us--;
2324                                                 cu->vfonti= 0;
2325                                         }
2326                                         if(cu->vfontbi) {
2327                                                 cu->vfontbi->id.us--;
2328                                                 cu->vfontbi= 0;
2329                                         }                                       
2330                                         /* other users */
2331                                         if(cu->id.us>1) {
2332                                                 ob1= G.main->object.first;
2333                                                 while(ob1) {
2334                                                         if(ob1->data==cu) {
2335                                                                 ob1->type= OB_CURVE;
2336                                                                 ob1->recalc |= OB_RECALC;
2337                                                         }
2338                                                         ob1= ob1->id.next;
2339                                                 }
2340                                         }
2341                                 }
2342                         }
2343                         else if ELEM(ob->type, OB_CURVE, OB_SURF) {
2344                                 if(nr==1) {
2345
2346                                         ob->flag |= OB_DONE;
2347                                         cu= ob->data;
2348                                         
2349                                         dl= cu->disp.first;
2350                                         if(dl==0) makeDispListCurveTypes(ob, 0);                // force creation
2351
2352                                         nurbs_to_mesh(ob); /* also does users */
2353
2354                                         /* texspace and normals */
2355                                         BASACT= base;
2356                                         enter_editmode();
2357                                         exit_editmode(1); // freedata, but no undo
2358                                         BASACT= basact;
2359                                 }
2360                         }
2361                         else if(ob->type==OB_MBALL) {
2362                         
2363                                 if(nr==1 || nr == 2) {
2364                                         ob= find_basis_mball(ob);
2365                                         
2366                                         if(ob->disp.first && !(ob->flag&OB_DONE)) {
2367                                                 basedel = base;
2368                                         
2369                                                 ob->flag |= OB_DONE;
2370
2371                                                 ob1= copy_object(ob);
2372                                                 ob1->recalc |= OB_RECALC;
2373
2374                                                 basen= MEM_mallocN(sizeof(Base), "duplibase");
2375                                                 *basen= *base;
2376                                                 BLI_addhead(&G.scene->base, basen);     /* addhead: othwise eternal loop */
2377                                                 basen->object= ob1;
2378                                                 basen->flag &= ~SELECT;
2379                                                 
2380                                                 mb= ob1->data;
2381                                                 mb->id.us--;
2382                                                 
2383                                                 ob1->data= add_mesh();
2384                                                 G.totmesh++;
2385                                                 ob1->type= OB_MESH;
2386                                                 
2387                                                 me= ob1->data;
2388                                                 me->totcol= mb->totcol;
2389                                                 if(ob1->totcol) {
2390                                                         me->mat= MEM_dupallocN(mb->mat);
2391                                                         for(a=0; a<ob1->totcol; a++) id_us_plus((ID *)me->mat[a]);
2392                                                 }
2393                                                 
2394                                                 mball_to_mesh(&ob->disp, ob1->data);
2395                                         }
2396                                 }
2397                         }
2398                 }
2399                 base= base->next;
2400                 if(basedel != NULL && nr == 2)
2401                         free_and_unlink_base(basedel);  
2402                 basedel = NULL;                         
2403         }
2404
2405         countall();
2406         allqueue(REDRAWVIEW3D, 0);
2407         allqueue(REDRAWOOPS, 0);
2408         allspace(OOPS_TEST, 0);
2409         allqueue(REDRAWBUTSEDIT, 0);
2410         BIF_undo_push("Convert Object");
2411
2412         DAG_scene_sort(G.scene);
2413 }
2414
2415         /* Change subdivision properties of mesh object ob, if
2416          * level==-1 then toggle subsurf, else set to level.
2417          */
2418 void flip_subdivison(Object *ob, int level)
2419 {
2420         ModifierData *md = modifiers_findByType(ob, eModifierType_Subsurf);
2421
2422         if (md) {
2423                 SubsurfModifierData *smd = (SubsurfModifierData*) md;
2424
2425                 if (level == -1) {
2426                         if (smd->modifier.mode&(eModifierMode_Render|eModifierMode_Realtime)) {
2427                                 smd->modifier.mode &= ~(eModifierMode_Render|eModifierMode_Realtime);
2428                         } else {
2429                                 smd->modifier.mode |= (eModifierMode_Render|eModifierMode_Realtime);
2430                         }
2431                 } else {
2432                         smd->levels = level;
2433                 }
2434         } else {
2435                 SubsurfModifierData *smd = (SubsurfModifierData*) modifier_new(eModifierType_Subsurf);
2436
2437                 BLI_addtail(&ob->modifiers, smd);
2438
2439                 if (level!=-1) {
2440                         smd->levels = level;
2441                 }
2442         }
2443
2444         countall();
2445         allqueue(REDRAWVIEW3D, 0);
2446         allqueue(REDRAWOOPS, 0);
2447         allqueue(REDRAWBUTSEDIT, 0);
2448         allqueue(REDRAWBUTSOBJECT, 0);
2449         DAG_object_flush_update(G.scene, ob, OB_RECALC_DATA);
2450         
2451         BIF_undo_push("Switch subsurf on/off");
2452 }
2453  
2454 static void copymenu_properties(Object *ob)
2455 {       
2456         bProperty *prop, *propn, *propc;
2457         Base *base;
2458         int nr, tot=0;
2459         char *str;
2460         
2461         prop= ob->prop.first;
2462         while(prop) {
2463                 tot++;
2464                 prop= prop->next;
2465         }
2466         
2467         if(tot==0) {
2468                 error("No properties in the active object to copy");
2469                 return;
2470         }
2471         
2472         str= MEM_callocN(24+32*tot, "copymenu prop");
2473         
2474         strcpy(str, "Copy Property %t");
2475         
2476         tot= 0; 
2477         prop= ob->prop.first;
2478         while(prop) {
2479                 tot++;
2480                 strcat(str, " |");
2481                 strcat(str, prop->name);
2482                 prop= prop->next;
2483         }
2484
2485         nr= pupmenu(str);
2486         if(nr>0) {
2487                 tot= 0;
2488                 prop= ob->prop.first;
2489                 while(prop) {
2490                         tot++;
2491                         if(tot==nr) break;
2492                         prop= prop->next;
2493                 }
2494                 if(prop) {
2495                         propc= prop;
2496                         
2497                         base= FIRSTBASE;
2498                         while(base) {
2499                                 if(base != BASACT) {
2500                                         if(TESTBASELIB(base)) {
2501                                                 prop= get_property(base->object, propc->name);
2502                                                 if(prop) {
2503                                                         free_property(prop);
2504                                                         BLI_remlink(&base->object->prop, prop);
2505                                                 }
2506                                                 propn= copy_property(propc);
2507                                                 BLI_addtail(&base->object->prop, propn);
2508                                         }
2509                                 }
2510                                 base= base->next;
2511                         }
2512                 }
2513         }
2514         MEM_freeN(str);
2515         allqueue(REDRAWVIEW3D, 0);
2516         
2517         BIF_undo_push("Copy properties");
2518 }
2519
2520 static void copymenu_logicbricks(Object *ob)
2521 {
2522         Base *base;
2523         
2524         base= FIRSTBASE;
2525         while(base) {
2526                 if(base->object != ob) {
2527                         if(TESTBASELIB(base)) {
2528                                 
2529                                 /* first: free all logic */
2530                                 free_sensors(&base->object->sensors);                           
2531                                 unlink_controllers(&base->object->controllers);
2532                                 free_controllers(&base->object->controllers);
2533                                 unlink_actuators(&base->object->actuators);
2534                                 free_actuators(&base->object->actuators);
2535                                 
2536                                 /* now copy it, this also works without logicbricks! */
2537                                 clear_sca_new_poins_ob(ob);
2538                                 copy_sensors(&base->object->sensors, &ob->sensors);
2539                                 copy_controllers(&base->object->controllers, &ob->controllers);
2540                                 copy_actuators(&base->object->actuators, &ob->actuators);
2541                                 set_sca_new_poins_ob(base->object);
2542                                 
2543                                 /* some menu settings */
2544                                 base->object->scavisflag= ob->scavisflag;
2545                                 base->object->scaflag= ob->scaflag;
2546                                 
2547                         }
2548                 }
2549                 base= base->next;
2550         }
2551         BIF_undo_push("Copy logic");
2552 }
2553
2554 static void copymenu_modifiers(Object *ob)
2555 {
2556         char str[512];
2557         int i, event;
2558         Base *base;
2559
2560         strcpy(str, "Copy Modifiers %t");
2561
2562         sprintf(str+strlen(str), "|All%%x%d|%%l", NUM_MODIFIER_TYPES);
2563
2564         for (i=eModifierType_None+1; i<NUM_MODIFIER_TYPES; i++) {
2565                 ModifierTypeInfo *mti = modifierType_getInfo(i);
2566
2567                 if (ELEM(i, eModifierType_Hook, eModifierType_Softbody)) continue;
2568
2569                 if (    (mti->flags&eModifierTypeFlag_AcceptsCVs) || 
2570                                 (ob->type==OB_MESH && (mti->flags&eModifierTypeFlag_AcceptsMesh))) {
2571                         sprintf(str+strlen(str), "|%s%%x%d", mti->name, i);
2572                 }
2573         }
2574
2575         event = pupmenu(str);
2576         if(event<=0) return;
2577
2578         for (base= FIRSTBASE; base; base= base->next) {
2579                 if(base->object != ob) {
2580                         if(TESTBASELIB(base)) {
2581                                 ModifierData *md;
2582
2583                                 base->object->recalc |= OB_RECALC_OB|OB_RECALC_DATA;
2584
2585                                 if (base->object->type==OB_MESH) {
2586                                         if (event==NUM_MODIFIER_TYPES) {
2587                                                 object_free_modifiers(base->object);
2588
2589                                                 for (md=ob->modifiers.first; md; md=md->next) {
2590                                                         if (md->type!=eModifierType_Hook) {
2591                                                                 ModifierData *nmd = modifier_new(md->type);
2592                                                                 modifier_copyData(md, nmd);
2593                                                                 BLI_addtail(&base->object->modifiers, nmd);
2594                                                         }
2595                                                 }
2596                                         } else {
2597                                                 ModifierData *md = modifiers_findByType(ob, event);
2598
2599                                                 if (md) {
2600                                                         ModifierData *tmd = modifiers_findByType(base->object, event);
2601
2602                                                         if (!tmd) {
2603                                                                 tmd = modifier_new(event);
2604                                                                 BLI_addtail(&base->object->modifiers, tmd);
2605                                                         }
2606
2607                                                         modifier_copyData(md, tmd);
2608                                                 }
2609                                         }
2610                                 }
2611                         }
2612                 }
2613         }
2614                                 
2615         allqueue(REDRAWVIEW3D, 0);
2616         allqueue(REDRAWBUTSOBJECT, 0);
2617         DAG_scene_sort(G.scene);
2618         
2619         BIF_undo_push("Copy modifiers");
2620 }
2621
2622 void copy_attr_menu()
2623 {
2624         Object *ob;
2625         short event;
2626         char str[512];
2627
2628         /* If you change this menu, don't forget to update the menu in header_view3d.c
2629          * view3d_edit_object_copyattrmenu() and in toolbox.c
2630          */
2631         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");
2632
2633         if(!(ob=OBACT)) return;
2634         
2635         strcat (str, "|Object Constraints%x22");
2636         
2637         if ELEM5(ob->type, OB_MESH, OB_CURVE, OB_SURF, OB_FONT, OB_MBALL) {
2638                 strcat(str, "|Texture Space%x17");
2639         }       
2640         
2641         if(ob->type == OB_FONT) strcat(str, "|Font Settings%x18|Bevel Settings%x19");
2642         if(ob->type == OB_CURVE) strcat(str, "|Bevel Settings%x19");
2643         
2644         if((ob->type == OB_FONT) || (ob->type == OB_CURVE)) {
2645                         strcat(str, "|Curve Resolution%x25");
2646         }
2647
2648         if(ob->type==OB_MESH){
2649                 strcat(str, "|Subdiv%x21");
2650         }
2651
2652         if( give_parteff(ob) ) strcat(str, "|Particle Settings%x20");
2653
2654         if(ob->soft) strcat(str, "|Soft Body Settings%x23");
2655         
2656         if(ob->type==OB_MESH){
2657                 strcat(str, "|Modifiers ...%x24");
2658         }
2659
2660         event= pupmenu(str);
2661         if(event<= 0) return;
2662         
2663         copy_attr(event);
2664 }
2665
2666 void copy_attr(short event)
2667 {
2668         Object *ob, *obt;
2669         Base *base;
2670         Curve *cu, *cu1;
2671         Nurb *nu;
2672         void *poin1, *poin2=0;
2673         
2674         if(G.scene->id.lib) return;
2675
2676         if(!(ob=OBACT)) return;
2677         
2678         if(G.obedit) {
2679                 /* obedit_copymenu(); */
2680                 return;
2681         }
2682         
2683         if ELEM5(ob->type, OB_MESH, OB_CURVE, OB_SURF, OB_FONT, OB_MBALL) {
2684                 if(ob->type==OB_MESH) poin2= &(((Mesh *)ob->data)->texflag);
2685                 else if ELEM3(ob->type, OB_CURVE, OB_SURF, OB_FONT) poin2= &(((Curve *)ob->data)->texflag);
2686                 else if(ob->type==OB_MBALL) poin2= &(((MetaBall *)ob->data)->texflag);
2687         }       
2688
2689         if(event==9) {
2690                 copymenu_properties(ob);
2691                 return;
2692         }
2693         else if(event==10) {
2694                 copymenu_logicbricks(ob);
2695                 return;
2696         }
2697         else if(event==24) {
2698                 copymenu_modifiers(ob);
2699                 return;
2700         }
2701
2702         base= FIRSTBASE;
2703         while(base) {
2704                 if(base != BASACT) {
2705                         if(TESTBASELIB(base)) {
2706                                 base->object->recalc |= OB_RECALC_OB;
2707                                 
2708                                 if(event==1) {  /* loc */
2709                                         VECCOPY(base->object->loc, ob->loc);
2710                                         VECCOPY(base->object->dloc, ob->dloc);
2711                                 }
2712                                 else if(event==2) {  /* rot */
2713                                         VECCOPY(base->object->rot, ob->rot);
2714                                         VECCOPY(base->object->drot, ob->drot);
2715                                         VECCOPY(base->object->quat, ob->quat);
2716                                         VECCOPY(base->object->dquat, ob->dquat);
2717                                 }
2718                                 else if(event==3) {  /* size */
2719                                         VECCOPY(base->object->size, ob->size);
2720                                         VECCOPY(base->object->dsize, ob->dsize);
2721                                 }
2722                                 else if(event==4) {  /* drawtype */
2723                                         base->object->dt= ob->dt;
2724                                         base->object->dtx= ob->dtx;
2725                                 }
2726                                 else if(event==5) {  /* time offs */
2727                                         base->object->sf= ob->sf;
2728                                 }
2729                                 else if(event==6) {  /* dupli */
2730                                         base->object->dupon= ob->dupon;
2731                                         base->object->dupoff= ob->dupoff;
2732                                         base->object->dupsta= ob->dupsta;
2733                                         base->object->dupend= ob->dupend;
2734                                         
2735                                         base->object->transflag &= ~OB_DUPLI;
2736                                         base->object->transflag |= (ob->transflag & OB_DUPLI);
2737                                 }
2738                                 else if(event==7) {     /* mass */
2739                                         base->object->mass= ob->mass;
2740                                 }
2741                                 else if(event==8) {     /* damping */
2742                                         base->object->damping= ob->damping;
2743                                         base->object->rdamping= ob->rdamping;
2744                                 }
2745                                 else if(event==17) {    /* tex space */
2746                                         obt= base->object;
2747                                         poin1= 0;
2748                                         if(obt->type==OB_MESH) poin1= &(((Mesh *)obt->data)->texflag);
2749                                         else if ELEM3(obt->type, OB_CURVE, OB_SURF, OB_FONT) poin1= &(((Curve *)obt->data)->texflag);
2750                                         else if(obt->type==OB_MBALL) poin1= &(((MetaBall *)obt->data)->texflag);                                        
2751                                         
2752                                         if(poin1) {
2753                                                 memcpy(poin1, poin2, 4+12+12+12);
2754                                         
2755                                                 if(obt->type==OB_MESH) ;
2756                                                 else if(obt->type==OB_MBALL) tex_space_mball(obt);
2757                                                 else tex_space_curve(obt->data);
2758                                         }
2759                                 }
2760                                 else if(event==18) {    /* font settings */
2761                                         
2762                                         if(base->object->type==ob->type) {
2763                                                 cu= ob->data;
2764                                                 cu1= base->object->data;
2765                                                 
2766                                                 cu1->spacemode= cu->spacemode;
2767                                                 cu1->spacing= cu->spacing;
2768                                                 cu1->linedist= cu->linedist;
2769                                                 cu1->shear= cu->shear;
2770                                                 cu1->fsize= cu->fsize;
2771                                                 cu1->xof= cu->xof;
2772                                                 cu1->yof= cu->yof;
2773                                                 cu1->textoncurve= cu->textoncurve;
2774                                                 cu1->wordspace= cu->wordspace;
2775                                                 cu1->ulpos= cu->ulpos;
2776                                                 cu1->ulheight= cu->ulheight;
2777                                                 if(cu1->vfont) cu1->vfont->id.us--;
2778                                                 cu1->vfont= cu->vfont;
2779                                                 id_us_plus((ID *)cu1->vfont);
2780                                                 if(cu1->vfontb) cu1->vfontb->id.us--;
2781                                                 cu1->vfontb= cu->vfontb;
2782                                                 id_us_plus((ID *)cu1->vfontb);
2783                                                 if(cu1->vfonti) cu1->vfonti->id.us--;
2784                                                 cu1->vfonti= cu->vfonti;
2785                                                 id_us_plus((ID *)cu1->vfonti);
2786                                                 if(cu1->vfontbi) cu1->vfontbi->id.us--;
2787                                                 cu1->vfontbi= cu->vfontbi;
2788                                                 id_us_plus((ID *)cu1->vfontbi);                                         
2789
2790                                                 text_to_curve(base->object, 0);         // needed?
2791
2792                                                 
2793                                                 strcpy(cu1->family, cu->family);
2794                                                 
2795                                                 base->object->recalc |= OB_RECALC_DATA;
2796                                         }
2797                                 }
2798                                 else if(event==19) {    /* bevel settings */
2799                                         
2800                                         if ELEM(base->object->type, OB_CURVE, OB_FONT) {
2801                                                 cu= ob->data;
2802                                                 cu1= base->object->data;
2803                                                 
2804                                                 cu1->bevobj= cu->bevobj;
2805                                                 cu1->taperobj= cu->taperobj;
2806                                                 cu1->width= cu->width;
2807                                                 cu1->bevresol= cu->bevresol;
2808                                                 cu1->ext1= cu->ext1;
2809                                                 cu1->ext2= cu->ext2;
2810                                                 
2811                                                 base->object->recalc |= OB_RECALC_DATA;
2812                                         }
2813                                 }
2814                                 else if(event==25) {    /* curve resolution */
2815
2816                                         if ELEM(base->object->type, OB_CURVE, OB_FONT) {
2817                                                 cu= ob->data;
2818                                                 cu1= base->object->data;
2819                                                 
2820                                                 cu1->resolu= cu->resolu;
2821                                                 
2822                                                 nu= cu1->nurb.first;
2823                                                 
2824                                                 while(nu) {
2825                                                         nu->resolu= cu1->resolu;
2826                                                         nu= nu->next;
2827                                                 }
2828                                                 
2829                                                 base->object->recalc |= OB_RECALC_DATA;
2830                                         }
2831                                 }
2832                                 else if(event==20) {    /* particle settings */
2833                                         PartEff *pa1, *pa2;
2834                                         char *p1, *p2;
2835                                         
2836                                         pa1= give_parteff(ob);
2837                                         pa2= give_parteff(base->object);
2838
2839                                         if(pa1==0 && pa2) {
2840                                                 BLI_remlink( &(base->object->effect), pa2);
2841                                                 free_effect( (Effect *) pa2);
2842                                         }
2843                                         else if(pa1 && pa2==0) {
2844                                                 free_effects(&(base->object->effect));
2845                                                 copy_effects(&(base->object->effect), &ob->effect);
2846                                                 build_particle_system(base->object);
2847                                         }
2848                                         else if(pa1 && pa2) {
2849                                                 if(pa2->keys) MEM_freeN(pa2->keys);
2850                                                 
2851                                                 p1= (char *)pa1; p2= (char *)pa2;
2852                                                 memcpy( p2+8, p1+8, sizeof(PartEff) - 8);
2853                                                 pa2->keys= 0;
2854                                                 
2855                                                 build_particle_system(base->object);
2856                                         }
2857                                 }
2858                                 else if(event==21){
2859                                         if (base->object->type==OB_MESH) {
2860                                                 ModifierData *md = modifiers_findByType(ob, eModifierType_Subsurf);
2861
2862                                                 if (md) {
2863                                                         ModifierData *tmd = modifiers_findByType(base->object, eModifierType_Subsurf);
2864
2865                                                         if (!tmd) {
2866                                                                 tmd = modifier_new(eModifierType_Subsurf);
2867                                                                 BLI_addtail(&base->object->modifiers, tmd);
2868                                                         }
2869
2870                                                         modifier_copyData(md, tmd);
2871                                                         base->object->recalc |= OB_RECALC_DATA;
2872                                                 }
2873                                         }
2874                                 }
2875                                 else if(event==22) {
2876                                         /* Clear the constraints on the target