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