Fix bug #22317 View reamins in camera's view after camera is deleted.
[blender.git] / source / blender / editors / object / object_add.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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, 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
28 #include <stdlib.h>
29 #include <string.h>
30
31 #include "MEM_guardedalloc.h"
32
33 #include "DNA_curve_types.h"
34 #include "DNA_group_types.h"
35 #include "DNA_lamp_types.h"
36 #include "DNA_material_types.h"
37 #include "DNA_mesh_types.h"
38 #include "DNA_meta_types.h"
39 #include "DNA_object_fluidsim.h"
40 #include "DNA_object_force.h"
41 #include "DNA_scene_types.h"
42 #include "DNA_vfont_types.h"
43
44 #include "BLI_math.h"
45 #include "BLI_listbase.h"
46
47 #include "BKE_anim.h"
48 #include "BKE_armature.h"
49 #include "BKE_constraint.h"
50 #include "BKE_context.h"
51 #include "BKE_curve.h"
52 #include "BKE_customdata.h"
53 #include "BKE_depsgraph.h"
54 #include "BKE_DerivedMesh.h"
55 #include "BKE_displist.h"
56 #include "BKE_effect.h"
57 #include "BKE_global.h"
58 #include "BKE_group.h"
59 #include "BKE_lattice.h"
60 #include "BKE_library.h"
61 #include "BKE_main.h"
62 #include "BKE_material.h"
63 #include "BKE_mball.h"
64 #include "BKE_mesh.h"
65 #include "BKE_modifier.h"
66 #include "BKE_object.h"
67 #include "BKE_particle.h"
68 #include "BKE_report.h"
69 #include "BKE_sca.h"
70 #include "BKE_scene.h"
71 #include "BKE_texture.h"
72 #include "BKE_utildefines.h"
73
74 #include "RNA_access.h"
75 #include "RNA_define.h"
76 #include "RNA_enum_types.h"
77
78 #include "WM_api.h"
79 #include "WM_types.h"
80
81 #include "ED_armature.h"
82 #include "ED_curve.h"
83 #include "ED_mball.h"
84 #include "ED_mesh.h"
85 #include "ED_object.h"
86 #include "ED_render.h"
87 #include "ED_screen.h"
88 #include "ED_transform.h"
89 #include "ED_view3d.h"
90
91 #include "UI_interface.h"
92 #include "UI_resources.h"
93
94 #include "object_intern.h"
95
96 /************************** Exported *****************************/
97
98 void ED_object_location_from_view(bContext *C, float *loc)
99 {
100         View3D *v3d= CTX_wm_view3d(C);
101         Scene *scene= CTX_data_scene(C);
102         float *cursor;
103         
104         cursor = give_cursor(scene, v3d);
105
106         copy_v3_v3(loc, cursor);
107 }
108
109 void ED_object_rotation_from_view(bContext *C, float *rot)
110 {
111         RegionView3D *rv3d= ED_view3d_context_rv3d(C);
112         
113         if(rv3d) {
114                 rv3d->viewquat[0]= -rv3d->viewquat[0];
115                 quat_to_eul( rot, rv3d->viewquat);
116                 rv3d->viewquat[0]= -rv3d->viewquat[0];
117         }
118         else
119                 rot[0] = rot[1] = rot[2] = 0.f;
120 }
121
122 void ED_object_base_init_transform(bContext *C, Base *base, float *loc, float *rot)
123 {
124         Object *ob= base->object;
125         Scene *scene= CTX_data_scene(C);
126         
127         if (!scene) return;
128         
129         if (loc)
130                 copy_v3_v3(ob->loc, loc);
131         
132         if (rot)
133                 copy_v3_v3(ob->rot, rot);
134         
135         where_is_object(scene, ob);
136 }
137
138 /* uses context to figure out transform for primitive */
139 /* returns standard diameter */
140 float ED_object_new_primitive_matrix(bContext *C, float *loc, float *rot, float primmat[][4])
141 {
142         Object *obedit= CTX_data_edit_object(C);
143         View3D *v3d =CTX_wm_view3d(C);
144         float mat[3][3], rmat[3][3], cmat[3][3], imat[3][3];
145         
146         unit_m4(primmat);
147         
148         eul_to_mat3(rmat, rot);
149         invert_m3(rmat);
150         
151         /* inverse transform for initial rotation and object */
152         copy_m3_m4(mat, obedit->obmat);
153         mul_m3_m3m3(cmat, rmat, mat);
154         invert_m3_m3(imat, cmat);
155         copy_m4_m3(primmat, imat);
156         
157         /* center */
158         VECCOPY(primmat[3], loc);
159         VECSUB(primmat[3], primmat[3], obedit->obmat[3]);
160         invert_m3_m3(imat, mat);
161         mul_m3_v3(imat, primmat[3]);
162         
163         if(v3d) return v3d->grid;
164         return 1.0f;
165 }
166
167 /********************* Add Object Operator ********************/
168
169 void add_object_draw(Scene *scene, View3D *v3d, int type)       /* for toolbox or menus, only non-editmode stuff */
170 {
171         /* keep here to get things compile, remove later */
172 }
173
174 void ED_object_add_generic_props(wmOperatorType *ot, int do_editmode)
175 {
176         PropertyRNA *prop;
177         RNA_def_boolean(ot->srna, "view_align", 0, "Align to View", "Align the new object to the view.");
178
179         if(do_editmode) {
180                 prop= RNA_def_boolean(ot->srna, "enter_editmode", 0, "Enter Editmode", "Enter editmode when adding this object.");
181                 RNA_def_property_flag(prop, PROP_HIDDEN);
182         }
183         
184         RNA_def_float_vector(ot->srna, "location", 3, NULL, -FLT_MAX, FLT_MAX, "Location", "Location for the newly added object.", -FLT_MAX, FLT_MAX);
185         RNA_def_float_rotation(ot->srna, "rotation", 3, NULL, -FLT_MAX, FLT_MAX, "Rotation", "Rotation for the newly added object", -FLT_MAX, FLT_MAX);
186         
187         prop = RNA_def_boolean_layer_member(ot->srna, "layer", 32, NULL, "Layer", "");
188         RNA_def_property_flag(prop, PROP_HIDDEN);
189 }
190
191 static void object_add_generic_invoke_options(bContext *C, wmOperator *op)
192 {
193         if(RNA_struct_find_property(op->ptr, "enter_editmode")) /* optional */
194                 if (!RNA_property_is_set(op->ptr, "enter_editmode"))
195                         RNA_boolean_set(op->ptr, "enter_editmode", U.flag & USER_ADD_EDITMODE);
196         
197         if (!RNA_property_is_set(op->ptr, "location")) {
198                 float loc[3];
199                 
200                 ED_object_location_from_view(C, loc);
201                 RNA_float_set_array(op->ptr, "location", loc);
202         }
203          
204         if (!RNA_property_is_set(op->ptr, "layer")) {
205                 View3D *v3d = CTX_wm_view3d(C);
206                 Scene *scene = CTX_data_scene(C);
207                 int a, values[32], layer;
208                 
209                 if (v3d) {
210                         if (v3d->localvd) {
211                                 layer = v3d->layact + v3d->lay;
212                                 for(a=0; a<32; a++)
213                                         values[a]= (layer & (1<<a));
214                         } else {
215                                 layer = (v3d->scenelock)?scene->layact:v3d->layact;
216
217                                 for(a=0; a<32; a++)
218                                         values[a]= (layer & (1<<a));
219                         }
220                 } else {
221                         layer = scene->layact;
222                         for(a=0; a<32; a++)
223                                 values[a]= (layer & (1<<a));
224                 }
225                 
226                 RNA_boolean_set_array(op->ptr, "layer", values);
227         }
228 }
229
230 int ED_object_add_generic_invoke(bContext *C, wmOperator *op, wmEvent *event)
231 {
232         object_add_generic_invoke_options(C, op);
233         return op->type->exec(C, op);
234 }
235
236 void ED_object_add_generic_get_opts(bContext *C, wmOperator *op, float *loc, float *rot, int *enter_editmode, unsigned int *layer)
237 {
238         int a, layer_values[32];
239         int view_align;
240         
241         *enter_editmode = FALSE;
242         if(RNA_struct_find_property(op->ptr, "enter_editmode") && RNA_boolean_get(op->ptr, "enter_editmode")) {
243                 *enter_editmode = TRUE;
244         }
245         
246         if (RNA_property_is_set(op->ptr, "view_align"))
247                 view_align = RNA_boolean_get(op->ptr, "view_align");
248         else
249                 view_align = U.flag & USER_ADD_VIEWALIGNED;
250
251         if (view_align)
252                 ED_object_rotation_from_view(C, rot);
253         else
254                 RNA_float_get_array(op->ptr, "rotation", rot);
255         
256
257         RNA_float_get_array(op->ptr, "location", loc);
258         RNA_boolean_get_array(op->ptr, "layer", layer_values);
259         
260         for(a=0; a<32; a++)
261                 if(layer_values[a])
262                         *layer |= (1 << a);
263                 else
264                         *layer &= ~(1 << a);
265 }
266
267 /* for object add primitive operators */
268 Object *ED_object_add_type(bContext *C, int type, float *loc, float *rot, int enter_editmode, unsigned int layer)
269 {
270         Scene *scene= CTX_data_scene(C);
271         Object *ob;
272         
273         /* for as long scene has editmode... */
274         if (CTX_data_edit_object(C)) 
275                 ED_object_exit_editmode(C, EM_FREEDATA|EM_FREEUNDO|EM_WAITCURSOR|EM_DO_UNDO); /* freedata, and undo */
276         
277         /* deselects all, sets scene->basact */
278         ob= add_object(scene, type);
279         BASACT->lay = ob->lay = layer;
280         /* editor level activate, notifiers */
281         ED_base_object_activate(C, BASACT);
282
283         /* more editor stuff */
284         ED_object_base_init_transform(C, BASACT, loc, rot);
285
286         DAG_scene_sort(scene);
287         ED_render_id_flush_update(G.main, ob->data);
288
289         if(enter_editmode)
290                 ED_object_enter_editmode(C, EM_IGNORE_LAYER);
291
292         return ob;
293 }
294
295 /* for object add operator */
296 static int object_add_exec(bContext *C, wmOperator *op)
297 {
298         int enter_editmode;
299         unsigned int layer;
300         float loc[3], rot[3];
301         
302         ED_object_add_generic_get_opts(C, op, loc, rot, &enter_editmode, &layer);
303         ED_object_add_type(C, RNA_enum_get(op->ptr, "type"), loc, rot, enter_editmode, layer);
304         
305         return OPERATOR_FINISHED;
306 }
307
308 void OBJECT_OT_add(wmOperatorType *ot)
309 {
310         /* identifiers */
311         ot->name= "Add Object";
312         ot->description = "Add an object to the scene";
313         ot->idname= "OBJECT_OT_add";
314         
315         /* api callbacks */
316         ot->invoke= ED_object_add_generic_invoke;
317         ot->exec= object_add_exec;
318         
319         ot->poll= ED_operator_scene_editable;
320         
321         /* flags */
322         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
323         
324         RNA_def_enum(ot->srna, "type", object_type_items, 0, "Type", "");
325
326         ED_object_add_generic_props(ot, TRUE);
327 }
328
329 /********************* Add Effector Operator ********************/
330 /* copy from rna_object_force.c*/
331 static EnumPropertyItem field_type_items[] = {
332         {PFIELD_FORCE, "FORCE", ICON_FORCE_FORCE, "Force", ""},
333         {PFIELD_WIND, "WIND", ICON_FORCE_WIND, "Wind", ""},
334         {PFIELD_VORTEX, "VORTEX", ICON_FORCE_VORTEX, "Vortex", ""},
335         {PFIELD_MAGNET, "MAGNET", ICON_FORCE_MAGNETIC, "Magnetic", ""},
336         {PFIELD_HARMONIC, "HARMONIC", ICON_FORCE_HARMONIC, "Harmonic", ""},
337         {PFIELD_CHARGE, "CHARGE", ICON_FORCE_CHARGE, "Charge", ""},
338         {PFIELD_LENNARDJ, "LENNARDJ", ICON_FORCE_LENNARDJONES, "Lennard-Jones", ""},
339         {PFIELD_TEXTURE, "TEXTURE", ICON_FORCE_TEXTURE, "Texture", ""},
340         {PFIELD_GUIDE, "GUIDE", ICON_FORCE_CURVE, "Curve Guide", ""},
341         {PFIELD_BOID, "BOID", ICON_FORCE_BOID, "Boid", ""},
342         {PFIELD_TURBULENCE, "TURBULENCE", ICON_FORCE_TURBULENCE, "Turbulence", ""},
343         {PFIELD_DRAG, "DRAG", ICON_FORCE_DRAG, "Drag", ""},
344         {0, NULL, 0, NULL, NULL}};
345
346 void add_effector_draw(Scene *scene, View3D *v3d, int type)     /* for toolbox or menus, only non-editmode stuff */
347 {
348         /* keep here to get things compile, remove later */
349 }
350
351 /* for effector add primitive operators */
352 static Object *effector_add_type(bContext *C, wmOperator *op, int type)
353 {
354         Object *ob;
355         int enter_editmode;
356         unsigned int layer;
357         float loc[3], rot[3];
358         float mat[4][4];
359         
360         object_add_generic_invoke_options(C, op);
361         ED_object_add_generic_get_opts(C, op, loc, rot, &enter_editmode, &layer);
362
363         if(type==PFIELD_GUIDE) {
364                 ob= ED_object_add_type(C, OB_CURVE, loc, rot, FALSE, layer);
365                 rename_id(&ob->id, "CurveGuide");
366
367                 ((Curve*)ob->data)->flag |= CU_PATH|CU_3D;
368                 ED_object_enter_editmode(C, 0);
369                 ED_object_new_primitive_matrix(C, loc, rot, mat);
370                 BLI_addtail(curve_get_editcurve(ob), add_nurbs_primitive(C, mat, CU_NURBS|CU_PRIM_PATH, 1));
371
372                 if(!enter_editmode)
373                         ED_object_exit_editmode(C, EM_FREEDATA);
374         }
375         else {
376                 ob= ED_object_add_type(C, OB_EMPTY, loc, rot, FALSE, layer);
377                 rename_id(&ob->id, "Field");
378
379                 switch(type) {
380                         case PFIELD_WIND:
381                         case PFIELD_VORTEX:
382                                 ob->empty_drawtype = OB_SINGLE_ARROW;
383                                 break;
384                 }
385         }
386
387         ob->pd= object_add_collision_fields(type);
388
389         return ob;
390 }
391
392 /* for object add operator */
393 static int effector_add_exec(bContext *C, wmOperator *op)
394 {
395         effector_add_type(C, op, RNA_int_get(op->ptr, "type"));
396         
397         return OPERATOR_FINISHED;
398 }
399
400 void OBJECT_OT_effector_add(wmOperatorType *ot)
401 {
402         /* identifiers */
403         ot->name= "Add Effector";
404         ot->description = "Add an empty object with a physics effector to the scene";
405         ot->idname= "OBJECT_OT_effector_add";
406         
407         /* api callbacks */
408         ot->invoke= WM_menu_invoke;
409         ot->exec= effector_add_exec;
410         
411         ot->poll= ED_operator_scene_editable;
412         
413         /* flags */
414         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
415         
416         ot->prop= RNA_def_enum(ot->srna, "type", field_type_items, 0, "Type", "");
417
418         ED_object_add_generic_props(ot, TRUE);
419 }
420
421 /* ***************** Add Camera *************** */
422
423 static int object_camera_add_exec(bContext *C, wmOperator *op)
424 {
425         View3D *v3d = CTX_wm_view3d(C);
426         Scene *scene= CTX_data_scene(C);
427         Object *ob;
428         int enter_editmode;
429         unsigned int layer;
430         float loc[3], rot[3];
431         
432         /* force view align for cameras */
433         RNA_boolean_set(op->ptr, "view_align", 1);
434         
435         object_add_generic_invoke_options(C, op);
436         ED_object_add_generic_get_opts(C, op, loc, rot, &enter_editmode, &layer);
437
438         ob= ED_object_add_type(C, OB_CAMERA, loc, rot, FALSE, layer);
439         
440         if (v3d) {
441                 if (v3d->camera == NULL)
442                         v3d->camera = ob;
443                 if (v3d->scenelock && scene->camera==NULL) {
444                         scene->camera = ob;
445                 }
446         }
447
448         return OPERATOR_FINISHED;
449 }
450
451 void OBJECT_OT_camera_add(wmOperatorType *ot)
452 {
453         /* identifiers */
454         ot->name= "Add Camera";
455         ot->description = "Add a camera object to the scene";
456         ot->idname= "OBJECT_OT_camera_add";
457         
458         /* api callbacks */
459         ot->exec= object_camera_add_exec;
460         ot->poll= ED_operator_scene_editable;
461         
462         /* flags */
463         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
464                 
465         ED_object_add_generic_props(ot, TRUE);
466 }
467
468
469 /* ***************** add primitives *************** */
470
471 static EnumPropertyItem prop_surface_types[]= {
472         {CU_PRIM_CURVE|CU_NURBS, "NURBS_CURVE", ICON_SURFACE_NCURVE, "NURBS Curve", ""},
473         {CU_PRIM_CIRCLE|CU_NURBS, "NURBS_CIRCLE", ICON_SURFACE_NCIRCLE, "NURBS Circle", ""},
474         {CU_PRIM_PATCH|CU_NURBS, "NURBS_SURFACE", ICON_SURFACE_NSURFACE, "NURBS Surface", ""},
475         {CU_PRIM_TUBE|CU_NURBS, "NURBS_TUBE", ICON_SURFACE_NTUBE, "NURBS Tube", ""},
476         {CU_PRIM_SPHERE|CU_NURBS, "NURBS_SPHERE", ICON_SURFACE_NSPHERE, "NURBS Sphere", ""},
477         {CU_PRIM_DONUT|CU_NURBS, "NURBS_DONUT", ICON_SURFACE_NDONUT, "NURBS Donut", ""},
478         {0, NULL, 0, NULL, NULL}
479 };
480
481 static int object_add_surface_exec(bContext *C, wmOperator *op)
482 {
483         Object *obedit= CTX_data_edit_object(C);
484         ListBase *editnurb;
485         Nurb *nu;
486         int newob= 0;
487         int enter_editmode;
488         unsigned int layer;
489         float loc[3], rot[3];
490         float mat[4][4];
491         
492         object_add_generic_invoke_options(C, op); // XXX these props don't get set right when only exec() is called
493         ED_object_add_generic_get_opts(C, op, loc, rot, &enter_editmode, &layer);
494         
495         if(obedit==NULL || obedit->type!=OB_SURF) {
496                 obedit= ED_object_add_type(C, OB_SURF, loc, rot, TRUE, layer);
497                 newob = 1;
498         }
499         else DAG_id_flush_update(&obedit->id, OB_RECALC_DATA);
500         
501         ED_object_new_primitive_matrix(C, loc, rot, mat);
502         
503         nu= add_nurbs_primitive(C, mat, RNA_enum_get(op->ptr, "type"), newob);
504         editnurb= curve_get_editcurve(obedit);
505         BLI_addtail(editnurb, nu);
506         
507         /* userdef */
508         if (newob && !enter_editmode) {
509                 ED_object_exit_editmode(C, EM_FREEDATA);
510         }
511         
512         WM_event_add_notifier(C, NC_OBJECT|ND_DRAW, obedit);
513         
514         return OPERATOR_FINISHED;
515 }
516
517 void OBJECT_OT_surface_add(wmOperatorType *ot)
518 {
519         /* identifiers */
520         ot->name= "Add Surface";
521         ot->description = "Add a surface object to the scene";
522         ot->idname= "OBJECT_OT_surface_add";
523         
524         /* api callbacks */
525         ot->invoke= WM_menu_invoke;
526         ot->exec= object_add_surface_exec;
527         
528         ot->poll= ED_operator_scene_editable;
529         
530         /* flags */
531         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
532         
533         ot->prop= RNA_def_enum(ot->srna, "type", prop_surface_types, 0, "Primitive", "");
534         ED_object_add_generic_props(ot, TRUE);
535 }
536
537 static EnumPropertyItem prop_metaball_types[]= {
538         {MB_BALL, "MBALL_BALL", ICON_META_BALL, "Meta Ball", ""},
539         {MB_TUBE, "MBALL_TUBE", ICON_META_TUBE, "Meta Tube", ""},
540         {MB_PLANE, "MBALL_PLANE", ICON_META_PLANE, "Meta Plane", ""},
541         {MB_CUBE, "MBALL_CUBE", ICON_META_CUBE, "Meta Cube", ""},
542         {MB_ELIPSOID, "MBALL_ELLIPSOID", ICON_META_ELLIPSOID, "Meta Ellipsoid", ""},
543         {0, NULL, 0, NULL, NULL}
544 };
545
546 static int object_metaball_add_exec(bContext *C, wmOperator *op)
547 {
548         Object *obedit= CTX_data_edit_object(C);
549         MetaBall *mball;
550         MetaElem *elem;
551         int newob= 0;
552         int enter_editmode;
553         unsigned int layer;
554         float loc[3], rot[3];
555         float mat[4][4];
556         
557         object_add_generic_invoke_options(C, op); // XXX these props don't get set right when only exec() is called
558         ED_object_add_generic_get_opts(C, op, loc, rot, &enter_editmode, &layer);
559         
560         if(obedit==NULL || obedit->type!=OB_MBALL) {
561                 obedit= ED_object_add_type(C, OB_MBALL, loc, rot, TRUE, layer);
562                 newob = 1;
563         }
564         else DAG_id_flush_update(&obedit->id, OB_RECALC_DATA);
565         
566         ED_object_new_primitive_matrix(C, loc, rot, mat);
567         
568         elem= (MetaElem*)add_metaball_primitive(C, mat, RNA_enum_get(op->ptr, "type"), newob);
569         mball= (MetaBall*)obedit->data;
570         BLI_addtail(mball->editelems, elem);
571         
572         /* userdef */
573         if (newob && !enter_editmode) {
574                 ED_object_exit_editmode(C, EM_FREEDATA);
575         }
576         
577         WM_event_add_notifier(C, NC_OBJECT|ND_DRAW, obedit);
578         
579         return OPERATOR_FINISHED;
580 }
581
582 static int object_metaball_add_invoke(bContext *C, wmOperator *op, wmEvent *event)
583 {
584         Object *obedit= CTX_data_edit_object(C);
585         uiPopupMenu *pup;
586         uiLayout *layout;
587
588         object_add_generic_invoke_options(C, op);
589
590         pup= uiPupMenuBegin(C, op->type->name, 0);
591         layout= uiPupMenuLayout(pup);
592         if(!obedit || obedit->type == OB_MBALL)
593                 uiItemsEnumO(layout, op->type->idname, "type");
594         else
595                 uiItemsEnumO(layout, "OBJECT_OT_metaball_add", "type");
596         uiPupMenuEnd(C, pup);
597
598         return OPERATOR_CANCELLED;
599 }
600
601 void OBJECT_OT_metaball_add(wmOperatorType *ot)
602 {
603         /* identifiers */
604         ot->name= "Add Metaball";
605         ot->description= "Add an metaball object to the scene";
606         ot->idname= "OBJECT_OT_metaball_add";
607
608         /* api callbacks */
609         ot->invoke= object_metaball_add_invoke;
610         ot->exec= object_metaball_add_exec;
611         ot->poll= ED_operator_scene_editable;
612
613         /* flags */
614         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
615         
616         RNA_def_enum(ot->srna, "type", prop_metaball_types, 0, "Primitive", "");
617         ED_object_add_generic_props(ot, TRUE);
618 }
619
620 static int object_add_text_exec(bContext *C, wmOperator *op)
621 {
622         Object *obedit= CTX_data_edit_object(C);
623         int enter_editmode;
624         unsigned int layer;
625         float loc[3], rot[3];
626         
627         object_add_generic_invoke_options(C, op); // XXX these props don't get set right when only exec() is called
628         ED_object_add_generic_get_opts(C, op, loc, rot, &enter_editmode, &layer);
629         
630         if(obedit && obedit->type==OB_FONT)
631                 return OPERATOR_CANCELLED;
632
633         obedit= ED_object_add_type(C, OB_FONT, loc, rot, enter_editmode, layer);
634         
635         WM_event_add_notifier(C, NC_OBJECT|ND_DRAW, obedit);
636         
637         return OPERATOR_FINISHED;
638 }
639
640 void OBJECT_OT_text_add(wmOperatorType *ot)
641 {
642         /* identifiers */
643         ot->name= "Add Text";
644         ot->description = "Add a text object to the scene";
645         ot->idname= "OBJECT_OT_text_add";
646         
647         /* api callbacks */
648         ot->invoke= ED_object_add_generic_invoke;
649         ot->exec= object_add_text_exec;
650         ot->poll= ED_operator_scene_editable;
651         
652         /* flags */
653         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
654         ED_object_add_generic_props(ot, TRUE);
655 }
656
657 static int object_armature_add_exec(bContext *C, wmOperator *op)
658 {
659         Object *obedit= CTX_data_edit_object(C);
660         View3D *v3d= CTX_wm_view3d(C);
661         RegionView3D *rv3d= NULL;
662         int newob= 0;
663         int enter_editmode;
664         unsigned int layer;
665         float loc[3], rot[3];
666         
667         object_add_generic_invoke_options(C, op); // XXX these props don't get set right when only exec() is called
668         ED_object_add_generic_get_opts(C, op, loc, rot, &enter_editmode, &layer);
669         
670         if ((obedit==NULL) || (obedit->type != OB_ARMATURE)) {
671                 obedit= ED_object_add_type(C, OB_ARMATURE, loc, rot, TRUE, layer);
672                 ED_object_enter_editmode(C, 0);
673                 newob = 1;
674         }
675         else DAG_id_flush_update(&obedit->id, OB_RECALC_DATA);
676         
677         if(obedit==NULL) {
678                 BKE_report(op->reports, RPT_ERROR, "Cannot create editmode armature.");
679                 return OPERATOR_CANCELLED;
680         }
681         
682         if(v3d) 
683                 rv3d= CTX_wm_region(C)->regiondata;
684         
685         /* v3d and rv3d are allowed to be NULL */
686         add_primitive_bone(CTX_data_scene(C), v3d, rv3d);
687
688         /* userdef */
689         if (newob && !enter_editmode)
690                 ED_object_exit_editmode(C, EM_FREEDATA);
691         
692         WM_event_add_notifier(C, NC_OBJECT|ND_DRAW, obedit);
693         
694         return OPERATOR_FINISHED;
695 }
696
697 void OBJECT_OT_armature_add(wmOperatorType *ot)
698 {       
699         /* identifiers */
700         ot->name= "Add Armature";
701         ot->description = "Add an armature object to the scene";
702         ot->idname= "OBJECT_OT_armature_add";
703         
704         /* api callbacks */
705         ot->invoke= ED_object_add_generic_invoke;
706         ot->exec= object_armature_add_exec;
707         ot->poll= ED_operator_scene_editable;
708         
709         /* flags */
710         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
711         ED_object_add_generic_props(ot, TRUE);
712 }
713
714 static int object_lamp_add_exec(bContext *C, wmOperator *op)
715 {
716         Object *ob;
717         int type= RNA_enum_get(op->ptr, "type");
718         int enter_editmode;
719         unsigned int layer;
720         float loc[3], rot[3];
721         
722         object_add_generic_invoke_options(C, op);
723         ED_object_add_generic_get_opts(C, op, loc, rot, &enter_editmode, &layer);
724
725         ob= ED_object_add_type(C, OB_LAMP, loc, rot, FALSE, layer);
726         if(ob && ob->data)
727                 ((Lamp*)ob->data)->type= type;
728         
729         return OPERATOR_FINISHED;
730 }
731
732 void OBJECT_OT_lamp_add(wmOperatorType *ot)
733 {       
734         static EnumPropertyItem lamp_type_items[] = {
735                 {LA_LOCAL, "POINT", ICON_LAMP_POINT, "Point", "Omnidirectional point light source"},
736                 {LA_SUN, "SUN", ICON_LAMP_SUN, "Sun", "Constant direction parallel ray light source"},
737                 {LA_SPOT, "SPOT", ICON_LAMP_SPOT, "Spot", "Directional cone light source"},
738                 {LA_HEMI, "HEMI", ICON_LAMP_HEMI, "Hemi", "180 degree constant light source"},
739                 {LA_AREA, "AREA", ICON_LAMP_AREA, "Area", "Directional area light source"},
740                 {0, NULL, 0, NULL, NULL}};
741
742         /* identifiers */
743         ot->name= "Add Lamp";
744         ot->description = "Add a lamp object to the scene";
745         ot->idname= "OBJECT_OT_lamp_add";
746         
747         /* api callbacks */
748         ot->invoke= WM_menu_invoke;
749         ot->exec= object_lamp_add_exec;
750         ot->poll= ED_operator_scene_editable;
751         
752         /* flags */
753         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
754
755         /* properties */
756         ot->prop= RNA_def_enum(ot->srna, "type", lamp_type_items, 0, "Type", "");
757
758         ED_object_add_generic_props(ot, FALSE);
759 }
760
761 static int group_instance_add_exec(bContext *C, wmOperator *op)
762 {
763         Group *group= BLI_findlink(&CTX_data_main(C)->group, RNA_enum_get(op->ptr, "group"));
764
765         int enter_editmode;
766         unsigned int layer;
767         float loc[3], rot[3];
768         
769         object_add_generic_invoke_options(C, op);
770         ED_object_add_generic_get_opts(C, op, loc, rot, &enter_editmode, &layer);
771
772         if(group) {
773                 Object *ob= ED_object_add_type(C, OB_EMPTY, loc, rot, FALSE, layer);
774                 rename_id(&ob->id, group->id.name+2);
775                 ob->dup_group= group;
776                 ob->transflag |= OB_DUPLIGROUP;
777                 id_us_plus(&group->id);
778
779
780                 WM_event_add_notifier(C, NC_OBJECT|ND_DRAW, ob);
781
782                 return OPERATOR_FINISHED;
783         }
784
785         return OPERATOR_CANCELLED;
786 }
787
788 /* only used as menu */
789 void OBJECT_OT_group_instance_add(wmOperatorType *ot)
790 {
791         PropertyRNA *prop;
792
793         /* identifiers */
794         ot->name= "Add Group Instance";
795         ot->description = "Add a dupligroup instance";
796         ot->idname= "OBJECT_OT_group_instance_add";
797
798         /* api callbacks */
799         ot->invoke= WM_enum_search_invoke;
800         ot->exec= group_instance_add_exec;
801
802         ot->poll= ED_operator_scene_editable;
803
804         /* flags */
805         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
806
807         /* properties */
808         prop= RNA_def_enum(ot->srna, "group", DummyRNA_NULL_items, 0, "Group", "");
809         RNA_def_enum_funcs(prop, RNA_group_itemf);
810         ot->prop= prop;
811         ED_object_add_generic_props(ot, FALSE);
812 }
813
814 /**************************** Delete Object *************************/
815
816 /* remove base from a specific scene */
817 /* note: now unlinks constraints as well */
818 void ED_base_object_free_and_unlink(Scene *scene, Base *base)
819 {
820         BLI_remlink(&scene->base, base);
821         free_libblock_us(&G.main->object, base->object);
822         if(scene->basact==base) scene->basact= NULL;
823         MEM_freeN(base);
824 }
825
826 static int object_delete_exec(bContext *C, wmOperator *op)
827 {
828         Scene *scene= CTX_data_scene(C);
829         View3D *v3d = CTX_wm_view3d(C);
830         RegionView3D *rv3d= CTX_wm_region_view3d(C);
831         int islamp= 0;
832         
833         if(CTX_data_edit_object(C)) 
834                 return OPERATOR_CANCELLED;
835         
836         CTX_DATA_BEGIN(C, Base*, base, selected_bases) {
837
838                 if(base->object->type==OB_LAMP) islamp= 1;
839                 else if (base->object->type == OB_CAMERA) {
840                         /* If we don't reset this, Blender crash
841                          * in fly mode because still have the
842                          * old object here!.
843                          * See Bug #22317
844                          */
845                         if (v3d && rv3d && rv3d->persp == RV3D_CAMOB && base->object == v3d->camera) {
846                                 rv3d->persp= RV3D_PERSP;
847                                 v3d->camera= NULL;
848                         }
849                 }
850                 /* remove from current scene only */
851                 ED_base_object_free_and_unlink(scene, base);
852         }
853         CTX_DATA_END;
854
855         if(islamp) reshadeall_displist(scene);  /* only frees displist */
856
857         DAG_scene_sort(scene);
858         DAG_ids_flush_update(0);
859         
860         WM_event_add_notifier(C, NC_SCENE|ND_OB_ACTIVE, CTX_data_scene(C));
861         
862         return OPERATOR_FINISHED;
863 }
864
865 void OBJECT_OT_delete(wmOperatorType *ot)
866 {
867         /* identifiers */
868         ot->name= "Delete";
869         ot->description = "Delete selected objects";
870         ot->idname= "OBJECT_OT_delete";
871         
872         /* api callbacks */
873         ot->invoke= WM_operator_confirm;
874         ot->exec= object_delete_exec;
875         ot->poll= ED_operator_scene_editable;
876         
877         /* flags */
878         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
879 }
880
881 /**************************** Copy Utilities ******************************/
882
883 static void copy_object__forwardModifierLinks(void *userData, Object *ob,
884                                                                                           ID **idpoin)
885 {
886         /* this is copied from ID_NEW; it might be better to have a macro */
887         if(*idpoin && (*idpoin)->newid) *idpoin = (*idpoin)->newid;
888 }
889
890 /* after copying objects, copied data should get new pointers */
891 static void copy_object_set_idnew(bContext *C, int dupflag)
892 {
893         Material *ma, *mao;
894         ID *id;
895         int a;
896         
897         /* XXX check object pointers */
898         CTX_DATA_BEGIN(C, Object*, ob, selected_editable_objects) {
899                 relink_constraints(&ob->constraints);
900                 if (ob->pose){
901                         bPoseChannel *chan;
902                         for (chan = ob->pose->chanbase.first; chan; chan=chan->next){
903                                 relink_constraints(&chan->constraints);
904                         }
905                 }
906                 modifiers_foreachIDLink(ob, copy_object__forwardModifierLinks, NULL);
907                 ID_NEW(ob->parent);
908                 ID_NEW(ob->proxy);
909                 ID_NEW(ob->proxy_group);
910         }
911         CTX_DATA_END;
912         
913         /* materials */
914         if( dupflag & USER_DUP_MAT) {
915                 mao= G.main->mat.first;
916                 while(mao) {
917                         if(mao->id.newid) {
918                                 
919                                 ma= (Material *)mao->id.newid;
920                                 
921                                 if(dupflag & USER_DUP_TEX) {
922                                         for(a=0; a<MAX_MTEX; a++) {
923                                                 if(ma->mtex[a]) {
924                                                         id= (ID *)ma->mtex[a]->tex;
925                                                         if(id) {
926                                                                 ID_NEW_US(ma->mtex[a]->tex)
927                                                                 else ma->mtex[a]->tex= copy_texture(ma->mtex[a]->tex);
928                                                                 id->us--;
929                                                         }
930                                                 }
931                                         }
932                                 }
933 #if 0 // XXX old animation system
934                                 id= (ID *)ma->ipo;
935                                 if(id) {
936                                         ID_NEW_US(ma->ipo)
937                                         else ma->ipo= copy_ipo(ma->ipo);
938                                         id->us--;
939                                 }
940 #endif // XXX old animation system
941                         }
942                         mao= mao->id.next;
943                 }
944         }
945         
946 #if 0 // XXX old animation system
947         /* lamps */
948         if( dupflag & USER_DUP_IPO) {
949                 Lamp *la= G.main->lamp.first;
950                 while(la) {
951                         if(la->id.newid) {
952                                 Lamp *lan= (Lamp *)la->id.newid;
953                                 id= (ID *)lan->ipo;
954                                 if(id) {
955                                         ID_NEW_US(lan->ipo)
956                                         else lan->ipo= copy_ipo(lan->ipo);
957                                         id->us--;
958                                 }
959                         }
960                         la= la->id.next;
961                 }
962         }
963         
964         /* ipos */
965         ipo= G.main->ipo.first;
966         while(ipo) {
967                 if(ipo->id.lib==NULL && ipo->id.newid) {
968                         Ipo *ipon= (Ipo *)ipo->id.newid;
969                         IpoCurve *icu;
970                         for(icu= ipon->curve.first; icu; icu= icu->next) {
971                                 if(icu->driver) {
972                                         ID_NEW(icu->driver->ob);
973                                 }
974                         }
975                 }
976                 ipo= ipo->id.next;
977         }
978 #endif // XXX old animation system
979         
980         set_sca_new_poins();
981         
982         clear_id_newpoins();
983 }
984
985 /********************* Make Duplicates Real ************************/
986
987 static void make_object_duplilist_real(bContext *C, Scene *scene, Base *base)
988 {
989         Base *basen;
990         Object *ob;
991         ListBase *lb;
992         DupliObject *dob;
993         
994         if(!base && !(base = BASACT))
995                 return;
996         
997         if(!(base->object->transflag & OB_DUPLI))
998                 return;
999         
1000         lb= object_duplilist(scene, base->object);
1001         
1002         for(dob= lb->first; dob; dob= dob->next) {
1003                 ob= copy_object(dob->ob);
1004                 /* font duplis can have a totcol without material, we get them from parent
1005                 * should be implemented better...
1006                 */
1007                 if(ob->mat==NULL) ob->totcol= 0;
1008                 
1009                 basen= MEM_dupallocN(base);
1010                 basen->flag &= ~OB_FROMDUPLI;
1011                 basen->lay= base->lay;
1012                 BLI_addhead(&scene->base, basen);       /* addhead: othwise eternal loop */
1013                 basen->object= ob;
1014                 ob->ipo= NULL;          /* make sure apply works */
1015                 ob->parent= ob->track= NULL;
1016                 ob->disp.first= ob->disp.last= NULL;
1017                 ob->transflag &= ~OB_DUPLI;     
1018                 ob->lay= base->lay;
1019                 
1020                 copy_m4_m4(ob->obmat, dob->mat);
1021                 object_apply_mat4(ob, ob->obmat);
1022         }
1023         
1024         copy_object_set_idnew(C, 0);
1025         
1026         free_object_duplilist(lb);
1027         
1028         base->object->transflag &= ~OB_DUPLI;   
1029 }
1030
1031 static int object_duplicates_make_real_exec(bContext *C, wmOperator *op)
1032 {
1033         Scene *scene= CTX_data_scene(C);
1034         
1035         clear_id_newpoins();
1036                 
1037         CTX_DATA_BEGIN(C, Base*, base, selected_editable_bases) {
1038                 make_object_duplilist_real(C, scene, base);
1039         }
1040         CTX_DATA_END;
1041
1042         DAG_scene_sort(scene);
1043         DAG_ids_flush_update(0);
1044         WM_event_add_notifier(C, NC_SCENE, scene);
1045         
1046         return OPERATOR_FINISHED;
1047 }
1048
1049 void OBJECT_OT_duplicates_make_real(wmOperatorType *ot)
1050 {
1051         
1052         /* identifiers */
1053         ot->name= "Make Duplicates Real";
1054         ot->description = "Make dupli objects attached to this object real";
1055         ot->idname= "OBJECT_OT_duplicates_make_real";
1056         
1057         /* api callbacks */
1058         ot->exec= object_duplicates_make_real_exec;
1059         
1060         ot->poll= ED_operator_scene_editable;
1061         
1062         /* flags */
1063         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
1064 }
1065
1066 /**************************** Convert **************************/
1067
1068 static EnumPropertyItem convert_target_items[]= {
1069         {OB_CURVE, "CURVE", ICON_OUTLINER_OB_CURVE, "Curve from Mesh/Text", ""},
1070         {OB_MESH, "MESH", ICON_OUTLINER_OB_MESH, "Mesh from Curve/Meta/Surf/Text", ""},
1071         {0, NULL, 0, NULL, NULL}};
1072
1073 static void curvetomesh(Scene *scene, Object *ob) 
1074 {
1075         Curve *cu= ob->data;
1076         
1077         if(cu->disp.first==0)
1078                 makeDispListCurveTypes(scene, ob, 0); /* force creation */
1079
1080         nurbs_to_mesh(ob); /* also does users */
1081
1082         if(ob->type == OB_MESH)
1083                 object_free_modifiers(ob);
1084 }
1085
1086 static int convert_poll(bContext *C)
1087 {
1088         Object *obact= CTX_data_active_object(C);
1089         Scene *scene= CTX_data_scene(C);
1090
1091         return (!scene->id.lib && obact && scene->obedit != obact && (obact->flag & SELECT) && !(obact->id.lib));
1092 }
1093
1094 /* Helper for convert_exec */
1095 static Base *duplibase_for_convert(Scene *scene, Base *base, Object *ob)
1096 {
1097         Object *obn;
1098         Base *basen;
1099
1100         if (ob == NULL) {
1101                 ob= base->object;
1102         }
1103
1104         obn= copy_object(ob);
1105         obn->recalc |= OB_RECALC;
1106
1107         basen= MEM_mallocN(sizeof(Base), "duplibase");
1108         *basen= *base;
1109         BLI_addhead(&scene->base, basen);       /* addhead: otherwise eternal loop */
1110         basen->object= obn;
1111         basen->flag |= SELECT;
1112         obn->flag |= SELECT;
1113         base->flag &= ~SELECT;
1114         ob->flag &= ~SELECT;
1115
1116         return basen;
1117 }
1118
1119 static int convert_exec(bContext *C, wmOperator *op)
1120 {
1121         Scene *scene= CTX_data_scene(C);
1122         Base *basen=NULL, *basact=NULL, *basedel=NULL;
1123         Object *ob, *ob1, *newob, *obact= CTX_data_active_object(C);
1124         DerivedMesh *dm;
1125         Curve *cu;
1126         Nurb *nu;
1127         MetaBall *mb;
1128         Mesh *me;
1129         int target= RNA_enum_get(op->ptr, "target");
1130         int keep_original= RNA_boolean_get(op->ptr, "keep_original");
1131         int a, mballConverted= 0;
1132
1133         /* don't forget multiple users! */
1134
1135         /* reset flags */
1136         CTX_DATA_BEGIN(C, Base*, base, selected_editable_bases) {
1137                 ob= base->object;
1138                 ob->flag &= ~OB_DONE;
1139         }
1140         CTX_DATA_END;
1141
1142         CTX_DATA_BEGIN(C, Base*, base, selected_editable_bases) {
1143                 ob= base->object;
1144
1145                 if(ob->flag & OB_DONE) {
1146                         if (ob->type != target) {
1147                                 base->flag &= ~SELECT;
1148                                 ob->flag &= ~SELECT;
1149                         }
1150                 }
1151                 else if (ob->type==OB_MESH && target == OB_CURVE) {
1152                         ob->flag |= OB_DONE;
1153
1154                         if (keep_original) {
1155                                 basen= duplibase_for_convert(scene, base, NULL);
1156                                 newob= basen->object;
1157
1158                                 /* decrement original mesh's usage count  */
1159                                 me= newob->data;
1160                                 me->id.us--;
1161
1162                                 /* make a new copy of the mesh */
1163                                 newob->data= copy_mesh(me);
1164                         } else {
1165                                 newob = ob;
1166                         }
1167
1168                         mesh_to_curve(scene, newob);
1169
1170                         if(newob->type==OB_CURVE)
1171                                 object_free_modifiers(newob);   /* after derivedmesh calls! */
1172                 }
1173                 else if(ob->type==OB_MESH && ob->modifiers.first) { /* converting a mesh with no modifiers causes a segfault */
1174                         ob->flag |= OB_DONE;
1175
1176                         if (keep_original) {
1177                                 basen= duplibase_for_convert(scene, base, NULL);
1178                                 newob= basen->object;
1179
1180                                 /* decrement original mesh's usage count  */
1181                                 me= newob->data;
1182                                 me->id.us--;
1183
1184                                 /* make a new copy of the mesh */
1185                                 newob->data= copy_mesh(me);
1186                         } else {
1187                                 newob = ob;
1188                                 ob->recalc |= OB_RECALC;
1189                         }
1190
1191                         /* make new mesh data from the original copy */
1192                         /* note: get the mesh from the original, not from the copy in some
1193                          * cases this doesnt give correct results (when MDEF is used for eg)
1194                          */
1195                         dm= mesh_get_derived_final(scene, newob, CD_MASK_MESH);
1196                         /* dm= mesh_create_derived_no_deform(ob1, NULL);        this was called original (instead of get_derived). man o man why! (ton) */
1197
1198                         DM_to_mesh(dm, newob->data);
1199
1200                         dm->release(dm);
1201                         object_free_modifiers(newob);   /* after derivedmesh calls! */
1202                 }
1203                 else if(ob->type==OB_FONT) {
1204                         ob->flag |= OB_DONE;
1205
1206                         if (keep_original) {
1207                                 basen= duplibase_for_convert(scene, base, NULL);
1208                                 newob= basen->object;
1209
1210                                 /* decrement original curve's usage count  */
1211                                 ((Curve *)newob->data)->id.us--;
1212
1213                                 /* make a new copy of the curve */
1214                                 newob->data= copy_curve(ob->data);
1215                         } else {
1216                                 newob= ob;
1217                         }
1218
1219                         cu= newob->data;
1220
1221                         if (!cu->disp.first)
1222                                 makeDispListCurveTypes(scene, newob, 0);
1223
1224                         newob->type= OB_CURVE;
1225
1226                         if(cu->vfont) {
1227                                 cu->vfont->id.us--;
1228                                 cu->vfont= 0;
1229                         }
1230                         if(cu->vfontb) {
1231                                 cu->vfontb->id.us--;
1232                                 cu->vfontb= 0;
1233                         }
1234                         if(cu->vfonti) {
1235                                 cu->vfonti->id.us--;
1236                                 cu->vfonti= 0;
1237                         }
1238                         if(cu->vfontbi) {
1239                                 cu->vfontbi->id.us--;
1240                                 cu->vfontbi= 0;
1241                         }
1242
1243                         if (!keep_original) {
1244                                 /* other users */
1245                                 if(cu->id.us>1) {
1246                                         for(ob1= G.main->object.first; ob1; ob1=ob1->id.next) {
1247                                                 if(ob1->data==ob->data) {
1248                                                         ob1->type= OB_CURVE;
1249                                                         ob1->recalc |= OB_RECALC;
1250                                                 }
1251                                         }
1252                                 }
1253                         }
1254
1255                         for(nu=cu->nurb.first; nu; nu=nu->next)
1256                                 nu->charidx= 0;
1257
1258                         if(target == OB_MESH)
1259                                 curvetomesh(scene, newob);
1260                 }
1261                 else if(ELEM(ob->type, OB_CURVE, OB_SURF)) {
1262                         ob->flag |= OB_DONE;
1263
1264                         if(target == OB_MESH) {
1265                                 if (keep_original) {
1266                                         basen= duplibase_for_convert(scene, base, NULL);
1267                                         newob= basen->object;
1268
1269                                         /* decrement original curve's usage count  */
1270                                         ((Curve *)newob->data)->id.us--;
1271
1272                                         /* make a new copy of the curve */
1273                                         newob->data= copy_curve(ob->data);
1274                                 } else {
1275                                         newob= ob;
1276                                 }
1277
1278                                 curvetomesh(scene, newob);
1279                         }
1280                 }
1281                 else if(ob->type==OB_MBALL) {
1282                         Object *baseob;
1283
1284                         if (target != OB_MESH) {
1285                                 ob->flag |= OB_DONE;
1286                                 continue;
1287                         }
1288
1289                         base->flag &= ~SELECT;
1290                         ob->flag &= ~SELECT;
1291
1292                         baseob= find_basis_mball(scene, ob);
1293
1294                         if (ob != baseob) {
1295                                 /* if motherball is converting it would be marked as done later */
1296                                 ob->flag |= OB_DONE;
1297                         }
1298
1299                         if (!baseob->disp.first) {
1300                                 makeDispListMBall(scene, baseob);
1301                         }
1302
1303                         if(!(baseob->flag & OB_DONE)) {
1304                                 baseob->flag |= OB_DONE;
1305
1306                                 basen= duplibase_for_convert(scene, base, baseob);
1307                                 newob= basen->object;
1308
1309                                 mb= newob->data;
1310                                 mb->id.us--;
1311
1312                                 newob->data= add_mesh("Mesh");
1313                                 newob->type= OB_MESH;
1314
1315                                 me= newob->data;
1316                                 me->totcol= mb->totcol;
1317                                 if(newob->totcol) {
1318                                         me->mat= MEM_dupallocN(mb->mat);
1319                                         for(a=0; a<newob->totcol; a++) id_us_plus((ID *)me->mat[a]);
1320                                 }
1321
1322                                 mball_to_mesh(&baseob->disp, newob->data);
1323
1324                                 if (obact->type == OB_MBALL) {
1325                                         basact= basen;
1326                                 }
1327
1328                                 mballConverted= 1;
1329                         }
1330                         else
1331                                 continue;
1332                 }
1333                 else
1334                         continue;
1335
1336                 /* If the original object is active then make this object active */
1337                 if(basen) {
1338                         if(ob == obact) {
1339                                 /* store new active base to update BASACT */
1340                                 basact= basen;
1341                         }
1342
1343                         basen= NULL;
1344                 }
1345
1346                 if (!keep_original) {
1347                         DAG_id_flush_update(&ob->id, OB_RECALC_DATA);
1348                 }
1349
1350                 /* delete original if needed */
1351                 if(basedel) {
1352                         if(!keep_original)
1353                                 ED_base_object_free_and_unlink(scene, basedel); 
1354
1355                         basedel = NULL;
1356                 }
1357         }
1358         CTX_DATA_END;
1359
1360         if(!keep_original) {
1361                 if (mballConverted) {
1362                         Base *base= scene->base.first, *tmpbase;
1363                         while (base) {
1364                                 ob= base->object;
1365                                 tmpbase= base;
1366                                 base= base->next;
1367
1368                                 if (ob->type == OB_MBALL) {
1369                                         ED_base_object_free_and_unlink(scene, tmpbase);
1370                                 }
1371                         }
1372                 }
1373
1374                 /* delete object should renew depsgraph */
1375                 DAG_scene_sort(scene);
1376         }
1377
1378 // XXX  ED_object_enter_editmode(C, 0);
1379 // XXX  exit_editmode(C, EM_FREEDATA|EM_WAITCURSOR); /* freedata, but no undo */
1380
1381         if (basact) {
1382                 /* active base was changed */
1383                 ED_base_object_activate(C, basact);
1384                 BASACT= basact;
1385         } else if (BASACT->object->flag & OB_DONE) {
1386                 WM_event_add_notifier(C, NC_OBJECT|ND_MODIFIER|ND_DATA, BASACT->object);
1387         }
1388
1389         DAG_scene_sort(scene);
1390         WM_event_add_notifier(C, NC_SCENE|NC_OBJECT|ND_DRAW, scene); /* is NC_SCENE needed ? */
1391
1392         return OPERATOR_FINISHED;
1393 }
1394
1395
1396 void OBJECT_OT_convert(wmOperatorType *ot)
1397 {
1398         /* identifiers */
1399         ot->name= "Convert";
1400         ot->description = "Convert selected objects to another type";
1401         ot->idname= "OBJECT_OT_convert";
1402         
1403         /* api callbacks */
1404         ot->invoke= WM_menu_invoke;
1405         ot->exec= convert_exec;
1406         ot->poll= convert_poll;
1407         
1408         /* flags */
1409         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
1410
1411         /* properties */
1412         ot->prop= RNA_def_enum(ot->srna, "target", convert_target_items, OB_MESH, "Target", "Type of object to convert to.");
1413         RNA_def_boolean(ot->srna, "keep_original", 0, "Keep Original", "Keep original objects instead of replacing them.");
1414 }
1415
1416 /**************************** Duplicate ************************/
1417
1418 /* 
1419         dupflag: a flag made from constants declared in DNA_userdef_types.h
1420         The flag tells adduplicate() weather to copy data linked to the object, or to reference the existing data.
1421         U.dupflag for default operations or you can construct a flag as python does
1422         if the dupflag is 0 then no data will be copied (linked duplicate) */
1423
1424 /* used below, assumes id.new is correct */
1425 /* leaves selection of base/object unaltered */
1426 static Base *object_add_duplicate_internal(Scene *scene, Base *base, int dupflag)
1427 {
1428         Base *basen= NULL;
1429         Material ***matarar;
1430         Object *ob, *obn;
1431         ID *id;
1432         int a, didit;
1433
1434         ob= base->object;
1435         if(ob->mode & OB_MODE_POSE) {
1436                 ; /* nothing? */
1437         }
1438         else {
1439                 obn= copy_object(ob);
1440                 obn->recalc |= OB_RECALC;
1441                 
1442                 basen= MEM_mallocN(sizeof(Base), "duplibase");
1443                 *basen= *base;
1444                 BLI_addhead(&scene->base, basen);       /* addhead: prevent eternal loop */
1445                 basen->object= obn;
1446                 
1447                 if(basen->flag & OB_FROMGROUP) {
1448                         Group *group;
1449                         for(group= G.main->group.first; group; group= group->id.next) {
1450                                 if(object_in_group(ob, group))
1451                                         add_to_group(group, obn, scene, basen);
1452                         }
1453                 }
1454                 
1455                 /* duplicates using userflags */
1456 #if 0 // XXX old animation system                               
1457                 if(dupflag & USER_DUP_IPO) {
1458                         bConstraintChannel *chan;
1459                         id= (ID *)obn->ipo;
1460                         
1461                         if(id) {
1462                                 ID_NEW_US( obn->ipo)
1463                                 else obn->ipo= copy_ipo(obn->ipo);
1464                                 id->us--;
1465                         }
1466                         /* Handle constraint ipos */
1467                         for (chan=obn->constraintChannels.first; chan; chan=chan->next){
1468                                 id= (ID *)chan->ipo;
1469                                 if(id) {
1470                                         ID_NEW_US( chan->ipo)
1471                                         else chan->ipo= copy_ipo(chan->ipo);
1472                                         id->us--;
1473                                 }
1474                         }
1475                 }
1476                 if(dupflag & USER_DUP_ACT){ /* Not buttons in the UI to modify this, add later? */
1477                         id= (ID *)obn->action;
1478                         if (id){
1479                                 ID_NEW_US(obn->action)
1480                                 else{
1481                                         obn->action= copy_action(obn->action);
1482                                 }
1483                                 id->us--;
1484                         }
1485                 }
1486 #endif // XXX old animation system
1487                 if(dupflag & USER_DUP_MAT) {
1488                         for(a=0; a<obn->totcol; a++) {
1489                                 id= (ID *)obn->mat[a];
1490                                 if(id) {
1491                                         ID_NEW_US(obn->mat[a])
1492                                         else obn->mat[a]= copy_material(obn->mat[a]);
1493                                         id->us--;
1494                                 }
1495                         }
1496                 }
1497                 if(dupflag & USER_DUP_PSYS) {
1498                         ParticleSystem *psys;
1499                         for(psys=obn->particlesystem.first; psys; psys=psys->next) {
1500                                 id= (ID*) psys->part;
1501                                 if(id) {
1502                                         ID_NEW_US(psys->part)
1503                                         else psys->part= psys_copy_settings(psys->part);
1504                                         id->us--;
1505                                 }
1506                         }
1507                 }
1508                 
1509                 id= obn->data;
1510                 didit= 0;
1511                 
1512                 switch(obn->type) {
1513                         case OB_MESH:
1514                                 if(dupflag & USER_DUP_MESH) {
1515                                         ID_NEW_US2( obn->data )
1516                                         else {
1517                                                 obn->data= copy_mesh(obn->data);
1518                                                 
1519                                                 if(obn->fluidsimSettings) {
1520                                                         obn->fluidsimSettings->orgMesh = (Mesh *)obn->data;
1521                                                 }
1522                                                 
1523                                                 didit= 1;
1524                                         }
1525                                         id->us--;
1526                                 }
1527                                 break;
1528                         case OB_CURVE:
1529                                 if(dupflag & USER_DUP_CURVE) {
1530                                         ID_NEW_US2(obn->data )
1531                                         else {
1532                                                 obn->data= copy_curve(obn->data);
1533                                                 didit= 1;
1534                                         }
1535                                         id->us--;
1536                                 }
1537                                 break;
1538                         case OB_SURF:
1539                                 if(dupflag & USER_DUP_SURF) {
1540                                         ID_NEW_US2( obn->data )
1541                                         else {
1542                                                 obn->data= copy_curve(obn->data);
1543                                                 didit= 1;
1544                                         }
1545                                         id->us--;
1546                                 }
1547                                 break;
1548                         case OB_FONT:
1549                                 if(dupflag & USER_DUP_FONT) {
1550                                         ID_NEW_US2( obn->data )
1551                                         else {
1552                                                 obn->data= copy_curve(obn->data);
1553                                                 didit= 1;
1554                                         }
1555                                         id->us--;
1556                                 }
1557                                 break;
1558                         case OB_MBALL:
1559                                 if(dupflag & USER_DUP_MBALL) {
1560                                         ID_NEW_US2(obn->data )
1561                                         else {
1562                                                 obn->data= copy_mball(obn->data);
1563                                                 didit= 1;
1564                                         }
1565                                         id->us--;
1566                                 }
1567                                 break;
1568                         case OB_LAMP:
1569                                 if(dupflag & USER_DUP_LAMP) {
1570                                         ID_NEW_US2(obn->data )
1571                                         else obn->data= copy_lamp(obn->data);
1572                                         id->us--;
1573                                 }
1574                                 break;
1575                                 
1576                         case OB_ARMATURE:
1577                                 obn->recalc |= OB_RECALC_DATA;
1578                                 if(obn->pose) obn->pose->flag |= POSE_RECALC;
1579                                         
1580                                         if(dupflag & USER_DUP_ARM) {
1581                                                 ID_NEW_US2(obn->data )
1582                                                 else {
1583                                                         obn->data= copy_armature(obn->data);
1584                                                         armature_rebuild_pose(obn, obn->data);
1585                                                         didit= 1;
1586                                                 }
1587                                                 id->us--;
1588                                         }
1589                                                 
1590                                                 break;
1591                                 
1592                         case OB_LATTICE:
1593                                 if(dupflag!=0) {
1594                                         ID_NEW_US2(obn->data )
1595                                         else obn->data= copy_lattice(obn->data);
1596                                         id->us--;
1597                                 }
1598                                 break;
1599                         case OB_CAMERA:
1600                                 if(dupflag!=0) {
1601                                         ID_NEW_US2(obn->data )
1602                                         else obn->data= copy_camera(obn->data);
1603                                         id->us--;
1604                                 }
1605                                 break;
1606                 }
1607                 
1608                 if(dupflag & USER_DUP_MAT) {
1609                         matarar= give_matarar(obn);
1610                         if(didit && matarar) {
1611                                 for(a=0; a<obn->totcol; a++) {
1612                                         id= (ID *)(*matarar)[a];
1613                                         if(id) {
1614                                                 ID_NEW_US( (*matarar)[a] )
1615                                                 else (*matarar)[a]= copy_material((*matarar)[a]);
1616                                                 
1617                                                 id->us--;
1618                                         }
1619                                 }
1620                         }
1621                 }
1622         }
1623         return basen;
1624 }
1625
1626 /* single object duplicate, if dupflag==0, fully linked, else it uses the flags given */
1627 /* leaves selection of base/object unaltered */
1628 Base *ED_object_add_duplicate(Scene *scene, Base *base, int dupflag)
1629 {
1630         Base *basen;
1631         Object *ob;
1632
1633         clear_id_newpoins();
1634         clear_sca_new_poins();  /* sensor/contr/act */
1635
1636         basen= object_add_duplicate_internal(scene, base, dupflag);
1637         if (basen == NULL) {
1638                 return NULL;
1639         }
1640
1641         ob= basen->object;
1642
1643         DAG_scene_sort(scene);
1644         ED_render_id_flush_update(G.main, ob->data);
1645
1646         return basen;
1647 }
1648
1649 /* contextual operator dupli */
1650 static int duplicate_exec(bContext *C, wmOperator *op)
1651 {
1652         Scene *scene= CTX_data_scene(C);
1653         int linked= RNA_boolean_get(op->ptr, "linked");
1654         int dupflag= (linked)? 0: U.dupflag;
1655         
1656         clear_id_newpoins();
1657         clear_sca_new_poins();  /* sensor/contr/act */
1658         
1659         CTX_DATA_BEGIN(C, Base*, base, selected_editable_bases) {
1660                 Base *basen= object_add_duplicate_internal(scene, base, dupflag);
1661                 
1662                 /* note that this is safe to do with this context iterator,
1663                    the list is made in advance */
1664                 ED_base_object_select(base, BA_DESELECT);
1665
1666                 if (basen == NULL) {
1667                         continue;
1668                 }
1669
1670                 /* new object becomes active */
1671                 if(BASACT==base)
1672                         ED_base_object_activate(C, basen);
1673                 
1674                 ED_render_id_flush_update(G.main, basen->object->data);
1675         }
1676         CTX_DATA_END;
1677
1678         copy_object_set_idnew(C, dupflag);
1679
1680         DAG_scene_sort(scene);
1681         DAG_ids_flush_update(0);
1682
1683         WM_event_add_notifier(C, NC_SCENE|ND_OB_SELECT, scene);
1684
1685         return OPERATOR_FINISHED;
1686 }
1687
1688 void OBJECT_OT_duplicate(wmOperatorType *ot)
1689 {
1690         PropertyRNA *prop;
1691         
1692         /* identifiers */
1693         ot->name= "Duplicate";
1694         ot->description = "Duplicate selected objects";
1695         ot->idname= "OBJECT_OT_duplicate";
1696         
1697         /* api callbacks */
1698         ot->exec= duplicate_exec;
1699         ot->poll= ED_operator_scene_editable;
1700         
1701         /* flags */
1702         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
1703         
1704         /* to give to transform */
1705         RNA_def_boolean(ot->srna, "linked", 0, "Linked", "Duplicate object but not object data, linking to the original data.");
1706         prop= RNA_def_int(ot->srna, "mode", TFM_TRANSLATION, 0, INT_MAX, "Mode", "", 0, INT_MAX);
1707         RNA_def_property_flag(prop, PROP_HIDDEN);
1708 }
1709
1710 /* **************** add named object, for dragdrop ************* */
1711
1712 /* contextual operator dupli */
1713
1714 static int add_named_poll(bContext *C)
1715 {
1716         if(!ED_operator_scene_editable(C)) {
1717                 return 0;
1718         } else {
1719                 Object *ob= CTX_data_active_object(C);
1720                 if(ob && ob->mode != OB_MODE_OBJECT)
1721                         return 0;
1722                 else
1723                         return 1;
1724         }
1725 }
1726
1727 static int add_named_exec(bContext *C, wmOperator *op)
1728 {
1729         Scene *scene= CTX_data_scene(C);
1730         Base *basen, *base;
1731         Object *ob;
1732         int linked= RNA_boolean_get(op->ptr, "linked");
1733         int dupflag= (linked)? 0: U.dupflag;
1734         char name[32];
1735
1736         /* find object, create fake base */
1737         RNA_string_get(op->ptr, "name", name);
1738         ob= (Object *)find_id("OB", name);
1739         if(ob==NULL) 
1740                 return OPERATOR_CANCELLED;
1741
1742         base= MEM_callocN(sizeof(Base), "duplibase");
1743         base->object= ob;
1744         base->flag= ob->flag;
1745
1746         /* prepare dupli */
1747         clear_id_newpoins();
1748         clear_sca_new_poins();  /* sensor/contr/act */
1749
1750         basen= object_add_duplicate_internal(scene, base, dupflag);
1751
1752         if (basen == NULL) {
1753                 MEM_freeN(base);
1754                 return OPERATOR_CANCELLED;
1755         }
1756
1757         basen->lay= basen->object->lay= scene->lay;
1758
1759         ED_object_location_from_view(C, basen->object->loc);
1760         ED_base_object_activate(C, basen);
1761
1762         copy_object_set_idnew(C, dupflag);
1763
1764         DAG_scene_sort(scene);
1765         DAG_ids_flush_update(0);
1766
1767         MEM_freeN(base);
1768
1769         WM_event_add_notifier(C, NC_SCENE|ND_OB_SELECT, scene);
1770
1771         return OPERATOR_FINISHED;
1772 }
1773
1774 void OBJECT_OT_add_named(wmOperatorType *ot)
1775 {
1776         /* identifiers */
1777         ot->name= "Add Named Object";
1778         ot->description = "Add named object";
1779         ot->idname= "OBJECT_OT_add_named";
1780         
1781         /* api callbacks */
1782         ot->exec= add_named_exec;
1783         ot->poll= add_named_poll;
1784         
1785         /* flags */
1786         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
1787         
1788         RNA_def_boolean(ot->srna, "linked", 0, "Linked", "Duplicate object but not object data, linking to the original data.");
1789         RNA_def_string(ot->srna, "name", "Cube", 24, "Name", "Object name to add.");
1790 }
1791
1792
1793
1794 /**************************** Join *************************/
1795 static int join_poll(bContext *C)
1796 {
1797         Object *ob= CTX_data_active_object(C);
1798         
1799         if (!ob || ob->id.lib) return 0;
1800         
1801         if (ELEM4(ob->type, OB_MESH, OB_CURVE, OB_SURF, OB_ARMATURE))
1802                 return ED_operator_screenactive(C);
1803         else
1804                 return 0;
1805 }
1806
1807
1808 static int join_exec(bContext *C, wmOperator *op)
1809 {
1810         Scene *scene= CTX_data_scene(C);
1811         Object *ob= CTX_data_active_object(C);
1812
1813         if(scene->obedit) {
1814                 BKE_report(op->reports, RPT_ERROR, "This data does not support joining in editmode.");
1815                 return OPERATOR_CANCELLED;
1816         }
1817         else if(object_data_is_libdata(ob)) {
1818                 BKE_report(op->reports, RPT_ERROR, "Can't edit external libdata.");
1819                 return OPERATOR_CANCELLED;
1820         }
1821
1822         if(ob->type == OB_MESH)
1823                 return join_mesh_exec(C, op);
1824         else if(ELEM(ob->type, OB_CURVE, OB_SURF))
1825                 return join_curve_exec(C, op);
1826         else if(ob->type == OB_ARMATURE)
1827                 return join_armature_exec(C, op);
1828         
1829         return OPERATOR_CANCELLED;
1830 }
1831
1832 void OBJECT_OT_join(wmOperatorType *ot)
1833 {
1834         /* identifiers */
1835         ot->name= "Join";
1836         ot->description = "Join selected objects into active object";
1837         ot->idname= "OBJECT_OT_join";
1838         
1839         /* api callbacks */
1840         ot->exec= join_exec;
1841         ot->poll= join_poll;
1842         
1843         /* flags */
1844         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
1845 }
1846
1847 /**************************** Join as Shape Key*************************/
1848 static int join_shapes_poll(bContext *C)
1849 {
1850         Object *ob= CTX_data_active_object(C);
1851         
1852         if (!ob || ob->id.lib) return 0;
1853         
1854         /* only meshes supported at the moment */
1855         if (ob->type == OB_MESH)
1856                 return ED_operator_screenactive(C);
1857         else
1858                 return 0;
1859 }
1860
1861 static int join_shapes_exec(bContext *C, wmOperator *op)
1862 {
1863         Scene *scene= CTX_data_scene(C);
1864         Object *ob= CTX_data_active_object(C);
1865         
1866         if(scene->obedit) {
1867                 BKE_report(op->reports, RPT_ERROR, "This data does not support joining in editmode.");
1868                 return OPERATOR_CANCELLED;
1869         }
1870         else if(object_data_is_libdata(ob)) {
1871                 BKE_report(op->reports, RPT_ERROR, "Can't edit external libdata.");
1872                 return OPERATOR_CANCELLED;
1873         }
1874         
1875         if(ob->type == OB_MESH)
1876                 return join_mesh_shapes_exec(C, op);
1877         
1878         return OPERATOR_CANCELLED;
1879 }
1880
1881 void OBJECT_OT_join_shapes(wmOperatorType *ot)
1882 {
1883         /* identifiers */
1884         ot->name= "Join as Shapes";
1885         ot->description = "Merge selected objects to shapes of active object";
1886         ot->idname= "OBJECT_OT_join_shapes";
1887         
1888         /* api callbacks */
1889         ot->exec= join_shapes_exec;
1890         ot->poll= join_shapes_poll;
1891         
1892         /* flags */
1893         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
1894 }