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