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