1bb546982f8f5f2c0f0fe3a7a5ec735a4945d607
[blender.git] / source / blender / src / editobject.c
1 /**
2  * $Id$
3  *
4  * ***** BEGIN GPL/BL DUAL LICENSE BLOCK *****
5  *
6  * This program is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public License
8  * as published by the Free Software Foundation; either version 2
9  * of the License, or (at your option) any later version. The Blender
10  * Foundation also sells licenses for use in proprietary software under
11  * the Blender License.  See http://www.blender.org/BL/ for information
12  * about this.
13  *
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17  * GNU General Public License for more details.
18  *
19  * You should have received a copy of the GNU General Public License
20  * along with this program; if not, write to the Free Software Foundation,
21  * Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
22  *
23  * The Original Code is Copyright (C) 2001-2002 by NaN Holding BV.
24  * All rights reserved.
25  *
26  * The Original Code is: all of this file.
27  *
28  * Contributor(s): none yet.
29  *
30  * ***** END GPL/BL DUAL LICENSE BLOCK *****
31  */
32
33 /** 
34  * Theorie: (matrices) A x B x C == A x ( B x C x Binv) x B
35  * ofwel: OB x PAR x EDIT = OB x (PAR x EDIT x PARinv) x PAR
36  */
37
38 #include <stdlib.h>
39 #include <string.h>
40 #include <math.h>
41
42 #ifdef HAVE_CONFIG_H
43 #include <config.h>
44 #endif
45
46 #ifndef WIN32
47 #include <unistd.h>
48 #else
49 #include <io.h>
50 #endif
51 #include "MEM_guardedalloc.h"
52 #include "PIL_time.h"
53
54 #include "BMF_Api.h"
55
56
57 #include "IMB_imbuf_types.h"
58
59 #include "DNA_action_types.h"
60 #include "DNA_armature_types.h"
61 #include "DNA_camera_types.h"
62 #include "DNA_constraint_types.h"
63 #include "DNA_curve_types.h"
64 #include "DNA_effect_types.h"
65 #include "DNA_group_types.h"
66 #include "DNA_image_types.h"
67 #include "DNA_ipo_types.h"
68 #include "DNA_key_types.h"
69 #include "DNA_lamp_types.h"
70 #include "DNA_lattice_types.h"
71 #include "DNA_material_types.h"
72 #include "DNA_mesh_types.h"
73 #include "DNA_meshdata_types.h"
74 #include "DNA_meta_types.h"
75 #include "DNA_nla_types.h"
76 #include "DNA_object_types.h"
77 #include "DNA_object_force.h"
78 #include "DNA_scene_types.h"
79 #include "DNA_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                 if (EM_texFaceCheck())
1618                         allqueue(REDRAWIMAGE, 0);
1619                 
1620         }
1621         if (ob->type==OB_ARMATURE){
1622                 arm= base->object->data;
1623                 if (!arm) return;
1624                 /*
1625                  * The function object_data_is_libdata make a problem here, the
1626                  * check for ob->proxy return 0 and let blender enter to edit mode
1627                  * this causa a crash when you try leave the edit mode.
1628                  * The problem is that i can't remove the ob->proxy check from
1629                  * object_data_is_libdata that prevent the bugfix #6614, so
1630                  * i add this little hack here.
1631                  */
1632                 if(arm->id.lib) {
1633                         error_libdata();
1634                         return;
1635                 }
1636                 ok=1;
1637                 G.obedit=ob;
1638                 make_editArmature();
1639                 /* to ensure all goes in restposition and without striding */
1640                 DAG_object_flush_update(G.scene, G.obedit, OB_RECALC);
1641
1642                 allqueue (REDRAWVIEW3D,0);
1643         }
1644         else if(ob->type==OB_FONT) {
1645                 G.obedit= ob;
1646                 ok= 1;
1647                 make_editText();
1648         }
1649         else if(ob->type==OB_MBALL) {
1650                 G.obedit= ob;
1651                 ok= 1;
1652                 make_editMball();
1653         }
1654         else if(ob->type==OB_LATTICE) {
1655                 G.obedit= ob;
1656                 ok= 1;
1657                 make_editLatt();
1658         }
1659         else if(ob->type==OB_SURF || ob->type==OB_CURVE) {
1660                 ok= 1;
1661                 G.obedit= ob;
1662                 make_editNurb();
1663         }
1664         allqueue(REDRAWBUTSEDIT, 0);
1665         allqueue(REDRAWOOPS, 0);
1666         countall();
1667         
1668         if(ok) {
1669                 setcursor_space(SPACE_VIEW3D, CURSOR_EDIT);
1670         
1671                 allqueue(REDRAWVIEW3D, 1);
1672                 DAG_object_flush_update(G.scene, G.obedit, OB_RECALC_DATA);
1673                 
1674         }
1675         else G.obedit= NULL;
1676         
1677         if(wc) waitcursor(0);
1678         
1679         scrarea_queue_headredraw(curarea);
1680 }
1681
1682 void exit_editmode(int flag)    /* freedata==0 at render, 1= freedata, 2= do undo buffer too */
1683 {
1684         Object *ob;
1685         int freedata = flag & EM_FREEDATA;
1686         
1687         if(G.obedit==NULL) return;
1688
1689         if(flag & EM_WAITCURSOR) waitcursor(1);
1690         if(G.obedit->type==OB_MESH) {
1691
1692                 /* temporal */
1693                 countall();
1694                 
1695                 if(EM_texFaceCheck())
1696                         allqueue(REDRAWIMAGE, 0);
1697                 
1698                 if(retopo_mesh_paint_check())
1699                         retopo_end_okee();
1700
1701                 if(G.totvert>MESH_MAX_VERTS) {
1702                         error("Too many vertices");
1703                         return;
1704                 }
1705                 load_editMesh();
1706
1707                 if(freedata) free_editMesh(G.editMesh);
1708                 
1709                 if(G.f & G_WEIGHTPAINT)
1710                         mesh_octree_table(G.obedit, NULL, 'e');
1711         }
1712         else if (G.obedit->type==OB_ARMATURE){  
1713                 load_editArmature();
1714                 if (freedata) free_editArmature();
1715         }
1716         else if ELEM(G.obedit->type, OB_CURVE, OB_SURF) {
1717                 extern ListBase editNurb;
1718                 load_editNurb();
1719                 if(freedata) freeNurblist(&editNurb);
1720         }
1721         else if(G.obedit->type==OB_FONT && freedata) {
1722                 load_editText();
1723         }
1724         else if(G.obedit->type==OB_LATTICE) {
1725                 load_editLatt();
1726                 if(freedata) free_editLatt();
1727         }
1728         else if(G.obedit->type==OB_MBALL) {
1729                 extern ListBase editelems;
1730                 load_editMball();
1731                 if(freedata) BLI_freelistN(&editelems);
1732         }
1733
1734         ob= G.obedit;
1735         
1736         /* for example; displist make is different in editmode */
1737         if(freedata) G.obedit= NULL;
1738
1739         /* total remake of softbody data */
1740         if(modifiers_isSoftbodyEnabled(ob)) {
1741                 if (ob->soft && ob->soft->keys) {
1742                         notice("Erased Baked SoftBody");
1743                 }
1744
1745                 sbObjectToSoftbody(ob);
1746         }
1747         
1748         if(ob->type==OB_MESH && get_mesh(ob)->mr)
1749                 multires_edge_level_update(ob, get_mesh(ob));
1750         
1751         /* also flush ob recalc, doesn't take much overhead, but used for particles */
1752         DAG_object_flush_update(G.scene, ob, OB_RECALC_OB|OB_RECALC_DATA);
1753
1754         if(freedata) {
1755                 setcursor_space(SPACE_VIEW3D, CURSOR_STD);
1756         }
1757         
1758         countall();
1759         allqueue(REDRAWVIEW3D, 1);
1760         allqueue(REDRAWBUTSALL, 0);
1761         allqueue(REDRAWACTION, 0);
1762         allqueue(REDRAWNLA, 0);
1763         allqueue(REDRAWIPO, 0);
1764         allqueue(REDRAWOOPS, 0);
1765
1766         scrarea_queue_headredraw(curarea);
1767         
1768         if(G.obedit==NULL && (flag & EM_FREEUNDO)) 
1769                 BIF_undo_push("Editmode");
1770         
1771         if(flag & EM_WAITCURSOR) waitcursor(0);
1772 }
1773
1774 void check_editmode(int type)
1775 {
1776         
1777         if (G.obedit==0 || G.obedit->type==type) return;
1778
1779         exit_editmode(EM_FREEDATA|EM_FREEUNDO|EM_WAITCURSOR); /* freedata, and undo */
1780 }
1781
1782 /* 0 == do center, 1 == center new, 2 == center cursor */
1783
1784 void docenter(int centermode)
1785 {
1786         EditMesh *em = G.editMesh;
1787         Base *base;
1788         Object *ob;
1789         Mesh *me, *tme;
1790         Curve *cu;
1791 /*      BezTriple *bezt;
1792         BPoint *bp; */
1793         Nurb *nu, *nu1;
1794         EditVert *eve;
1795         float cent[3], centn[3], min[3], max[3], omat[3][3];
1796         int a, total= 0;
1797         
1798         /* keep track of what is changed */
1799         int tot_change=0, tot_lib_error=0, tot_key_error=0, tot_multiuser_arm_error=0;
1800         MVert *mvert;
1801
1802         if(G.scene->id.lib || G.vd==NULL) return;
1803         
1804         cent[0]= cent[1]= cent[2]= 0.0;
1805         
1806         if(G.obedit) {
1807
1808                 INIT_MINMAX(min, max);
1809         
1810                 if(G.obedit->type==OB_MESH) {
1811                         for(eve= em->verts.first; eve; eve= eve->next) {
1812                                 if(G.vd->around==V3D_CENTROID) {
1813                                         total++;
1814                                         VECADD(cent, cent, eve->co);
1815                                 }
1816                                 else {
1817                                         DO_MINMAX(eve->co, min, max);
1818                                 }
1819                         }
1820                         
1821                         if(G.vd->around==V3D_CENTROID) {
1822                                 VecMulf(cent, 1.0f/(float)total);
1823                         }
1824                         else {
1825                                 cent[0]= (min[0]+max[0])/2.0f;
1826                                 cent[1]= (min[1]+max[1])/2.0f;
1827                                 cent[2]= (min[2]+max[2])/2.0f;
1828                         }
1829                         
1830                         for(eve= em->verts.first; eve; eve= eve->next) {
1831                                 VecSubf(eve->co, eve->co, cent);                        
1832                         }
1833                         
1834                         recalc_editnormals();
1835                         tot_change++;
1836                 }
1837         }
1838         
1839         /* reset flags */
1840         for (base=FIRSTBASE; base; base= base->next) {
1841                 if TESTBASELIB(base)
1842                         base->object->flag &= ~OB_DONE;
1843         }
1844         
1845         for (me= G.main->mesh.first; me; me= me->id.next) {
1846                 me->flag &= ~ME_ISDONE;
1847         }
1848         
1849         base= FIRSTBASE;
1850         while(base) {
1851                 
1852                 if TESTBASELIB(base) {
1853                         if((base->object->flag & OB_DONE)==0) {
1854                                 base->object->flag |= OB_DONE;
1855                                 
1856                                 if(base->object->id.lib) {
1857                                         tot_lib_error++;
1858                                 }
1859                                 else if(G.obedit==0 && (me=get_mesh(base->object)) ) {
1860                                         
1861                                         if(me->key) {
1862                                                 /*error("Can't change the center of a mesh with vertex keys");
1863                                                 return;*/
1864                                                 tot_key_error++;
1865                                         } else if (me->id.lib) {
1866                                                 tot_lib_error++;
1867                                         } else {
1868                                                 if(centermode==2) {
1869                                                         VECCOPY(cent, give_cursor());
1870                                                         Mat4Invert(base->object->imat, base->object->obmat);
1871                                                         Mat4MulVecfl(base->object->imat, cent);
1872                                                 } else {
1873                                                         INIT_MINMAX(min, max);
1874                                                         mvert= me->mvert;
1875                                                         for(a=0; a<me->totvert; a++, mvert++) {
1876                                                                 DO_MINMAX(mvert->co, min, max);
1877                                                         }
1878                                         
1879                                                         cent[0]= (min[0]+max[0])/2.0f;
1880                                                         cent[1]= (min[1]+max[1])/2.0f;
1881                                                         cent[2]= (min[2]+max[2])/2.0f;
1882                                                 }
1883
1884                                                 mvert= me->mvert;
1885                                                 for(a=0; a<me->totvert; a++, mvert++) {
1886                                                         VecSubf(mvert->co, mvert->co, cent);
1887                                                 }
1888                                                 me->flag |= ME_ISDONE;
1889                                                 
1890                                                 if(centermode) {
1891                                                         Mat3CpyMat4(omat, base->object->obmat);
1892                                                         
1893                                                         VECCOPY(centn, cent);
1894                                                         Mat3MulVecfl(omat, centn);
1895                                                         base->object->loc[0]+= centn[0];
1896                                                         base->object->loc[1]+= centn[1];
1897                                                         base->object->loc[2]+= centn[2];
1898                                                         
1899                                                         /* other users? */
1900                                                         ob= G.main->object.first;
1901                                                         while(ob) {
1902                                                                 if((ob->flag & OB_DONE)==0) {
1903                                                                         tme= get_mesh(ob);
1904                                                                         
1905                                                                         if(tme==me) {
1906                                                                                 
1907                                                                                 ob->flag |= OB_DONE;
1908                                                                                 ob->recalc= OB_RECALC_OB|OB_RECALC_DATA;
1909
1910                                                                                 Mat3CpyMat4(omat, ob->obmat);
1911                                                                                 VECCOPY(centn, cent);
1912                                                                                 Mat3MulVecfl(omat, centn);
1913                                                                                 ob->loc[0]+= centn[0];
1914                                                                                 ob->loc[1]+= centn[1];
1915                                                                                 ob->loc[2]+= centn[2];
1916                                                                                 
1917                                                                                 if(tme && (tme->flag & ME_ISDONE)==0) {
1918                                                                                         mvert= tme->mvert;
1919                                                                                         for(a=0; a<tme->totvert; a++, mvert++) {
1920                                                                                                 VecSubf(mvert->co, mvert->co, cent);
1921                                                                                         }
1922                                                                                         tme->flag |= ME_ISDONE;
1923                                                                                 }
1924                                                                         }
1925                                                                 }
1926                                                                 
1927                                                                 ob= ob->id.next;
1928                                                         }
1929                                                 }
1930                                                 tot_change++;
1931                                         }
1932                                 }
1933                                 else if ELEM(base->object->type, OB_CURVE, OB_SURF) {
1934                                         
1935                                         /* totally weak code here... (ton) */
1936                                         if(G.obedit==base->object) {
1937                                                 extern ListBase editNurb;
1938                                                 nu1= editNurb.first;
1939                                                 cu= G.obedit->data;
1940                                         }
1941                                         else {
1942                                                 cu= base->object->data;
1943                                                 nu1= cu->nurb.first;
1944                                         }
1945                                         
1946                                         if (cu->id.lib) {
1947                                                 tot_lib_error++;
1948                                         } else {
1949                                                 if(centermode==2) {
1950                                                         VECCOPY(cent, give_cursor());
1951                                                         Mat4Invert(base->object->imat, base->object->obmat);
1952                                                         Mat4MulVecfl(base->object->imat, cent);
1953
1954                                                         /* don't allow Z change if curve is 2D */
1955                                                         if( !( cu->flag & CU_3D ) )
1956                                                                 cent[2] = 0.0;
1957                                                 } else {
1958                                                         INIT_MINMAX(min, max);
1959                 
1960                                                         nu= nu1;
1961                                                         while(nu) {
1962                                                                 minmaxNurb(nu, min, max);
1963                                                                 nu= nu->next;
1964                                                         }
1965                                                         
1966                                                         cent[0]= (min[0]+max[0])/2.0f;
1967                                                         cent[1]= (min[1]+max[1])/2.0f;
1968                                                         cent[2]= (min[2]+max[2])/2.0f;
1969                                                 }
1970                                                 
1971                                                 nu= nu1;
1972                                                 while(nu) {
1973                                                         if( (nu->type & 7)==1) {
1974                                                                 a= nu->pntsu;
1975                                                                 while (a--) {
1976                                                                         VecSubf(nu->bezt[a].vec[0], nu->bezt[a].vec[0], cent);
1977                                                                         VecSubf(nu->bezt[a].vec[1], nu->bezt[a].vec[1], cent);
1978                                                                         VecSubf(nu->bezt[a].vec[2], nu->bezt[a].vec[2], cent);
1979                                                                 }
1980                                                         }
1981                                                         else {
1982                                                                 a= nu->pntsu*nu->pntsv;
1983                                                                 while (a--)
1984                                                                         VecSubf(nu->bp[a].vec, nu->bp[a].vec, cent);
1985                                                         }
1986                                                         nu= nu->next;
1987                                                 }
1988                                 
1989                                                 if(centermode && G.obedit==0) {
1990                                                         Mat3CpyMat4(omat, base->object->obmat);
1991                                                         
1992                                                         Mat3MulVecfl(omat, cent);
1993                                                         base->object->loc[0]+= cent[0];
1994                                                         base->object->loc[1]+= cent[1];
1995                                                         base->object->loc[2]+= cent[2];
1996                                                 }
1997                                                 
1998                                                 tot_change++;
1999                                                 if(G.obedit) {
2000                                                         if (centermode==0) {
2001                                                                 DAG_object_flush_update(G.scene, G.obedit, OB_RECALC_DATA);
2002                                                         }
2003                                                         break;
2004                                                 }
2005                                         }
2006                                 }
2007                                 else if(base->object->type==OB_FONT) {
2008                                         /* get from bb */
2009                                         
2010                                         cu= base->object->data;
2011                                         
2012                                         if(cu->bb==0) {
2013                                                 /* do nothing*/
2014                                         } else if (cu->id.lib) {
2015                                                 tot_lib_error++;
2016                                         } else {
2017                                                 cu->xof= -0.5f*( cu->bb->vec[4][0] - cu->bb->vec[0][0]);
2018                                                 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 */
2019                                                 
2020                                                 /* not really ok, do this better once! */
2021                                                 cu->xof /= cu->fsize;
2022                                                 cu->yof /= cu->fsize;
2023
2024                                                 allqueue(REDRAWBUTSEDIT, 0);
2025                                                 tot_change++;
2026                                         }
2027                                 }
2028                                 else if(base->object->type==OB_ARMATURE) {
2029                                         bArmature *arm = base->object->data;
2030                                         
2031                                         if (arm->id.lib) {
2032                                                 tot_lib_error++;
2033                                         } else if(arm->id.us>1) {
2034                                                 /*error("Can't apply to a multi user armature");
2035                                                 return;*/
2036                                                 tot_multiuser_arm_error++;
2037                                         } else {
2038                                                 /* Function to recenter armatures in editarmature.c 
2039                                                  * Bone + object locations are handled there.
2040                                                  */
2041                                                 docenter_armature(base->object, centermode);
2042                                                 tot_change++;
2043                                                 if(G.obedit) 
2044                                                         break;
2045                                         }
2046                                 }
2047                                 base->object->recalc= OB_RECALC_OB|OB_RECALC_DATA;
2048                         }
2049                 }
2050                 base= base->next;
2051         }
2052         if (tot_change) {
2053                 DAG_scene_flush_update(G.scene, screen_view3d_layers());
2054                 allqueue(REDRAWVIEW3D, 0);
2055                 BIF_undo_push("Do Center");     
2056         }
2057         
2058         /* Warn if any errors occured */
2059         if (tot_lib_error+tot_key_error+tot_multiuser_arm_error) {
2060                 char err[512];
2061                 sprintf(err, "Warning %i Object(s) Not Centered, %i Changed%%t", tot_lib_error+tot_key_error+tot_multiuser_arm_error, tot_change);
2062                 
2063                 if (tot_lib_error)
2064                         sprintf(err+strlen(err), "|%i linked library objects", tot_lib_error);
2065                 if (tot_key_error)
2066                         sprintf(err+strlen(err), "|%i mesh key object(s)", tot_key_error);
2067                 if (tot_multiuser_arm_error)
2068                         sprintf(err+strlen(err), "|%i multiuser armature object(s)", tot_multiuser_arm_error);
2069                 
2070                 pupmenu(err);
2071         }
2072 }
2073
2074 void docenter_new(void)
2075 {
2076         if(G.scene->id.lib) return;
2077
2078         if(G.obedit) {
2079                 error("Unable to center new in Edit Mode");
2080         }
2081         else {
2082                 docenter(1);
2083         }
2084 }
2085
2086 void docenter_cursor(void)
2087 {
2088         if(G.scene->id.lib) return;
2089
2090         if(G.obedit) {
2091                 error("Unable to center cursor in Edit Mode");
2092         }
2093         else {
2094                 docenter(2);
2095         }
2096 }
2097
2098 void movetolayer(void)
2099 {
2100         Base *base;
2101         unsigned int lay= 0, local;
2102         int islamp= 0;
2103         
2104         if(G.scene->id.lib) return;
2105
2106         base= FIRSTBASE;
2107         while(base) {
2108                 if TESTBASE(base) lay |= base->lay;
2109                 base= base->next;
2110         }
2111         if(lay==0) return;
2112         lay &= 0xFFFFFF;
2113         
2114         if(lay==0) return;
2115         
2116         if(G.vd->localview) {
2117                 /* now we can move out of localview. */
2118                 if (!okee("Move from localview")) return;
2119                 base= FIRSTBASE;
2120                 while(base) {
2121                         if TESTBASE(base) {
2122                                 lay= base->lay & ~G.vd->lay;
2123                                 base->lay= lay;
2124                                 base->object->lay= lay;
2125                                 base->object->flag &= ~SELECT;
2126                                 base->flag &= ~SELECT;
2127                                 if(base->object->type==OB_LAMP) islamp= 1;
2128                         }
2129                         base= base->next;
2130                 }
2131         } else {
2132                 if( movetolayer_buts(&lay, NULL)==0 ) return;
2133                 
2134                 /* normal non localview operation */
2135                 base= FIRSTBASE;
2136                 while(base) {
2137                         if TESTBASE(base) {
2138                                 /* upper byte is used for local view */
2139                                 local= base->lay & 0xFF000000;  
2140                                 base->lay= lay + local;
2141                                 base->object->lay= lay;
2142                                 if(base->object->type==OB_LAMP) islamp= 1;
2143                         }
2144                         base= base->next;
2145                 }
2146         }
2147         if(islamp) reshadeall_displist();       /* only frees */
2148         
2149         /* warning, active object may be hidden now */
2150         
2151         countall();
2152         DAG_scene_sort(G.scene);
2153         
2154         allqueue(REDRAWBUTSEDIT, 0);
2155         allqueue(REDRAWVIEW3D, 0);
2156         allqueue(REDRAWOOPS, 0);
2157         allqueue(REDRAWINFO, 0);
2158         
2159         BIF_undo_push("Move to layer");
2160 }
2161
2162 /* THIS IS BAD CODE! do not bring back before it has a real implementation (ton) */
2163 void split_font()
2164 {
2165         Object *ob = OBACT;
2166         Base *oldbase = BASACT;
2167         Curve *cu= ob->data;
2168         char *p= cu->str;
2169         int slen= strlen(p);
2170         int i;
2171
2172         for (i = 0; i<=slen; p++, i++) {
2173                 adduplicate(1, U.dupflag);
2174                 cu= OBACT->data;
2175                 cu->sepchar = i+1;
2176                 text_to_curve(OBACT, 0);        /* pass 1: only one letter, adapt position */
2177                 text_to_curve(OBACT, 0);        /* pass 2: remake */
2178                 freedisplist(&OBACT->disp);
2179                 makeDispListCurveTypes(OBACT, 0);
2180                 
2181                 OBACT->flag &= ~SELECT;
2182                 BASACT->flag &= ~SELECT;
2183                 oldbase->flag |= SELECT;
2184                 oldbase->object->flag |= SELECT;
2185                 set_active_base(oldbase);               
2186         }
2187 }
2188
2189 void special_editmenu(void)
2190 {
2191         static short numcuts= 2;
2192         Object *ob= OBACT;
2193         float fac;
2194         int nr,ret;
2195         short randfac;
2196         
2197         if(ob==NULL) return;
2198         
2199         if(G.obedit==NULL) {
2200                 
2201                 if(ob->flag & OB_POSEMODE) {
2202                         pose_special_editmenu();
2203                 }
2204                 else if(G.f & G_FACESELECT) {
2205                         Mesh *me= get_mesh(ob);
2206                         MTFace *tface;
2207                         MFace *mface;
2208                         int a;
2209                         
2210                         if(me==0 || me->mtface==0) return;
2211                         
2212                         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");
2213                         
2214                         tface= me->mtface;
2215                         mface= me->mface;
2216                         for(a=me->totface; a>0; a--, tface++, mface++) {
2217                                 if(mface->flag & ME_FACE_SEL) {
2218                                         switch(nr) {
2219                                         case 1:
2220                                                 tface->mode |= TF_TEX; break;
2221                                         case 2:
2222                                                 tface->mode |= TF_SHAREDCOL; break;
2223                                         case 3:
2224                                                 tface->mode |= TF_LIGHT; break; 
2225                                         case 4:
2226                                                 tface->mode |= TF_INVISIBLE; break;
2227                                         case 5:
2228                                                 tface->mode |= TF_DYNAMIC; break;
2229                                         case 6:
2230                                                 tface->mode |= TF_TWOSIDE; break;
2231                                         case 7:
2232                                                 tface->mode &= ~TF_TEX;
2233                                                 tface->tpage= 0;
2234                                                 break;
2235                                         case 8:
2236                                                 tface->mode &= ~TF_SHAREDCOL; break;
2237                                         case 9:
2238                                                 tface->mode &= ~TF_LIGHT; break;
2239                                         case 10:
2240                                                 tface->mode &= ~TF_INVISIBLE; break;
2241                                         case 11:
2242                                                 tface->mode &= ~TF_DYNAMIC; break;
2243                                         case 12:
2244                                                 tface->mode &= ~TF_TWOSIDE; break;
2245                                         }
2246                                 }
2247                         }
2248                         DAG_object_flush_update(G.scene, ob, OB_RECALC_DATA);
2249                         allqueue(REDRAWVIEW3D, 0);
2250                         allqueue(REDRAWBUTSEDIT, 0);
2251                         BIF_undo_push("Change texture face");
2252                 }
2253                 else if(G.f & G_VERTEXPAINT) {
2254                         Mesh *me= get_mesh(ob);
2255                         
2256                         if(me==0 || (me->mcol==NULL && me->mtface==NULL) ) return;
2257                         
2258                         nr= pupmenu("Specials%t|Shared VertexCol%x1");
2259                         if(nr==1) {
2260                                 
2261                                 do_shared_vertexcol(me);
2262                                 
2263                                 BIF_undo_push("Shared VertexCol");
2264
2265                                 DAG_object_flush_update(G.scene, ob, OB_RECALC_DATA);
2266                         }
2267                 }
2268                 else if(G.f & G_WEIGHTPAINT) {
2269                         Object *par= modifiers_isDeformedByArmature(ob);
2270
2271                         if(par && (par->flag & OB_POSEMODE)) {
2272                                 nr= pupmenu("Specials%t|Apply Bone Envelopes to Vertex Groups %x1|Apply Bone Heat Weights to Vertex Groups %x2");
2273
2274                                 if(nr==1 || nr==2)
2275                                         pose_adds_vgroups(ob, (nr == 2));
2276                         }
2277                 }
2278                 else {
2279                         Base *base, *base_select= NULL;
2280                         
2281                         /* Get the active object mesh. */
2282                         Mesh *me= get_mesh(ob);
2283
2284                         /* Booleans, if the active object is a mesh... */
2285                         if (me && ob->id.lib==NULL) {
2286                                 
2287                                 /* Bring up a little menu with the boolean operation choices on. */
2288                                 nr= pupmenu("Boolean Tools%t|Intersect%x1|Union%x2|Difference%x3|Add Intersect Modifier%x4|Add Union Modifier%x5|Add Difference Modifier%x6");
2289
2290                                 if (nr > 0) {
2291                                         /* user has made a choice of a menu element.
2292                                            All of the boolean functions require 2 mesh objects 
2293                                            we search through the object list to find the other 
2294                                            selected item and make sure it is distinct and a mesh. */
2295
2296                                         for(base= FIRSTBASE; base; base= base->next) {
2297                                                 if TESTBASELIB(base) {
2298                                                         if(base->object != ob) base_select= base;
2299                                                 }
2300                                         }
2301
2302                                         if (base_select) {
2303                                                 if (get_mesh(base_select->object)) {
2304                                                         if(nr <= 3){
2305                                                                 waitcursor(1);
2306                                                                 ret = NewBooleanMesh(BASACT,base_select,nr);
2307                                                                 if (ret==0) {
2308                                                                         error("An internal error occurred");
2309                                                                 } else if(ret==-1) {
2310                                                                         error("Selected meshes must have faces to perform boolean operations");
2311                                                                 } else if (ret==-2) {
2312                                                                         error("Both meshes must be a closed mesh");
2313                                                                 }
2314                                                                 else BIF_undo_push("Boolean");
2315                                                                 waitcursor(0);
2316                                                         } else {
2317                                                                 BooleanModifierData *bmd = NULL;
2318                                                                 bmd = (BooleanModifierData *)modifier_new(eModifierType_Boolean);
2319                                                                 BLI_addtail(&ob->modifiers, bmd);
2320                                                                 bmd->object = base_select->object;
2321                                                                 bmd->modifier.mode |= eModifierMode_Realtime;
2322                                                                 switch(nr){
2323                                                                         case 4: bmd->operation = eBooleanModifierOp_Intersect; break;
2324                                                                         case 5: bmd->operation = eBooleanModifierOp_Union; break;
2325                                                                         case 6: bmd->operation = eBooleanModifierOp_Difference; break;
2326                                                                 }
2327                                                                 do_common_editbuts(B_CHANGEDEP);
2328                                                                 BIF_undo_push("Add Boolean modifier");                                                          
2329                                                         }                                                               
2330                                                 } else {
2331                                                         error("Please select 2 meshes");
2332                                                 }
2333                                         } else {
2334                                                 error("Please select 2 meshes");
2335                                         }
2336                                 }
2337
2338                                 allqueue(REDRAWVIEW3D, 0);
2339                         }
2340                         else if (ob->type == OB_FONT) {
2341                                 /* removed until this gets a decent implementation (ton) */
2342 /*                              nr= pupmenu("Split %t|Characters%x1");
2343                                 if (nr > 0) {
2344                                         switch(nr) {
2345                                                 case 1: split_font();
2346                                         }
2347                                 }
2348 */
2349                         }                       
2350                 }
2351         }
2352         else if(G.obedit->type==OB_MESH) {
2353
2354                 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");
2355                 
2356                 switch(nr) {
2357                 case 1:
2358                         waitcursor(1);
2359                         esubdivideflag(1, 0.0, G.scene->toolsettings->editbutflag, 1, 0);
2360                         
2361                         BIF_undo_push("ESubdivide Single");            
2362                         break;
2363                 case 2:
2364                         if(button(&numcuts, 1, 128, "Number of Cuts:")==0) return;
2365                         waitcursor(1);
2366                         esubdivideflag(1, 0.0, G.scene->toolsettings->editbutflag, numcuts, 0);
2367                         BIF_undo_push("ESubdivide");
2368                         break;
2369                 case 3:
2370                         if(button(&numcuts, 1, 128, "Number of Cuts:")==0) return;
2371                         randfac= 10;
2372                         if(button(&randfac, 1, 100, "Rand fac:")==0) return;
2373                         waitcursor(1);                  
2374                         fac= -( (float)randfac )/100;
2375                         esubdivideflag(1, fac, G.scene->toolsettings->editbutflag, numcuts, 0);
2376                         BIF_undo_push("Subdivide Fractal");
2377                         break;
2378                         
2379                 case 12:        /* smooth */
2380                         /* if(button(&numcuts, 1, 128, "Number of Cuts:")==0) return; */
2381                         fac= 1.0f;
2382                         if(fbutton(&fac, 0.0f, 5.0f, 10, 10, "Smooth:")==0) return;
2383                                 fac= 0.292f*fac;
2384                         
2385                         waitcursor(1);
2386                         esubdivideflag(1, fac, G.scene->toolsettings->editbutflag | B_SMOOTH, 1, 0);
2387                         BIF_undo_push("Subdivide Smooth");
2388                         break;          
2389                         
2390                 case 4:
2391                         mergemenu();
2392                         break;
2393                 case 5:
2394                         notice("Removed %d Vertices", removedoublesflag(1, G.scene->toolsettings->doublimit));
2395                         BIF_undo_push("Remove Doubles");
2396                         break;
2397                 case 6:
2398                         hide_mesh(0);
2399                         break;
2400                 case 7:
2401                         reveal_mesh();
2402                         break;
2403                 case 8:
2404                         selectswap_mesh();
2405                         break;
2406                 case 9:
2407                         flip_editnormals();
2408                         BIF_undo_push("Flip Normals");
2409                         break;
2410                 case 10:
2411                         vertexsmooth();
2412                         break;
2413                 case 11:
2414                         bevel_menu();
2415                         break;
2416                 case 14:
2417                         mesh_set_smooth_faces(1);
2418                         break;
2419                 case 15: 
2420                         mesh_set_smooth_faces(0);
2421                         break;
2422                 case 16: 
2423                         shape_copy_select_from();
2424                         break;
2425                 case 17: 
2426                         shape_propagate();
2427                         break;
2428                 case 18:
2429                         pathselect();
2430                         BIF_undo_push("Select Vertex Path");
2431                         break;
2432                 }
2433                 
2434                 DAG_object_flush_update(G.scene, G.obedit, OB_RECALC_DATA);
2435                 
2436                 if(nr>0) waitcursor(0);
2437                 
2438         }
2439         else if ELEM(G.obedit->type, OB_CURVE, OB_SURF) {
2440
2441                 nr= pupmenu("Specials%t|Subdivide%x1|Switch Direction%x2|Set Goal Weight %x3|Set Radius %x4");
2442                 
2443                 switch(nr) {
2444                 case 1:
2445                         subdivideNurb();
2446                         break;
2447                 case 2:
2448                         switchdirectionNurb2();
2449                         break;
2450                 case 3:
2451                         {
2452                                 static float weight= 1.0f;
2453                                 extern ListBase editNurb;
2454                                 Nurb *nu;
2455                                 BezTriple *bezt;
2456                                 BPoint *bp;
2457                                 int a;
2458                                 
2459                                 if(fbutton(&weight, 0.0f, 1.0f, 10, 10, "Set Weight")) {
2460                                         for(nu= editNurb.first; nu; nu= nu->next) {
2461                                                 if(nu->bezt) {
2462                                                         for(bezt=nu->bezt, a=0; a<nu->pntsu; a++, bezt++) {
2463                                                                 if(bezt->f2 & SELECT)
2464                                                                         bezt->weight= weight;
2465                                                         }
2466                                                 }
2467                                                 else if(nu->bp) {
2468                                                         for(bp=nu->bp, a=0; a<nu->pntsu*nu->pntsv; a++, bp++) {
2469                                                                 if(bp->f1 & SELECT)
2470                                                                         bp->weight= weight;
2471                                                         }
2472                                                 }
2473                                         }       
2474                                 }
2475                         }
2476                         break;
2477                 case 4:
2478                         {
2479                                 static float radius= 1.0f;
2480                                 extern ListBase editNurb;
2481                                 Nurb *nu;
2482                                 BezTriple *bezt;
2483                                 BPoint *bp;
2484                                 int a;
2485                                 
2486                                 if(fbutton(&radius, 0.0001f, 10.0f, 10, 10, "Set Radius")) {
2487                                         for(nu= editNurb.first; nu; nu= nu->next) {
2488                                                 if(nu->bezt) {
2489                                                         for(bezt=nu->bezt, a=0; a<nu->pntsu; a++, bezt++) {
2490                                                                 if(bezt->f2 & SELECT)
2491                                                                         bezt->radius= radius;
2492                                                         }
2493                                                 }
2494                                                 else if(nu->bp) {
2495                                                         for(bp=nu->bp, a=0; a<nu->pntsu*nu->pntsv; a++, bp++) {
2496                                                                 if(bp->f1 & SELECT)
2497                                                                         bp->radius= radius;
2498                                                         }
2499                                                 }
2500                                         }       
2501                                 }
2502                                 
2503                                 DAG_object_flush_update(G.scene, ob, OB_RECALC_DATA);
2504                                 allqueue(REDRAWVIEW3D, 0);
2505                                 allqueue(REDRAWBUTSALL, 0);
2506                                 allqueue(REDRAWINFO, 1);        /* 1, because header->win==0! */
2507                                 
2508                         }
2509                         break;
2510                 }
2511                 
2512                 DAG_object_flush_update(G.scene, G.obedit, OB_RECALC_DATA);
2513         }
2514         else if(G.obedit->type==OB_ARMATURE) {
2515                 nr= pupmenu("Specials%t|Subdivide %x1|Subdivide Multi%x2|Flip Left-Right Names%x3");
2516                 if(nr==1)
2517                         subdivide_armature(1);
2518                 if(nr==2) {
2519                         if(button(&numcuts, 1, 128, "Number of Cuts:")==0) return;
2520                         waitcursor(1);
2521                         subdivide_armature(numcuts);
2522                 }
2523                 else if(nr==3)
2524                         armature_flip_names();
2525         }
2526         else if(G.obedit->type==OB_LATTICE) {
2527                 static float weight= 1.0f;
2528                 if(fbutton(&weight, 0.0f, 1.0f, 10, 10, "Set Weight")) {
2529                         int a= editLatt->pntsu*editLatt->pntsv*editLatt->pntsw;
2530                         BPoint *bp= editLatt->def;
2531                         
2532                         while(a--) {
2533                                 if(bp->f1 & SELECT)
2534                                         bp->weight= weight;
2535                                 bp++;
2536                         }       
2537                 }
2538         }
2539
2540         countall();
2541         allqueue(REDRAWVIEW3D, 0);
2542         
2543 }
2544
2545 static void curvetomesh(Object *ob) 
2546 {
2547         Curve *cu;
2548         DispList *dl;
2549         
2550         ob->flag |= OB_DONE;
2551         cu= ob->data;
2552         
2553         dl= cu->disp.first;
2554         if(dl==0) makeDispListCurveTypes(ob, 0);                /* force creation */
2555
2556         nurbs_to_mesh(ob); /* also does users */
2557         if (ob->type != OB_MESH) {
2558                 error("can't convert curve to mesh");
2559         } else {
2560                 object_free_modifiers(ob);
2561         }
2562 }
2563
2564 void convertmenu(void)
2565 {
2566         Base *base, *basen=NULL, *basact, *basedel=NULL;
2567         Object *obact, *ob, *ob1;
2568         Curve *cu;
2569         Nurb *nu;
2570         MetaBall *mb;
2571         Mesh *me;
2572         int ok=0, nr = 0, a;
2573         
2574         if(G.scene->id.lib) return;
2575
2576         obact= OBACT;
2577         if(obact==0) return;
2578         if(!obact->flag & SELECT) return;
2579         if(G.obedit) return;
2580         
2581         basact= BASACT; /* will be restored */
2582                 
2583         if(obact->type==OB_FONT) {
2584                 nr= pupmenu("Convert Font to%t|Curve%x1|Curve (Single filling group)%x2|Mesh%x3");
2585                 if(nr>0) ok= 1;
2586         }
2587         else if(obact->type==OB_MBALL) {
2588                 nr= pupmenu("Convert Metaball to%t|Mesh (keep original)%x1|Mesh (Delete Original)%x2");
2589                 if(nr>0) ok= 1;
2590         }
2591         else if(obact->type==OB_CURVE) {
2592                 nr= pupmenu("Convert Curve to%t|Mesh");
2593                 if(nr>0) ok= 1;
2594         }
2595         else if(obact->type==OB_SURF) {
2596                 nr= pupmenu("Convert Nurbs Surface to%t|Mesh");
2597                 if(nr>0) ok= 1;
2598         }
2599         else if(obact->type==OB_MESH) {
2600                 nr= pupmenu("Convert Modifiers to%t|Mesh (Keep Original)%x1|Mesh (Delete Original)%x2");
2601                 if(nr>0) ok= 1;
2602         }
2603         if(ok==0) return;
2604
2605         /* don't forget multiple users! */
2606
2607         /* reset flags */
2608         base= FIRSTBASE;
2609         while(base) {
2610                 if TESTBASELIB(base) {
2611                         base->object->flag &= ~OB_DONE;
2612                 }
2613                 base= base->next;
2614         }
2615
2616         base= FIRSTBASE;
2617         while(base) {
2618                 if TESTBASELIB(base) {
2619                         
2620                         ob= base->object;
2621                         
2622                         if(ob->flag & OB_DONE);
2623                         else if(ob->type==OB_MESH && ob->modifiers.first) { /* converting a mesh with no modifiers causes a segfault */
2624                                 DerivedMesh *dm;
2625                                 
2626                                 basedel = base;
2627
2628                                 ob->flag |= OB_DONE;
2629
2630                                 ob1= copy_object(ob);
2631                                 ob1->recalc |= OB_RECALC;
2632
2633                                 basen= MEM_mallocN(sizeof(Base), "duplibase");
2634                                 *basen= *base;
2635                                 BLI_addhead(&G.scene->base, basen);     /* addhead: otherwise eternal loop */
2636                                 basen->object= ob1;
2637                                 basen->flag |= SELECT;
2638                                 base->flag &= ~SELECT;
2639                                 ob->flag &= ~SELECT;
2640
2641                                 /* decrement original mesh's usage count  */
2642                                 me= ob1->data;
2643                                 me->id.us--;
2644
2645                                 /* make a new copy of the mesh */
2646                                 ob1->data= copy_mesh(me);
2647                                 G.totmesh++;
2648
2649                                 /* make new mesh data from the original copy */
2650                                 dm= mesh_get_derived_final(ob1, CD_MASK_MESH);
2651                                 /* dm= mesh_create_derived_no_deform(ob1, NULL);        this was called original (instead of get_derived). man o man why! (ton) */
2652                                 
2653                                 DM_to_mesh(dm, ob1->data);
2654
2655                                 dm->release(dm);
2656                                 object_free_modifiers(ob1);     /* after derivedmesh calls! */
2657                                 
2658                                 /* If the original object is active then make this object active */
2659                                 if (ob == obact) {
2660                                         set_active_base( basen );
2661                                         basact = basen;
2662                                 }
2663                         }
2664                         else if(ob->type==OB_FONT) {
2665                                 ob->flag |= OB_DONE;
2666
2667                                 ob->type= OB_CURVE;
2668                                 cu= ob->data;
2669
2670                                 if(cu->vfont) {
2671                                         cu->vfont->id.us--;
2672                                         cu->vfont= 0;
2673                                 }
2674                                 if(cu->vfontb) {
2675                                         cu->vfontb->id.us--;
2676                                         cu->vfontb= 0;
2677                                 }
2678                                 if(cu->vfonti) {
2679                                         cu->vfonti->id.us--;
2680                                         cu->vfonti= 0;
2681                                 }
2682                                 if(cu->vfontbi) {
2683                                         cu->vfontbi->id.us--;
2684                                         cu->vfontbi= 0;
2685                                 }                                       
2686                                 /* other users */
2687                                 if(cu->id.us>1) {
2688                                         ob1= G.main->object.first;
2689                                         while(ob1) {
2690                                                 if(ob1->data==cu) {
2691                                                         ob1->type= OB_CURVE;
2692                                                         ob1->recalc |= OB_RECALC;
2693                                                 }
2694                                                 ob1= ob1->id.next;
2695                                         }
2696                                 }
2697                                 if (nr==2 || nr==3) {
2698                                         nu= cu->nurb.first;
2699                                         while(nu) {
2700                                                 nu->charidx= 0;
2701                                                 nu= nu->next;
2702                                         }                                       
2703                                 }
2704                                 if (nr==3) {
2705                                         curvetomesh(ob);
2706                                 }
2707                         }
2708                         else if ELEM(ob->type, OB_CURVE, OB_SURF) {
2709                                 if(nr==1) {
2710                                         curvetomesh(ob);
2711                                 }
2712                         }
2713                         else if(ob->type==OB_MBALL) {
2714                         
2715                                 if(nr==1 || nr == 2) {
2716                                         ob= find_basis_mball(ob);
2717                                         
2718                                         if(ob->disp.first && !(ob->flag&OB_DONE)) {
2719                                                 basedel = base;
2720                                         
2721                                                 ob->flag |= OB_DONE;
2722
2723                                                 ob1= copy_object(ob);
2724                                                 ob1->recalc |= OB_RECALC;
2725
2726                                                 basen= MEM_mallocN(sizeof(Base), "duplibase");
2727                                                 *basen= *base;
2728                                                 BLI_addhead(&G.scene->base, basen);     /* addhead: othwise eternal loop */
2729                                                 basen->object= ob1;
2730                                                 basen->flag |= SELECT;
2731                                                 basedel->flag &= ~SELECT;
2732                                                 ob->flag &= ~SELECT;
2733                                                 
2734                                                 mb= ob1->data;
2735                                                 mb->id.us--;
2736                                                 
2737                                                 ob1->data= add_mesh("Mesh");
2738                                                 G.totmesh++;
2739                                                 ob1->type= OB_MESH;
2740                                                 
2741                                                 me= ob1->data;
2742                                                 me->totcol= mb->totcol;
2743                                                 if(ob1->totcol) {
2744                                                         me->mat= MEM_dupallocN(mb->mat);
2745                                                         for(a=0; a<ob1->totcol; a++) id_us_plus((ID *)me->mat[a]);
2746                                                 }
2747                                                 
2748                                                 mball_to_mesh(&ob->disp, ob1->data);
2749                                                 
2750                                                 /* So we can see the wireframe */
2751                                                 BASACT= basen;
2752                                                 
2753                                                 /* If the original object is active then make this object active */
2754                                                 if (ob == obact) {
2755                                                         set_active_base( basen );
2756                                                         basact = basen;
2757                                                 }
2758                                                 
2759                                         }
2760                                 }
2761                         }
2762                 }
2763                 base= base->next;
2764                 if(basedel != NULL && nr == 2) {
2765                         if(basedel==basact)
2766                                 basact= NULL;
2767                         free_and_unlink_base(basedel);  
2768                 }
2769                 basedel = NULL;                         
2770         }
2771         
2772         /* texspace and normals */
2773         if(!basen) BASACT= base;
2774
2775         enter_editmode(EM_WAITCURSOR);
2776         exit_editmode(EM_FREEDATA|EM_WAITCURSOR); /* freedata, but no undo */
2777         BASACT= basact;
2778
2779         countall();
2780         allqueue(REDRAWVIEW3D, 0);
2781         allqueue(REDRAWOOPS, 0);
2782         allspace(OOPS_TEST, 0);
2783         allqueue(REDRAWBUTSEDIT, 0);
2784         BIF_undo_push("Convert Object");
2785
2786         DAG_scene_sort(G.scene);
2787 }
2788
2789         /* Change subdivision properties of mesh object ob, if
2790          * level==-1 then toggle subsurf, else set to level.
2791      * *set allows to toggle multiple selections
2792          */
2793 static void object_flip_subdivison(Object *ob, int *set, int level, int mode)
2794 {
2795         ModifierData *md;
2796
2797         if(ob->type!=OB_MESH)
2798                 return;
2799         
2800         md = modifiers_findByType(ob, eModifierType_Subsurf);
2801         
2802         if (md) {
2803                 SubsurfModifierData *smd = (SubsurfModifierData*) md;
2804
2805                 if (level == -1) {
2806                         if(*set == -1) 
2807                                 *set= smd->modifier.mode&(mode);
2808                                                                                   
2809                         if (*set) {
2810                                 smd->modifier.mode &= ~(mode);
2811                         } else {
2812                                 smd->modifier.mode |= (mode);
2813                         }
2814                 } else {
2815                         smd->levels = level;
2816                 }
2817         } 
2818         else if(*set != 0) {
2819                 SubsurfModifierData *smd = (SubsurfModifierData*) modifier_new(eModifierType_Subsurf);
2820
2821                 BLI_addtail(&ob->modifiers, smd);
2822
2823                 if (level!=-1) {
2824                         smd->levels = level;
2825                 }
2826                 
2827                 if(*set == -1)
2828                         *set= 1;
2829         }
2830         ob->recalc |= OB_RECALC_DATA;
2831 }
2832
2833 /* Change subdivision properties of mesh object ob, if
2834 * level==-1 then toggle subsurf, else set to level.
2835 */
2836
2837 void flip_subdivison(int level)
2838 {
2839         Base *base;
2840         int set= -1;
2841         int mode;
2842         
2843         if(G.qual & LR_ALTKEY)
2844                 mode= eModifierMode_Realtime;
2845         else
2846                 mode= eModifierMode_Render|eModifierMode_Realtime;
2847         
2848         for(base= G.scene->base.first; base; base= base->next) {
2849                 if(((level==-1) && (TESTBASE(base))) || (TESTBASELIB(base))) {
2850                         object_flip_subdivison(base->object, &set, level, mode);
2851                         if(base->object->dup_group) {
2852                                 GroupObject *go;
2853                                 for(go= base->object->dup_group->gobject.first; go; go= go->next)
2854                                         object_flip_subdivison(go->ob, &set, level, mode);
2855                         }
2856                 }
2857         }
2858         
2859         countall();
2860         allqueue(REDRAWVIEW3D, 0);
2861         allqueue(REDRAWOOPS, 0);
2862         allqueue(REDRAWBUTSEDIT, 0);
2863         allqueue(REDRAWBUTSOBJECT, 0);
2864         DAG_scene_flush_update(G.scene, screen_view3d_layers());
2865         
2866         BIF_undo_push("Switch subsurf on/off");
2867 }
2868  
2869 static void copymenu_properties(Object *ob)
2870 {       
2871         bProperty *prop, *propn, *propc;
2872         Base *base;
2873         int nr, tot=0;
2874         char *str;
2875         
2876         prop= ob->prop.first;
2877         while(prop) {
2878                 tot++;
2879                 prop= prop->next;
2880         }
2881         
2882         if(tot==0) {
2883                 error("No properties in the active object to copy");
2884                 return;
2885         }
2886         
2887         str= MEM_callocN(24+32*tot, "copymenu prop");
2888         
2889         strcpy(str, "Copy Property %t");
2890         
2891         tot= 0;