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