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