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