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