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