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