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