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