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