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