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