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