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