2.5
[blender-staging.git] / source / blender / editors / object / object_edit.c
1 /**
2  * $Id$
3  *
4  * ***** BEGIN GPL 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.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software Foundation,
18  * Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
19  *
20  * The Original Code is Copyright (C) 2001-2002 by NaN Holding BV.
21  * All rights reserved.
22  *
23  * Contributor(s): Blender Foundation, 2002-2008 full recode
24  *
25  * ***** END GPL LICENSE BLOCK *****
26  */
27 #include <stdlib.h>
28 #include <string.h>
29 #include <math.h>
30 #include <time.h>
31
32 #include "MEM_guardedalloc.h"
33
34 #include "IMB_imbuf_types.h"
35
36 #include "DNA_action_types.h"
37 #include "DNA_armature_types.h"
38 #include "DNA_camera_types.h"
39 #include "DNA_constraint_types.h"
40 #include "DNA_curve_types.h"
41 #include "DNA_effect_types.h"
42 #include "DNA_group_types.h"
43 #include "DNA_image_types.h"
44 #include "DNA_key_types.h"
45 #include "DNA_lamp_types.h"
46 #include "DNA_lattice_types.h"
47 #include "DNA_material_types.h"
48 #include "DNA_mesh_types.h"
49 #include "DNA_meshdata_types.h"
50 #include "DNA_meta_types.h"
51 #include "DNA_nla_types.h"
52 #include "DNA_object_types.h"
53 #include "DNA_object_fluidsim.h"
54 #include "DNA_object_force.h"
55 #include "DNA_scene_types.h"
56 #include "DNA_space_types.h"
57 #include "DNA_screen_types.h"
58 #include "DNA_texture_types.h"
59 #include "DNA_particle_types.h"
60 #include "DNA_property_types.h"
61 #include "DNA_userdef_types.h"
62 #include "DNA_view3d_types.h"
63 #include "DNA_vfont_types.h"
64 #include "DNA_world_types.h"
65 #include "DNA_modifier_types.h"
66
67 #include "BLI_blenlib.h"
68 #include "BLI_arithb.h"
69 #include "BLI_editVert.h"
70 #include "BLI_ghash.h"
71 #include "BLI_rand.h"
72
73 #include "BKE_action.h"
74 #include "BKE_anim.h"
75 #include "BKE_armature.h"
76 #include "BKE_booleanops.h"
77 #include "BKE_constraint.h"
78 #include "BKE_context.h"
79 #include "BKE_customdata.h"
80 #include "BKE_blender.h"
81 #include "BKE_booleanops.h"
82 #include "BKE_cloth.h"
83 #include "BKE_curve.h"
84 #include "BKE_displist.h"
85 #include "BKE_depsgraph.h"
86 #include "BKE_DerivedMesh.h"
87 #include "BKE_effect.h"
88 #include "BKE_font.h"
89 #include "BKE_global.h"
90 #include "BKE_group.h"
91 #include "BKE_image.h"
92 #include "BKE_key.h"
93 #include "BKE_lattice.h"
94 #include "BKE_library.h"
95 #include "BKE_main.h"
96 #include "BKE_material.h"
97 #include "BKE_mball.h"
98 #include "BKE_mesh.h"
99 #include "BKE_nla.h"
100 #include "BKE_object.h"
101 #include "BKE_particle.h"
102 #include "BKE_property.h"
103 #include "BKE_report.h"
104 #include "BKE_sca.h"
105 #include "BKE_scene.h"
106 #include "BKE_softbody.h"
107 #include "BKE_subsurf.h"
108 #include "BKE_texture.h"
109 #include "BKE_utildefines.h"
110 #include "BKE_modifier.h"
111
112 #include "ED_anim_api.h"
113 #include "ED_armature.h"
114 #include "ED_curve.h"
115 #include "ED_particle.h"
116 #include "ED_mesh.h"
117 #include "ED_object.h"
118 #include "ED_screen.h"
119 #include "ED_types.h"
120 #include "ED_util.h"
121 #include "ED_view3d.h"
122
123 #include "BMF_Api.h"
124
125 #include "BIF_transform.h"
126
127 #include "UI_interface.h"
128
129 #include "RNA_access.h"
130 #include "RNA_define.h"
131
132 /* for menu/popup icons etc etc*/
133 #include "UI_interface.h"
134 #include "UI_resources.h"
135
136 #include "WM_api.h"
137 #include "WM_types.h"
138
139 #include "object_intern.h"      // own include
140
141 /* ************* XXX **************** */
142 static void error() {}
143 static void waitcursor() {}
144 static int pupmenu() {return 0;}
145 static int pupmenu_col() {return 0;}
146 static int okee() {return 0;}
147
148 /* port over here */
149 static bContext *C;
150 static void error_libdata() {}
151
152 /* ********************************** */
153
154 /* --------------------------------- */
155
156 /* simple API for object selection, rather than just using the flag
157  * this takes into account the 'restrict selection in 3d view' flag.
158  * deselect works always, the restriction just prevents selection */
159
160 /* Note: send a NC_SCENE|ND_OB_SELECT notifier yourself! */
161
162 void ED_base_object_select(Base *base, short mode)
163 {
164         if (base) {
165                 if (mode==BA_SELECT) {
166                         if (!(base->object->restrictflag & OB_RESTRICT_SELECT))
167                                 if (mode==BA_SELECT) base->flag |= SELECT;
168                 }
169                 else if (mode==BA_DESELECT) {
170                         base->flag &= ~SELECT;
171                 }
172                 base->object->flag= base->flag;
173         }
174 }
175
176 /* also to set active NULL */
177 void ED_base_object_activate(bContext *C, Base *base)
178 {
179         Scene *scene= CTX_data_scene(C);
180         Base *tbase;
181         
182         /* activating a non-mesh, should end a couple of modes... */
183         if(base && base->object->type!=OB_MESH)
184                 ED_view3d_exit_paint_modes(C);
185         
186         /* sets scene->basact */
187         BASACT= base;
188         
189         if(base) {
190                 
191                 /* XXX old signals, remember to handle notifiers now! */
192                 //              select_actionchannel_by_name(base->object->action, "Object", 1);
193                 
194                 /* disable temporal locks */
195                 for(tbase=FIRSTBASE; tbase; tbase= tbase->next) {
196                         if(base!=tbase && (tbase->object->shapeflag & OB_SHAPE_TEMPLOCK)) {
197                                 tbase->object->shapeflag &= ~OB_SHAPE_TEMPLOCK;
198                                 DAG_object_flush_update(scene, tbase->object, OB_RECALC_DATA);
199                         }
200                 }
201                 WM_event_add_notifier(C, NC_SCENE|ND_OB_ACTIVE, scene);
202         }
203         else
204                 WM_event_add_notifier(C, NC_SCENE|ND_OB_ACTIVE, NULL);
205 }
206
207
208 /*
209  * Returns true if the Object data is a from an external blend file (libdata)
210  */
211 int object_data_is_libdata(Object *ob)
212 {
213         if (!ob) return 0;
214         if (ob->proxy) return 0;
215         if (ob->id.lib) return 1;
216         if (!ob->data) return 0;
217         if (((ID *)ob->data)->lib) return 1;
218         return 0;
219 }
220
221
222
223 /* exported */
224 void ED_object_base_init_from_view(bContext *C, Base *base)
225 {
226         View3D *v3d= CTX_wm_view3d(C);
227         Scene *scene= CTX_data_scene(C);
228         Object *ob= base->object;
229         
230         if (scene==NULL)
231                 return;
232         
233         if (v3d==NULL) {
234                 base->lay = scene->lay;
235                 VECCOPY(ob->loc, scene->cursor);
236         } 
237         else {
238                 if (v3d->localview) {
239                         base->lay= ob->lay= v3d->layact | v3d->lay;
240                         VECCOPY(ob->loc, v3d->cursor);
241                 } 
242                 else {
243                         base->lay= ob->lay= v3d->layact;
244                         VECCOPY(ob->loc, scene->cursor);
245                 }
246                 
247                 if (U.flag & USER_ADD_VIEWALIGNED) {
248                         ARegion *ar= CTX_wm_region(C);
249                         if(ar) {
250                                 RegionView3D *rv3d= ar->regiondata;
251                                 
252                                 rv3d->viewquat[0]= -rv3d->viewquat[0];
253                                 QuatToEul(rv3d->viewquat, ob->rot);
254                                 rv3d->viewquat[0]= -rv3d->viewquat[0];
255                         }
256                 }
257         }
258         where_is_object(scene, ob);
259 }
260
261 /* ******************* add object operator ****************** */
262
263 static EnumPropertyItem prop_object_types[] = {
264         {OB_EMPTY, "EMPTY", "Empty", ""},
265         {OB_MESH, "MESH", "Mesh", ""},
266         {OB_CURVE, "CURVE", "Curve", ""},
267         {OB_SURF, "SURFACE", "Surface", ""},
268         {OB_FONT, "TEXT", "Text", ""},
269         {OB_MBALL, "META", "Meta", ""},
270         {OB_LAMP, "LAMP", "Lamp", ""},
271         {OB_CAMERA, "CAMERA", "Camera", ""},
272         {OB_ARMATURE, "ARMATURE", "Armature", ""},
273         {OB_LATTICE, "LATTICE", "Lattice", ""},
274         {0, NULL, NULL, NULL}
275 };
276
277
278
279 void add_object_draw(Scene *scene, View3D *v3d, int type)       /* for toolbox or menus, only non-editmode stuff */
280 {
281         /* keep here to get things compile, remove later */
282 }
283
284 /* for object add primitive operators */
285 static Object *object_add_type(bContext *C, int type)
286 {
287         Scene *scene= CTX_data_scene(C);
288         Object *ob;
289         
290         /* XXX hrms, this is editor level operator, remove? */
291         ED_view3d_exit_paint_modes(C);
292         
293         /* for as long scene has editmode... */
294         if (CTX_data_edit_object(C)) 
295                 ED_object_exit_editmode(C, EM_FREEDATA|EM_FREEUNDO|EM_WAITCURSOR); /* freedata, and undo */
296         
297         /* deselects all, sets scene->basact */
298         ob= add_object(scene, type);
299         /* editor level activate, notifiers */
300         ED_base_object_activate(C, BASACT);
301         
302         /* more editor stuff */
303         ED_object_base_init_from_view(C, BASACT);
304         
305         DAG_scene_sort(scene);
306         
307         return ob;
308 }
309
310 /* for object add operator */
311 static int object_add_exec(bContext *C, wmOperator *op)
312 {
313         object_add_type(C, RNA_int_get(op->ptr, "type"));
314         
315         return OPERATOR_FINISHED;
316 }
317
318 void OBJECT_OT_object_add(wmOperatorType *ot)
319 {
320         /* identifiers */
321         ot->name= "Add Object";
322         ot->idname= "OBJECT_OT_object_add";
323         
324         /* api callbacks */
325         ot->invoke= WM_menu_invoke;
326         ot->exec= object_add_exec;
327         
328         ot->poll= ED_operator_scene_editable;
329         
330         /* flags */
331         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
332         
333         RNA_def_enum(ot->srna, "type", prop_object_types, 0, "Type", "");
334 }
335
336 /* ***************** add primitives *************** */
337 /* ******  work both in and outside editmode ****** */
338
339 static EnumPropertyItem prop_mesh_types[] = {
340         {0, "PLANE", "Plane", ""},
341         {1, "CUBE", "Cube", ""},
342         {2, "CIRCLE", "Circle", ""},
343         {3, "UVSPHERE", "UVsphere", ""},
344         {4, "ICOSPHERE", "Icosphere", ""},
345         {5, "CYLINDER", "Cylinder", ""},
346         {6, "CONE", "Cone", ""},
347         {7, "GRID", "Grid", ""},
348         {8, "MONKEY", "Monkey", ""},
349         {0, NULL, NULL, NULL}
350 };
351
352 static int object_add_mesh_exec(bContext *C, wmOperator *op)
353 {
354         Object *obedit= CTX_data_edit_object(C);
355         int newob= 0;
356         
357         if(obedit==NULL || obedit->type!=OB_MESH) {
358                 object_add_type(C, OB_MESH);
359                 ED_object_enter_editmode(C, 0);
360                 newob = 1;
361         }
362         else DAG_object_flush_update(CTX_data_scene(C), obedit, OB_RECALC_DATA);
363
364         switch(RNA_enum_get(op->ptr, "type")) {
365                 case 0:
366                         WM_operator_name_call(C, "MESH_OT_add_primitive_plane", WM_OP_INVOKE_REGION_WIN, NULL);
367                         break;
368                 case 1:
369                         WM_operator_name_call(C, "MESH_OT_add_primitive_cube", WM_OP_INVOKE_REGION_WIN, NULL);
370                         break;
371                 case 2:
372                         WM_operator_name_call(C, "MESH_OT_add_primitive_circle", WM_OP_INVOKE_REGION_WIN, NULL);
373                         break;
374                 case 3:
375                         WM_operator_name_call(C, "MESH_OT_add_primitive_uv_sphere", WM_OP_INVOKE_REGION_WIN, NULL);
376                         break;
377                 case 4:
378                         WM_operator_name_call(C, "MESH_OT_add_primitive_ico_sphere", WM_OP_INVOKE_REGION_WIN, NULL);
379                         break;
380                 case 5:
381                         WM_operator_name_call(C, "MESH_OT_add_primitive_cylinder", WM_OP_INVOKE_REGION_WIN, NULL);
382                         break;
383                 case 6:
384                         WM_operator_name_call(C, "MESH_OT_add_primitive_cone", WM_OP_INVOKE_REGION_WIN, NULL);
385                         break;
386                 case 7:
387                         WM_operator_name_call(C, "MESH_OT_add_primitive_grid", WM_OP_INVOKE_REGION_WIN, NULL);
388                         break;
389                 case 8:
390                         WM_operator_name_call(C, "MESH_OT_add_primitive_monkey", WM_OP_INVOKE_REGION_WIN, NULL);
391                         break;
392         }
393         /* userdef */
394         if (newob && (U.flag & USER_ADD_EDITMODE)==0) {
395                 ED_object_exit_editmode(C, EM_FREEDATA);
396         }
397         
398         WM_event_add_notifier(C, NC_OBJECT|ND_GEOM_SELECT, obedit);
399         
400         return OPERATOR_FINISHED;
401 }
402
403
404 void OBJECT_OT_mesh_add(wmOperatorType *ot)
405 {
406         /* identifiers */
407         ot->name= "Add Mesh";
408         ot->idname= "OBJECT_OT_mesh_add";
409         
410         /* api callbacks */
411         ot->invoke= WM_menu_invoke;
412         ot->exec= object_add_mesh_exec;
413         
414         ot->poll= ED_operator_scene_editable;
415         
416         /* flags */
417         ot->flag= 0;
418         
419         RNA_def_enum(ot->srna, "type", prop_mesh_types, 0, "Primitive", "");
420 }
421
422 static EnumPropertyItem prop_curve_types[] = {
423         {CU_BEZIER|CU_2D|CU_PRIM_CURVE, "BEZIER_CURVE", "Bezier Curve", ""},
424         {CU_BEZIER|CU_2D|CU_PRIM_CIRCLE, "BEZIER_CIRCLE", "Bezier Circle", ""},
425         {CU_NURBS|CU_2D|CU_PRIM_CURVE, "NURBS_CURVE", "NURBS Curve", ""},
426         {CU_NURBS|CU_2D|CU_PRIM_CIRCLE, "NURBS_CIRCLE", "NURBS Circle", ""},
427         {CU_NURBS|CU_2D|CU_PRIM_PATH, "PATH", "Path", ""},
428         {0, NULL, NULL, NULL}
429 };
430
431 static int object_add_curve_exec(bContext *C, wmOperator *op)
432 {
433         Object *obedit= CTX_data_edit_object(C);
434         ListBase *editnurb;
435         Nurb *nu;
436         int newob= 0;
437         
438         if(obedit==NULL || obedit->type!=OB_CURVE) {
439                 object_add_type(C, OB_CURVE);
440                 ED_object_enter_editmode(C, 0);
441                 newob = 1;
442         }
443         else DAG_object_flush_update(CTX_data_scene(C), obedit, OB_RECALC_DATA);
444         
445         obedit= CTX_data_edit_object(C);
446         nu= add_nurbs_primitive(C, RNA_enum_get(op->ptr, "type"), newob);
447         editnurb= curve_get_editcurve(obedit);
448         BLI_addtail(editnurb, nu);
449         
450         /* userdef */
451         if (newob && (U.flag & USER_ADD_EDITMODE)==0) {
452                 ED_object_exit_editmode(C, EM_FREEDATA);
453         }
454         
455         WM_event_add_notifier(C, NC_OBJECT|ND_GEOM_SELECT, obedit);
456         
457         return OPERATOR_FINISHED;
458 }
459
460 static int object_add_curve_invoke(bContext *C, wmOperator *op, wmEvent *event)
461 {
462         Object *obedit= CTX_data_edit_object(C);
463         uiMenuItem *head;
464
465         head= uiPupMenuBegin(op->type->name, 0);
466         if(!obedit || obedit->type == OB_CURVE)
467                 uiMenuItemsEnumO(head, op->type->idname, "type");
468         else
469                 uiMenuItemsEnumO(head, "OBJECT_OT_surface_add", "type");
470         uiPupMenuEnd(C, head);
471
472         return OPERATOR_CANCELLED;
473 }
474
475 void OBJECT_OT_curve_add(wmOperatorType *ot)
476 {
477         /* identifiers */
478         ot->name= "Add Curve";
479         ot->idname= "OBJECT_OT_curve_add";
480         
481         /* api callbacks */
482         ot->invoke= object_add_curve_invoke;
483         ot->exec= object_add_curve_exec;
484         
485         ot->poll= ED_operator_scene_editable;
486         
487         /* flags */
488         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
489         
490         RNA_def_enum(ot->srna, "type", prop_curve_types, 0, "Primitive", "");
491 }
492
493 static EnumPropertyItem prop_surface_types[]= {
494         {CU_PRIM_CURVE|CU_NURBS, "NURBS_CURVE", "NURBS Curve", ""},
495         {CU_PRIM_CIRCLE|CU_NURBS, "NURBS_CIRCLE", "NURBS Circle", ""},
496         {CU_PRIM_PATCH|CU_NURBS, "NURBS_SURFACE", "NURBS Surface", ""},
497         {CU_PRIM_TUBE|CU_NURBS, "NURBS_TUBE", "NURBS Tube", ""},
498         {CU_PRIM_SPHERE|CU_NURBS, "NURBS_SPHERE", "NURBS Sphere", ""},
499         {CU_PRIM_DONUT|CU_NURBS, "NURBS_DONUT", "NURBS Donut", ""},
500         {0, NULL, NULL, NULL}
501 };
502
503 static int object_add_surface_exec(bContext *C, wmOperator *op)
504 {
505         Object *obedit= CTX_data_edit_object(C);
506         ListBase *editnurb;
507         Nurb *nu;
508         int newob= 0;
509         
510         if(obedit==NULL || obedit->type!=OB_SURF) {
511                 object_add_type(C, OB_SURF);
512                 ED_object_enter_editmode(C, 0);
513                 newob = 1;
514         }
515         else DAG_object_flush_update(CTX_data_scene(C), obedit, OB_RECALC_DATA);
516         
517         obedit= CTX_data_edit_object(C);
518         nu= add_nurbs_primitive(C, RNA_enum_get(op->ptr, "type"), newob);
519         editnurb= curve_get_editcurve(obedit);
520         BLI_addtail(editnurb, nu);
521         
522         /* userdef */
523         if (newob && (U.flag & USER_ADD_EDITMODE)==0) {
524                 ED_object_exit_editmode(C, EM_FREEDATA);
525         }
526         
527         WM_event_add_notifier(C, NC_OBJECT|ND_GEOM_SELECT, obedit);
528         
529         return OPERATOR_FINISHED;
530 }
531
532 void OBJECT_OT_surface_add(wmOperatorType *ot)
533 {
534         /* identifiers */
535         ot->name= "Add Surface";
536         ot->idname= "OBJECT_OT_surface_add";
537         
538         /* api callbacks */
539         ot->invoke= WM_menu_invoke;
540         ot->exec= object_add_surface_exec;
541         
542         ot->poll= ED_operator_scene_editable;
543         
544         /* flags */
545         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
546         
547         RNA_def_enum(ot->srna, "type", prop_surface_types, 0, "Primitive", "");
548 }
549
550 static int object_add_text_exec(bContext *C, wmOperator *op)
551 {
552         Object *obedit= CTX_data_edit_object(C);
553         
554         if(obedit && obedit->type==OB_FONT)
555                 return OPERATOR_CANCELLED;
556
557         object_add_type(C, OB_FONT);
558         obedit= CTX_data_active_object(C);
559
560         if(U.flag & USER_ADD_EDITMODE)
561                 ED_object_enter_editmode(C, 0);
562         
563         WM_event_add_notifier(C, NC_OBJECT|ND_GEOM_SELECT, obedit);
564         
565         return OPERATOR_FINISHED;
566 }
567
568 void OBJECT_OT_text_add(wmOperatorType *ot)
569 {
570         /* identifiers */
571         ot->name= "Add Text";
572         ot->idname= "OBJECT_OT_text_add";
573         
574         /* api callbacks */
575         ot->exec= object_add_text_exec;
576         ot->poll= ED_operator_scene_editable;
577         
578         /* flags */
579         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
580 }
581
582 static int object_add_armature_exec(bContext *C, wmOperator *op)
583 {
584         Object *obedit= CTX_data_edit_object(C);
585         View3D *v3d= CTX_wm_view3d(C);
586         RegionView3D *rv3d= NULL;
587         int newob= 0;
588         
589         if ((obedit==NULL) || (obedit->type != OB_ARMATURE)) {
590                 object_add_type(C, OB_ARMATURE);
591                 ED_object_enter_editmode(C, 0);
592                 newob = 1;
593         }
594         else DAG_object_flush_update(CTX_data_scene(C), obedit, OB_RECALC_DATA);
595         
596         if(v3d) 
597                 rv3d= CTX_wm_region(C)->regiondata;
598         
599         /* v3d and rv3d are allowed to be NULL */
600         add_primitive_bone(CTX_data_scene(C), v3d, rv3d);
601
602         /* userdef */
603         if (newob && (U.flag & USER_ADD_EDITMODE)==0) {
604                 ED_object_exit_editmode(C, EM_FREEDATA);
605         }
606         
607         WM_event_add_notifier(C, NC_OBJECT|ND_GEOM_SELECT, obedit);
608         
609         return OPERATOR_FINISHED;
610 }
611
612 void OBJECT_OT_armature_add(wmOperatorType *ot)
613 {       
614         /* identifiers */
615         ot->name= "Add Armature";
616         ot->idname= "OBJECT_OT_armature_add";
617         
618         /* api callbacks */
619         ot->exec= object_add_armature_exec;
620         ot->poll= ED_operator_scene_editable;
621         
622         /* flags */
623         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
624 }
625
626
627 static int object_add_primitive_invoke(bContext *C, wmOperator *op, wmEvent *event)
628 {
629         uiMenuItem *head= uiPupMenuBegin("Add Object", 0);
630         
631         uiMenuLevelEnumO(head, "OBJECT_OT_mesh_add", "type");
632         uiMenuLevelEnumO(head, "OBJECT_OT_curve_add", "type");
633         uiMenuLevelEnumO(head, "OBJECT_OT_surface_add", "type");
634         uiMenuItemO(head, 0, "OBJECT_OT_text_add");
635         uiMenuItemEnumO(head, "", 0, "OBJECT_OT_object_add", "type", OB_MBALL);
636         uiMenuItemEnumO(head, "", 0, "OBJECT_OT_object_add", "type", OB_CAMERA);
637         uiMenuItemEnumO(head, "", 0, "OBJECT_OT_object_add", "type", OB_LAMP);
638         uiMenuItemEnumO(head, "", 0, "OBJECT_OT_object_add", "type", OB_EMPTY);
639         uiMenuItemO(head, 0, "OBJECT_OT_armature_add");
640         uiMenuItemEnumO(head, "", 0, "OBJECT_OT_object_add", "type", OB_LATTICE);
641         
642         uiPupMenuEnd(C, head);
643         
644         /* this operator is only for a menu, not used further */
645         return OPERATOR_CANCELLED;
646 }
647
648 /* only used as menu */
649 void OBJECT_OT_primitive_add(wmOperatorType *ot)
650 {
651         /* identifiers */
652         ot->name= "Add Primitive";
653         ot->idname= "OBJECT_OT_primitive_add";
654         
655         /* api callbacks */
656         ot->invoke= object_add_primitive_invoke;
657         
658         ot->poll= ED_operator_scene_editable;
659         
660         /* flags */
661         ot->flag= 0;
662 }
663
664
665 /* ******************************* */
666
667 /* remove base from a specific scene */
668 /* note: now unlinks constraints as well */
669 void ED_base_object_free_and_unlink(Scene *scene, Base *base)
670 {
671         BLI_remlink(&scene->base, base);
672         free_libblock_us(&G.main->object, base->object);
673         if(scene->basact==base) scene->basact= NULL;
674         MEM_freeN(base);
675 }
676
677 static int object_delete_exec(bContext *C, wmOperator *op)
678 {
679         Scene *scene= CTX_data_scene(C);
680         int islamp= 0;
681         
682         if(CTX_data_edit_object(C)) 
683                 return OPERATOR_CANCELLED;
684         
685         ED_view3d_exit_paint_modes(C);
686
687         CTX_DATA_BEGIN(C, Base*, base, selected_editable_bases) {
688
689                 if(base->object->type==OB_LAMP) islamp= 1;
690                 
691                 /* remove from current scene only */
692                 ED_base_object_free_and_unlink(scene, base);
693         }
694         CTX_DATA_END;
695
696         if(islamp) reshadeall_displist(scene);  /* only frees displist */
697         
698         DAG_scene_sort(scene);
699         ED_anim_dag_flush_update(C);
700         
701         WM_event_add_notifier(C, NC_SCENE|ND_OB_ACTIVE, CTX_data_scene(C));
702         
703         return OPERATOR_FINISHED;
704 }
705
706 void OBJECT_OT_delete(wmOperatorType *ot)
707 {
708         
709         /* identifiers */
710         ot->name= "Delete Objects";
711         ot->idname= "OBJECT_OT_delete";
712         
713         /* api callbacks */
714         ot->invoke= WM_operator_confirm;
715         ot->exec= object_delete_exec;
716         ot->poll= ED_operator_scene_editable;
717         
718         /* flags */
719         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
720         
721 }
722
723
724 static void single_object_users__forwardModifierLinks(void *userData, Object *ob, Object **obpoin)
725 {
726         ID_NEW(*obpoin);
727 }
728
729 static void copy_object__forwardModifierLinks(void *userData, Object *ob,
730                                               ID **idpoin)
731 {
732         /* this is copied from ID_NEW; it might be better to have a macro */
733         if(*idpoin && (*idpoin)->newid) *idpoin = (*idpoin)->newid;
734 }
735
736
737 /* after copying objects, copied data should get new pointers */
738 static void copy_object_set_idnew(Scene *scene, View3D *v3d, int dupflag)
739 {
740         Base *base;
741         Object *ob;
742         Material *ma, *mao;
743         ID *id;
744 #if 0 // XXX old animation system
745         Ipo *ipo;
746         bActionStrip *strip;
747 #endif // XXX old animation system
748         int a;
749         
750         /* XXX check object pointers */
751         for(base= FIRSTBASE; base; base= base->next) {
752                 if(TESTBASELIB(v3d, base)) {
753                         ob= base->object;
754                         relink_constraints(&ob->constraints);
755                         if (ob->pose){
756                                 bPoseChannel *chan;
757                                 for (chan = ob->pose->chanbase.first; chan; chan=chan->next){
758                                         relink_constraints(&chan->constraints);
759                                 }
760                         }
761                         modifiers_foreachIDLink(ob, copy_object__forwardModifierLinks, NULL);
762                         ID_NEW(ob->parent);
763                         ID_NEW(ob->track);
764                         ID_NEW(ob->proxy);
765                         ID_NEW(ob->proxy_group);
766                         
767 #if 0 // XXX old animation system
768                         for(strip= ob->nlastrips.first; strip; strip= strip->next) {
769                                 bActionModifier *amod;
770                                 for(amod= strip->modifiers.first; amod; amod= amod->next)
771                                         ID_NEW(amod->ob);
772                         }
773 #endif // XXX old animation system
774                 }
775         }
776         
777         /* materials */
778         if( dupflag & USER_DUP_MAT) {
779                 mao= G.main->mat.first;
780                 while(mao) {
781                         if(mao->id.newid) {
782                                 
783                                 ma= (Material *)mao->id.newid;
784                                 
785                                 if(dupflag & USER_DUP_TEX) {
786                                         for(a=0; a<MAX_MTEX; a++) {
787                                                 if(ma->mtex[a]) {
788                                                         id= (ID *)ma->mtex[a]->tex;
789                                                         if(id) {
790                                                                 ID_NEW_US(ma->mtex[a]->tex)
791                                                                 else ma->mtex[a]->tex= copy_texture(ma->mtex[a]->tex);
792                                                                 id->us--;
793                                                         }
794                                                 }
795                                         }
796                                 }
797 #if 0 // XXX old animation system
798                                 id= (ID *)ma->ipo;
799                                 if(id) {
800                                         ID_NEW_US(ma->ipo)
801                                         else ma->ipo= copy_ipo(ma->ipo);
802                                         id->us--;
803                                 }
804 #endif // XXX old animation system
805                         }
806                         mao= mao->id.next;
807                 }
808         }
809         
810 #if 0 // XXX old animation system
811         /* lamps */
812         if( dupflag & USER_DUP_IPO) {
813                 Lamp *la= G.main->lamp.first;
814                 while(la) {
815                         if(la->id.newid) {
816                                 Lamp *lan= (Lamp *)la->id.newid;
817                                 id= (ID *)lan->ipo;
818                                 if(id) {
819                                         ID_NEW_US(lan->ipo)
820                                         else lan->ipo= copy_ipo(lan->ipo);
821                                         id->us--;
822                                 }
823                         }
824                         la= la->id.next;
825                 }
826         }
827         
828         /* ipos */
829         ipo= G.main->ipo.first;
830         while(ipo) {
831                 if(ipo->id.lib==NULL && ipo->id.newid) {
832                         Ipo *ipon= (Ipo *)ipo->id.newid;
833                         IpoCurve *icu;
834                         for(icu= ipon->curve.first; icu; icu= icu->next) {
835                                 if(icu->driver) {
836                                         ID_NEW(icu->driver->ob);
837                                 }
838                         }
839                 }
840                 ipo= ipo->id.next;
841         }
842 #endif // XXX old animation system
843         
844         set_sca_new_poins();
845         
846         clear_id_newpoins();
847         
848 }
849
850 static int return_editmesh_indexar(EditMesh *em, int *tot, int **indexar, float *cent)
851 {
852         EditVert *eve;
853         int *index, nr, totvert=0;
854         
855         for(eve= em->verts.first; eve; eve= eve->next) {
856                 if(eve->f & SELECT) totvert++;
857         }
858         if(totvert==0) return 0;
859         
860         *indexar= index= MEM_mallocN(4*totvert, "hook indexar");
861         *tot= totvert;
862         nr= 0;
863         cent[0]= cent[1]= cent[2]= 0.0;
864         
865         for(eve= em->verts.first; eve; eve= eve->next) {
866                 if(eve->f & SELECT) {
867                         *index= nr; index++;
868                         VecAddf(cent, cent, eve->co);
869                 }
870                 nr++;
871         }
872         
873         VecMulf(cent, 1.0f/(float)totvert);
874         
875         return totvert;
876 }
877
878 static int return_editmesh_vgroup(Object *obedit, EditMesh *em, char *name, float *cent)
879 {
880         MDeformVert *dvert;
881         EditVert *eve;
882         int i, totvert=0;
883         
884         cent[0]= cent[1]= cent[2]= 0.0;
885         
886         if(obedit->actdef) {
887                 
888                 /* find the vertices */
889                 for(eve= em->verts.first; eve; eve= eve->next) {
890                         dvert= CustomData_em_get(&em->vdata, eve->data, CD_MDEFORMVERT);
891
892                         if(dvert) {
893                                 for(i=0; i<dvert->totweight; i++){
894                                         if(dvert->dw[i].def_nr == (obedit->actdef-1)) {
895                                                 totvert++;
896                                                 VecAddf(cent, cent, eve->co);
897                                         }
898                                 }
899                         }
900                 }
901                 if(totvert) {
902                         bDeformGroup *defGroup = BLI_findlink(&obedit->defbase, obedit->actdef-1);
903                         strcpy(name, defGroup->name);
904                         VecMulf(cent, 1.0f/(float)totvert);
905                         return 1;
906                 }
907         }
908         
909         return 0;
910 }       
911
912 static void select_editmesh_hook(Object *ob, HookModifierData *hmd)
913 {
914         Mesh *me= ob->data;
915         EditMesh *em= me->edit_mesh;
916         EditVert *eve;
917         int index=0, nr=0;
918         
919         for(eve= em->verts.first; eve; eve= eve->next, nr++) {
920                 if(nr==hmd->indexar[index]) {
921                         eve->f |= SELECT;
922                         if(index < hmd->totindex-1) index++;
923                 }
924         }
925         EM_select_flush(em);
926 }
927
928 static int return_editlattice_indexar(Lattice *editlatt, int *tot, int **indexar, float *cent)
929 {
930         BPoint *bp;
931         int *index, nr, totvert=0, a;
932         
933         /* count */
934         a= editlatt->pntsu*editlatt->pntsv*editlatt->pntsw;
935         bp= editlatt->def;
936         while(a--) {
937                 if(bp->f1 & SELECT) {
938                         if(bp->hide==0) totvert++;
939                 }
940                 bp++;
941         }
942
943         if(totvert==0) return 0;
944         
945         *indexar= index= MEM_mallocN(4*totvert, "hook indexar");
946         *tot= totvert;
947         nr= 0;
948         cent[0]= cent[1]= cent[2]= 0.0;
949         
950         a= editlatt->pntsu*editlatt->pntsv*editlatt->pntsw;
951         bp= editlatt->def;
952         while(a--) {
953                 if(bp->f1 & SELECT) {
954                         if(bp->hide==0) {
955                                 *index= nr; index++;
956                                 VecAddf(cent, cent, bp->vec);
957                         }
958                 }
959                 bp++;
960                 nr++;
961         }
962         
963         VecMulf(cent, 1.0f/(float)totvert);
964         
965         return totvert;
966 }
967
968 static void select_editlattice_hook(Object *obedit, HookModifierData *hmd)
969 {
970         Lattice *lt= obedit->data;
971         BPoint *bp;
972         int index=0, nr=0, a;
973         
974         /* count */
975         a= lt->editlatt->pntsu*lt->editlatt->pntsv*lt->editlatt->pntsw;
976         bp= lt->editlatt->def;
977         while(a--) {
978                 if(hmd->indexar[index]==nr) {
979                         bp->f1 |= SELECT;
980                         if(index < hmd->totindex-1) index++;
981                 }
982                 nr++;
983                 bp++;
984         }
985 }
986
987 static int return_editcurve_indexar(Object *obedit, int *tot, int **indexar, float *cent)
988 {
989         ListBase *editnurb= curve_get_editcurve(obedit);
990         Nurb *nu;
991         BPoint *bp;
992         BezTriple *bezt;
993         int *index, a, nr, totvert=0;
994         
995         for(nu= editnurb->first; nu; nu= nu->next) {
996                 if((nu->type & 7)==CU_BEZIER) {
997                         bezt= nu->bezt;
998                         a= nu->pntsu;
999                         while(a--) {
1000                                 if(bezt->f1 & SELECT) totvert++;
1001                                 if(bezt->f2 & SELECT) totvert++;
1002                                 if(bezt->f3 & SELECT) totvert++;
1003                                 bezt++;
1004                         }
1005                 }
1006                 else {
1007                         bp= nu->bp;
1008                         a= nu->pntsu*nu->pntsv;
1009                         while(a--) {
1010                                 if(bp->f1 & SELECT) totvert++;
1011                                 bp++;
1012                         }
1013                 }
1014         }
1015         if(totvert==0) return 0;
1016         
1017         *indexar= index= MEM_mallocN(4*totvert, "hook indexar");
1018         *tot= totvert;
1019         nr= 0;
1020         cent[0]= cent[1]= cent[2]= 0.0;
1021         
1022         for(nu= editnurb->first; nu; nu= nu->next) {
1023                 if((nu->type & 7)==CU_BEZIER) {
1024                         bezt= nu->bezt;
1025                         a= nu->pntsu;
1026                         while(a--) {
1027                                 if(bezt->f1 & SELECT) {
1028                                         *index= nr; index++;
1029                                         VecAddf(cent, cent, bezt->vec[0]);
1030                                 }
1031                                 nr++;
1032                                 if(bezt->f2 & SELECT) {
1033                                         *index= nr; index++;
1034                                         VecAddf(cent, cent, bezt->vec[1]);
1035                                 }
1036                                 nr++;
1037                                 if(bezt->f3 & SELECT) {
1038                                         *index= nr; index++;
1039                                         VecAddf(cent, cent, bezt->vec[2]);
1040                                 }
1041                                 nr++;
1042                                 bezt++;
1043                         }
1044                 }
1045                 else {
1046                         bp= nu->bp;
1047                         a= nu->pntsu*nu->pntsv;
1048                         while(a--) {
1049                                 if(bp->f1 & SELECT) {
1050                                         *index= nr; index++;
1051                                         VecAddf(cent, cent, bp->vec);
1052                                 }
1053                                 nr++;
1054                                 bp++;
1055                         }
1056                 }
1057         }
1058         
1059         VecMulf(cent, 1.0f/(float)totvert);
1060         
1061         return totvert;
1062 }
1063
1064 void ED_object_apply_obmat(Object *ob)
1065 {
1066         float mat[3][3], imat[3][3], tmat[3][3];
1067         
1068         /* from obmat to loc rot size */
1069         
1070         if(ob==NULL) return;
1071         Mat3CpyMat4(mat, ob->obmat);
1072         
1073         VECCOPY(ob->loc, ob->obmat[3]);
1074
1075         Mat3ToEul(mat, ob->rot);
1076         EulToMat3(ob->rot, tmat);
1077
1078         Mat3Inv(imat, tmat);
1079         
1080         Mat3MulMat3(tmat, imat, mat);
1081         
1082         ob->size[0]= tmat[0][0];
1083         ob->size[1]= tmat[1][1];
1084         ob->size[2]= tmat[2][2];
1085         
1086 }
1087
1088 int hook_getIndexArray(Object *obedit, int *tot, int **indexar, char *name, float *cent_r)
1089 {
1090         *indexar= NULL;
1091         *tot= 0;
1092         name[0]= 0;
1093         
1094         switch(obedit->type) {
1095                 case OB_MESH:
1096                 {
1097                         Mesh *me= obedit->data;
1098                         /* check selected vertices first */
1099                         if( return_editmesh_indexar(me->edit_mesh, tot, indexar, cent_r)) return 1;
1100                         else return return_editmesh_vgroup(obedit, me->edit_mesh, name, cent_r);
1101                 }
1102                 case OB_CURVE:
1103                 case OB_SURF:
1104                         return return_editcurve_indexar(obedit, tot, indexar, cent_r);
1105                 case OB_LATTICE:
1106                 {
1107                         Lattice *lt= obedit->data;
1108                         return return_editlattice_indexar(lt->editlatt, tot, indexar, cent_r);
1109                 }
1110                 default:
1111                         return 0;
1112         }
1113 }
1114
1115 static void select_editcurve_hook(Object *obedit, HookModifierData *hmd)
1116 {
1117         ListBase *editnurb= curve_get_editcurve(obedit);
1118         Nurb *nu;
1119         BPoint *bp;
1120         BezTriple *bezt;
1121         int index=0, a, nr=0;
1122         
1123         for(nu= editnurb->first; nu; nu= nu->next) {
1124                 if((nu->type & 7)==CU_BEZIER) {
1125                         bezt= nu->bezt;
1126                         a= nu->pntsu;
1127                         while(a--) {
1128                                 if(nr == hmd->indexar[index]) {
1129                                         bezt->f1 |= SELECT;
1130                                         if(index<hmd->totindex-1) index++;
1131                                 }
1132                                 nr++;
1133                                 if(nr == hmd->indexar[index]) {
1134                                         bezt->f2 |= SELECT;
1135                                         if(index<hmd->totindex-1) index++;
1136                                 }
1137                                 nr++;
1138                                 if(nr == hmd->indexar[index]) {
1139                                         bezt->f3 |= SELECT;
1140                                         if(index<hmd->totindex-1) index++;
1141                                 }
1142                                 nr++;
1143                                 
1144                                 bezt++;
1145                         }
1146                 }
1147                 else {
1148                         bp= nu->bp;
1149                         a= nu->pntsu*nu->pntsv;
1150                         while(a--) {
1151                                 if(nr == hmd->indexar[index]) {
1152                                         bp->f1 |= SELECT;
1153                                         if(index<hmd->totindex-1) index++;
1154                                 }
1155                                 nr++;
1156                                 bp++;
1157                         }
1158                 }
1159         }
1160 }
1161
1162 void obedit_hook_select(Object *ob, HookModifierData *hmd) 
1163 {
1164         
1165         if(ob->type==OB_MESH) select_editmesh_hook(ob, hmd);
1166         else if(ob->type==OB_LATTICE) select_editlattice_hook(ob, hmd);
1167         else if(ob->type==OB_CURVE) select_editcurve_hook(ob, hmd);
1168         else if(ob->type==OB_SURF) select_editcurve_hook(ob, hmd);
1169 }
1170
1171
1172 void add_hook(Scene *scene, View3D *v3d, int mode)
1173 {
1174         ModifierData *md = NULL;
1175         HookModifierData *hmd = NULL;
1176         Object *ob=NULL;
1177         Object *obedit= scene->obedit;  // XXX get from context
1178         
1179         if(obedit==NULL) return;
1180         
1181         /* preconditions */
1182         if(mode==2) { /* selected object */
1183                 Base *base;
1184                 for(base= FIRSTBASE; base; base= base->next) {
1185                         if(TESTBASELIB(v3d, base)) {
1186                                 if(base!=BASACT) {
1187                                         ob= base->object;
1188                                         break;
1189                                 }
1190                         }
1191                 }
1192                 if(ob==NULL) {
1193                         error("Requires selected Object");
1194                         return;
1195                 }
1196         }
1197         else if(mode!=1) {
1198                 int maxlen=0, a, nr;
1199                 char *cp;
1200                 
1201                 /* make pupmenu with hooks */
1202                 for(md=obedit->modifiers.first; md; md= md->next) {
1203                         if (md->type==eModifierType_Hook) 
1204                                 maxlen+=32;
1205                 }
1206                 
1207                 if(maxlen==0) {
1208                         error("Object has no hooks yet");
1209                         return;
1210                 }
1211                 
1212                 cp= MEM_callocN(maxlen+32, "temp string");
1213                 if(mode==3) strcpy(cp, "Remove %t|");
1214                 else if(mode==4) strcpy(cp, "Reassign %t|");
1215                 else if(mode==5) strcpy(cp, "Select %t|");
1216                 else if(mode==6) strcpy(cp, "Clear Offset %t|");
1217                 
1218                 for(md=obedit->modifiers.first; md; md= md->next) {
1219                         if (md->type==eModifierType_Hook) {
1220                                 strcat(cp, md->name);
1221                                 strcat(cp, " |");
1222                         }
1223                 }
1224                 
1225                 nr= pupmenu(cp);
1226                 MEM_freeN(cp);
1227                 
1228                 if(nr<1) return;
1229                 
1230                 a= 1;
1231                 for(md=obedit->modifiers.first; md; md=md->next) {
1232                         if (md->type==eModifierType_Hook) {
1233                                 if(a==nr) break;
1234                                 a++;
1235                         }
1236                 }
1237                 
1238                 hmd = (HookModifierData*) md;
1239                 ob= hmd->object;
1240         }
1241
1242         /* do it, new hooks or reassign */
1243         if(mode==1 || mode==2 || mode==4) {
1244                 float cent[3];
1245                 int tot, ok, *indexar;
1246                 char name[32];
1247                 
1248                 ok = hook_getIndexArray(obedit, &tot, &indexar, name, cent);
1249                 
1250                 if(ok==0) {
1251                         error("Requires selected vertices or active Vertex Group");
1252                 }
1253                 else {
1254                         
1255                         if(mode==1) {
1256                                 Base *base= BASACT, *newbase;
1257                                 
1258                                 ob= add_object(scene, OB_EMPTY);
1259                                 /* set layers OK */
1260                                 newbase= BASACT;
1261                                 newbase->lay= base->lay;
1262                                 ob->lay= newbase->lay;
1263                                 
1264                                 /* transform cent to global coords for loc */
1265                                 VecMat4MulVecfl(ob->loc, obedit->obmat, cent);
1266                                 
1267                                 /* restore, add_object sets active */
1268                                 BASACT= base;
1269                         }
1270                         /* if mode is 2 or 4, ob has been set */
1271                         
1272                         /* new hook */
1273                         if(mode==1 || mode==2) {
1274                                 ModifierData *md = obedit->modifiers.first;
1275                                 
1276                                 while (md && modifierType_getInfo(md->type)->type==eModifierTypeType_OnlyDeform) {
1277                                         md = md->next;
1278                                 }
1279                                 
1280                                 hmd = (HookModifierData*) modifier_new(eModifierType_Hook);
1281                                 BLI_insertlinkbefore(&obedit->modifiers, md, hmd);
1282                                 sprintf(hmd->modifier.name, "Hook-%s", ob->id.name+2);
1283                         }
1284                         else if (hmd->indexar) MEM_freeN(hmd->indexar); /* reassign, hook was set */
1285                 
1286                         hmd->object= ob;
1287                         hmd->indexar= indexar;
1288                         VECCOPY(hmd->cent, cent);
1289                         hmd->totindex= tot;
1290                         BLI_strncpy(hmd->name, name, 32);
1291                         
1292                         if(mode==1 || mode==2) {
1293                                 /* matrix calculus */
1294                                 /* vert x (obmat x hook->imat) x hook->obmat x ob->imat */
1295                                 /*        (parentinv         )                          */
1296                                 
1297                                 where_is_object(scene, ob);
1298                                 
1299                                 Mat4Invert(ob->imat, ob->obmat);
1300                                 /* apparently this call goes from right to left... */
1301                                 Mat4MulSerie(hmd->parentinv, ob->imat, obedit->obmat, NULL, 
1302                                                          NULL, NULL, NULL, NULL, NULL);
1303                         }
1304                 }
1305         }
1306         else if(mode==3) { /* remove */
1307                 BLI_remlink(&obedit->modifiers, md);
1308                 modifier_free(md);
1309         }
1310         else if(mode==5) { /* select */
1311                 obedit_hook_select(obedit, hmd);
1312         }
1313         else if(mode==6) { /* clear offset */
1314                 where_is_object(scene, ob);     /* ob is hook->parent */
1315
1316                 Mat4Invert(ob->imat, ob->obmat);
1317                 /* this call goes from right to left... */
1318                 Mat4MulSerie(hmd->parentinv, ob->imat, obedit->obmat, NULL, 
1319                                          NULL, NULL, NULL, NULL, NULL);
1320         }
1321
1322         DAG_scene_sort(scene);
1323 }
1324
1325
1326 /* use this when the loc/size/rot of the parent has changed but the children should stay in the same place
1327  * apply-size-rot or object center for eg */
1328 static void ignore_parent_tx(Scene *scene, Object *ob ) 
1329 {
1330         Object workob;
1331         Object *ob_child;
1332         
1333         /* a change was made, adjust the children to compensate */
1334         for (ob_child=G.main->object.first; ob_child; ob_child=ob_child->id.next) {
1335                 if (ob_child->parent == ob) {
1336                         ED_object_apply_obmat(ob_child);
1337                         what_does_parent(scene, ob_child, &workob);
1338                         Mat4Invert(ob_child->parentinv, workob.obmat);
1339                 }
1340         }
1341 }
1342
1343
1344 void add_hook_menu(Scene *scene, View3D *v3d)
1345 {
1346         Object *obedit= scene->obedit;  // XXX get from context
1347         int mode;
1348         
1349         if(obedit==NULL) return;
1350         
1351         if(modifiers_findByType(obedit, eModifierType_Hook))
1352                 mode= pupmenu("Hooks %t|Add, To New Empty %x1|Add, To Selected Object %x2|Remove... %x3|Reassign... %x4|Select... %x5|Clear Offset...%x6");
1353         else
1354                 mode= pupmenu("Hooks %t|Add, New Empty %x1|Add, To Selected Object %x2");
1355
1356         if(mode<1) return;
1357                 
1358         /* do operations */
1359         add_hook(scene, v3d, mode);
1360 }
1361
1362 /* ******************** clear parent operator ******************* */
1363
1364 static EnumPropertyItem prop_clear_parent_types[] = {
1365         {0, "CLEAR", "Clear Parent", ""},
1366         {1, "CLEAR_KEEP_TRANSFORM", "Clear and Keep Transformation (Clear Track)", ""},
1367         {2, "CLEAR_INVERSE", "Clear Parent Inverse", ""},
1368         {0, NULL, NULL, NULL}
1369 };
1370
1371 /* note, poll should check for editable scene */
1372 static int clear_parent_exec(bContext *C, wmOperator *op)
1373 {
1374         
1375         CTX_DATA_BEGIN(C, Object*, ob, selected_editable_objects) {
1376
1377                 if(RNA_enum_is_equal(op->ptr, "type", "CLEAR")) {
1378                         ob->parent= NULL;
1379                 }                       
1380                 if(RNA_enum_is_equal(op->ptr, "type", "CLEAR_KEEP_TRANSFORM")) {
1381                         ob->parent= NULL;
1382                         ob->track= NULL;
1383                         ED_object_apply_obmat(ob);
1384                 }
1385                 if(RNA_enum_is_equal(op->ptr, "type", "CLEAR_INVERSE")) {
1386                         Mat4One(ob->parentinv);
1387                 }
1388                 ob->recalc |= OB_RECALC;
1389         }
1390         CTX_DATA_END;
1391         
1392         DAG_scene_sort(CTX_data_scene(C));
1393         ED_anim_dag_flush_update(C);
1394         
1395         return OPERATOR_FINISHED;
1396 }
1397
1398 void OBJECT_OT_clear_parent(wmOperatorType *ot)
1399 {
1400         /* identifiers */
1401         ot->name= "Clear parent";
1402         ot->idname= "OBJECT_OT_clear_parent";
1403         
1404         /* api callbacks */
1405         ot->invoke= WM_menu_invoke;
1406         ot->exec= clear_parent_exec;
1407         
1408         ot->poll= ED_operator_object_active;
1409         
1410         /* flags */
1411         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
1412         
1413         RNA_def_enum(ot->srna, "type", prop_clear_parent_types, 0, "Type", "");
1414 }
1415
1416 /* ******************** clear track operator ******************* */
1417
1418
1419 static EnumPropertyItem prop_clear_track_types[] = {
1420         {0, "CLEAR", "Clear Track", ""},
1421         {1, "CLEAR_KEEP_TRANSFORM", "Clear and Keep Transformation (Clear Track)", ""},
1422         {0, NULL, NULL, NULL}
1423 };
1424
1425 /* note, poll should check for editable scene */
1426 static int object_clear_track_exec(bContext *C, wmOperator *op)
1427 {
1428         if(CTX_data_edit_object(C)) {
1429                 BKE_report(op->reports, RPT_ERROR, "Operation cannot be performed in EditMode");
1430                 return OPERATOR_CANCELLED;
1431         }
1432         CTX_DATA_BEGIN(C, Object*, ob, selected_editable_objects) {
1433                 ob->track= NULL;
1434                 ob->recalc |= OB_RECALC;
1435                 
1436                 if(RNA_enum_is_equal(op->ptr, "type", "CLEAR_KEEP_TRANSFORM")) {
1437                         ED_object_apply_obmat(ob);
1438                 }                       
1439         }
1440         CTX_DATA_END;
1441
1442         DAG_scene_sort(CTX_data_scene(C));
1443         ED_anim_dag_flush_update(C);
1444
1445         return OPERATOR_FINISHED;
1446 }
1447
1448 void OBJECT_OT_clear_track(wmOperatorType *ot)
1449 {
1450         /* identifiers */
1451         ot->name= "Clear track";
1452         ot->idname= "OBJECT_OT_clear_track";
1453         
1454         /* api callbacks */
1455         ot->invoke= WM_menu_invoke;
1456         ot->exec= object_clear_track_exec;
1457         
1458         ot->poll= ED_operator_scene_editable;
1459         
1460         /* flags */
1461         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
1462         
1463         RNA_def_enum(ot->srna, "type", prop_clear_track_types, 0, "Type", "");
1464 }
1465
1466
1467 /* ***************************** */
1468 /* ****** Select by Type ****** */
1469
1470 static int object_select_by_type_exec(bContext *C, wmOperator *op)
1471 {
1472         short obtype;
1473         
1474         obtype = RNA_enum_get(op->ptr, "type");
1475                 
1476         CTX_DATA_BEGIN(C, Base*, base, visible_bases) {
1477                 if(base->object->type==obtype) {
1478                         ED_base_object_select(base, BA_SELECT);
1479                 }
1480         }
1481         CTX_DATA_END;
1482         
1483         WM_event_add_notifier(C, NC_SCENE|ND_OB_SELECT, CTX_data_scene(C));
1484         
1485         return OPERATOR_FINISHED;
1486 }
1487
1488 void OBJECT_OT_select_by_type(wmOperatorType *ot)
1489 {
1490         /* identifiers */
1491         ot->name= "Select By Type";
1492         ot->idname= "OBJECT_OT_select_by_type";
1493         
1494         /* api callbacks */
1495         ot->invoke= WM_menu_invoke;
1496         ot->exec= object_select_by_type_exec;
1497         ot->poll= ED_operator_scene_editable;
1498         
1499         /* flags */
1500         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
1501         
1502         RNA_def_enum(ot->srna, "type", prop_object_types, 0, "Type", "");
1503
1504 }
1505 /* ****** selection by links *******/
1506
1507 static EnumPropertyItem prop_select_linked_types[] = {
1508         {1, "IPO", "Object IPO", ""}, // XXX depreceated animation system stuff...
1509         {2, "OBDATA", "Ob Data", ""},
1510         {3, "MATERIAL", "Material", ""},
1511         {4, "TEXTURE", "Texture", ""},
1512         {5, "DUPGROUP", "Dupligroup", ""},
1513         {6, "PARTICLE", "Particle System", ""},
1514         {0, NULL, NULL, NULL}
1515 };
1516
1517 static int object_select_linked_exec(bContext *C, wmOperator *op)
1518 {
1519         Scene *scene= CTX_data_scene(C);
1520         Object *ob;
1521         void *obdata = NULL;
1522         Material *mat = NULL, *mat1;
1523         Tex *tex=0;
1524         int a, b;
1525         int nr = RNA_enum_get(op->ptr, "type");
1526         short changed = 0;
1527         /* events (nr):
1528          * Object Ipo: 1
1529          * ObData: 2
1530          * Current Material: 3
1531          * Current Texture: 4
1532          * DupliGroup: 5
1533          * PSys: 6
1534          */
1535         
1536         
1537         ob= OBACT;
1538         if(ob==0){ 
1539                 BKE_report(op->reports, RPT_ERROR, "No Active Object");
1540                 return OPERATOR_CANCELLED;
1541         }
1542         
1543         if(nr==1) {     
1544                         // XXX old animation system
1545                 //ipo= ob->ipo;
1546                 //if(ipo==0) return OPERATOR_CANCELLED;
1547                 return OPERATOR_CANCELLED;
1548         }
1549         else if(nr==2) {
1550                 if(ob->data==0) return OPERATOR_CANCELLED;
1551                 obdata= ob->data;
1552         }
1553         else if(nr==3 || nr==4) {
1554                 mat= give_current_material(ob, ob->actcol);
1555                 if(mat==0) return OPERATOR_CANCELLED;
1556                 if(nr==4) {
1557                         if(mat->mtex[ (int)mat->texact ]) tex= mat->mtex[ (int)mat->texact ]->tex;
1558                         if(tex==0) return OPERATOR_CANCELLED;
1559                 }
1560         }
1561         else if(nr==5) {
1562                 if(ob->dup_group==NULL) return OPERATOR_CANCELLED;
1563         }
1564         else if(nr==6) {
1565                 if(ob->particlesystem.first==NULL) return OPERATOR_CANCELLED;
1566         }
1567         else return OPERATOR_CANCELLED;
1568         
1569         CTX_DATA_BEGIN(C, Base*, base, visible_bases) {
1570                 if (!(base->flag & SELECT)) {
1571                         if(nr==1) {
1572                                         // XXX old animation system
1573                                 //if(base->object->ipo==ipo) base->flag |= SELECT;
1574                                 //changed = 1;
1575                         }
1576                         else if(nr==2) {
1577                                 if(base->object->data==obdata) base->flag |= SELECT;
1578                                 changed = 1;
1579                         }
1580                         else if(nr==3 || nr==4) {
1581                                 ob= base->object;
1582                                 
1583                                 for(a=1; a<=ob->totcol; a++) {
1584                                         mat1= give_current_material(ob, a);
1585                                         if(nr==3) {
1586                                                 if(mat1==mat) base->flag |= SELECT;
1587                                                 changed = 1;
1588                                         }
1589                                         else if(mat1 && nr==4) {
1590                                                 for(b=0; b<MAX_MTEX; b++) {
1591                                                         if(mat1->mtex[b]) {
1592                                                                 if(tex==mat1->mtex[b]->tex) {
1593                                                                         base->flag |= SELECT;
1594                                                                         changed = 1;
1595                                                                         break;
1596                                                                 }
1597                                                         }
1598                                                 }
1599                                         }
1600                                 }
1601                         }
1602                         else if(nr==5) {
1603                                 if(base->object->dup_group==ob->dup_group) {
1604                                          base->flag |= SELECT;
1605                                          changed = 1;
1606                                 }
1607                         }
1608                         else if(nr==6) {
1609                                 /* loop through other, then actives particles*/
1610                                 ParticleSystem *psys;
1611                                 ParticleSystem *psys_act;
1612                                 
1613                                 for(psys=base->object->particlesystem.first; psys; psys=psys->next) {
1614                                         for(psys_act=ob->particlesystem.first; psys_act; psys_act=psys_act->next) {
1615                                                 if (psys->part == psys_act->part) {
1616                                                         base->flag |= SELECT;
1617                                                         changed = 1;
1618                                                         break;
1619                                                 }
1620                                         }
1621                                         
1622                                         if (base->flag & SELECT) {
1623                                                 break;
1624                                         }
1625                                 }
1626                         }
1627                         base->object->flag= base->flag;
1628                 }
1629         }
1630         CTX_DATA_END;
1631         
1632         if (changed) {
1633                 WM_event_add_notifier(C, NC_SCENE|ND_OB_SELECT, CTX_data_scene(C));
1634                 return OPERATOR_FINISHED;
1635         }
1636         
1637         return OPERATOR_CANCELLED;
1638 }
1639
1640 void OBJECT_OT_select_linked(wmOperatorType *ot)
1641 {
1642         /* identifiers */
1643         ot->name= "Select Linked";
1644         ot->idname= "OBJECT_OT_select_linked";
1645         
1646         /* api callbacks */
1647         ot->invoke= WM_menu_invoke;
1648         ot->exec= object_select_linked_exec;
1649         ot->poll= ED_operator_scene_editable;
1650         
1651         /* flags */
1652         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
1653         
1654         RNA_def_enum(ot->srna, "type", prop_select_linked_types, 0, "Type", "");
1655
1656 }
1657 /* ****** selection by layer *******/
1658
1659 static int object_select_by_layer_exec(bContext *C, wmOperator *op)
1660 {
1661         unsigned int layernum;
1662         
1663         layernum = RNA_int_get(op->ptr, "layer");
1664                 
1665         CTX_DATA_BEGIN(C, Base*, base, visible_bases) {
1666                 if(base->lay == (1<< (layernum -1)))
1667                         ED_base_object_select(base, BA_SELECT);
1668         }
1669         CTX_DATA_END;
1670         
1671         /* undo? */
1672         WM_event_add_notifier(C, NC_SCENE|ND_OB_SELECT, CTX_data_scene(C));
1673         
1674         return OPERATOR_FINISHED;
1675 }
1676
1677 void OBJECT_OT_select_by_layer(wmOperatorType *ot)
1678 {
1679         /* identifiers */
1680         ot->name= "Selection by layer";
1681         ot->idname= "OBJECT_OT_select_by_layer";
1682         
1683         /* api callbacks */
1684         /*ot->invoke = XXX - need a int grid popup*/
1685         ot->exec= object_select_by_layer_exec;
1686         ot->poll= ED_operator_scene_editable;
1687         
1688         /* flags */
1689         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
1690         
1691         RNA_def_int(ot->srna, "layer", 1, 1, 20, "Layer", "", 1, 20);
1692 }
1693
1694 /* ****** invert selection *******/
1695 static int object_select_invert_exec(bContext *C, wmOperator *op)
1696 {
1697         CTX_DATA_BEGIN(C, Base*, base, visible_bases) {
1698                 if (base->flag & SELECT)
1699                         ED_base_object_select(base, BA_DESELECT);
1700                 else
1701                         ED_base_object_select(base, BA_SELECT);
1702         }
1703         CTX_DATA_END;
1704         
1705         /* undo? */
1706         WM_event_add_notifier(C, NC_SCENE|ND_OB_SELECT, CTX_data_scene(C));
1707         
1708         return OPERATOR_FINISHED;
1709 }
1710
1711 void OBJECT_OT_select_invert(wmOperatorType *ot)
1712 {
1713         
1714         /* identifiers */
1715         ot->name= "Invert selection";
1716         ot->idname= "OBJECT_OT_select_invert";
1717         
1718         /* api callbacks */
1719         ot->exec= object_select_invert_exec;
1720         ot->poll= ED_operator_scene_editable;
1721         
1722         /* flags */
1723         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
1724         
1725 }
1726 /* ****** (de)select All *******/
1727
1728 static int object_de_select_all_exec(bContext *C, wmOperator *op)
1729 {
1730         
1731         int a=0, ok=0; 
1732         
1733         CTX_DATA_BEGIN(C, Base*, base, visible_bases) {
1734                 if (base->flag & SELECT) {
1735                         ok= a= 1;
1736                         break;
1737                 }
1738                 else ok=1;
1739         }
1740         CTX_DATA_END;
1741         
1742         if (!ok) return OPERATOR_PASS_THROUGH;
1743         
1744         CTX_DATA_BEGIN(C, Base*, base, visible_bases) {
1745                 if (a) ED_base_object_select(base, BA_DESELECT);
1746                 else ED_base_object_select(base, BA_SELECT);
1747         }
1748         CTX_DATA_END;
1749         
1750         /* undo? */
1751         WM_event_add_notifier(C, NC_SCENE|ND_OB_SELECT, CTX_data_scene(C));
1752         
1753         return OPERATOR_FINISHED;
1754 }
1755
1756 void OBJECT_OT_de_select_all(wmOperatorType *ot)
1757 {
1758         
1759         /* identifiers */
1760         ot->name= "deselect all";
1761         ot->idname= "OBJECT_OT_de_select_all";
1762         
1763         /* api callbacks */
1764         ot->exec= object_de_select_all_exec;
1765         ot->poll= ED_operator_scene_editable;
1766         
1767         /* flags */
1768         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
1769         
1770 }
1771 /* ****** random selection *******/
1772
1773 static int object_select_random_exec(bContext *C, wmOperator *op)
1774 {       
1775         float percent;
1776         
1777         percent = RNA_float_get(op->ptr, "percent");
1778                 
1779         CTX_DATA_BEGIN(C, Base*, base, visible_bases) {
1780                 if ((!base->flag & SELECT && BLI_frand() < percent)) {
1781                                 ED_base_object_select(base, BA_SELECT);
1782                 }
1783         }
1784         CTX_DATA_END;
1785         
1786         WM_event_add_notifier(C, NC_SCENE|ND_OB_SELECT, CTX_data_scene(C));
1787         
1788         return OPERATOR_FINISHED;
1789 }
1790
1791 void OBJECT_OT_select_random(wmOperatorType *ot)
1792 {
1793         /* identifiers */
1794         ot->name= "Random selection";
1795         ot->idname= "OBJECT_OT_select_random";
1796         
1797         /* api callbacks */
1798         /*ot->invoke= object_select_random_invoke XXX - need a number popup ;*/
1799         ot->exec = object_select_random_exec;
1800         ot->poll= ED_operator_scene_editable;
1801         
1802         /* flags */
1803         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
1804         
1805         RNA_def_float_percentage(ot->srna, "percent", 0.5f, 0.0f, 1.0f, "Percent", "percentage of objects to randomly select", 0.0001f, 1.0f);
1806 }
1807
1808 /* ******** Clear object Translation *********** */
1809
1810 static int object_clear_location_exec(bContext *C, wmOperator *op)
1811 {
1812         int armature_clear= 0;
1813
1814         CTX_DATA_BEGIN(C, Object*, ob, selected_editable_objects) {
1815                 if((G.f & G_WEIGHTPAINT)==0) {
1816                         if ((ob->protectflag & OB_LOCK_LOCX)==0)
1817                                 ob->loc[0]= ob->dloc[0]= 0.0f;
1818                         if ((ob->protectflag & OB_LOCK_LOCY)==0)
1819                                 ob->loc[1]= ob->dloc[1]= 0.0f;
1820                         if ((ob->protectflag & OB_LOCK_LOCZ)==0)
1821                                 ob->loc[2]= ob->dloc[2]= 0.0f;
1822                 }
1823                 ob->recalc |= OB_RECALC_OB;
1824         }
1825         CTX_DATA_END;
1826
1827         if(armature_clear==0) /* in this case flush was done */
1828                 ED_anim_dag_flush_update(C);    
1829         
1830         WM_event_add_notifier(C, NC_OBJECT|ND_TRANSFORM, NULL);
1831         
1832         return OPERATOR_FINISHED;
1833 }
1834
1835
1836 void OBJECT_OT_clear_location(wmOperatorType *ot)
1837 {
1838         
1839         /* identifiers */
1840         ot->name= "Clear Object Location";
1841         ot->idname= "OBJECT_OT_clear_location";
1842         
1843         /* api callbacks */
1844         ot->invoke= WM_operator_confirm;
1845         ot->exec= object_clear_location_exec;
1846         ot->poll= ED_operator_object_active;
1847         
1848         /* flags */
1849         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
1850 }
1851
1852 static int object_clear_rotation_exec(bContext *C, wmOperator *op)
1853 {
1854         int armature_clear= 0;
1855
1856         CTX_DATA_BEGIN(C, Object*, ob, selected_editable_objects) {
1857                 if((G.f & G_WEIGHTPAINT)==0) {
1858                         /* eulers can only get cleared if they are not protected */
1859                         if ((ob->protectflag & OB_LOCK_ROTX)==0)
1860                                 ob->rot[0]= ob->drot[0]= 0.0f;
1861                         if ((ob->protectflag & OB_LOCK_ROTY)==0)
1862                                 ob->rot[1]= ob->drot[1]= 0.0f;
1863                         if ((ob->protectflag & OB_LOCK_ROTZ)==0)
1864                                 ob->rot[2]= ob->drot[2]= 0.0f;
1865                 }
1866                 ob->recalc |= OB_RECALC_OB;
1867         }
1868         CTX_DATA_END;
1869
1870         if(armature_clear==0) /* in this case flush was done */
1871                 ED_anim_dag_flush_update(C);    
1872         
1873         WM_event_add_notifier(C, NC_OBJECT|ND_TRANSFORM, NULL);
1874         
1875         return OPERATOR_FINISHED;
1876 }
1877
1878
1879 void OBJECT_OT_clear_rotation(wmOperatorType *ot)
1880 {
1881         
1882         /* identifiers */
1883         ot->name= "Clear Object Rotation";
1884         ot->idname= "OBJECT_OT_clear_rotation";
1885         
1886         /* api callbacks */
1887         ot->invoke= WM_operator_confirm;
1888         ot->exec= object_clear_rotation_exec;
1889         ot->poll= ED_operator_object_active;
1890         
1891         /* flags */
1892         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
1893 }
1894
1895 static int object_clear_scale_exec(bContext *C, wmOperator *op)
1896 {
1897         int armature_clear= 0;
1898
1899         CTX_DATA_BEGIN(C, Object*, ob, selected_editable_objects) {
1900                 if((G.f & G_WEIGHTPAINT)==0) {
1901                         if ((ob->protectflag & OB_LOCK_SCALEX)==0) {
1902                                 ob->dsize[0]= 0.0f;
1903                                 ob->size[0]= 1.0f;
1904                         }
1905                         if ((ob->protectflag & OB_LOCK_SCALEY)==0) {
1906                                 ob->dsize[1]= 0.0f;
1907                                 ob->size[1]= 1.0f;
1908                         }
1909                         if ((ob->protectflag & OB_LOCK_SCALEZ)==0) {
1910                                 ob->dsize[2]= 0.0f;
1911                                 ob->size[2]= 1.0f;
1912                         }
1913                 }
1914                 ob->recalc |= OB_RECALC_OB;
1915         }
1916         CTX_DATA_END;
1917         
1918         if(armature_clear==0) /* in this case flush was done */
1919                 ED_anim_dag_flush_update(C);    
1920         
1921         WM_event_add_notifier(C, NC_OBJECT|ND_TRANSFORM, NULL);
1922         
1923         return OPERATOR_FINISHED;
1924 }
1925
1926 void OBJECT_OT_clear_scale(wmOperatorType *ot)
1927 {
1928         
1929         /* identifiers */
1930         ot->name= "Clear Object Scale";
1931         ot->idname= "OBJECT_OT_clear_scale";
1932         
1933         /* api callbacks */
1934         ot->invoke= WM_operator_confirm;
1935         ot->exec= object_clear_scale_exec;
1936         ot->poll= ED_operator_object_active;
1937         
1938         /* flags */
1939         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
1940 }
1941
1942 static int object_clear_origin_exec(bContext *C, wmOperator *op)
1943 {
1944         float *v1, *v3, mat[3][3];
1945         int armature_clear= 0;
1946
1947         CTX_DATA_BEGIN(C, Object*, ob, selected_editable_objects) {
1948                 if(ob->parent) {
1949                         v1= ob->loc;
1950                         v3= ob->parentinv[3];
1951                         
1952                         Mat3CpyMat4(mat, ob->parentinv);
1953                         VECCOPY(v3, v1);
1954                         v3[0]= -v3[0];
1955                         v3[1]= -v3[1];
1956                         v3[2]= -v3[2];
1957                         Mat3MulVecfl(mat, v3);
1958                 }
1959                 ob->recalc |= OB_RECALC_OB;
1960         }
1961         CTX_DATA_END;
1962
1963         if(armature_clear==0) /* in this case flush was done */
1964                 ED_anim_dag_flush_update(C);    
1965         
1966         WM_event_add_notifier(C, NC_OBJECT|ND_TRANSFORM, NULL);
1967         
1968         return OPERATOR_FINISHED;
1969 }
1970
1971 void OBJECT_OT_clear_origin(wmOperatorType *ot)
1972 {
1973
1974         /* identifiers */
1975         ot->name= "Clear Object Origin";
1976         ot->idname= "OBJECT_OT_clear_origin";
1977         
1978         /* api callbacks */
1979         ot->invoke= WM_operator_confirm;
1980         ot->exec= object_clear_origin_exec;
1981         ot->poll= ED_operator_object_active;
1982         
1983         /* flags */
1984         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
1985 }
1986
1987 /* ********* clear/set restrict view *********/
1988 static int object_clear_restrictview_exec(bContext *C, wmOperator *op)
1989 {
1990         ScrArea *sa= CTX_wm_area(C);
1991         View3D *v3d= sa->spacedata.first;
1992         Scene *scene= CTX_data_scene(C);
1993         Base *base;
1994         int changed = 0;
1995         
1996         /* XXX need a context loop to handle such cases */
1997         for(base = FIRSTBASE; base; base=base->next){
1998                 if((base->lay & v3d->lay) && base->object->restrictflag & OB_RESTRICT_VIEW) {
1999                         base->flag |= SELECT;
2000                         base->object->flag = base->flag;
2001                         base->object->restrictflag &= ~OB_RESTRICT_VIEW; 
2002                         changed = 1;
2003                 }
2004         }
2005         if (changed) {
2006                 DAG_scene_sort(scene);
2007                 WM_event_add_notifier(C, NC_SCENE|ND_OB_SELECT, scene);
2008         }
2009
2010         return OPERATOR_FINISHED;
2011 }
2012
2013 void OBJECT_OT_clear_restrictview(wmOperatorType *ot)
2014 {
2015         
2016         /* identifiers */
2017         ot->name= "Clear restrict view";
2018         ot->idname= "OBJECT_OT_clear_restrictview";
2019         
2020         /* api callbacks */
2021         ot->invoke= WM_operator_confirm;
2022         ot->exec= object_clear_restrictview_exec;
2023         ot->poll= ED_operator_view3d_active;
2024         
2025         /* flags */
2026         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
2027 }
2028
2029 static EnumPropertyItem prop_set_restrictview_types[] = {
2030         {0, "SELECTED", "Selected", ""},
2031         {1, "UNSELECTED", "Unselected ", ""},
2032         {0, NULL, NULL, NULL}
2033 };
2034
2035 static int object_set_restrictview_exec(bContext *C, wmOperator *op)
2036 {
2037         Scene *scene= CTX_data_scene(C);
2038         short changed = 0;
2039         
2040         CTX_DATA_BEGIN(C, Base*, base, visible_bases) {
2041                 if(RNA_enum_is_equal(op->ptr, "type", "SELECTED")){
2042                         if (base->flag & SELECT){
2043                                 base->flag &= ~SELECT;
2044                                 base->object->flag = base->flag;
2045                                 base->object->restrictflag |= OB_RESTRICT_VIEW;
2046                                 changed = 1;
2047                                 if (base==BASACT) {
2048                                         ED_base_object_activate(C, NULL);
2049                                 }
2050                         }
2051                 }
2052                 else if (RNA_enum_is_equal(op->ptr, "type", "UNSELECTED")){
2053                         if (!(base->flag & SELECT)){
2054                                 base->object->restrictflag |= OB_RESTRICT_VIEW;
2055                                 changed = 1;
2056                         }
2057                 }       
2058         }
2059         CTX_DATA_END;
2060
2061         if (changed) {
2062                 DAG_scene_sort(scene);
2063                 
2064                 WM_event_add_notifier(C, NC_SCENE|ND_OB_SELECT, CTX_data_scene(C));
2065                 
2066         }
2067
2068         return OPERATOR_FINISHED;
2069 }
2070
2071 void OBJECT_OT_set_restrictview(wmOperatorType *ot)
2072 {
2073         /* identifiers */
2074         ot->name= "Set restrict view";
2075         ot->idname= "OBJECT_OT_set_restrictview";
2076         
2077         /* api callbacks */
2078         ot->invoke= WM_menu_invoke;
2079         ot->exec= object_set_restrictview_exec;
2080         ot->poll= ED_operator_view3d_active;
2081         
2082         /* flags */
2083         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
2084         
2085         RNA_def_enum(ot->srna, "type", prop_set_restrictview_types, 0, "Type", "");
2086         
2087 }
2088 /* ************* Slow Parent ******************* */
2089 static int object_set_slowparent_exec(bContext *C, wmOperator *op)
2090 {
2091
2092         CTX_DATA_BEGIN(C, Base*, base, selected_editable_bases) {
2093                 
2094                 if(base->object->parent) base->object->partype |= PARSLOW;
2095                 base->object->recalc |= OB_RECALC_OB;
2096                 
2097         }
2098         CTX_DATA_END;
2099
2100         ED_anim_dag_flush_update(C);    
2101         
2102         WM_event_add_notifier(C, NC_SCENE, CTX_data_scene(C));
2103         
2104         return OPERATOR_FINISHED;
2105 }
2106
2107 void OBJECT_OT_set_slowparent(wmOperatorType *ot)
2108 {
2109         
2110         /* identifiers */
2111         ot->name= "Set Slow Parent";
2112         ot->idname= "OBJECT_OT_set_slow_parent";
2113         
2114         /* api callbacks */
2115         ot->invoke= WM_operator_confirm;
2116         ot->exec= object_set_slowparent_exec;
2117         ot->poll= ED_operator_view3d_active;
2118         
2119         /* flags */
2120         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
2121 }
2122
2123 static int object_clear_slowparent_exec(bContext *C, wmOperator *op)
2124 {
2125         Scene *scene= CTX_data_scene(C);
2126
2127         CTX_DATA_BEGIN(C, Base*, base, selected_editable_bases) {
2128                 if(base->object->parent) {
2129                         if(base->object->partype & PARSLOW) {
2130                                 base->object->partype -= PARSLOW;
2131                                 where_is_object(scene, base->object);
2132                                 base->object->partype |= PARSLOW;
2133                                 base->object->recalc |= OB_RECALC_OB;
2134                         }
2135                 }
2136                 
2137         }
2138         CTX_DATA_END;
2139
2140         ED_anim_dag_flush_update(C);    
2141         
2142         WM_event_add_notifier(C, NC_SCENE, CTX_data_scene(C));
2143         
2144         return OPERATOR_FINISHED;
2145 }
2146
2147 void OBJECT_OT_clear_slowparent(wmOperatorType *ot)
2148 {
2149         
2150         /* identifiers */
2151         ot->name= "Clear Slow Parent";
2152         ot->idname= "OBJECT_OT_clear_slow_parent";
2153         
2154         /* api callbacks */
2155         ot->invoke= WM_operator_confirm;
2156         ot->exec= object_clear_slowparent_exec;
2157         ot->poll= ED_operator_view3d_active;
2158         
2159         /* flags */
2160         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
2161 }
2162 /* ******************** **************** */
2163
2164 // XXX
2165 #define BEZSELECTED_HIDDENHANDLES(bezt)   ((G.f & G_HIDDENHANDLES) ? (bezt)->f2 & SELECT : BEZSELECTED(bezt))
2166 /* only in edit mode */
2167 void make_vertex_parent(Scene *scene, Object *obedit, View3D *v3d)
2168 {
2169         EditVert *eve;
2170         Base *base;
2171         Nurb *nu;
2172         BezTriple *bezt;
2173         BPoint *bp;
2174         Object *par, *ob;
2175         int a, v1=0, v2=0, v3=0, v4=0, nr=1;
2176         
2177         /* we need 1 to 3 selected vertices */
2178         
2179         if(obedit->type==OB_MESH) {
2180                 Mesh *me= obedit->data;
2181                 
2182                 eve= me->edit_mesh->verts.first;
2183                 while(eve) {
2184                         if(eve->f & 1) {
2185                                 if(v1==0) v1= nr;
2186                                 else if(v2==0) v2= nr;
2187                                 else if(v3==0) v3= nr;
2188                                 else if(v4==0) v4= nr;
2189                                 else break;
2190                         }
2191                         nr++;
2192                         eve= eve->next;
2193                 }
2194         }
2195         else if(ELEM(obedit->type, OB_SURF, OB_CURVE)) {
2196                 ListBase *editnurb= curve_get_editcurve(obedit);
2197
2198                 nu= editnurb->first;
2199                 while(nu) {
2200                         if((nu->type & 7)==CU_BEZIER) {
2201                                 bezt= nu->bezt;
2202                                 a= nu->pntsu;
2203                                 while(a--) {
2204                                         if(BEZSELECTED_HIDDENHANDLES(bezt)) {
2205                                                 if(v1==0) v1= nr;
2206                                                 else if(v2==0) v2= nr;
2207                                                 else if(v3==0) v3= nr;
2208                                                 else if(v4==0) v4= nr;
2209                                                 else break;
2210                                         }
2211                                         nr++;
2212                                         bezt++;
2213                                 }
2214                         }
2215                         else {
2216                                 bp= nu->bp;
2217                                 a= nu->pntsu*nu->pntsv;
2218                                 while(a--) {
2219                                         if(bp->f1 & SELECT) {
2220                                                 if(v1==0) v1= nr;
2221                                                 else if(v2==0) v2= nr;
2222                                                 else if(v3==0) v3= nr;
2223                                                 else if(v4==0) v4= nr;
2224                                                 else break;
2225                                         }
2226                                         nr++;
2227                                         bp++;
2228                                 }
2229                         }
2230                         nu= nu->next;
2231                 }
2232         }
2233         else if(obedit->type==OB_LATTICE) {
2234                 Lattice *lt= obedit->data;
2235                 
2236                 a= lt->editlatt->pntsu*lt->editlatt->pntsv*lt->editlatt->pntsw;
2237                 bp= lt->editlatt->def;
2238                 while(a--) {
2239                         if(bp->f1 & SELECT) {
2240                                 if(v1==0) v1= nr;
2241                                 else if(v2==0) v2= nr;
2242                                 else if(v3==0) v3= nr;
2243                                 else if(v4==0) v4= nr;
2244                                 else break;
2245                         }
2246                         nr++;
2247                         bp++;
2248                 }
2249         }
2250         
2251         if(v4 || !((v1 && v2==0 && v3==0) || (v1 && v2 && v3)) ) {
2252                 error("Select either 1 or 3 vertices to parent to");
2253                 return;
2254         }
2255         
2256         if(okee("Make vertex parent")==0) return;
2257         
2258         for(base= FIRSTBASE; base; base= base->next) {
2259                 if(TESTBASELIB(v3d, base)) {
2260                         if(base!=BASACT) {
2261                                 
2262                                 ob= base->object;
2263                                 ob->recalc |= OB_RECALC;
2264                                 par= BASACT->object->parent;
2265                                 
2266                                 while(par) {
2267                                         if(par==ob) break;
2268                                         par= par->parent;
2269                                 }
2270                                 if(par) {
2271                                         error("Loop in parents");
2272                                 }
2273                                 else {
2274                                         Object workob;
2275                                         
2276                                         ob->parent= BASACT->object;
2277                                         if(v3) {
2278                                                 ob->partype= PARVERT3;
2279                                                 ob->par1= v1-1;
2280                                                 ob->par2= v2-1;
2281                                                 ob->par3= v3-1;
2282
2283                                                 /* inverse parent matrix */
2284                                                 what_does_parent(scene, ob, &workob);
2285                                                 Mat4Invert(ob->parentinv, workob.obmat);
2286                                         }
2287                                         else {
2288                                                 ob->partype= PARVERT1;
2289                                                 ob->par1= v1-1;
2290
2291                                                 /* inverse parent matrix */
2292                                                 what_does_parent(scene, ob, &workob);
2293                                                 Mat4Invert(ob->parentinv, workob.obmat);
2294                                         }
2295                                 }
2296                         }
2297                 }
2298         }
2299         
2300         DAG_scene_sort(scene);
2301 }
2302
2303 static Object *group_objects_menu(Group *group)
2304 {
2305         GroupObject *go;
2306         int len= 0;
2307         short a, nr;
2308         char *str;
2309                 
2310         for(go= group->gobject.first; go; go= go->next) {
2311                 if(go->ob)
2312                         len++;
2313         }
2314         if(len==0) return NULL;
2315         
2316         str= MEM_callocN(40+32*len, "menu");
2317         
2318         strcpy(str, "Make Proxy for: %t");
2319         a= strlen(str);
2320         for(nr=1, go= group->gobject.first; go; go= go->next, nr++) {
2321                 a+= sprintf(str+a, "|%s %%x%d", go->ob->id.name+2, nr);
2322         }
2323         
2324         a= pupmenu_col(str, 20);
2325         MEM_freeN(str);
2326         if(a>0) {
2327                 go= BLI_findlink(&group->gobject, a-1);
2328                 return go->ob;
2329         }
2330         return NULL;
2331 }
2332
2333
2334 /* adds empty object to become local replacement data of a library-linked object */
2335 void make_proxy(Scene *scene)
2336 {
2337         Object *ob= OBACT;
2338         Object *gob= NULL;
2339         
2340         if(scene->id.lib) return;
2341         if(ob==NULL) return;
2342         
2343         
2344         if(ob->dup_group && ob->dup_group->id.lib) {
2345                 gob= ob;
2346                 /* gives menu with list of objects in group */
2347                 ob= group_objects_menu(ob->dup_group);
2348         }
2349         else if(ob->id.lib) {
2350                 if(okee("Make Proxy Object")==0)
2351                 return;
2352         }
2353         else {
2354                 error("Can only make proxy for a referenced object or group");
2355                 return;
2356         }
2357         
2358         if(ob) {
2359                 Object *newob;
2360                 Base *newbase, *oldbase= BASACT;
2361                 char name[32];
2362                 
2363                 newob= add_object(scene, OB_EMPTY);
2364                 if(gob)
2365                         strcpy(name, gob->id.name+2);
2366                 else
2367                         strcpy(name, ob->id.name+2);
2368                 strcat(name, "_proxy");
2369                 rename_id(&newob->id, name);
2370                 
2371                 /* set layers OK */
2372                 newbase= BASACT;        /* add_object sets active... */
2373                 newbase->lay= oldbase->lay;
2374                 newob->lay= newbase->lay;
2375                 
2376                 /* remove base, leave user count of object, it gets linked in object_make_proxy */
2377                 if(gob==NULL) {
2378                         BLI_remlink(&scene->base, oldbase);
2379                         MEM_freeN(oldbase);
2380                 }               
2381                 object_make_proxy(newob, ob, gob);
2382                 
2383                 DAG_scene_sort(scene);
2384                 DAG_object_flush_update(scene, newob, OB_RECALC);
2385         }
2386 }
2387
2388 /* ******************** make parent operator *********************** */
2389
2390 #define PAR_OBJECT              0
2391 #define PAR_ARMATURE    1
2392 #define PAR_BONE                2
2393 #define PAR_CURVE               3
2394 #define PAR_FOLLOW              4
2395 #define PAR_PATH_CONST  5
2396 #define PAR_LATTICE             6
2397 #define PAR_VERTEX              7
2398 #define PAR_TRIA                8
2399
2400 static EnumPropertyItem prop_make_parent_types[] = {
2401         {PAR_OBJECT, "OBJECT", "Object", ""},
2402         {PAR_ARMATURE, "ARMATURE", "Armature Deform", ""},
2403         {PAR_BONE, "BONE", "Bone", ""},
2404         {PAR_CURVE, "CURVE", "Curve Deform", ""},
2405         {PAR_FOLLOW, "FOLLOW", "Follow Path", ""},
2406         {PAR_PATH_CONST, "PATH_CONST", "Path Constraint", ""},
2407         {PAR_LATTICE, "LATTICE", "Lattice Deform", ""},
2408         {PAR_VERTEX, "VERTEX", "Vertex", ""},
2409         {PAR_TRIA, "TRIA", "Triangle", ""},
2410         {0, NULL, NULL, NULL}
2411 };
2412
2413 static int test_parent_loop(Object *par, Object *ob)
2414 {
2415         /* test if 'ob' is a parent somewhere in par's parents */
2416         
2417         if(par == NULL) return 0;
2418         if(ob == par) return 1;
2419         
2420         return test_parent_loop(par->parent, ob);
2421 }
2422
2423
2424 static int make_parent_exec(bContext *C, wmOperator *op)
2425 {
2426         Scene *scene= CTX_data_scene(C);
2427         Object *par= CTX_data_active_object(C);
2428         bPoseChannel *pchan= NULL;
2429         int partype= RNA_enum_get(op->ptr, "type");
2430         
2431         par->recalc |= OB_RECALC_OB;
2432         
2433         /* preconditions */
2434         if(partype==PAR_FOLLOW || partype==PAR_PATH_CONST) {
2435                 if(par->type!=OB_CURVE)
2436                         return OPERATOR_CANCELLED;
2437                 else {
2438                         Curve *cu= par->data;
2439                         
2440                         if((cu->flag & CU_PATH)==0) {
2441                                 cu->flag |= CU_PATH|CU_FOLLOW;
2442                                 makeDispListCurveTypes(scene, par, 0);  /* force creation of path data */
2443                         }
2444                         else cu->flag |= CU_FOLLOW;
2445                         
2446                         /* fall back on regular parenting now */
2447                         partype= PAR_OBJECT;
2448                 }               
2449         }
2450         else if(partype==PAR_BONE) {
2451                 pchan= get_active_posechannel(par);
2452                 
2453                 if(pchan==NULL) {
2454                         error("No active Bone");
2455                         return OPERATOR_CANCELLED;
2456                 }
2457         }
2458         
2459         /* context itterator */
2460         CTX_DATA_BEGIN(C, Object*, ob, selected_editable_objects) {
2461                 
2462                 if(ob!=par) {
2463                         
2464                         if( test_parent_loop(par, ob) ) {
2465                                 error("Loop in parents");
2466                         }
2467                         else {
2468                                 Object workob;
2469                                 
2470                                 /* apply transformation of previous parenting */
2471                                 ED_object_apply_obmat(ob);
2472                                 
2473                                 ob->parent= par;
2474                                 
2475                                 /* handle types */
2476                                 if (pchan)
2477                                         strcpy (ob->parsubstr, pchan->name);
2478                                 else
2479                                         ob->parsubstr[0]= 0;
2480                                 
2481                                 /* constraint */
2482                                 if(partype==PAR_PATH_CONST) {
2483                                         bConstraint *con;
2484                                         bFollowPathConstraint *data;
2485                                         float cmat[4][4], vec[3];
2486                                         
2487                                         con = add_new_constraint(CONSTRAINT_TYPE_FOLLOWPATH);
2488                                         strcpy (con->name, "AutoPath");
2489                                         
2490                                         data = con->data;
2491                                         data->tar = par;
2492                                         
2493                                         add_constraint_to_object(con, ob);
2494                                         
2495                                         get_constraint_target_matrix(con, 0, CONSTRAINT_OBTYPE_OBJECT, NULL, cmat, scene->r.cfra - give_timeoffset(ob));
2496                                         VecSubf(vec, ob->obmat[3], cmat[3]);
2497                                         
2498                                         ob->loc[0] = vec[0];
2499                                         ob->loc[1] = vec[1];
2500                                 }
2501                                 else if(partype==PAR_ARMATURE && ob->type==OB_MESH && par->type == OB_ARMATURE) {
2502                                         
2503                                         if(1) {
2504                                                 /* Prompt the user as to whether he wants to
2505                                                 * add some vertex groups based on the bones
2506                                                 * in the parent armature.
2507                                                 */
2508                                                 create_vgroups_from_armature(scene, ob, par);
2509                                                 
2510                                                 /* get corrected inverse */
2511                                                 ob->partype= PAROBJECT;
2512                                                 what_does_parent(scene, ob, &workob);
2513                                                 
2514                                                 ob->partype= PARSKEL;
2515                                         }
2516                                         else
2517                                                 what_does_parent(scene, ob, &workob);
2518                                         
2519                                         Mat4Invert(ob->parentinv, workob.obmat);
2520                                 }
2521                                 else {
2522                                         /* calculate inverse parent matrix */
2523                                         what_does_parent(scene, ob, &workob);
2524                                         Mat4Invert(ob->parentinv, workob.obmat);
2525                                 }
2526                                 
2527                                 ob->recalc |= OB_RECALC_OB|OB_RECALC_DATA;
2528                                 
2529                                 if( ELEM3(partype, PAR_CURVE, PAR_ARMATURE, PAR_LATTICE) )
2530                                         ob->partype= PARSKEL; /* note, dna define, not operator property */
2531                                 else
2532                                         ob->partype= PAROBJECT; /* note, dna define, not operator property */
2533                         }
2534                 }
2535         }
2536         CTX_DATA_END;
2537         
2538         DAG_scene_sort(CTX_data_scene(C));
2539         ED_anim_dag_flush_update(C);    
2540         
2541         return OPERATOR_FINISHED;
2542 }
2543
2544 static int make_parent_invoke(bContext *C, wmOperator *op, wmEvent *event)
2545 {
2546         Object *ob= CTX_data_active_object(C);
2547         uiMenuItem *head= uiPupMenuBegin("Make Parent To", 0);
2548         
2549         uiMenuContext(head, WM_OP_EXEC_DEFAULT);
2550         uiMenuItemEnumO(head, "", 0, "OBJECT_OT_make_parent", "type", PAR_OBJECT);
2551         
2552         /* ob becomes parent, make the associated menus */
2553         if(ob->type==OB_ARMATURE) {
2554                 uiMenuItemEnumO(head, "", 0, "OBJECT_OT_make_parent", "type", PAR_ARMATURE);
2555                 uiMenuItemEnumO(head, "", 0, "OBJECT_OT_make_parent", "type", PAR_BONE);
2556         }
2557         else if(ob->type==OB_CURVE) {
2558                 uiMenuItemEnumO(head, "", 0, "OBJECT_OT_make_parent", "type", PAR_CURVE);
2559                 uiMenuItemEnumO(head, "", 0, "OBJECT_OT_make_parent", "type", PAR_FOLLOW);
2560                 uiMenuItemEnumO(head, "", 0, "OBJECT_OT_make_parent", "type", PAR_PATH_CONST);
2561         }
2562         else if(ob->type == OB_LATTICE) {
2563                 uiMenuItemEnumO(head, "", 0, "OBJECT_OT_make_parent", "type", PAR_LATTICE);
2564         }
2565         
2566         uiPupMenuEnd(C, head);
2567         
2568         return OPERATOR_CANCELLED;
2569 }
2570
2571
2572 void OBJECT_OT_make_parent(wmOperatorType *ot)
2573 {
2574         /* identifiers */
2575         ot->name= "Make parent";
2576         ot->idname= "OBJECT_OT_make_parent";
2577         
2578         /* api callbacks */
2579         ot->invoke= make_parent_invoke;
2580         ot->exec= make_parent_exec;
2581         
2582         ot->poll= ED_operator_object_active;
2583         
2584         /* flags */
2585         ot->flag= 0;
2586         
2587         RNA_def_enum(ot->srna, "type", prop_make_parent_types, 0, "Type", "");
2588 }
2589
2590 /* *** make track ***** */
2591 static EnumPropertyItem prop_make_track_types[] = {
2592         {1, "TRACKTO", "TrackTo Constraint", ""},
2593         {2, "LOCKTRACK", "LockTrack Constraint", ""},
2594         {3, "OLDTRACK", "Old Track", ""},
2595         {0, NULL, NULL, NULL}
2596 };
2597
2598 static int make_track_exec(bContext *C, wmOperator *op)
2599 {
2600         Scene *scene= CTX_data_scene(C);
2601                 
2602         if(RNA_enum_is_equal(op->ptr, "type", "TRACKTO")){
2603                 bConstraint *con;
2604                 bTrackToConstraint *data;
2605
2606                 CTX_DATA_BEGIN(C, Base*, base, selected_editable_bases) {
2607                         if(base!=BASACT) {
2608                                 con = add_new_constraint(CONSTRAINT_TYPE_TRACKTO);
2609                                 strcpy (con->name, "AutoTrack");
2610
2611                                 data = con->data;
2612                                 data->tar = BASACT->object;
2613                                 base->object->recalc |= OB_RECALC;
2614                                 
2615                                 /* Lamp and Camera track differently by default */
2616                                 if (base->object->type == OB_LAMP || base->object->type == OB_CAMERA) {
2617                                         data->reserved1 = TRACK_nZ;
2618                                         data->reserved2 = UP_Y;
2619                                 }
2620
2621                                 add_constraint_to_object(con, base->object);
2622                         }
2623                 }
2624                 CTX_DATA_END;
2625         }
2626         else if(RNA_enum_is_equal(op->ptr, "type", "LOCKTRACK")){
2627                 bConstraint *con;
2628                 bLockTrackConstraint *data;
2629
2630                 CTX_DATA_BEGIN(C, Base*, base, selected_editable_bases) {
2631                         if(base!=BASACT) {
2632                                 con = add_new_constraint(CONSTRAINT_TYPE_LOCKTRACK);
2633                                 strcpy (con->name, "AutoTrack");
2634
2635                                 data = con->data;
2636                                 data->tar = BASACT->object;
2637                                 base->object->recalc |= OB_RECALC;
2638                                 
2639                                 /* Lamp and Camera track differently by default */
2640                                 if (base->object->type == OB_LAMP || base->object->type == OB_CAMERA) {
2641                                         data->trackflag = TRACK_nZ;
2642                                         data->lockflag = LOCK_Y;
2643                                 }
2644
2645                                 add_constraint_to_object(con, base->object);
2646                         }
2647                 }
2648                 CTX_DATA_END;
2649         }
2650         else if(RNA_enum_is_equal(op->ptr, "type", "OLDTRACK")){
2651                 CTX_DATA_BEGIN(C, Base*, base, selected_editable_bases) {
2652                         if(base!=BASACT) {
2653                                 base->object->track= BASACT->object;
2654                                 base->object->recalc |= OB_RECALC;
2655                         }
2656                 }
2657                 CTX_DATA_END;
2658         }
2659         DAG_scene_sort(CTX_data_scene(C));
2660         ED_anim_dag_flush_update(C);    
2661         
2662         return OPERATOR_FINISHED;
2663 }
2664
2665 void OBJECT_OT_make_track(wmOperatorType *ot)
2666 {
2667         /* identifiers */
2668         ot->name= "Make Track";
2669         ot->idname= "OBJECT_OT_make_track";
2670         
2671         /* api callbacks */
2672         ot->invoke= WM_menu_invoke;
2673         ot->exec= make_track_exec;
2674         
2675         ot->poll= ED_operator_scene_editable;
2676         
2677         /* flags */
2678         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
2679         
2680         RNA_def_enum(ot->srna, "type", prop_make_track_types, 0, "Type", "");
2681 }
2682
2683 /* ************* Make Dupli Real ********* */
2684 static void make_object_duplilist_real(Scene *scene, View3D *v3d, Base *base)
2685 {
2686         Base *basen;
2687         Object *ob;
2688         ListBase *lb;
2689         DupliObject *dob;
2690         
2691         if(!base && !(base = BASACT))
2692                 return;
2693         
2694         if(!(base->object->transflag & OB_DUPLI))
2695                 return;
2696         
2697         lb= object_duplilist(scene, base->object);
2698         
2699         for(dob= lb->first; dob; dob= dob->next) {
2700                 ob= copy_object(dob->ob);
2701                 /* font duplis can have a totcol without material, we get them from parent
2702                 * should be implemented better...
2703                 */
2704                 if(ob->mat==NULL) ob->totcol= 0;
2705                 
2706                 basen= MEM_dupallocN(base);
2707                 basen->flag &= ~OB_FROMDUPLI;
2708                 BLI_addhead(&scene->base, basen);       /* addhead: othwise eternal loop */
2709                 basen->object= ob;
2710                 ob->ipo= NULL;          /* make sure apply works */
2711                 ob->parent= ob->track= NULL;
2712                 ob->disp.first= ob->disp.last= NULL;
2713                 ob->transflag &= ~OB_DUPLI;     
2714                 
2715                 Mat4CpyMat4(ob->obmat, dob->mat);
2716                 ED_object_apply_obmat(ob);
2717         }
2718         
2719         copy_object_set_idnew(scene, v3d, 0);
2720         
2721         free_object_duplilist(lb);
2722         
2723         base->object->transflag &= ~OB_DUPLI;   
2724 }
2725
2726
2727 static int object_make_dupli_real_exec(bContext *C, wmOperator *op)
2728 {
2729         Scene *scene= CTX_data_scene(C);
2730         ScrArea *sa= CTX_wm_area(C);
2731         View3D *v3d= sa->spacedata.first;
2732         
2733         clear_id_newpoins();
2734                 
2735         CTX_DATA_BEGIN(C, Base*, base, selected_editable_bases) {
2736                 make_object_duplilist_real(scene, v3d, base);
2737         }
2738         CTX_DATA_END;
2739
2740         DAG_scene_sort(CTX_data_scene(C));
2741         ED_anim_dag_flush_update(C);    
2742         WM_event_add_notifier(C, NC_SCENE, CTX_data_scene(C));
2743         
2744         return OPERATOR_FINISHED;
2745 }
2746
2747 void OBJECT_OT_make_dupli_real(wmOperatorType *ot)
2748 {
2749         
2750         /* identifiers */
2751         ot->name= "Make Dupli Real";
2752         ot->idname= "OBJECT_OT_make_dupli_real";
2753         
2754         /* api callbacks */
2755         ot->invoke= WM_operator_confirm;
2756         ot->exec= object_make_dupli_real_exec;
2757         
2758         ot->poll= ED_operator_scene_editable;
2759         
2760         /* flags */
2761         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
2762 }
2763 /* ******************* Set Object Center ********************** */
2764
2765 static EnumPropertyItem prop_set_center_types[] = {
2766         {0, "CENTER", "ObData to Center", "Move object data around Object center"},
2767         {1, "CENTERNEW", "Center New", "Move Object center to center of object data"},
2768         {2, "CENTERCURSOR", "Center Cursor", "Move Object Center to position of the 3d cursor"},
2769         {0, NULL, NULL, NULL}
2770 };
2771
2772 /* 0 == do center, 1 == center new, 2 == center cursor */
2773 static int object_set_center_exec(bContext *C, wmOperator *op)
2774 {
2775         Scene *scene= CTX_data_scene(C);
2776         ScrArea *sa= CTX_wm_area(C);
2777         View3D *v3d= sa->spacedata.first;
2778         Object *obedit= CTX_data_edit_object(C);
2779         Object *ob;
2780         Mesh *me, *tme;
2781         Curve *cu;
2782 /*      BezTriple *bezt;
2783         BPoint *bp; */
2784         Nurb *nu, *nu1;
2785         EditVert *eve;
2786         float cent[3], centn[3], min[3], max[3], omat[3][3];
2787         int a, total= 0;
2788         int centermode = RNA_enum_get(op->ptr, "type");
2789         
2790         /* keep track of what is changed */
2791         int tot_change=0, tot_lib_error=0, tot_multiuser_arm_error=0;
2792         MVert *mvert;
2793
2794         if(scene->id.lib || v3d==NULL){
2795                 BKE_report(op->reports, RPT_ERROR, "Operation cannot be performed on Lib data");
2796                  return OPERATOR_CANCELLED;
2797         }
2798         if (obedit && centermode > 0) {
2799                 BKE_report(op->reports, RPT_ERROR, "Operation cannot be performed in EditMode");
2800                 return OPERATOR_CANCELLED;
2801         }       
2802         cent[0]= cent[1]= cent[2]= 0.0; 
2803         
2804         if(obedit) {
2805
2806                 INIT_MINMAX(min, max);
2807         
2808                 if(obedit->type==OB_MESH) {
2809                         Mesh *me= obedit->data;
2810                         
2811                         for(eve= me->edit_mesh->verts.first; eve; eve= eve->next) {
2812                                 if(v3d->around==V3D_CENTROID) {
2813                                         total++;
2814                                         VECADD(cent, cent, eve->co);
2815                                 }
2816                                 else {
2817                                         DO_MINMAX(eve->co, min, max);
2818                                 }
2819                         }
2820                         
2821                         if(v3d->around==V3D_CENTROID) {
2822                                 VecMulf(cent, 1.0f/(float)total);
2823                         }
2824                         else {
2825                                 cent[0]= (min[0]+max[0])/2.0f;
2826                                 cent[1]= (min[1]+max[1])/2.0f;
2827                                 cent[2]= (min[2]+max[2])/2.0f;
2828                         }
2829                         
2830                         for(eve= me->edit_mesh->verts.first; eve; eve= eve->next) {
2831                                 VecSubf(eve->co, eve->co, cent);                        
2832                         }
2833                         
2834                         recalc_editnormals(me->edit_mesh);
2835                         tot_change++;
2836                         DAG_object_flush_update(scene, obedit, OB_RECALC_DATA);
2837                 }
2838         }
2839         
2840         /* reset flags */
2841         CTX_DATA_BEGIN(C, Base*, base, selected_editable_bases) {
2842                         base->object->flag &= ~OB_DONE;
2843         }
2844         CTX_DATA_END;
2845         
2846         for (me= G.main->mesh.first; me; me= me->id.next) {
2847                 me->flag &= ~ME_ISDONE;
2848         }
2849         
2850         CTX_DATA_BEGIN(C, Base*, base, selected_editable_bases) {
2851                 if((base->object->flag & OB_DONE)==0) {
2852                         base->object->flag |= OB_DONE;
2853                                 
2854                         if(obedit==NULL && (me=get_mesh(base->object)) ) {
2855                                 if (me->id.lib) {
2856                                         tot_lib_error++;
2857                                 } else {
2858                                         if(centermode==2) {
2859                                                 VECCOPY(cent, give_cursor(scene, v3d));
2860                                                 Mat4Invert(base->object->imat, base->object->obmat);
2861                                                 Mat4MulVecfl(base->object->imat, cent);
2862                                         } else {
2863                                                 INIT_MINMAX(min, max);
2864                                                 mvert= me->mvert;
2865                                                 for(a=0; a<me->totvert; a++, mvert++) {
2866                                                         DO_MINMAX(mvert->co, min, max);
2867                                                 }
2868                                         
2869                                                 cent[0]= (min[0]+max[0])/2.0f;
2870                                                 cent[1]= (min[1]+max[1])/2.0f;
2871                                                 cent[2]= (min[2]+max[2])/2.0f;
2872                                         }
2873
2874                                         mvert= me->mvert;
2875                                         for(a=0; a<me->totvert; a++, mvert++) {
2876                                                 VecSubf(mvert->co, mvert->co, cent);
2877                                         }
2878                                         
2879                                         if (me->key) {
2880                                                 KeyBlock *kb;
2881                                                 for (kb=me->key->block.first; kb; kb=kb->next) {
2882                                                         float *fp= kb->data;
2883                                                         
2884                                                         for (a=0; a<kb->totelem; a++, fp+=3) {
2885                                                                 VecSubf(fp, fp, cent);
2886                                                         }
2887                                                 }
2888                                         }
2889                                                 
2890                                         me->flag |= ME_ISDONE;
2891                                                 
2892                                         if(centermode) {
2893                                                 Mat3CpyMat4(omat, base->object->obmat);
2894                                                 
2895                                                 VECCOPY(centn, cent);
2896                                                 Mat3MulVecfl(omat, centn);
2897                                                 base->object->loc[0]+= centn[0];
2898                                                 base->object->loc[1]+= centn[1];
2899                                                 base->object->loc[2]+= centn[2];
2900                                                 
2901                                                 where_is_object(scene, base->object);
2902                                                 ignore_parent_tx(scene, base->object);
2903                                                 
2904                                                 /* other users? */
2905                                                 CTX_DATA_BEGIN(C, Base*, base, selected_editable_bases) {
2906                                                         ob = base->object;
2907                                                         if((ob->flag & OB_DONE)==0) {
2908                                                                 tme= get_mesh(ob);
2909                                                                 
2910                                                                 if(tme==me) {
2911                                                                         
2912                                                                         ob->flag |= OB_DONE;
2913                                                                         ob->recalc= OB_RECALC_OB|OB_RECALC_DATA;
2914
2915                                                                         Mat3CpyMat4(omat, ob->obmat);
2916                                                                         VECCOPY(centn, cent);
2917                                                                         Mat3MulVecfl(omat, centn);
2918                                                                         ob->loc[0]+= centn[0];
2919                                                                         ob->loc[1]+= centn[1];
2920                                                                         ob->loc[2]+= centn[2];
2921                                                                         
2922                                                                         where_is_object(scene, ob);
2923                                                                         ignore_parent_tx(scene, ob);
2924                                                                         
2925                                                                         if(tme && (tme->flag & ME_ISDONE)==0) {
2926                                                                                 mvert= tme->mvert;
2927                                                                                 for(a=0; a<tme->totvert; a++, mvert++) {
2928                                                                                         VecSubf(mvert->co, mvert->co, cent);
2929                                                                                 }
2930                                                                                 
2931                                                                                 if (tme->key) {
2932                                                                                         KeyBlock *kb;
2933                                                                                         for (kb=tme->key->block.first; kb; kb=kb->next) {
2934                                                                                                 float *fp= kb->data;
2935                                                                                                 
2936                                                                                                 for (a=0; a<kb->totelem; a++, fp+=3) {
2937                                                                                                         VecSubf(fp, fp, cent);
2938                                                                                                 }
2939                                                                                         }
2940                                                                                 }
2941                                                                                 
2942                                                                                 tme->flag |= ME_ISDONE;
2943                                                                         }
2944                                                                 }
2945                                                         }
2946                                                         
2947                                                         ob= ob->id.next;
2948                                                 }
2949                                                 CTX_DATA_END;
2950                                         }
2951                                         tot_change++;
2952                                 }
2953                         }
2954                         else if (ELEM(base->object->type, OB_CURVE, OB_SURF)) {
2955                                 
2956                                 /* weak code here... (ton) */
2957                                 if(obedit==base->object) {
2958                                         ListBase *editnurb= curve_get_editcurve(obedit);
2959
2960                                         nu1= editnurb->first;
2961                                         cu= obedit->data;
2962                                 }
2963                                 else {
2964                                         cu= base->object->data;
2965                                         nu1= cu->nurb.first;
2966                                 }
2967                                 
2968                                 if (cu->id.lib) {
2969                                         tot_lib_error++;
2970                                 } else {
2971                                         if(centermode==2) {
2972                                                 VECCOPY(cent, give_cursor(scene, v3d));
2973                                                 Mat4Invert(base->object->imat, base->object->obmat);
2974                                                 Mat4MulVecfl(base->object->imat, cent);
2975
2976                                                 /* don't allow Z change if curve is 2D */
2977                                                 if( !( cu->flag & CU_3D ) )
2978                                                         cent[2] = 0.0;
2979                                         } 
2980                                         else {
2981                                                 INIT_MINMAX(min, max);
2982                                                 
2983                                                 nu= nu1;
2984                                                 while(nu) {
2985                                                         minmaxNurb(nu, min, max);
2986                                                         nu= nu->next;
2987                                                 }
2988                                                 
2989                                                 cent[0]= (min[0]+max[0])/2.0f;
2990                                                 cent[1]= (min[1]+max[1])/2.0f;
2991                                                 cent[2]= (min[2]+max[2])/2.0f;
2992                                         }
2993                                         
2994                                         nu= nu1;
2995                                         while(nu) {
2996                                                 if( (nu->type & 7)==1) {
2997                                                         a= nu->pntsu;
2998                                                         while (a--) {
2999                                                                 VecSubf(nu->bezt[a].vec[0], nu->bezt[a].vec[0], cent);
3000                                                                 VecSubf(nu->bezt[a].vec[1], nu->bezt[a].vec[1], cent);
3001                                                                 VecSubf(nu->bezt[a].vec[2], nu->bezt[a].vec[2], cent);
3002                                                         }
3003                                                 }
3004                                                 else {
3005                                                         a= nu->pntsu*nu->pntsv;