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