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