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