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