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