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