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