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