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