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