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