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