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