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