Nasty Library linking and appending stuff!
[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
779                         base= FIRSTBASE;
780                         while(base) {
781                                 if TESTBASELIB(base) {
782                                         if(base!=BASACT) {
783                                                 if(base->object->type==OB_MESH) {
784                                                         mode= pupmenu("Make Parent %t|Use Bone %x1|Use Armature %x2|Use Object %x3");
785                                                         break;
786                                                 }
787                                                 else {
788                                                         mode= pupmenu("Make Parent %t|Use Bone %x1|Use Object %x3");
789                                                         break;
790                                                 }
791                                         }
792                                 }
793                                 base= base->next;
794                         }
795                 
796                         switch (mode){
797                         case 1:
798                                 mode=PARBONE;
799                                 /* Make bone popup menu */
800
801                                 bonestr = make_bone_menu(get_armature(par));
802                 //              if(mbutton(&bone, bonestr, 1, 24, "Bone: ")==0) {
803
804                                 bonenr= pupmenu_col(bonestr, 20);
805                                 if (bonestr)
806                                         MEM_freeN (bonestr);
807                                 
808                                 if (bonenr==-1){
809                                         allqueue(REDRAWVIEW3D, 0);
810                                         return;
811                                 }
812
813                                 apply_pose_armature(get_armature(par), par->pose, 0);
814                                 bone=get_indexed_bone(get_armature(par), bonenr); 
815                                 if (!bone){
816                 //                      error ("Invalid bone!");
817                                         allqueue(REDRAWVIEW3D, 0);
818                                         return;
819                                 }
820
821                                 break;
822                         case 2:
823                                 mode=PARSKEL;
824                                 break;
825                         case 3:
826                                 mode=PAROBJECT;
827                                 break;
828                         default:
829                                 return;
830                         }
831         }
832                 else {
833                 if(qual & LR_SHIFTKEY) {
834                         if(okee("Make Parent without inverse")==0) return;
835                 }
836                 else {
837                         if(qual & LR_ALTKEY) {
838                                 if(okee("Make VertexParent")==0) return;
839                         }
840                         else if(okee("Make Parent")==0) return;
841
842                         /* test effchild */
843                         base= FIRSTBASE;
844                         while(base) {
845                                 if TESTBASELIB(base) {
846                                         if(base->object->type==OB_IKA && base->object != par) {
847                                                 if(effchild==0) {
848                                                         if(okee("Effector as Child")) effchild= 1;
849                                                         else effchild= 2;
850                                                 }
851                                         }
852                                 }
853                                 if(effchild) break;
854                                 base= base->next;
855                         }
856                         
857                         /* now we'll clearparentandkeeptransform all objects */
858                         base= FIRSTBASE;
859                         while(base) {
860                                 if TESTBASELIB(base) {
861                                         if(base!=BASACT && base->object->parent) {
862                                                 if(base->object->type==OB_IKA && effchild==1);
863                                                 else {
864                                                         base->object->parent= 0;
865                                                         apply_obmat(base->object);
866                                                 }
867                                         }
868                                 }
869                                 base= base->next;
870                         }
871                 }
872         }
873         
874         
875         base= FIRSTBASE;
876         while(base) {
877                 if TESTBASELIB(base) {
878                         if(base!=BASACT) {
879                                 
880                                 ok= 1;
881                                 if(base->object->type==OB_IKA) {
882                                 
883                                         if(effchild==1) {
884                                         
885                                                 if( test_parent_loop(par, base->object)==0 ) {
886                                                 
887                                                         Ika *ika= base->object->data;
888                                                         
889                                                         ika->parent= par;
890                                                         ika->par1= limbnr;
891                                                         ika->partype= mode;
892                                                         itterate_ika(base->object);
893                                                         ok= 0;
894                                                 }
895                                                 else {
896                                                         ok= 0;
897                                                         error("Loop in parents");
898                                                 }
899                                         }
900                                 }
901                                 
902                                 if(ok) {
903                                         if( test_parent_loop(par, base->object) ) {
904                                                 error("Loop in parents");
905                                         }
906                                         else {
907                                                 
908                                                 if(par->type==OB_IKA){
909                                                         base->object->partype= mode;
910                                                         base->object->par1= limbnr;
911                                                 }
912                                                 else if (par->type==OB_ARMATURE){
913                                                         base->object->partype= mode;
914                                                         if (bone)
915                                                                 strcpy (base->object->parsubstr, bone->name);
916                                                         else
917                                                                 base->object->parsubstr[0]=0;
918                                                 }
919                                                 
920                                                 else if(qual & LR_ALTKEY) {
921                                                         base->object->partype= PARVERT1;
922                                                 }
923                                                 else {
924                                                         base->object->partype= PAROBJECT;
925                                                 }
926                                                 
927                                                 base->object->parent= par;
928                                                 
929                                                 /* calculate inverse parent matrix? */
930                                                 if( (qual & LR_SHIFTKEY) ) {
931                                                         /* not... */
932                                                         Mat4One(base->object->parentinv);
933                                                         memset(base->object->loc, 0, 3*sizeof(float));
934                                                 }
935                                                 else {
936                                                         if(mode==PARSKEL && par->type == OB_ARMATURE) {
937                                                                 /* Prompt the user as to whether he wants to
938                                                                  * add some vertex groups based on the bones
939                                                                  * in the parent armature.
940                                                                  */
941                                                                 create_vgroups_from_armature(base->object, 
942                                                                                                                          par);
943
944                                                                 base->object->partype= PAROBJECT;
945                                                                 what_does_parent(base->object);
946                                                                 Mat4One (base->object->parentinv);
947                                                                 base->object->partype= mode;
948                                                         }
949                                                         else
950                                                                 what_does_parent(base->object);
951                                                         Mat4Invert(base->object->parentinv, workob.obmat);
952                                                 }
953                                                 
954                                                 if(par->type==OB_LATTICE) makeDispList(base->object);
955                                                 if(par->type==OB_IKA && mode==PARSKEL) makeDispList(base->object);
956                                                 if(par->type==OB_ARMATURE && mode == PARSKEL){
957                                                         verify_defgroups(base->object);
958                                                         makeDispList(base->object);
959                                                 }
960                                         }
961                                 }
962                         }
963                 }
964                 base= base->next;
965         }
966         allqueue(REDRAWVIEW3D, 0);
967         allqueue(REDRAWOOPS, 0);
968         
969         test_scene_constraints();
970         sort_baselist(G.scene);
971 }
972
973
974 void enter_editmode(void)
975 {
976         Base *base;
977         Object *ob;
978         Ika *ika;
979         ID *id;
980         Mesh *me;
981         int ok= 0;
982         bArmature *arm;
983         
984         if(G.scene->id.lib) return;
985         base= BASACT;
986         if(base==0) return;
987         if((base->lay & G.vd->lay)==0) return;
988         
989         ob= base->object;
990         if(ob->data==0) return;
991         
992         id= ob->data;
993         if(id->lib) {
994                 error("Can't edit libdata");
995                 return;
996         }
997         
998         if(ob->type==OB_MESH) {
999                 me= get_mesh(ob);
1000                 if( me==0 ) return;
1001                 if(me->id.lib) {
1002                         error("Can't edit libdata");
1003                         return;
1004                 }
1005                 ok= 1;
1006                 G.obedit= ob;
1007                 make_editMesh();
1008                 allqueue(REDRAWBUTSLOGIC, 0);
1009         }
1010         if (ob->type==OB_ARMATURE){
1011                 arm=base->object->data;
1012                 if (!arm) return;
1013                 if (arm->id.lib){
1014                         error("Can't edit libdata");
1015                         return;
1016                 }
1017                 ok=1;
1018                 G.obedit=ob;
1019                 make_editArmature();
1020                 allqueue (REDRAWVIEW3D,0);
1021         }
1022         else if(ob->type==OB_IKA) {     /* grab type */
1023                 base= FIRSTBASE;
1024                 while(base) {
1025                         if TESTBASE(base) {
1026                                 if(base->object->type==OB_IKA) {
1027                                         ika= base->object->data;
1028                                         if(ika->flag & IK_GRABEFF) ika->flag &= ~IK_GRABEFF;
1029                                         else ika->flag |= IK_GRABEFF;
1030                                 }
1031                         }
1032                         base= base->next;
1033                 }
1034                 allqueue(REDRAWVIEW3D, 0);
1035         }
1036         else if(ob->type==OB_FONT) {
1037                 G.obedit= ob;
1038                 ok= 1;
1039                 make_editText();
1040         }
1041         else if(ob->type==OB_MBALL) {
1042                 G.obedit= ob;
1043                 ok= 1;
1044                 make_editMball();
1045         }
1046         else if(ob->type==OB_LATTICE) {
1047                 G.obedit= ob;
1048                 ok= 1;
1049                 make_editLatt();
1050         }
1051         else if(ob->type==OB_SURF || ob->type==OB_CURVE) {
1052                 ok= 1;
1053                 G.obedit= ob;
1054                 make_editNurb();
1055         }
1056         allqueue(REDRAWBUTSEDIT, 0);
1057         countall();
1058         
1059         if(ok) {
1060                 setcursor_space(SPACE_VIEW3D, CURSOR_EDIT);
1061         
1062                 allqueue(REDRAWVIEW3D, 0);
1063         }
1064         else G.obedit= 0;
1065
1066         if (G.obpose)
1067                 exit_posemode (1);
1068         scrarea_queue_headredraw(curarea);
1069 }
1070
1071 void make_displists_by_parent(Object *ob) {
1072         Base *base;
1073         
1074         for (base= FIRSTBASE; base; base= base->next)
1075                 if (ob==base->object->parent)
1076                         makeDispList(base->object);
1077 }
1078
1079 void exit_editmode(int freedata)        /* freedata==0 at render */
1080 {
1081         Base *base;
1082         Object *ob;
1083         Curve *cu;
1084
1085         if(G.obedit==0) return;
1086
1087         if(G.obedit->type==OB_MESH) {
1088
1089                 /* temporal */
1090                 countall();
1091
1092                 if(G.totvert>MESH_MAX_VERTS) {
1093                         error("too many vertices");
1094                         return;
1095                 }
1096                 load_editMesh();        /* makes new displist */
1097
1098                 if(freedata) free_editMesh();
1099                 
1100                 if(G.f & G_FACESELECT) allqueue(REDRAWIMAGE, 0);
1101
1102                 build_particle_system(G.obedit);
1103         }
1104         else if (G.obedit->type==OB_ARMATURE){  
1105                 load_editArmature();
1106                 if (freedata) free_editArmature();
1107         }
1108         else if ELEM(G.obedit->type, OB_CURVE, OB_SURF) {
1109                 load_editNurb();
1110                 if(freedata) freeNurblist(&editNurb);
1111         }
1112         else if(G.obedit->type==OB_FONT && freedata==1) {
1113                 load_editText();
1114         }
1115         else if(G.obedit->type==OB_LATTICE) {
1116                 load_editLatt();
1117                 if(freedata) free_editLatt();
1118         }
1119         else if(G.obedit->type==OB_MBALL) {
1120                 load_editMball();
1121                 if(freedata) BLI_freelistN(&editelems);
1122         }
1123
1124         ob= G.obedit;
1125         
1126         /* displist make is different in editmode */
1127         if(freedata) G.obedit= NULL;
1128         makeDispList(ob);
1129
1130         /* has this influence at other objects? */
1131         if(ob->type==OB_CURVE) {
1132
1133                 /* test if ob is use as bevelcurve r textoncurve */
1134                 base= FIRSTBASE;
1135                 while(base) {
1136                         if ELEM(base->object->type, OB_CURVE, OB_FONT) {
1137                                 cu= base->object->data;
1138                                 
1139                                 if(cu->textoncurve==ob) {
1140                                         text_to_curve(base->object, 0);
1141                                         makeDispList(base->object);
1142                                 }
1143                                 if(cu->bevobj== ob) {
1144                                         makeDispList(base->object);
1145                                 }
1146                         }
1147                         base= base->next;
1148                 }
1149                 
1150         }
1151         else if(ob->type==OB_LATTICE) {
1152                 make_displists_by_parent(ob);
1153         }
1154
1155         if(freedata) {
1156                 setcursor_space(SPACE_VIEW3D, CURSOR_STD);
1157         
1158                 countall();
1159                 allqueue(REDRAWVIEW3D, 0);
1160                 allqueue(REDRAWBUTSEDIT, 0);
1161                 allqueue(REDRAWBUTSLOGIC, 0);
1162         }
1163         scrarea_queue_headredraw(curarea);
1164
1165 }
1166
1167 void check_editmode(int type)
1168 {
1169         
1170         if (G.obedit==0 || G.obedit->type==type) return;
1171
1172         exit_editmode(1);
1173 }
1174
1175 static int centremode= 0; /* 0 == do centre, 1 == centre new, 2 == centre cursor */
1176
1177 void docentre(void)
1178 {
1179         EditMesh *em = G.editMesh;
1180         Base *base;
1181         Object *ob;
1182         Mesh *me, *tme;
1183         Curve *cu;
1184 //      BezTriple *bezt;
1185 //      BPoint *bp;
1186         Nurb *nu, *nu1;
1187         EditVert *eve;
1188         float cent[3], centn[3], min[3], max[3], omat[3][3];
1189         int a;
1190         MVert *mvert;
1191
1192         if(G.scene->id.lib) return;
1193
1194         if(G.obedit) {
1195
1196                 INIT_MINMAX(min, max);
1197         
1198                 if(G.obedit->type==OB_MESH) {
1199                         eve= em->verts.first;
1200                         while(eve) {
1201                                 DO_MINMAX(eve->co, min, max);
1202                                 eve= eve->next;
1203                         }
1204                         cent[0]= (min[0]+max[0])/2.0;
1205                         cent[1]= (min[1]+max[1])/2.0;
1206                         cent[2]= (min[2]+max[2])/2.0;
1207                         
1208                         eve= em->verts.first;
1209                         while(eve) {
1210                                 VecSubf(eve->co, eve->co, cent);                        
1211                                 eve= eve->next;
1212                         }
1213                 }
1214         }
1215         
1216         /* reset flags */
1217         base= FIRSTBASE;
1218         while(base) {
1219                 if TESTBASELIB(base) {
1220                         base->object->flag &= ~OB_DONE;
1221                 }
1222                 base= base->next;
1223         }
1224         me= G.main->mesh.first;
1225         while(me) {
1226                 me->flag &= ~ME_ISDONE;
1227                 me= me->id.next;
1228         }
1229         
1230         base= FIRSTBASE;
1231         while(base) {
1232                 
1233                 if TESTBASELIB(base) {
1234                         if((base->object->flag & OB_DONE)==0) {
1235                                 
1236                                 base->object->flag |= OB_DONE;
1237                                 
1238                                 if(G.obedit==0 && (me=get_mesh(base->object)) ) {
1239                                         
1240                                         if(me->key) {
1241                                                 error("Mesh with vertexkey!");
1242                                                 return;
1243                                         }
1244                                         
1245                                         if(centremode==2) {
1246                                                 VECCOPY(cent, give_cursor());
1247                                                 Mat4Invert(base->object->imat, base->object->obmat);
1248                                                 Mat4MulVecfl(base->object->imat, cent);
1249                                         } else {
1250                                                 INIT_MINMAX(min, max);
1251                 
1252                                                 mvert= me->mvert;
1253                                                 for(a=0; a<me->totvert; a++, mvert++) {
1254                                                         DO_MINMAX(mvert->co, min, max);
1255                                                 }
1256                                 
1257                                                 cent[0]= (min[0]+max[0])/2.0;
1258                                                 cent[1]= (min[1]+max[1])/2.0;
1259                                                 cent[2]= (min[2]+max[2])/2.0;
1260                                         }
1261                                                 
1262                                         mvert= me->mvert;
1263                                         for(a=0; a<me->totvert; a++, mvert++) {
1264                                                 VecSubf(mvert->co, mvert->co, cent);
1265                                         }
1266                                         me->flag |= ME_ISDONE;
1267                                         
1268                                         if(centremode) {
1269                                                 Mat3CpyMat4(omat, base->object->obmat);
1270                                                 
1271                                                 VECCOPY(centn, cent);
1272                                                 Mat3MulVecfl(omat, centn);
1273                                                 base->object->loc[0]+= centn[0];
1274                                                 base->object->loc[1]+= centn[1];
1275                                                 base->object->loc[2]+= centn[2];
1276                                                 
1277                                                 /* other users? */
1278                                                 ob= G.main->object.first;
1279                                                 while(ob) {
1280                                                         if((ob->flag & OB_DONE)==0) {
1281                                                                 tme= get_mesh(ob);
1282                                                                 
1283                                                                 if(tme==me) {
1284                                                                         
1285                                                                         ob->flag |= OB_DONE;
1286
1287                                                                         Mat3CpyMat4(omat, ob->obmat);
1288                                                                         VECCOPY(centn, cent);
1289                                                                         Mat3MulVecfl(omat, centn);
1290                                                                         ob->loc[0]+= centn[0];
1291                                                                         ob->loc[1]+= centn[1];
1292                                                                         ob->loc[2]+= centn[2];
1293                                                                         
1294                                                                         if(tme && (tme->flag & ME_ISDONE)==0) {
1295                                                                                 mvert= tme->mvert;
1296                                                                                 for(a=0; a<tme->totvert; a++, mvert++) {
1297                                                                                         VecSubf(mvert->co, mvert->co, cent);
1298                                                                                 }
1299                                                                                 tme->flag |= ME_ISDONE;
1300                                                                         }
1301                                                                 }
1302                                                         }
1303                                                         
1304                                                         ob= ob->id.next;
1305                                                 }
1306                                         }
1307                                 
1308                                         /* displist of all users, also this one */
1309                                         makeDispList(base->object);
1310                                         
1311                                         /* DO: check all users... */
1312                                         tex_space_mesh(me);
1313                 
1314                                 }
1315                                 else if ELEM(base->object->type, OB_CURVE, OB_SURF) {
1316                                                                         
1317                                         if(G.obedit) {
1318                                                 nu1= editNurb.first;
1319                                         }
1320                                         else {
1321                                                 cu= base->object->data;
1322                                                 nu1= cu->nurb.first;
1323                                         }
1324                                         
1325                                         if(centremode==2) {
1326                                                 VECCOPY(cent, give_cursor());
1327                                                 Mat4Invert(base->object->imat, base->object->obmat);
1328                                                 Mat4MulVecfl(base->object->imat, cent);
1329                                                         
1330                                                         /* Curves need to be 2d, never offset in
1331                                                          * Z. Is a somewhat arbitrary restriction, 
1332                                                          * would probably be nice to remove.
1333                                                          */
1334                                                 cent[2]= 0.0;
1335                                         } else {
1336                                                 INIT_MINMAX(min, max);
1337         
1338                                                 nu= nu1;
1339                                                 while(nu) {
1340                                                         minmaxNurb(nu, min, max);
1341                                                         nu= nu->next;
1342                                                 }
1343                                                 
1344                                                 cent[0]= (min[0]+max[0])/2.0;
1345                                                 cent[1]= (min[1]+max[1])/2.0;
1346                                                 cent[2]= (min[2]+max[2])/2.0;
1347                                         }
1348                                         
1349                                         nu= nu1;
1350                                         while(nu) {
1351                                                 if( (nu->type & 7)==1) {
1352                                                         a= nu->pntsu;
1353                                                         while (a--) {
1354                                                                 VecSubf(nu->bezt[a].vec[0], nu->bezt[a].vec[0], cent);
1355                                                                 VecSubf(nu->bezt[a].vec[1], nu->bezt[a].vec[1], cent);
1356                                                                 VecSubf(nu->bezt[a].vec[2], nu->bezt[a].vec[2], cent);
1357                                                         }
1358                                                 }
1359                                                 else {
1360                                                         a= nu->pntsu*nu->pntsv;
1361                                                         while (a--)
1362                                                                 VecSubf(nu->bp[a].vec, nu->bp[a].vec, cent);
1363                                                 }
1364                                                 nu= nu->next;
1365                                         }
1366                         
1367                                         if(centremode && G.obedit==0) {
1368                                                 Mat3CpyMat4(omat, base->object->obmat);
1369                                                 
1370                                                 Mat3MulVecfl(omat, cent);
1371                                                 base->object->loc[0]+= cent[0];
1372                                                 base->object->loc[1]+= cent[1];
1373                                                 base->object->loc[2]+= cent[2];
1374                                         }
1375                         
1376                                         if(G.obedit) {
1377                                                 makeDispList(G.obedit);
1378                                                 break;
1379                                         }
1380                                         else makeDispList(base->object);
1381         
1382                                 }
1383                                 else if(base->object->type==OB_FONT) {
1384                                         /* get from bb */
1385                                         
1386                                         cu= base->object->data;
1387                                         if(cu->bb==0) return;
1388                                         
1389                                         cu->xof= -0.5*( cu->bb->vec[4][0] - cu->bb->vec[0][0]);
1390                                         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 */
1391                                         
1392                                         /* not really ok, do this better once! */
1393                                         cu->xof /= cu->fsize;
1394                                         cu->yof /= cu->fsize;
1395                                         
1396                                         text_to_curve(base->object, 0);
1397                                         makeDispList(base->object);
1398                                         
1399                                         allqueue(REDRAWBUTSEDIT, 0);
1400                                 }
1401                         }
1402                 }
1403                 base= base->next;
1404         }
1405
1406         allqueue(REDRAWVIEW3D, 0);
1407 }
1408
1409 void docentre_new(void)
1410 {
1411         if(G.scene->id.lib) return;
1412
1413         if(G.obedit) {
1414                 error("Unable to perform function in EditMode");
1415         }
1416         else {
1417                 centremode= 1;
1418                 docentre();
1419                 centremode= 0;
1420         }
1421 }
1422
1423 void docentre_cursor(void)
1424 {
1425         if(G.scene->id.lib) return;
1426
1427         if(G.obedit) {
1428                 error("Unable to perform function in EditMode");
1429         }
1430         else {
1431                 centremode= 2;
1432                 docentre();
1433                 centremode= 0;
1434         }
1435 }
1436
1437 void movetolayer(void)
1438 {
1439         Base *base;
1440         unsigned int lay= 0, local;
1441
1442         if(G.scene->id.lib) return;
1443
1444         base= FIRSTBASE;
1445         while(base) {
1446                 if TESTBASE(base) lay |= base->lay;
1447                 base= base->next;
1448         }
1449         if(lay==0) return;
1450         lay &= 0xFFFFFF;
1451         
1452         if( movetolayer_buts(&lay)==0 ) return;
1453         if(lay==0) return;
1454
1455         base= FIRSTBASE;
1456         while(base) {
1457                 if TESTBASE(base) {
1458                         local= base->lay & 0xFF000000;
1459                         base->lay= lay + local;
1460
1461                         base->object->lay= lay;
1462                 }
1463                 base= base->next;
1464         }
1465         countall();
1466         allqueue(REDRAWBUTSEDIT, 0);
1467         allqueue(REDRAWVIEW3D, 0);
1468         allqueue(REDRAWOOPS, 0);
1469         allqueue(REDRAWINFO, 0);
1470 }
1471
1472
1473 void special_editmenu(void)
1474 {
1475         extern short editbutflag;
1476         extern float doublimit;
1477         float fac;
1478         int nr,ret;
1479         short randfac;
1480         
1481         if(G.obedit==0) {
1482                 if(G.f & G_FACESELECT) {
1483                         Mesh *me= get_mesh(OBACT);
1484                         TFace *tface;
1485                         int a;
1486                         
1487                         if(me==0 || me->tface==0) return;
1488                         
1489                         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");
1490         
1491                         for(a=me->totface, tface= me->tface; a>0; a--, tface++) {
1492                                 if(tface->flag & SELECT) {
1493                                         switch(nr) {
1494                                         case 1:
1495                                                 tface->mode |= TF_TEX; break;
1496                                         case 2:
1497                                                 tface->mode |= TF_SHAREDCOL; break;
1498                                         case 3:
1499                                                 tface->mode |= TF_LIGHT; break; 
1500                                         case 4:
1501                                                 tface->mode |= TF_INVISIBLE; break;
1502                                         case 5:
1503                                                 tface->mode |= TF_DYNAMIC; break;
1504                                         case 6:
1505                                                 tface->mode &= ~TF_TEX;
1506                                                 tface->tpage= 0;
1507                                                 break;
1508                                         case 7:
1509                                                 tface->mode &= ~TF_SHAREDCOL; break;
1510                                         case 8:
1511                                                 tface->mode &= ~TF_LIGHT; break;
1512                                         case 9:
1513                                                 tface->mode &= ~TF_INVISIBLE; break;
1514                                         case 10:
1515                                                 tface->mode &= ~TF_DYNAMIC; break;
1516                                         }
1517                                 }
1518                         }
1519                         allqueue(REDRAWVIEW3D, 0);
1520                         allqueue(REDRAWBUTSLOGIC, 0);
1521                 }
1522                 else if(G.f & G_VERTEXPAINT) {
1523                         Mesh *me= get_mesh(OBACT);
1524                         
1525                         if(me==0 || (me->mcol==NULL && me->tface==NULL) ) return;
1526                         
1527                         nr= pupmenu("Specials%t|Shared VertexCol%x1");
1528                         if(nr==1) {
1529                                 
1530                                 if(me->tface) tface_to_mcol(me);
1531                                 
1532                                 copy_vpaint_undo( (unsigned int *)me->mcol, me->totface);
1533                                 do_shared_vertexcol(me);
1534                                 
1535                                 if(me->tface) mcol_to_tface(me, 1);
1536                         }
1537                 }
1538                 else {
1539                         Base *base, *base_select= NULL;
1540
1541                         // Get the active object mesh.
1542                         Mesh *me= get_mesh(OBACT);
1543
1544                         // If the active object is a mesh...
1545                         if (me) {
1546                                 // Bring up a little menu with the boolean operation choices on.
1547                                 nr= pupmenu("Boolean %t|Intersect%x1|Union%x2|Difference%x3");
1548
1549                                 if (nr > 0) {
1550                                         // user has made a choice of a menu element.
1551                                         // All of the boolean functions require 2 mesh objects 
1552                                         // we search through the object list to find the other 
1553                                         // selected item and make sure it is distinct and a mesh.
1554
1555                                         base= FIRSTBASE;
1556                                         while(base) {
1557                                                 if(base->flag & SELECT) {
1558                                                         if(base->object != OBACT) base_select= base;
1559                                                 }
1560
1561                                                 base= base->next;
1562                                         }
1563
1564                                         if (base_select) {
1565                                                 if (get_mesh(base_select->object)) {
1566                                                         waitcursor(1);
1567                                                         ret = NewBooleanMesh(BASACT,base_select,nr);
1568                                                         if (ret==0) {
1569                                                                 error("An internal error occurred -- sorry!");
1570                                                         } else if(ret==-1) {
1571                                                                 error("Boolean ops with faceless meshes is not allowed");
1572                                                         }
1573
1574                                                         waitcursor(0);
1575                                                 } else {
1576                                                         error("Please select 2 meshes");
1577                                                 }
1578                                         } else {
1579                                                 error("Please select 2 meshes");
1580                                         }
1581                                 }
1582
1583                                 allqueue(REDRAWVIEW3D, 0);
1584                         }
1585                 }
1586         }
1587         else if(G.obedit->type==OB_MESH) {
1588
1589                 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");
1590                 if(nr>0) waitcursor(1);
1591                 
1592                 switch(nr) {
1593                 case 1:
1594                         undo_push_mesh("Subdivide");
1595                         subdivideflag(1, 0.0, editbutflag);
1596                         break;
1597                 case 2:
1598                         randfac= 10;
1599                         if(button(&randfac, 1, 100, "Rand fac:")==0) return;
1600                         fac= -( (float)randfac )/100;
1601                         undo_push_mesh("Subdivide Fractal");
1602                         subdivideflag(1, fac, editbutflag);
1603                         break;
1604                 case 3:
1605                         undo_push_mesh("Subdivide Smooth");
1606                         subdivideflag(1, 0.0, editbutflag | B_SMOOTH);
1607                         break;
1608                 case 4:
1609                         mergemenu();
1610                         break;
1611                 case 5:
1612                         undo_push_mesh("Rem Doubles");
1613                         notice("Removed: %d", removedoublesflag(1, doublimit));
1614                         break;
1615                 case 6:
1616                         hide_mesh(0);
1617                         break;
1618                 case 7:
1619                         reveal_mesh();
1620                         break;
1621                 case 8:
1622                         selectswap_mesh();
1623                         break;
1624                 case 9:
1625                         undo_push_mesh("Flip Normals");
1626                         flip_editnormals();
1627                         break;
1628                 case 10:
1629                         undo_push_mesh("Smooth");
1630                         vertexsmooth();
1631                         break;
1632                 case 11:
1633                         bevel_menu();
1634                         break;
1635                 }               
1636                 
1637                 makeDispList(G.obedit);
1638                 
1639                 if(nr>0) waitcursor(0);
1640                 
1641         }
1642         else if ELEM(G.obedit->type, OB_CURVE, OB_SURF) {
1643
1644                 nr= pupmenu("Specials%t|Subdivide%x1|Switch Direction%x2");
1645                 
1646                 switch(nr) {
1647                 case 1:
1648                         subdivideNurb();
1649                         break;
1650                 case 2:
1651                         switchdirectionNurb2();
1652                         break;
1653                 }
1654         }
1655
1656         countall();
1657         allqueue(REDRAWVIEW3D, 0);
1658         
1659 }
1660
1661 void convertmenu(void)
1662 {
1663         Base *base, *basen, *basact;
1664         Object *ob, *ob1;
1665         Curve *cu;
1666         MetaBall *mb;
1667         Mesh *me;
1668         DispList *dl;
1669         int ok=0, nr = 0, a;
1670         
1671         if(G.scene->id.lib) return;
1672
1673         ob= OBACT;
1674         if(ob==0) return;
1675         if(G.obedit) return;
1676         
1677         basact= BASACT; /* will be restored */
1678                 
1679         if(ob->type==OB_FONT) {
1680                 nr= pupmenu("Convert Font to%t|Curve");
1681                 if(nr>0) ok= 1;
1682         }
1683         else if(ob->type==OB_MBALL) {
1684                 nr= pupmenu("Convert MetaBall to%t|Mesh (keep original)");
1685                 if(nr>0) ok= 1;
1686         }
1687         else if(ob->type==OB_CURVE) {
1688                 nr= pupmenu("Convert Curve to%t|Mesh");
1689                 if(nr>0) ok= 1;
1690         }
1691         else if(ob->type==OB_SURF) {
1692                 nr= pupmenu("Convert Nurbs Surf to%t|Mesh");
1693                 if(nr>0) ok= 1;
1694         }
1695         else if(ob->type==OB_MESH && mesh_uses_displist((Mesh*) ob->data)) {
1696                 nr= pupmenu("Convert SubSurf to%t|Mesh (keep original)");
1697                 if(nr>0) ok= 1;
1698         }
1699         if(ok==0) return;
1700
1701         /* don't forget multiple users! */
1702
1703         /* reset flags */
1704         base= FIRSTBASE;
1705         while(base) {
1706                 if TESTBASELIB(base) {
1707                         base->object->flag &= ~OB_DONE;
1708                 }
1709                 base= base->next;
1710         }
1711
1712         base= FIRSTBASE;
1713         while(base) {
1714                 if TESTBASELIB(base) {
1715                         
1716                         ob= base->object;
1717                         
1718                         if(ob->flag & OB_DONE);
1719                         else if(ob->type==OB_MESH) {
1720                                 Mesh *oldme= ob->data;
1721                                 
1722                                 if (mesh_uses_displist(oldme)) {
1723                                         DispListMesh *dlm;
1724
1725                                         ob->flag |= OB_DONE;
1726
1727                                         ob1= copy_object(ob);
1728
1729                                         basen= MEM_mallocN(sizeof(Base), "duplibase");
1730                                         *basen= *base;
1731                                         BLI_addhead(&G.scene->base, basen);     /* addhead: otherwise eternal loop */
1732                                         basen->object= ob1;
1733                                         basen->flag &= ~SELECT;
1734                                                 
1735                                         me= ob1->data;
1736                                         me->id.us--;
1737                                                 
1738                                         ob1->data= add_mesh();
1739                                         G.totmesh++;
1740                                         ob1->type= OB_MESH;
1741
1742                                         me= ob1->data;
1743                                         me->totcol= oldme->totcol;
1744                                         if(ob1->totcol) {
1745                                                 me->mat= MEM_dupallocN(oldme->mat);
1746                                                 for(a=0; a<ob1->totcol; a++) id_us_plus((ID *)me->mat[a]);
1747                                         }
1748                                                 
1749                                         dlm= subsurf_make_dispListMesh_from_mesh(oldme, NULL, oldme->subdiv, oldme->flag);
1750                                         displistmesh_to_mesh(dlm, ob1->data);
1751                                         displistmesh_free(dlm);
1752
1753                                         tex_space_mesh(me);
1754                                 }
1755                         }
1756                         else if(ob->type==OB_FONT) {
1757                                 if(nr==1) {
1758                                 
1759                                         ob->flag |= OB_DONE;
1760                                 
1761                                         ob->type= OB_CURVE;
1762                                         cu= ob->data;
1763                                         
1764                                         if(cu->vfont) {
1765                                                 cu->vfont->id.us--;
1766                                                 cu->vfont= 0;
1767                                         }
1768                                         /* other users */
1769                                         if(cu->id.us>1) {
1770                                                 ob1= G.main->object.first;
1771                                                 while(ob1) {
1772                                                         if(ob1->data==cu) ob1->type= OB_CURVE;
1773                                                         ob1= ob1->id.next;
1774                                                 }
1775                                         }
1776                                 }
1777                         }
1778                         else if ELEM(ob->type, OB_CURVE, OB_SURF) {
1779                                 if(nr==1) {
1780
1781                                         ob->flag |= OB_DONE;
1782                                         cu= ob->data;
1783                                         
1784                                         dl= cu->disp.first;
1785                                         if(dl==0) makeDispList(ob);
1786
1787                                         nurbs_to_mesh(ob); /* also does users */
1788
1789                                         /* texspace and normals */
1790                                         BASACT= base;
1791                                         enter_editmode();
1792                                         exit_editmode(1);
1793                                         BASACT= basact;
1794                                 }
1795                         }
1796                         else if(ob->type==OB_MBALL) {
1797                         
1798                                 if(nr==1) {
1799                                         ob= find_basis_mball(ob);
1800                                         
1801                                         if(ob->disp.first && !(ob->flag&OB_DONE)) {
1802                                         
1803                                                 ob->flag |= OB_DONE;
1804
1805                                                 ob1= copy_object(ob);
1806
1807                                                 basen= MEM_mallocN(sizeof(Base), "duplibase");
1808                                                 *basen= *base;
1809                                                 BLI_addhead(&G.scene->base, basen);     /* addhead: othwise eternal loop */
1810                                                 basen->object= ob1;
1811                                                 basen->flag &= ~SELECT;
1812                                                 
1813                                                 mb= ob1->data;
1814                                                 mb->id.us--;
1815                                                 
1816                                                 ob1->data= add_mesh();
1817                                                 G.totmesh++;
1818                                                 ob1->type= OB_MESH;
1819                                                 
1820                                                 me= ob1->data;
1821                                                 me->totcol= mb->totcol;
1822                                                 if(ob1->totcol) {
1823                                                         me->mat= MEM_dupallocN(mb->mat);
1824                                                         for(a=0; a<ob1->totcol; a++) id_us_plus((ID *)me->mat[a]);
1825                                                 }
1826                                                 
1827                                                 mball_to_mesh(&ob->disp, ob1->data);
1828                                                 tex_space_mesh(me);
1829                                         }
1830                                 }
1831                         }
1832                 }
1833                 base= base->next;
1834         }
1835         
1836         allqueue(REDRAWVIEW3D, 0);
1837         allqueue(REDRAWOOPS, 0);
1838         allqueue(REDRAWBUTSEDIT, 0);
1839 }
1840
1841         /* Change subdivision properties of mesh object ob, if
1842          * level==-1 then toggle subsurf, else set to level.
1843          */
1844 void flip_subdivison(Object *ob, int level)
1845 {
1846         Mesh *me = ob->data;
1847
1848         if (level == -1) {
1849                 me->flag ^= ME_SUBSURF;
1850         } else {
1851                 me->subdiv = level;
1852         }
1853
1854         allqueue(REDRAWVIEW3D, 0);
1855         allqueue(REDRAWOOPS, 0);
1856         allqueue(REDRAWBUTSEDIT, 0);
1857         makeDispList(ob);
1858 }
1859  
1860 void copymenu_properties(Object *ob)
1861 {       
1862         bProperty *prop, *propn, *propc;
1863         Base *base;
1864         int nr, tot=0;
1865         char *str;
1866         
1867         prop= ob->prop.first;
1868         while(prop) {
1869                 tot++;
1870                 prop= prop->next;
1871         }
1872         
1873         if(tot==0) {
1874                 error("No properties in Object");
1875                 return;
1876         }
1877         
1878         str= MEM_callocN(24+32*tot, "copymenu prop");
1879         
1880         strcpy(str, "Copy Property %t");
1881         
1882         tot= 0; 
1883         prop= ob->prop.first;
1884         while(prop) {
1885                 tot++;
1886                 strcat(str, " |");
1887                 strcat(str, prop->name);
1888                 prop= prop->next;
1889         }
1890
1891         nr= pupmenu(str);
1892         if(nr>0) {
1893                 tot= 0;
1894                 prop= ob->prop.first;
1895                 while(prop) {
1896                         tot++;
1897                         if(tot==nr) break;
1898                         prop= prop->next;
1899                 }
1900                 if(prop) {
1901                         propc= prop;
1902                         
1903                         base= FIRSTBASE;
1904                         while(base) {
1905                                 if(base != BASACT) {
1906                                         if(TESTBASELIB(base)) {
1907                                                 prop= get_property(base->object, propc->name);
1908                                                 if(prop) {
1909                                                         free_property(prop);
1910                                                         BLI_remlink(&base->object->prop, prop);
1911                                                 }
1912                                                 propn= copy_property(propc);
1913                                                 BLI_addtail(&base->object->prop, propn);
1914                                         }
1915                                 }
1916                                 base= base->next;
1917                         }
1918                 }
1919         }
1920         MEM_freeN(str);
1921         allqueue(REDRAWVIEW3D, 0);
1922 }
1923
1924 void copymenu_logicbricks(Object *ob)
1925 {
1926         Base *base;
1927         
1928         base= FIRSTBASE;
1929         while(base) {
1930                 if(base->object != ob) {
1931                         if(TESTBASELIB(base)) {
1932                                 
1933                                 /* first: free all logic */
1934                                 free_sensors(&base->object->sensors);                           
1935                                 unlink_controllers(&base->object->controllers);
1936                                 free_controllers(&base->object->controllers);
1937                                 unlink_actuators(&base->object->actuators);
1938                                 free_actuators(&base->object->actuators);
1939                                 
1940                                 /* now copy it, this also works without logicbricks! */
1941                                 clear_sca_new_poins_ob(ob);
1942                                 copy_sensors(&base->object->sensors, &ob->sensors);
1943                                 copy_controllers(&base->object->controllers, &ob->controllers);
1944                                 copy_actuators(&base->object->actuators, &ob->actuators);
1945                                 set_sca_new_poins_ob(base->object);
1946                                 
1947                                 /* some menu settings */
1948                                 base->object->scavisflag= ob->scavisflag;
1949                                 base->object->scaflag= ob->scaflag;
1950                                 
1951                         }
1952                 }
1953                 base= base->next;
1954         }
1955 }
1956
1957 void copy_attr_menu()
1958 {
1959         Object *ob;
1960         short event;
1961         char str[256];
1962
1963         /* If you change this menu, don't forget to update the menu in header_view3d.c
1964          * view3d_edit_object_copyattrmenu() and in toolbox.c
1965          */
1966         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");
1967
1968         if(!(ob=OBACT)) return;
1969         
1970         strcat (str, "|Object Constraints%x22");
1971         
1972         if ELEM5(ob->type, OB_MESH, OB_CURVE, OB_SURF, OB_FONT, OB_MBALL) {
1973                 strcat(str, "|Texture Space%x17");
1974         }       
1975         
1976         if(ob->type == OB_FONT) strcat(str, "|Font Settings%x18|Bevel Settings%x19");
1977         if(ob->type == OB_CURVE) strcat(str, "|Bevel Settings%x19");
1978
1979         if(ob->type==OB_MESH){
1980                 strcat(str, "|Subdiv%x21");
1981         }
1982
1983         if( give_parteff(ob) ) strcat(str, "|Particle Settings%x20");
1984
1985         event= pupmenu(str);
1986         if(event<= 0) return;
1987         
1988         copy_attr(event);
1989 }
1990
1991 void copy_attr(short event)
1992 {
1993         Object *ob, *obt;
1994         Base *base;
1995         Curve *cu, *cu1;
1996         void *poin1, *poin2=0;
1997         
1998         if(G.scene->id.lib) return;
1999
2000         if(!(ob=OBACT)) return;
2001         
2002         if(G.obedit) {
2003                 /* obedit_copymenu(); */
2004                 return;
2005         }
2006         
2007         if ELEM5(ob->type, OB_MESH, OB_CURVE, OB_SURF, OB_FONT, OB_MBALL) {
2008                 if(ob->type==OB_MESH) poin2= &(((Mesh *)ob->data)->texflag);
2009                 else if ELEM3(ob->type, OB_CURVE, OB_SURF, OB_FONT) poin2= &(((Curve *)ob->data)->texflag);
2010                 else if(ob->type==OB_MBALL) poin2= &(((MetaBall *)ob->data)->texflag);
2011         }       
2012
2013         if(event==9) {
2014                 copymenu_properties(ob);
2015                 return;
2016         }
2017         else if(event==10) {
2018                 copymenu_logicbricks(ob);
2019                 return;
2020         }
2021
2022         base= FIRSTBASE;
2023         while(base) {
2024                 if(base != BASACT) {
2025                         if(TESTBASELIB(base)) {
2026                                 
2027                                 if(event==1) {  /* loc */
2028                                         VECCOPY(base->object->loc, ob->loc);
2029                                         VECCOPY(base->object->dloc, ob->dloc);
2030                                 }
2031                                 else if(event==2) {  /* rot */
2032                                         VECCOPY(base->object->rot, ob->rot);
2033                                         VECCOPY(base->object->drot, ob->drot);
2034                                         VECCOPY(base->object->quat, ob->quat);
2035                                         VECCOPY(base->object->dquat, ob->dquat);
2036                                 }
2037                                 else if(event==3) {  /* size */
2038                                         VECCOPY(base->object->size, ob->size);
2039                                         VECCOPY(base->object->dsize, ob->dsize);
2040                                 }
2041                                 else if(event==4) {  /* drawtype */
2042                                         base->object->dt= ob->dt;
2043                                         base->object->dtx= ob->dtx;
2044                                         }
2045                                 else if(event==5) {  /* time offs */
2046                                         base->object->sf= ob->sf;
2047                                 }
2048                                 else if(event==6) {  /* dupli */
2049                                         base->object->dupon= ob->dupon;
2050                                         base->object->dupoff= ob->dupoff;
2051                                         base->object->dupsta= ob->dupsta;
2052                                         base->object->dupend= ob->dupend;
2053                                         
2054                                         base->object->transflag &= ~OB_DUPLI;
2055                                         base->object->transflag |= (ob->transflag & OB_DUPLI);
2056                                 }
2057                                 else if(event==7) {     /* mass */
2058                                         base->object->mass= ob->mass;
2059                                 }
2060                                 else if(event==8) {     /* damping */
2061                                         base->object->damping= ob->damping;
2062                                         base->object->rdamping= ob->rdamping;
2063                                 }
2064                                 else if(event==17) {    /* tex space */
2065                                         obt= base->object;
2066                                         poin1= 0;
2067                                         if(obt->type==OB_MESH) poin1= &(((Mesh *)obt->data)->texflag);
2068                                         else if ELEM3(obt->type, OB_CURVE, OB_SURF, OB_FONT) poin1= &(((Curve *)obt->data)->texflag);
2069                                         else if(obt->type==OB_MBALL) poin1= &(((MetaBall *)obt->data)->texflag);                                        
2070                                         
2071                                         if(poin1) {
2072                                                 memcpy(poin1, poin2, 4+12+12+12);
2073                                         
2074                                                 if(obt->type==OB_MESH) tex_space_mesh(obt->data);
2075                                                 else if(obt->type==OB_MBALL) tex_space_mball(obt);
2076                                                 else tex_space_curve(obt->data);
2077                                         }
2078                                 }
2079                                 else if(event==18) {    /* font settings */
2080                                         
2081                                         if(base->object->type==ob->type) {
2082                                                 cu= ob->data;
2083                                                 cu1= base->object->data;
2084                                                 
2085                                                 cu1->spacemode= cu->spacemode;
2086                                                 cu1->spacing= cu->spacing;
2087                                                 cu1->linedist= cu->linedist;
2088                                                 cu1->shear= cu->shear;
2089                                                 cu1->fsize= cu->fsize;
2090                                                 cu1->xof= cu->xof;
2091                                                 cu1->yof= cu->yof;
2092                                                 cu1->textoncurve= cu->textoncurve;
2093                                                 if(cu1->vfont) cu1->vfont->id.us--;
2094                                                 cu1->vfont= cu->vfont;
2095                                                 id_us_plus((ID *)cu1->vfont);
2096                                                 text_to_curve(base->object, 0);
2097                                                 
2098                                                 strcpy(cu1->family, cu->family);
2099                                                 
2100                                                 makeDispList(base->object);
2101                                         }
2102                                 }
2103                                 else if(event==19) {    /* bevel settings */
2104                                         
2105                                         if ELEM(base->object->type, OB_CURVE, OB_FONT) {
2106                                                 cu= ob->data;
2107                                                 cu1= base->object->data;
2108                                                 
2109                                                 cu1->bevobj= cu->bevobj;
2110                                                 cu1->width= cu->width;
2111                                                 cu1->bevresol= cu->bevresol;
2112                                                 cu1->ext1= cu->ext1;
2113                                                 cu1->ext2= cu->ext2;
2114                                                 
2115                                                 makeDispList(base->object);
2116                                         }
2117                                 }
2118                                 else if(event==20) {    /* particle settings */
2119                                         PartEff *pa1, *pa2;
2120                                         char *p1, *p2;
2121                                         
2122                                         pa1= give_parteff(ob);
2123                                         pa2= give_parteff(base->object);
2124
2125                                         if(pa1==0 && pa2) {
2126                                                 BLI_remlink( &(base->object->effect), pa2);
2127                                                 free_effect( (Effect *) pa2);
2128                                         }
2129                                         else if(pa1 && pa2==0) {
2130                                                 free_effects(&(base->object->effect));
2131                                                 copy_effects(&(base->object->effect), &ob->effect);
2132                                                 build_particle_system(base->object);
2133                                         }
2134                                         else if(pa1 && pa2) {
2135                                                 if(pa2->keys) MEM_freeN(pa2->keys);
2136                                                 
2137                                                 p1= (char *)pa1; p2= (char *)pa2;
2138                                                 memcpy( p2+8, p1+8, sizeof(PartEff) - 8);
2139                                                 pa2->keys= 0;
2140                                                 
2141                                                 build_particle_system(base->object);
2142                                         }
2143                                 }
2144                                 else if(event==21){
2145                                         if (base->object->type==OB_MESH) {
2146                                                 Mesh *targetme= base->object->data;
2147                                                 Mesh *sourceme= ob->data;
2148
2149                                                 targetme->flag= (targetme->flag&~ME_SUBSURF) | (sourceme->flag&ME_SUBSURF);
2150                                                 targetme->subsurftype = sourceme->subsurftype;
2151                                                 targetme->subdiv= sourceme->subdiv;
2152                                                 targetme->subdivr= sourceme->subdivr;
2153                                                 makeDispList(base->object);
2154                                         }
2155                                 }
2156                                 else if(event==22){
2157                                         /* Clear the constraints on the target */
2158                                         free_constraints(&base->object->constraints);
2159                                         free_constraint_channels(&base->object->constraintChannels);
2160
2161                                         /* Copy the constraint channels over */
2162                                         copy_constraints(&base->object->constraints, &ob->constraints);
2163                                         if (U.dupflag& USER_DUP_IPO)
2164                                                 copy_constraint_channels(&base->object->constraintChannels, &ob->constraintChannels);
2165                                         else
2166                                                 clone_constraint_channels (&base->object->constraintChannels, &ob->constraintChannels, NULL);
2167
2168                                         base->object->activecon = NULL;
2169                                 }
2170                         }
2171                 }
2172                 base= base->next;
2173         }
2174         
2175         allqueue(REDRAWVIEW3D, 0);
2176         if(event==20) {
2177                 allqueue(REDRAWBUTSOBJECT, 0);
2178         }
2179         
2180 }
2181
2182 void link_to_scene(unsigned short nr)
2183 {       
2184         Scene *sce= (Scene*) BLI_findlink(&G.main->scene, G.curscreen->scenenr-1);
2185         Base *base, *nbase;
2186         
2187         if(sce==0) return;
2188         if(sce->id.lib) return;
2189         
2190         base= FIRSTBASE;
2191         while(base) {
2192                 if(TESTBASE(base)) {
2193                         
2194                         nbase= MEM_mallocN( sizeof(Base), "newbase");
2195                         *nbase= *base;
2196                         BLI_addhead( &(sce->base), nbase);
2197                         id_us_plus((ID *)base->object);
2198                 }
2199                 base= base->next;
2200         }
2201 }
2202
2203 void make_links_menu()
2204 {
2205         Object *ob;
2206         short event=0;
2207         char str[140];
2208         
2209         if(!(ob=OBACT)) return;
2210         
2211         strcpy(str, "Make Links %t|To Scene...%x1|%l|Object Ipo%x4");
2212         
2213         if(ob->type==OB_MESH)
2214                 strcat(str, "|Mesh Data%x2|Materials%x3");
2215         else if(ob->type==OB_CURVE)
2216                 strcat(str, "|Curve Data%x2|Materials%x3");
2217         else if(ob->type==OB_FONT)
2218                 strcat(str, "|Text Data%x2|Materials%x3");
2219         else if(ob->type==OB_SURF)
2220                 strcat(str, "|Surface Data%x2|Materials%x3");
2221         else if(ob->type==OB_MBALL)
2222                 strcat(str, "|Materials%x3");
2223         else if(ob->type==OB_CAMERA)
2224                 strcat(str, "|Camera Data%x2");
2225         else if(ob->type==OB_LAMP)
2226                 strcat(str, "|Lamp Data%x2");
2227         else if(ob->type==OB_LATTICE)
2228                 strcat(str, "|Lattice Data%x2");
2229         else if(ob->type==OB_ARMATURE)
2230                 strcat(str, "|Armature Data%x2");
2231
2232         event= pupmenu(str);
2233
2234         if(event<= 0) return;
2235         
2236         make_links(event);
2237 }
2238
2239 void make_links(short event)
2240 {
2241         Object *ob, *obt;
2242         Base *base, *nbase, *sbase;
2243         Scene *sce = NULL;
2244         ID *id;
2245         Material ***matarar, ***obmatarar, **matar1, **matar2;
2246         int a;
2247         short *totcolp, nr;
2248         char *strp;
2249
2250         if(!(ob=OBACT)) return;
2251
2252         if(event==1) {
2253                 IDnames_to_pupstring(&strp, NULL, NULL, &(G.main->scene), 0, &nr);
2254                 
2255                 if(strncmp(strp, "DataBrow", 8)==0) {
2256                         MEM_freeN(strp);
2257
2258                         activate_databrowse((ID *)G.scene, ID_SCE, 0, B_INFOSCE, &(G.curscreen->scenenr), link_to_scene );
2259                         
2260                         return;                 
2261                 }
2262                 else {
2263                         event= pupmenu(strp);
2264                         MEM_freeN(strp);
2265                 
2266                         if(event<= 0) return;
2267                 
2268                         nr= 1;
2269                         sce= G.main->scene.first;
2270                         while(sce) {
2271                                 if(nr==event) break;
2272                                 nr++;
2273                                 sce= sce->id.next;
2274                         }
2275                         if(sce==G.scene) {
2276                                 error("This is current scene");
2277                                 return;
2278                         }
2279                         if(sce==0 || sce->id.lib) return;
2280                         
2281                         /* remember: is needed below */
2282                         event= 1;
2283                 }
2284         }
2285
2286         base= FIRSTBASE;
2287         while(base) {
2288                 if(event==1 || base != BASACT) {
2289                         
2290                         obt= base->object;
2291
2292                         if(TESTBASE(base)) {
2293                                 
2294                                 if(event==1) {          /* to scene */
2295                                         
2296                                         /* test if already linked */
2297                                         sbase= sce->base.first;
2298                                         while(sbase) {
2299                                                 if(sbase->object==base->object) break;
2300                                                 sbase= sbase->next;
2301                                         }
2302                                         if(sbase) {     /* remove */
2303                                                 base= base->next;
2304                                                 continue;
2305                                         }
2306                                         
2307                                         nbase= MEM_mallocN( sizeof(Base), "newbase");
2308                                         *nbase= *base;
2309                                         BLI_addhead( &(sce->base), nbase);
2310                                         id_us_plus((ID *)base->object);
2311                                 }
2312                         }
2313                         if(TESTBASELIB(base)) {
2314                                 if(event==2 || event==5) {  /* obdata */
2315                                         if(ob->type==obt->type) {
2316                                                 
2317                                                         id= obt->data;
2318                                                         id->us--;
2319                                                         
2320                                                         id= ob->data;
2321                                                         id_us_plus(id);
2322                                                         obt->data= id;
2323                                                         
2324                                                         /* if amount of material indices changed: */
2325                                                         test_object_materials(obt->data);
2326                                                 }
2327                                         }
2328                                 else if(event==4) {  /* ob ipo */
2329                                         if(obt->ipo) obt->ipo->id.us--;
2330                                         obt->ipo= ob->ipo;
2331                                         if(obt->ipo) {
2332                                                 id_us_plus((ID *)obt->ipo);
2333                                                 do_ob_ipo(obt);
2334                                         }
2335                                 }
2336                                 else if(event==3) {  /* materials */
2337                                         
2338                                         /* only if obt has no material: make arrays */
2339                                         /* from ob to obt! */
2340                                         
2341                                         obmatarar= give_matarar(ob);
2342                                         matarar= give_matarar(obt);
2343                                         totcolp= give_totcolp(obt);
2344
2345                                         /* if one of the two is zero: no render-able object */                                          
2346                                         if( matarar && obmatarar) {
2347                                                 
2348                                                 /* take care of users! so first a copy of original: */
2349
2350                                                 if(ob->totcol) {
2351                                                         matar1= MEM_dupallocN(ob->mat);
2352                                                         matar2= MEM_dupallocN(*obmatarar);
2353                                                 }
2354                                                 else {
2355                                                         matar1= matar2= 0;
2356                                                 }
2357                                                 
2358                                                 /* remove links from obt */
2359                                                 for(a=0; a<obt->totcol; a++) {
2360                                                         if(obt->mat[a]) obt->mat[a]->id.us--;
2361                                                         if( (*matarar)[a]) (*matarar)[a]->id.us--;
2362                                                 }
2363                                                 
2364                                                 /* free */
2365                                                 if(obt->mat) MEM_freeN(obt->mat);
2366                                                 if(*matarar) MEM_freeN(*matarar);
2367                                                 
2368                                                 /* connect a copy */
2369                                                 obt->mat= matar1;
2370                                                 *matarar= matar2;
2371                                                 obt->totcol= ob->totcol;
2372                                                 *totcolp= ob->totcol;
2373                                         
2374                                                 /* increase users */
2375                                                 for(a=0; a<obt->totcol; a++) {
2376                                                         if(obt->mat[a]) id_us_plus((ID *)obt->mat[a]);
2377                                                         if( (*matarar)[a]) id_us_plus((ID *)(*matarar)[a]);
2378                                                 }
2379
2380                                                 obt->colbits= ob->colbits;
2381                                                 
2382                                                 /* if amount of material indices changed: */
2383                                                 test_object_materials(obt->data);
2384                                         }
2385                                 }
2386                         }
2387                 }
2388                 base= base->next;
2389         }
2390         
2391         allqueue(REDRAWVIEW3D, 0);
2392         allqueue(REDRAWOOPS, 0);
2393         allqueue(REDRAWBUTSHEAD, 0);
2394 }
2395
2396 void make_duplilist_real()
2397 {
2398         Base *base, *basen;
2399         Object *ob;
2400         extern ListBase duplilist;
2401         
2402         if(okee("Make dupli's real")==0) return;
2403         
2404         base= FIRSTBASE;
2405         while(base) {
2406                 if TESTBASELIB(base) {
2407
2408                         if(base->object->transflag & OB_DUPLI) {
2409                                 
2410                                 make_duplilist(G.scene, base->object);
2411                                 ob= duplilist.first;
2412                                 while(ob) {
2413                                         
2414                                         /* font duplis can have a totcol without material, we get them from parent
2415                                          * should be implemented better...
2416                                          */
2417                                         if(ob->mat==0) ob->totcol= 0;
2418                                         
2419                                         basen= MEM_dupallocN(base);
2420                                         basen->flag &= ~OB_FROMDUPLI;
2421                                         BLI_addhead(&G.scene->base, basen);     /* addhead: othwise eternal loop */
2422                                         ob->ipo= 0;             /* make sure apply works */
2423                                         ob->parent= ob->track= 0;
2424                                         ob->disp.first= ob->disp.last= 0;
2425                                         ob->transflag &= ~OB_DUPLI;
2426                                         basen->object= copy_object(ob);
2427                                         
2428                                         apply_obmat(basen->object);
2429                                         ob= ob->id.next;
2430                                 }
2431                                 
2432                                 free_duplilist();
2433                                 
2434                                 base->object->transflag &= ~OB_DUPLI;   
2435                         }
2436                 }
2437                 base= base->next;
2438         }
2439         
2440         allqueue(REDRAWVIEW3D, 0);
2441         allqueue(REDRAWOOPS, 0);
2442 }
2443
2444 void apply_object()
2445 {
2446         Base *base, *basact;
2447         Object *ob;
2448         Mesh *me;
2449         Curve *cu;
2450         Nurb *nu;
2451         BPoint *bp;
2452         BezTriple *bezt;
2453         MVert *mvert;
2454         float mat[3][3];
2455         int a;
2456
2457         if(G.scene->id.lib) return;
2458         if(G.obedit) return;
2459         basact= BASACT;
2460         
2461         if(G.qual & LR_SHIFTKEY) {
2462                 ob= OBACT;
2463                 if(ob==0) return;
2464                 
2465                 if(ob->transflag & OB_DUPLI) make_duplilist_real();
2466                 else if(ob->parent && ob->parent->type==OB_LATTICE) apply_lattice();
2467                 
2468                 return;
2469         }
2470
2471         if(okee("Apply size/rot")==0) return;
2472
2473         base= FIRSTBASE;
2474         while(base) {
2475                 if TESTBASELIB(base) {
2476                         ob= base->object;
2477         
2478                         if(ob->type==OB_MESH) {
2479                                 object_to_mat3(ob, mat);
2480                                 me= ob->data;
2481                                 
2482                                 if(me->id.us>1) {
2483                                         error("Can't do multi user mesh");
2484                                         return;
2485                                 }
2486                                 if(me->key) {
2487                                         error("Can't do key && mesh");
2488                                         return;
2489                                 }
2490                                 
2491                                 mvert= me->mvert;
2492                                 for(a=0; a<me->totvert; a++, mvert++) {
2493                                         Mat3MulVecfl(mat, mvert->co);
2494                                 }
2495                                 ob->size[0]= ob->size[1]= ob->size[2]= 1.0;
2496                                 ob->rot[0]= ob->rot[1]= ob->rot[2]= 0.0;
2497                                 QuatOne(ob->quat);
2498                                 
2499                                 where_is_object(ob);
2500                                 
2501                                 /* texspace and normals */
2502                                 BASACT= base;
2503                                 enter_editmode();
2504                                 exit_editmode(1);
2505                                 BASACT= basact;                         
2506                                 
2507                         }
2508                         else if (ob->type==OB_ARMATURE){
2509                                 bArmature *arm;
2510
2511                                 object_to_mat3(ob, mat);
2512                                 arm= ob->data;
2513                                 if(arm->id.us>1) {
2514                                         error("Can't do multi user armature");
2515                                         return;
2516                                 }
2517
2518                                 apply_rot_armature (ob, mat);
2519                                 /* Reset the object's transforms */
2520                                 ob->size[0]= ob->size[1]= ob->size[2]= 1.0;
2521                                 ob->rot[0]= ob->rot[1]= ob->rot[2]= 0.0;
2522                                 QuatOne(ob->quat);
2523                                 
2524                                 where_is_object(ob);
2525                         }
2526                         else if ELEM(ob->type, OB_CURVE, OB_SURF) {
2527                                 object_to_mat3(ob, mat);
2528                                 cu= ob->data;
2529                                 
2530                                 if(cu->id.us>1) {
2531                                         error("Can't do multi user curve");
2532                                         return;
2533                                 }
2534                                 if(cu->key) {
2535                                         error("Can't do keys");
2536                                         return;
2537                                 }
2538                                 
2539                                 nu= cu->nurb.first;
2540                                 while(nu) {
2541                                         if( (nu->type & 7)==1) {
2542                                                 a= nu->pntsu;
2543                                                 bezt= nu->bezt;
2544                                                 while(a--) {
2545                                                         Mat3MulVecfl(mat, bezt->vec[0]);
2546                                                         Mat3MulVecfl(mat, bezt->vec[1]);
2547                                                         Mat3MulVecfl(mat, bezt->vec[2]);
2548                                                         bezt++;
2549                                                 }
2550                                         }
2551                                         else {
2552                                                 a= nu->pntsu*nu->pntsv;
2553                                                 bp= nu->bp;
2554                                                 while(a--) {
2555                                                         Mat3MulVecfl(mat, bp->vec);
2556                                                         bp++;
2557                                                 }
2558                                         }
2559                                         nu= nu->next;
2560                                 }
2561                         
2562                                 ob->size[0]= ob->size[1]= ob->size[2]= 1.0;
2563                                 ob->rot[0]= ob->rot[1]= ob->rot[2]= 0.0;
2564                                 QuatOne(ob->quat);
2565                                 
2566                                 where_is_object(ob);
2567                                 
2568                                 /* texspace and normals */
2569                                 BASACT= base;
2570                                 enter_editmode();
2571                                 exit_editmode(1);
2572                                 BASACT= basact;
2573                         }
2574                 }
2575                 base= base->next;
2576         }
2577         
2578         allqueue(REDRAWVIEW3D, 0);
2579 }
2580
2581
2582
2583 /* ************ GENERAL  *************** */
2584
2585 static Object *is_a_parent_selected_int(Object *startob, Object *ob, GHash *done_hash) {
2586         if (ob!=startob && TESTBASE(ob))
2587                 return ob;
2588                 
2589         if (BLI_ghash_haskey(done_hash, ob))
2590                 return NULL;
2591         else
2592                 BLI_ghash_insert(done_hash, ob, NULL);
2593         
2594         if (ob->parent) {
2595                 Object *par= is_a_parent_selected_int(startob, ob->parent, done_hash);
2596                 if (par)
2597                         return par;
2598         }
2599
2600         /* IK is more complex in parents... */
2601         
2602         /* XXX, should we be handling armatures or constraints here? - zr */
2603
2604         if(ob->type==OB_IKA) {
2605                 Ika *ika= ob->data;
2606                 
2607                 if (ika->def) {
2608                         int i;
2609                         
2610                         for (i=0; i<ika->totdef; i++) {
2611                                 Deform *def= &ika->def[i];
2612                                 
2613                                 if (def->ob && ob!=def->ob && def->ob!=startob) {
2614                                         Object *par= is_a_parent_selected_int(startob, def->ob, done_hash);
2615                                         if (par)
2616                                                 return par;
2617                                 }
2618                         }
2619                 }
2620                 
2621                 if (ika->parent) {
2622                         Object *par= is_a_parent_selected_int(startob, ika->parent, done_hash);
2623                         if (par)
2624                                 return par;
2625                 }
2626         }
2627         
2628         return NULL;
2629 }
2630
2631 static Object *is_a_parent_selected(Object *ob) {
2632         GHash *gh= BLI_ghash_new(BLI_ghashutil_ptrhash, BLI_ghashutil_ptrcmp);
2633         Object *res= is_a_parent_selected_int(ob, ob, gh);
2634         BLI_ghash_free(gh, NULL, NULL);
2635         
2636         return res;
2637 }
2638
2639 /*** POSE FIGURIN' -- START ***/
2640
2641 static void clear_pose_update_flag(Object *ob) {
2642         /* Clear the flag for each pose channel that indicates that
2643          * pose should be updated on every redraw
2644          */
2645         bPoseChannel *chan;
2646         
2647         if (ob->pose) {
2648                 for (chan = ob->pose->chanbase.first; chan; 
2649                          chan=chan->next){
2650                         chan->flag &= ~PCHAN_TRANS_UPDATE;
2651                 }
2652         }
2653 }
2654
2655 static int pose_flags_reset_done(Object *ob) {
2656         /* Clear the constraint done status for every pose channe;
2657          * that has been flagged as needing constant updating
2658          */
2659         bPoseChannel *chan;
2660         int numreset = 0;
2661
2662         if (ob->pose) {
2663                 for (chan = ob->pose->chanbase.first; chan; chan=chan->next){
2664                         if (chan->flag & PCHAN_TRANS_UPDATE) {
2665                                 chan->flag &= ~PCHAN_DONE;
2666                                 numreset++;
2667                         }
2668
2669                 }
2670         }
2671         return numreset;
2672 }
2673
2674 static int is_ob_constraint_target(Object *ob, ListBase *conlist) {
2675         /* Is this object the target of a constraint in this list? 
2676          */
2677
2678         bConstraint *con;
2679
2680         for (con=conlist->first; con; con=con->next)
2681         {
2682                 if (get_con_target(con) == ob)
2683                         return 1;
2684         }
2685         return 0;
2686
2687 }
2688
2689 int clear_bone_nocalc(Object *ob, Bone *bone, void *ptr) {
2690         /* When we aren't transform()-ing, we'll want to turn off
2691          * the no calc flag for bone bone in case the frame changes,
2692          * or something
2693          */
2694         bone->flag &= ~BONE_NOCALC;
2695         
2696         return 0;
2697 }
2698
2699
2700 static void clear_bone_nocalc_ob(Object *ob) {
2701         /* Let's clear no calc for all of the bones in the whole darn armature
2702          */
2703         bArmature *arm;
2704         arm = get_armature(ob);
2705         if (arm) {
2706                 bone_looper(ob, arm->bonebase.first, NULL, 
2707                                         clear_bone_nocalc);
2708         }
2709
2710 }
2711
2712 int set_bone_nocalc(Object *ob, Bone *bone, void *ptr) {
2713         /* Calculating bone transformation makes thins slow ...
2714          * lets set the no calc flag for a bone by default
2715          */
2716         bone->flag |= BONE_NOCALC;
2717         
2718         return 0;
2719 }
2720
2721 int selected_bone_docalc(Object *ob, Bone *bone, void *ptr) {
2722         /* Let's clear the no calc flag for selected bones.
2723          * This function always returns 1 for non-no calc bones
2724          * (a.k.a., the 'do calc' bones) so that the bone_looper 
2725          * will count these
2726          */
2727         if (bone->flag & BONE_NOCALC) {
2728                 if ( (bone->flag & BONE_SELECTED) ) {
2729                         bone->flag &= ~BONE_NOCALC;
2730                         return 1;
2731                 }
2732                 
2733         }
2734         else {
2735                 return 1;
2736         }
2737         return 0;
2738 }
2739
2740 static Bone *get_parent_bone_docalc(Bone *bone) {
2741         Bone            *parBone;
2742
2743         for (parBone = bone->parent; parBone; parBone=parBone->parent)
2744                 if (~parBone->flag & BONE_NOCALC)
2745                         return parBone;
2746
2747         return NULL;
2748 }
2749
2750 static int is_bone_parent(Bone *childBone, Bone *parBone) {
2751         Bone            *currBone;
2752
2753         for (currBone = childBone->parent; currBone; currBone=currBone->parent)
2754                 if (currBone == parBone)
2755                         return 1;
2756
2757         return 0;
2758 }
2759
2760 static void figure_bone_nocalc_constraint(Bone *conbone, bConstraint *con,
2761                                                                                   Object *ob, bArmature *arm) {
2762         /* If this bone has a constraint with a subtarget that has
2763          * the nocalc flag cleared, then we better clear the no calc flag
2764          * on this bone too (and the whole IK chain if this is an IK
2765          * constraint).
2766          *
2767          * Conversly, if this bone has an IK constraint and the root of
2768          * the chain has the no calc flag cleared, we had best clear that
2769          * flag for the whole chain.
2770          */
2771         Bone *subtarbone;
2772         Bone *parBone;
2773         char *subtar;
2774
2775         subtar = get_con_subtarget_name(con, ob);
2776
2777         if (subtar) {
2778                 if ( (subtarbone = get_named_bone(arm, subtar)) ) {
2779                         if ( (~subtarbone->flag & BONE_NOCALC) ||
2780                                  (get_parent_bone_docalc(subtarbone)) ) {
2781                                 if (con->type == CONSTRAINT_TYPE_KINEMATIC)
2782                                         /* IK target is flaged for updating, so we
2783                                          * must update the whole chain.
2784                                          */
2785                                         ik_chain_looper(ob, conbone, NULL, 
2786                                                                         clear_bone_nocalc);
2787                                 else
2788                                         /* Constraint target is flagged for
2789                                          * updating, so we update this bone only
2790                                          */
2791                                         conbone->flag &= ~BONE_NOCALC;
2792                         }
2793                         else {
2794                                 if ( (parBone = get_parent_bone_docalc(conbone)) ) {
2795                                         /* a parent is flagged for updating */
2796                                         if (!is_bone_parent(subtarbone, parBone)) {
2797                                                 /* if the subtarget is also a child of
2798                                                  * this bone, we needn't worry, other
2799                                                  * wise, we have to update
2800                                                  */
2801                                                 if (con->type == CONSTRAINT_TYPE_KINEMATIC)
2802                                                         ik_chain_looper(ob, conbone, NULL, 
2803                                                                                         clear_bone_nocalc);
2804                                                 else
2805                                                         conbone->flag &= ~BONE_NOCALC;
2806                                         }
2807                                 }
2808
2809                         }
2810                 }
2811         }
2812         else {
2813                 /* no subtarget ... target is regular object */
2814                 if ( (parBone = get_parent_bone_docalc(conbone)) ) {
2815                         /* parent is flagged for updating ... since
2816                          * the target will never move (not a bone)
2817                          * we had better update this bone/chain
2818                          */
2819                         if (con->type == CONSTRAINT_TYPE_KINEMATIC)
2820                                 ik_chain_looper(ob, conbone, NULL, 
2821                                                                 clear_bone_nocalc);
2822                         else
2823                                 conbone->flag &= ~BONE_NOCALC;
2824                 }
2825         }
2826
2827 }
2828
2829 static void figure_bone_nocalc_core(Object *ob, bArmature *arm) {
2830         /* Let's figure out which bones need to be recalculated,
2831          * and which don't. Calculations are based on which bones
2832          * are selected, and the constraints that love them.
2833          */
2834         bPoseChannel *chan;
2835         bConstraint *con;
2836         Bone *conbone;
2837
2838         int numbones, oldnumbones, iterations;
2839
2840         oldnumbones = -1;
2841         numbones    =  0;
2842         iterations  =  0;
2843
2844         /* O.K., lets loop until we don't clear any more no calc bones
2845          */
2846         while (oldnumbones != numbones) {
2847                 /* I wonder if this will ever get executed? */
2848                 if ( (++iterations) == 1000) {
2849                         printf("figurin' nocalc is talking too long\n");
2850                         break;
2851                 }
2852
2853                 oldnumbones = numbones;
2854
2855                 /* clear no calc for selected bones and count */
2856                 numbones = bone_looper(ob, arm->bonebase.first, NULL, 
2857                                                            selected_bone_docalc);
2858
2859                 if (ob->pose) {
2860                         for (chan = ob->pose->chanbase.first; chan; chan=chan->next){