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