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