use ICON_NULL define rather then 0, makes UI calls less confusing. (no functional...
[blender.git] / source / blender / editors / object / object_relations.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 <stdio.h>
29 #include <stdlib.h>
30 #include <string.h>
31
32 #include "MEM_guardedalloc.h"
33
34 #include "DNA_anim_types.h"
35 #include "DNA_constraint_types.h"
36 #include "DNA_group_types.h"
37 #include "DNA_lamp_types.h"
38 #include "DNA_lattice_types.h"
39 #include "DNA_material_types.h"
40 #include "DNA_meta_types.h"
41 #include "DNA_particle_types.h"
42 #include "DNA_scene_types.h"
43 #include "DNA_world_types.h"
44 #include "DNA_object_types.h"
45
46 #include "BLI_math.h"
47 #include "BLI_editVert.h"
48 #include "BLI_listbase.h"
49 #include "BLI_string.h"
50
51 #include "BKE_action.h"
52 #include "BKE_animsys.h"
53 #include "BKE_armature.h"
54 #include "BKE_context.h"
55 #include "BKE_constraint.h"
56 #include "BKE_curve.h"
57 #include "BKE_depsgraph.h"
58 #include "BKE_displist.h"
59 #include "BKE_global.h"
60 #include "BKE_fcurve.h"
61 #include "BKE_lattice.h"
62 #include "BKE_library.h"
63 #include "BKE_main.h"
64 #include "BKE_material.h"
65 #include "BKE_mball.h"
66 #include "BKE_mesh.h"
67 #include "BKE_modifier.h"
68 #include "BKE_object.h"
69 #include "BKE_report.h"
70 #include "BKE_sca.h"
71 #include "BKE_scene.h"
72 #include "BKE_texture.h"
73
74 #include "WM_api.h"
75 #include "WM_types.h"
76
77 #include "UI_interface.h"
78 #include "UI_resources.h"
79
80 #include "RNA_access.h"
81 #include "RNA_define.h"
82 #include "RNA_enum_types.h"
83
84 #include "ED_armature.h"
85 #include "ED_curve.h"
86 #include "ED_keyframing.h"
87 #include "ED_object.h"
88 #include "ED_screen.h"
89 #include "ED_view3d.h"
90
91 #include "object_intern.h"
92
93 /*********************** Make Vertex Parent Operator ************************/
94
95 static int vertex_parent_set_poll(bContext *C)
96 {
97         return ED_operator_editmesh(C) || ED_operator_editsurfcurve(C) || ED_operator_editlattice(C);
98 }
99
100 static int vertex_parent_set_exec(bContext *C, wmOperator *op)
101 {
102         Main *bmain= CTX_data_main(C);
103         Scene *scene= CTX_data_scene(C);
104         Object *obedit= CTX_data_edit_object(C);
105         EditVert *eve;
106         Curve *cu;
107         Nurb *nu;
108         BezTriple *bezt;
109         BPoint *bp;
110         Object *par;
111         int a, v1=0, v2=0, v3=0, v4=0, nr=1;
112         
113         /* we need 1 to 3 selected vertices */
114         
115         if(obedit->type==OB_MESH) {
116                 Mesh *me= obedit->data;
117                 EditMesh *em = BKE_mesh_get_editmesh(me);
118
119                 eve= em->verts.first;
120                 while(eve) {
121                         if(eve->f & 1) {
122                                 if(v1==0) v1= nr;
123                                 else if(v2==0) v2= nr;
124                                 else if(v3==0) v3= nr;
125                                 else if(v4==0) v4= nr;
126                                 else break;
127                         }
128                         nr++;
129                         eve= eve->next;
130                 }
131
132                 BKE_mesh_end_editmesh(me, em);
133         }
134         else if(ELEM(obedit->type, OB_SURF, OB_CURVE)) {
135                 ListBase *editnurb= curve_get_editcurve(obedit);
136                 
137                 cu= obedit->data;
138
139                 nu= editnurb->first;
140                 while(nu) {
141                         if(nu->type == CU_BEZIER) {
142                                 bezt= nu->bezt;
143                                 a= nu->pntsu;
144                                 while(a--) {
145                                         if(BEZSELECTED_HIDDENHANDLES(cu, bezt)) {
146                                                 if(v1==0) v1= nr;
147                                                 else if(v2==0) v2= nr;
148                                                 else if(v3==0) v3= nr;
149                                                 else if(v4==0) v4= nr;
150                                                 else break;
151                                         }
152                                         nr++;
153                                         bezt++;
154                                 }
155                         }
156                         else {
157                                 bp= nu->bp;
158                                 a= nu->pntsu*nu->pntsv;
159                                 while(a--) {
160                                         if(bp->f1 & SELECT) {
161                                                 if(v1==0) v1= nr;
162                                                 else if(v2==0) v2= nr;
163                                                 else if(v3==0) v3= nr;
164                                                 else if(v4==0) v4= nr;
165                                                 else break;
166                                         }
167                                         nr++;
168                                         bp++;
169                                 }
170                         }
171                         nu= nu->next;
172                 }
173         }
174         else if(obedit->type==OB_LATTICE) {
175                 Lattice *lt= obedit->data;
176                 
177                 a= lt->editlatt->latt->pntsu*lt->editlatt->latt->pntsv*lt->editlatt->latt->pntsw;
178                 bp= lt->editlatt->latt->def;
179                 while(a--) {
180                         if(bp->f1 & SELECT) {
181                                 if(v1==0) v1= nr;
182                                 else if(v2==0) v2= nr;
183                                 else if(v3==0) v3= nr;
184                                 else if(v4==0) v4= nr;
185                                 else break;
186                         }
187                         nr++;
188                         bp++;
189                 }
190         }
191         
192         if(v4 || !((v1 && v2==0 && v3==0) || (v1 && v2 && v3)) ) {
193                 BKE_report(op->reports, RPT_ERROR, "Select either 1 or 3 vertices to parent to");
194                 return OPERATOR_CANCELLED;
195         }
196         
197         CTX_DATA_BEGIN(C, Object*, ob, selected_editable_objects) {
198                 if(ob != obedit) {
199                         ob->recalc |= OB_RECALC_OB|OB_RECALC_DATA|OB_RECALC_TIME;
200                         par= obedit->parent;
201                         
202                         while(par) {
203                                 if(par==ob) break;
204                                 par= par->parent;
205                         }
206                         if(par) {
207                                 BKE_report(op->reports, RPT_ERROR, "Loop in parents");
208                         }
209                         else {
210                                 Object workob;
211                                 
212                                 ob->parent= BASACT->object;
213                                 if(v3) {
214                                         ob->partype= PARVERT3;
215                                         ob->par1= v1-1;
216                                         ob->par2= v2-1;
217                                         ob->par3= v3-1;
218
219                                         /* inverse parent matrix */
220                                         what_does_parent(scene, ob, &workob);
221                                         invert_m4_m4(ob->parentinv, workob.obmat);
222                                 }
223                                 else {
224                                         ob->partype= PARVERT1;
225                                         ob->par1= v1-1;
226
227                                         /* inverse parent matrix */
228                                         what_does_parent(scene, ob, &workob);
229                                         invert_m4_m4(ob->parentinv, workob.obmat);
230                                 }
231                         }
232                 }
233         }
234         CTX_DATA_END;
235         
236         DAG_scene_sort(bmain, scene);
237
238         WM_event_add_notifier(C, NC_OBJECT, NULL);
239
240         return OPERATOR_FINISHED;
241 }
242
243 void OBJECT_OT_vertex_parent_set(wmOperatorType *ot)
244 {
245         /* identifiers */
246         ot->name= "Make Vertex Parent";
247         ot->description = "Parent selected objects to the selected vertices";
248         ot->idname= "OBJECT_OT_vertex_parent_set";
249         
250         /* api callbacks */
251         ot->invoke= WM_operator_confirm;
252         ot->poll= vertex_parent_set_poll;
253         ot->exec= vertex_parent_set_exec;
254         
255         /* flags */
256         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
257 }
258
259 /********************** Make Proxy Operator *************************/
260
261 /* set the object to proxify */
262 static int make_proxy_invoke (bContext *C, wmOperator *op, wmEvent *evt)
263 {
264         Scene *scene= CTX_data_scene(C);
265         Object *ob= ED_object_active_context(C);
266         
267         /* sanity checks */
268         if (!scene || scene->id.lib || !ob)
269                 return OPERATOR_CANCELLED;
270                 
271         /* Get object to work on - use a menu if we need to... */
272         if (ob->dup_group && ob->dup_group->id.lib) {
273                 /* gives menu with list of objects in group */
274                 //proxy_group_objects_menu(C, op, ob, ob->dup_group);
275                 WM_enum_search_invoke(C, op, evt);
276                 return OPERATOR_CANCELLED;
277
278         }
279         else if (ob->id.lib) {
280                 uiPopupMenu *pup= uiPupMenuBegin(C, "OK?", ICON_QUESTION);
281                 uiLayout *layout= uiPupMenuLayout(pup);
282                 PointerRNA props_ptr;
283                 
284                 /* create operator menu item with relevant properties filled in */
285                 props_ptr= uiItemFullO(layout, op->idname, op->type->name, ICON_NULL, NULL, WM_OP_EXEC_REGION_WIN, UI_ITEM_O_RETURN_PROPS);
286                 
287                 /* present the menu and be done... */
288                 uiPupMenuEnd(C, pup);
289         }
290         else {
291                 /* error.. cannot continue */
292                 BKE_report(op->reports, RPT_ERROR, "Can only make proxy for a referenced object or group");
293         }
294         
295         /* this invoke just calls another instance of this operator... */
296         return OPERATOR_CANCELLED;
297 }
298
299 static int make_proxy_exec (bContext *C, wmOperator *op)
300 {
301         Main *bmain= CTX_data_main(C);
302         Object *ob, *gob= ED_object_active_context(C);
303         GroupObject *go;
304         Scene *scene= CTX_data_scene(C);
305
306         if (gob->dup_group != NULL)
307         {
308                 go= BLI_findlink(&gob->dup_group->gobject, RNA_enum_get(op->ptr, "type"));
309                 ob= go->ob;
310         }
311         else
312         {
313                 ob= gob;
314                 gob = NULL;
315         }
316         
317         if (ob) {
318                 Object *newob;
319                 Base *newbase, *oldbase= BASACT;
320                 char name[32];
321                 
322                 /* Add new object for the proxy */
323                 newob= add_object(scene, OB_EMPTY);
324                 if (gob)
325                         strcpy(name, gob->id.name+2);
326                 else
327                         strcpy(name, ob->id.name+2);
328                 strcat(name, "_proxy");
329                 rename_id(&newob->id, name);
330                 
331                 /* set layers OK */
332                 newbase= BASACT;        /* add_object sets active... */
333                 newbase->lay= oldbase->lay;
334                 newob->lay= newbase->lay;
335                 
336                 /* remove base, leave user count of object, it gets linked in object_make_proxy */
337                 if (gob==NULL) {
338                         BLI_remlink(&scene->base, oldbase);
339                         MEM_freeN(oldbase);
340                 }
341                 
342                 object_make_proxy(newob, ob, gob);
343                 
344                 /* depsgraph flushes are needed for the new data */
345                 DAG_scene_sort(bmain, scene);
346                 DAG_id_tag_update(&newob->id, OB_RECALC_OB|OB_RECALC_DATA|OB_RECALC_TIME);
347                 WM_event_add_notifier(C, NC_OBJECT|ND_DRAW, newob);
348         }
349         else {
350                 BKE_report(op->reports, RPT_ERROR, "No object to make proxy for");
351                 return OPERATOR_CANCELLED;
352         }
353         
354         return OPERATOR_FINISHED;
355 }
356
357 /* Generic itemf's for operators that take library args */
358 static EnumPropertyItem *proxy_group_object_itemf(bContext *C, PointerRNA *UNUSED(ptr), int *free)
359 {
360         EnumPropertyItem item_tmp= {0}, *item= NULL;
361         int totitem= 0;
362         int i= 0;
363         Object *ob= ED_object_active_context(C);
364         GroupObject *go;
365
366         if(!ob || !ob->dup_group)
367                 return DummyRNA_DEFAULT_items;
368
369         /* find the object to affect */
370         for (go= ob->dup_group->gobject.first; go; go= go->next) {
371                 item_tmp.identifier= item_tmp.name= go->ob->id.name+2;
372                 item_tmp.value= i++;
373                 RNA_enum_item_add(&item, &totitem, &item_tmp);
374         }
375
376         RNA_enum_item_end(&item, &totitem);
377         *free= 1;
378
379         return item;
380 }
381
382 void OBJECT_OT_proxy_make (wmOperatorType *ot)
383 {
384         PropertyRNA *prop;
385
386         /* identifiers */
387         ot->name= "Make Proxy";
388         ot->idname= "OBJECT_OT_proxy_make";
389         ot->description= "Add empty object to become local replacement data of a library-linked object";
390         
391         /* callbacks */
392         ot->invoke= make_proxy_invoke;
393         ot->exec= make_proxy_exec;
394         ot->poll= ED_operator_object_active;
395         
396         /* flags */
397         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
398         
399         /* properties */
400         RNA_def_string(ot->srna, "object", "", 19, "Proxy Object", "Name of lib-linked/grouped object to make a proxy for.");
401         prop= RNA_def_enum(ot->srna, "type", DummyRNA_DEFAULT_items, 0, "Type", "Group object"); /* XXX, relies on hard coded ID at the moment */
402         RNA_def_enum_funcs(prop, proxy_group_object_itemf);
403         ot->prop= prop;
404 }
405
406 /********************** Clear Parent Operator ******************* */
407
408 static EnumPropertyItem prop_clear_parent_types[] = {
409         {0, "CLEAR", 0, "Clear Parent", ""},
410         {1, "CLEAR_KEEP_TRANSFORM", 0, "Clear and Keep Transformation", ""},
411         {2, "CLEAR_INVERSE", 0, "Clear Parent Inverse", ""},
412         {0, NULL, 0, NULL, NULL}
413 };
414
415 /* note, poll should check for editable scene */
416 static int parent_clear_exec(bContext *C, wmOperator *op)
417 {
418         Main *bmain= CTX_data_main(C);
419         Scene *scene= CTX_data_scene(C);
420         int type= RNA_enum_get(op->ptr, "type");
421         
422         CTX_DATA_BEGIN(C, Object*, ob, selected_editable_objects) {
423
424                 if(ob->parent == NULL)
425                         continue;
426                 
427                 if(type == 0) {
428                         ob->parent= NULL;
429                 }                       
430                 else if(type == 1) {
431                         ob->parent= NULL;
432                         object_apply_mat4(ob, ob->obmat, TRUE, FALSE);
433                 }
434                 else if(type == 2)
435                         unit_m4(ob->parentinv);
436
437                 ob->recalc |= OB_RECALC_OB|OB_RECALC_DATA|OB_RECALC_TIME;
438         }
439         CTX_DATA_END;
440         
441         DAG_scene_sort(bmain, scene);
442         DAG_ids_flush_update(bmain, 0);
443         WM_event_add_notifier(C, NC_OBJECT|ND_TRANSFORM, NULL);
444
445         return OPERATOR_FINISHED;
446 }
447
448 void OBJECT_OT_parent_clear(wmOperatorType *ot)
449 {
450         /* identifiers */
451         ot->name= "Clear Parent";
452         ot->description = "Clear the object's parenting";
453         ot->idname= "OBJECT_OT_parent_clear";
454         
455         /* api callbacks */
456         ot->invoke= WM_menu_invoke;
457         ot->exec= parent_clear_exec;
458         
459         ot->poll= ED_operator_object_active_editable;
460         
461         /* flags */
462         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
463         
464         ot->prop= RNA_def_enum(ot->srna, "type", prop_clear_parent_types, 0, "Type", "");
465 }
466
467 /* ******************** Make Parent Operator *********************** */
468
469 #define PAR_OBJECT                              0
470 #define PAR_ARMATURE                    1
471 #define PAR_ARMATURE_NAME               2
472 #define PAR_ARMATURE_ENVELOPE   3
473 #define PAR_ARMATURE_AUTO               4
474 #define PAR_BONE                                5
475 #define PAR_CURVE                               6
476 #define PAR_FOLLOW                              7
477 #define PAR_PATH_CONST                  8
478 #define PAR_LATTICE                             9
479 #define PAR_VERTEX                              10
480 #define PAR_TRIA                                11
481
482 static EnumPropertyItem prop_make_parent_types[] = {
483         {PAR_OBJECT, "OBJECT", 0, "Object", ""},
484         {PAR_ARMATURE, "ARMATURE", 0, "Armature Deform", ""},
485         {PAR_ARMATURE_NAME, "ARMATURE_NAME", 0, "   With Empty Groups", ""},
486         {PAR_ARMATURE_AUTO, "ARMATURE_AUTO", 0, "   With Automatic Weights", ""},
487         {PAR_ARMATURE_ENVELOPE, "ARMATURE_ENVELOPE", 0, "   With Envelope Weights", ""},
488         {PAR_BONE, "BONE", 0, "Bone", ""},
489         {PAR_CURVE, "CURVE", 0, "Curve Deform", ""},
490         {PAR_FOLLOW, "FOLLOW", 0, "Follow Path", ""},
491         {PAR_PATH_CONST, "PATH_CONST", 0, "Path Constraint", ""},
492         {PAR_LATTICE, "LATTICE", 0, "Lattice Deform", ""},
493         {PAR_VERTEX, "VERTEX", 0, "Vertex", ""},
494         {PAR_TRIA, "TRIA", 0, "Triangle", ""},
495         {0, NULL, 0, NULL, NULL}
496 };
497
498 static int test_parent_loop(Object *par, Object *ob)
499 {
500         /* test if 'ob' is a parent somewhere in par's parents */
501         
502         if(par == NULL) return 0;
503         if(ob == par) return 1;
504         
505         return test_parent_loop(par->parent, ob);
506 }
507
508 void ED_object_parent(Object *ob, Object *par, int type, const char *substr)
509 {
510         if(!par || test_parent_loop(par, ob)) {
511                 ob->parent= NULL;
512                 ob->partype= PAROBJECT;
513                 ob->parsubstr[0]= 0;
514                 return;
515         }
516
517         /* this could use some more checks */
518
519         ob->parent= par;
520         ob->partype &= ~PARTYPE;
521         ob->partype |= type;
522         BLI_strncpy(ob->parsubstr, substr, sizeof(ob->parsubstr));
523 }
524
525 static int parent_set_exec(bContext *C, wmOperator *op)
526 {
527         Main *bmain= CTX_data_main(C);
528         Scene *scene= CTX_data_scene(C);
529         Object *par= ED_object_active_context(C);
530         bPoseChannel *pchan= NULL;
531         int partype= RNA_enum_get(op->ptr, "type");
532         int pararm= ELEM4(partype, PAR_ARMATURE, PAR_ARMATURE_NAME, PAR_ARMATURE_ENVELOPE, PAR_ARMATURE_AUTO);
533         
534         par->recalc |= OB_RECALC_OB;
535         
536         /* preconditions */
537         if(partype==PAR_FOLLOW || partype==PAR_PATH_CONST) {
538                 if(par->type!=OB_CURVE)
539                         return OPERATOR_CANCELLED;
540                 else {
541                         Curve *cu= par->data;
542                         
543                         if((cu->flag & CU_PATH)==0) {
544                                 cu->flag |= CU_PATH|CU_FOLLOW;
545                                 makeDispListCurveTypes(scene, par, 0);  /* force creation of path data */
546                         }
547                         else cu->flag |= CU_FOLLOW;
548                         
549                         /* if follow, add F-Curve for ctime (i.e. "eval_time") so that path-follow works */
550                         if(partype == PAR_FOLLOW) {
551                                 /* get or create F-Curve */
552                                 bAction *act = verify_adt_action(&cu->id, 1);
553                                 FCurve *fcu = verify_fcurve(act, NULL, "eval_time", 0, 1);
554                                 
555                                 /* setup dummy 'generator' modifier here to get 1-1 correspondance still working */
556                                 if (!fcu->bezt && !fcu->fpt && !fcu->modifiers.first)
557                                         add_fmodifier(&fcu->modifiers, FMODIFIER_TYPE_GENERATOR);
558                         }
559                         
560                         /* fall back on regular parenting now (for follow only) */
561                         if(partype == PAR_FOLLOW)
562                                 partype= PAR_OBJECT;
563                 }               
564         }
565         else if(partype==PAR_BONE) {
566                 pchan= get_active_posechannel(par);
567                 
568                 if(pchan==NULL) {
569                         BKE_report(op->reports, RPT_ERROR, "No active Bone");
570                         return OPERATOR_CANCELLED;
571                 }
572         }
573         
574         /* context itterator */
575         CTX_DATA_BEGIN(C, Object*, ob, selected_editable_objects) {
576                 
577                 if(ob!=par) {
578                         
579                         if( test_parent_loop(par, ob) ) {
580                                 BKE_report(op->reports, RPT_ERROR, "Loop in parents");
581                         }
582                         else {
583                                 Object workob;
584                                 
585                                 /* apply transformation of previous parenting */
586                                 /* object_apply_mat4(ob, ob->obmat); */ /* removed because of bug [#23577] */
587
588                                 /* set the parent (except for follow-path constraint option) */
589                                 if(partype != PAR_PATH_CONST)
590                                         ob->parent= par;
591                                 
592                                 /* handle types */
593                                 if (pchan)
594                                         strcpy(ob->parsubstr, pchan->name);
595                                 else
596                                         ob->parsubstr[0]= 0;
597                                         
598                                 if(partype == PAR_PATH_CONST)
599                                         ; /* don't do anything here, since this is not technically "parenting" */
600                                 else if( ELEM(partype, PAR_CURVE, PAR_LATTICE) || pararm )
601                                 {
602                                         /* partype is now set to PAROBJECT so that invisible 'virtual' modifiers don't need to be created
603                                          * NOTE: the old (2.4x) method was to set ob->partype = PARSKEL, creating the virtual modifiers
604                                          */
605                                         ob->partype= PAROBJECT; /* note, dna define, not operator property */
606                                         //ob->partype= PARSKEL; /* note, dna define, not operator property */
607                                         
608                                         /* BUT, to keep the deforms, we need a modifier, and then we need to set the object that it uses */
609                                         // XXX currently this should only happen for meshes, curves, surfaces, and lattices - this stuff isn't available for metas yet
610                                         if (ELEM5(ob->type, OB_MESH, OB_CURVE, OB_SURF, OB_FONT, OB_LATTICE)) 
611                                         {
612                                                 ModifierData *md;
613
614                                                 switch (partype) {
615                                                 case PAR_CURVE: /* curve deform */
616                                                         md= ED_object_modifier_add(op->reports, bmain, scene, ob, NULL, eModifierType_Curve);
617                                                         ((CurveModifierData *)md)->object= par;
618                                                         break;
619                                                 case PAR_LATTICE: /* lattice deform */
620                                                         md= ED_object_modifier_add(op->reports, bmain, scene, ob, NULL, eModifierType_Lattice);
621                                                         ((LatticeModifierData *)md)->object= par;
622                                                         break;
623                                                 default: /* armature deform */
624                                                         md= ED_object_modifier_add(op->reports, bmain, scene, ob, NULL, eModifierType_Armature);
625                                                         ((ArmatureModifierData *)md)->object= par;
626                                                         break;
627                                                 }
628                                         }
629                                 }
630                                 else if (partype == PAR_BONE)
631                                         ob->partype= PARBONE; /* note, dna define, not operator property */
632                                 else
633                                         ob->partype= PAROBJECT; /* note, dna define, not operator property */
634                                 
635                                 /* constraint */
636                                 if(partype == PAR_PATH_CONST) {
637                                         bConstraint *con;
638                                         bFollowPathConstraint *data;
639                                         float cmat[4][4], vec[3];
640                                         
641                                         con = add_ob_constraint(ob, "AutoPath", CONSTRAINT_TYPE_FOLLOWPATH);
642                                         
643                                         data = con->data;
644                                         data->tar = par;
645                                         
646                                         get_constraint_target_matrix(scene, con, 0, CONSTRAINT_OBTYPE_OBJECT, NULL, cmat, scene->r.cfra - give_timeoffset(ob));
647                                         sub_v3_v3v3(vec, ob->obmat[3], cmat[3]);
648                                         
649                                         ob->loc[0] = vec[0];
650                                         ob->loc[1] = vec[1];
651                                         ob->loc[2] = vec[2];
652                                 }
653                                 else if(pararm && ob->type==OB_MESH && par->type == OB_ARMATURE) {
654                                         if(partype == PAR_ARMATURE_NAME)
655                                                 create_vgroups_from_armature(op->reports, scene, ob, par, ARM_GROUPS_NAME, 0);
656                                         else if(partype == PAR_ARMATURE_ENVELOPE)
657                                                 create_vgroups_from_armature(op->reports, scene, ob, par, ARM_GROUPS_ENVELOPE, 0);
658                                         else if(partype == PAR_ARMATURE_AUTO)
659                                                 create_vgroups_from_armature(op->reports, scene, ob, par, ARM_GROUPS_AUTO, 0);
660                                         
661                                         /* get corrected inverse */
662                                         ob->partype= PAROBJECT;
663                                         what_does_parent(scene, ob, &workob);
664                                         
665                                         invert_m4_m4(ob->parentinv, workob.obmat);
666                                 }
667                                 else {
668                                         /* calculate inverse parent matrix */
669                                         what_does_parent(scene, ob, &workob);
670                                         invert_m4_m4(ob->parentinv, workob.obmat);
671                                 }
672                                 
673                                 ob->recalc |= OB_RECALC_OB|OB_RECALC_DATA;
674                         }
675                 }
676         }
677         CTX_DATA_END;
678         
679         DAG_scene_sort(bmain, scene);
680         DAG_ids_flush_update(bmain, 0);
681         WM_event_add_notifier(C, NC_OBJECT|ND_TRANSFORM, NULL);
682         WM_event_add_notifier(C, NC_OBJECT|ND_PARENT, NULL);
683         
684         return OPERATOR_FINISHED;
685 }
686
687 static int parent_set_invoke(bContext *C, wmOperator *UNUSED(op), wmEvent *UNUSED(event))
688 {
689         Object *ob= ED_object_active_context(C);
690         uiPopupMenu *pup= uiPupMenuBegin(C, "Set Parent To", ICON_NULL);
691         uiLayout *layout= uiPupMenuLayout(pup);
692         
693         uiLayoutSetOperatorContext(layout, WM_OP_EXEC_DEFAULT);
694         uiItemEnumO(layout, "OBJECT_OT_parent_set", NULL, 0, "type", PAR_OBJECT);
695         
696         /* ob becomes parent, make the associated menus */
697         if(ob->type==OB_ARMATURE) {
698                 uiItemEnumO(layout, "OBJECT_OT_parent_set", NULL, 0, "type", PAR_ARMATURE);
699                 uiItemEnumO(layout, "OBJECT_OT_parent_set", NULL, 0, "type", PAR_ARMATURE_NAME);
700                 uiItemEnumO(layout, "OBJECT_OT_parent_set", NULL, 0, "type", PAR_ARMATURE_ENVELOPE);
701                 uiItemEnumO(layout, "OBJECT_OT_parent_set", NULL, 0, "type", PAR_ARMATURE_AUTO);
702                 uiItemEnumO(layout, "OBJECT_OT_parent_set", NULL, 0, "type", PAR_BONE);
703         }
704         else if(ob->type==OB_CURVE) {
705                 uiItemEnumO(layout, "OBJECT_OT_parent_set", NULL, 0, "type", PAR_CURVE);
706                 uiItemEnumO(layout, "OBJECT_OT_parent_set", NULL, 0, "type", PAR_FOLLOW);
707                 uiItemEnumO(layout, "OBJECT_OT_parent_set", NULL, 0, "type", PAR_PATH_CONST);
708         }
709         else if(ob->type == OB_LATTICE) {
710                 uiItemEnumO(layout, "OBJECT_OT_parent_set", NULL, 0, "type", PAR_LATTICE);
711         }
712         
713         uiPupMenuEnd(C, pup);
714         
715         return OPERATOR_CANCELLED;
716 }
717
718
719 void OBJECT_OT_parent_set(wmOperatorType *ot)
720 {
721         /* identifiers */
722         ot->name= "Make Parent";
723         ot->description = "Set the object's parenting";
724         ot->idname= "OBJECT_OT_parent_set";
725         
726         /* api callbacks */
727         ot->invoke= parent_set_invoke;
728         ot->exec= parent_set_exec;
729         
730         ot->poll= ED_operator_object_active;
731         
732         /* flags */
733         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
734         
735         RNA_def_enum(ot->srna, "type", prop_make_parent_types, 0, "Type", "");
736 }
737
738 /* ************ Make Parent Without Inverse Operator ******************* */
739
740 static int parent_noinv_set_exec(bContext *C, wmOperator *op)
741 {
742         Main *bmain= CTX_data_main(C);
743         Object *par= ED_object_active_context(C);
744         
745         par->recalc |= OB_RECALC_OB;
746         
747         /* context itterator */
748         CTX_DATA_BEGIN(C, Object*, ob, selected_editable_objects) {
749                 if (ob != par) {
750                         if (test_parent_loop(par, ob)) {
751                                 BKE_report(op->reports, RPT_ERROR, "Loop in parents");
752                         }
753                         else {
754                                 /* clear inverse matrix and also the object location */
755                                 unit_m4(ob->parentinv);
756                                 memset(ob->loc, 0, 3*sizeof(float));
757                                 
758                                 /* set recalc flags */
759                                 ob->recalc |= OB_RECALC_OB|OB_RECALC_DATA;
760                                 
761                                 /* set parenting type for object - object only... */
762                                 ob->parent= par;
763                                 ob->partype= PAROBJECT; /* note, dna define, not operator property */
764                         }
765                 }
766         }
767         CTX_DATA_END;
768         
769         DAG_scene_sort(bmain, CTX_data_scene(C));
770         DAG_ids_flush_update(bmain, 0);
771         WM_event_add_notifier(C, NC_OBJECT|ND_TRANSFORM, NULL);
772         
773         return OPERATOR_FINISHED;
774 }
775
776 void OBJECT_OT_parent_no_inverse_set(wmOperatorType *ot)
777 {
778         /* identifiers */
779         ot->name= "Make Parent without Inverse";
780         ot->description = "Set the object's parenting without setting the inverse parent correction";
781         ot->idname= "OBJECT_OT_parent_no_inverse_set";
782         
783         /* api callbacks */
784         ot->invoke= WM_operator_confirm;
785         ot->exec= parent_noinv_set_exec;
786         ot->poll= ED_operator_object_active_editable;
787         
788         /* flags */
789         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
790 }
791
792 /************************ Clear Slow Parent Operator *********************/
793
794 static int object_slow_parent_clear_exec(bContext *C, wmOperator *UNUSED(op))
795 {
796         Main *bmain= CTX_data_main(C);
797         Scene *scene= CTX_data_scene(C);
798
799         CTX_DATA_BEGIN(C, Object*, ob, selected_editable_objects) {
800                 if(ob->parent) {
801                         if(ob->partype & PARSLOW) {
802                                 ob->partype -= PARSLOW;
803                                 where_is_object(scene, ob);
804                                 ob->partype |= PARSLOW;
805                                 ob->recalc |= OB_RECALC_OB;
806                         }
807                 }
808         }
809         CTX_DATA_END;
810
811         DAG_ids_flush_update(bmain, 0);
812         WM_event_add_notifier(C, NC_SCENE, scene);
813         
814         return OPERATOR_FINISHED;
815 }
816
817 void OBJECT_OT_slow_parent_clear(wmOperatorType *ot)
818 {
819         
820         /* identifiers */
821         ot->name= "Clear Slow Parent";
822         ot->description = "Clear the object's slow parent";
823         ot->idname= "OBJECT_OT_slow_parent_clear";
824         
825         /* api callbacks */
826         ot->invoke= WM_operator_confirm;
827         ot->exec= object_slow_parent_clear_exec;
828         ot->poll= ED_operator_view3d_active;
829         
830         /* flags */
831         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
832 }
833
834 /********************** Make Slow Parent Operator *********************/
835
836 static int object_slow_parent_set_exec(bContext *C, wmOperator *UNUSED(op))
837 {
838         Main *bmain= CTX_data_main(C);
839         Scene *scene= CTX_data_scene(C);
840
841         CTX_DATA_BEGIN(C, Object*, ob, selected_editable_objects) {
842                 if(ob->parent)
843                         ob->partype |= PARSLOW;
844
845                 ob->recalc |= OB_RECALC_OB;
846                 
847         }
848         CTX_DATA_END;
849
850         DAG_ids_flush_update(bmain, 0);
851         WM_event_add_notifier(C, NC_SCENE, scene);
852         
853         return OPERATOR_FINISHED;
854 }
855
856 void OBJECT_OT_slow_parent_set(wmOperatorType *ot)
857 {
858         
859         /* identifiers */
860         ot->name= "Set Slow Parent";
861         ot->description = "Set the object's slow parent";
862         ot->idname= "OBJECT_OT_slow_parent_set";
863         
864         /* api callbacks */
865         ot->invoke= WM_operator_confirm;
866         ot->exec= object_slow_parent_set_exec;
867         ot->poll= ED_operator_view3d_active;
868         
869         /* flags */
870         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
871 }
872
873 /* ******************** Clear Track Operator ******************* */
874
875 static EnumPropertyItem prop_clear_track_types[] = {
876         {0, "CLEAR", 0, "Clear Track", ""},
877         {1, "CLEAR_KEEP_TRANSFORM", 0, "Clear and Keep Transformation (Clear Track)", ""},
878         {0, NULL, 0, NULL, NULL}
879 };
880
881 /* note, poll should check for editable scene */
882 static int object_track_clear_exec(bContext *C, wmOperator *op)
883 {
884         Main *bmain= CTX_data_main(C);
885         Scene *scene= CTX_data_scene(C);
886         int type= RNA_enum_get(op->ptr, "type");
887
888         if(CTX_data_edit_object(C)) {
889                 BKE_report(op->reports, RPT_ERROR, "Operation cannot be performed in EditMode");
890                 return OPERATOR_CANCELLED;
891         }
892         CTX_DATA_BEGIN(C, Object*, ob, selected_editable_objects) {
893                 bConstraint *con, *pcon;
894                 
895                 /* remove track-object for old track */
896                 ob->track= NULL;
897                 ob->recalc |= OB_RECALC_OB|OB_RECALC_DATA|OB_RECALC_TIME;
898                 
899                 /* also remove all tracking constraints */
900                 for (con= ob->constraints.last; con; con= pcon) {
901                         pcon= con->prev;
902                         if (ELEM3(con->type, CONSTRAINT_TYPE_TRACKTO, CONSTRAINT_TYPE_LOCKTRACK, CONSTRAINT_TYPE_DAMPTRACK))
903                                 remove_constraint(&ob->constraints, con);
904                 }
905                 
906                 if(type == 1)
907                         object_apply_mat4(ob, ob->obmat, TRUE, TRUE);
908         }
909         CTX_DATA_END;
910
911         DAG_ids_flush_update(bmain, 0);
912         DAG_scene_sort(bmain, scene);
913         WM_event_add_notifier(C, NC_OBJECT|ND_TRANSFORM, NULL);
914
915         return OPERATOR_FINISHED;
916 }
917
918 void OBJECT_OT_track_clear(wmOperatorType *ot)
919 {
920         /* identifiers */
921         ot->name= "Clear track";
922         ot->description = "Clear tracking constraint or flag from object";
923         ot->idname= "OBJECT_OT_track_clear";
924         
925         /* api callbacks */
926         ot->invoke= WM_menu_invoke;
927         ot->exec= object_track_clear_exec;
928         
929         ot->poll= ED_operator_objectmode;
930         
931         /* flags */
932         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
933         
934         ot->prop= RNA_def_enum(ot->srna, "type", prop_clear_track_types, 0, "Type", "");
935 }
936
937 /************************** Make Track Operator *****************************/
938
939 static EnumPropertyItem prop_make_track_types[] = {
940         {1, "DAMPTRACK", 0, "Damped Track Constraint", ""},
941         {2, "TRACKTO", 0, "Track To Constraint", ""},
942         {3, "LOCKTRACK", 0, "Lock Track Constraint", ""},
943         {0, NULL, 0, NULL, NULL}
944 };
945
946 static int track_set_exec(bContext *C, wmOperator *op)
947 {
948         Main *bmain= CTX_data_main(C);
949         Scene *scene= CTX_data_scene(C);
950         Object *obact= ED_object_active_context(C); 
951         
952         int type= RNA_enum_get(op->ptr, "type");
953         
954         if(type == 1) {
955                 bConstraint *con;
956                 bDampTrackConstraint *data;
957
958                 CTX_DATA_BEGIN(C, Object*, ob, selected_editable_objects) {
959                         if(ob!=obact) {
960                                 con = add_ob_constraint(ob, "AutoTrack", CONSTRAINT_TYPE_DAMPTRACK);
961
962                                 data = con->data;
963                                 data->tar = obact;
964                                 ob->recalc |= OB_RECALC_OB|OB_RECALC_DATA|OB_RECALC_TIME;
965                                 
966                                 /* Lamp and Camera track differently by default */
967                                 if (ob->type == OB_LAMP || ob->type == OB_CAMERA)
968                                         data->trackflag = TRACK_nZ;
969                         }
970                 }
971                 CTX_DATA_END;
972         }
973         else if(type == 2) {
974                 bConstraint *con;
975                 bTrackToConstraint *data;
976
977                 CTX_DATA_BEGIN(C, Object*, ob, selected_editable_objects) {
978                         if(ob!=obact) {
979                                 con = add_ob_constraint(ob, "AutoTrack", CONSTRAINT_TYPE_TRACKTO);
980
981                                 data = con->data;
982                                 data->tar = obact;
983                                 ob->recalc |= OB_RECALC_OB|OB_RECALC_DATA|OB_RECALC_TIME;
984                                 
985                                 /* Lamp and Camera track differently by default */
986                                 if (ob->type == OB_LAMP || ob->type == OB_CAMERA) {
987                                         data->reserved1 = TRACK_nZ;
988                                         data->reserved2 = UP_Y;
989                                 }
990                         }
991                 }
992                 CTX_DATA_END;
993         }
994         else if(type == 3) {
995                 bConstraint *con;
996                 bLockTrackConstraint *data;
997
998                 CTX_DATA_BEGIN(C, Object*, ob, selected_editable_objects) {
999                         if(ob!=obact) {
1000                                 con = add_ob_constraint(ob, "AutoTrack", CONSTRAINT_TYPE_LOCKTRACK);
1001
1002                                 data = con->data;
1003                                 data->tar = obact;
1004                                 ob->recalc |= OB_RECALC_OB|OB_RECALC_DATA|OB_RECALC_TIME;
1005                                 
1006                                 /* Lamp and Camera track differently by default */
1007                                 if (ob->type == OB_LAMP || ob->type == OB_CAMERA) {
1008                                         data->trackflag = TRACK_nZ;
1009                                         data->lockflag = LOCK_Y;
1010                                 }
1011                         }
1012                 }
1013                 CTX_DATA_END;
1014         }
1015         
1016         DAG_scene_sort(bmain, scene);
1017         DAG_ids_flush_update(bmain, 0);
1018         WM_event_add_notifier(C, NC_OBJECT|ND_TRANSFORM, NULL);
1019         
1020         return OPERATOR_FINISHED;
1021 }
1022
1023 void OBJECT_OT_track_set(wmOperatorType *ot)
1024 {
1025         /* identifiers */
1026         ot->name= "Make Track";
1027         ot->description = "Make the object track another object, either by constraint or old way or locked track";
1028         ot->idname= "OBJECT_OT_track_set";
1029         
1030         /* api callbacks */
1031         ot->invoke= WM_menu_invoke;
1032         ot->exec= track_set_exec;
1033         
1034         ot->poll= ED_operator_objectmode;
1035         
1036         /* flags */
1037         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
1038         
1039         /* properties */
1040         ot->prop= RNA_def_enum(ot->srna, "type", prop_make_track_types, 0, "Type", "");
1041 }
1042
1043 /************************** Move to Layer Operator *****************************/
1044
1045 static unsigned int move_to_layer_init(bContext *C, wmOperator *op)
1046 {
1047         int values[20], a;
1048         unsigned int lay= 0;
1049
1050         if(!RNA_property_is_set(op->ptr, "layers")) {
1051                 CTX_DATA_BEGIN(C, Base*, base, selected_editable_bases) {
1052                         lay |= base->lay;
1053                 }
1054                 CTX_DATA_END;
1055
1056                 for(a=0; a<20; a++)
1057                         values[a]= (lay & (1<<a));
1058                 
1059                 RNA_boolean_set_array(op->ptr, "layers", values);
1060         }
1061         else {
1062                 RNA_boolean_get_array(op->ptr, "layers", values);
1063
1064                 for(a=0; a<20; a++)
1065                         if(values[a])
1066                                 lay |= (1 << a);
1067         }
1068
1069         return lay;
1070 }
1071
1072 static int move_to_layer_invoke(bContext *C, wmOperator *op, wmEvent *event)
1073 {
1074         View3D *v3d= CTX_wm_view3d(C);
1075         if(v3d && v3d->localvd) {
1076                 return WM_operator_confirm_message(C, op, "Move from localview");
1077         }
1078         else {
1079                 move_to_layer_init(C, op);
1080                 return WM_operator_props_popup(C, op, event);
1081         }
1082 }
1083
1084 static int move_to_layer_exec(bContext *C, wmOperator *op)
1085 {
1086         Main *bmain= CTX_data_main(C);
1087         Scene *scene= CTX_data_scene(C);
1088         View3D *v3d= CTX_wm_view3d(C);
1089         unsigned int lay, local;
1090         int islamp= 0;
1091         
1092         lay= move_to_layer_init(C, op);
1093         lay &= 0xFFFFFF;
1094
1095         if(lay==0) return OPERATOR_CANCELLED;
1096         
1097         if(v3d && v3d->localvd) {
1098                 /* now we can move out of localview. */
1099                 // XXX if (!okee("Move from localview")) return;
1100                 CTX_DATA_BEGIN(C, Base*, base, selected_editable_bases) {
1101                         lay= base->lay & ~v3d->lay;
1102                         base->lay= lay;
1103                         base->object->lay= lay;
1104                         base->object->flag &= ~SELECT;
1105                         base->flag &= ~SELECT;
1106                         if(base->object->type==OB_LAMP) islamp= 1;
1107                 }
1108                 CTX_DATA_END;
1109         }
1110         else {
1111                 /* normal non localview operation */
1112                 CTX_DATA_BEGIN(C, Base*, base, selected_editable_bases) {
1113                         /* upper byte is used for local view */
1114                         local= base->lay & 0xFF000000;  
1115                         base->lay= lay + local;
1116                         base->object->lay= lay;
1117                         if(base->object->type==OB_LAMP) islamp= 1;
1118                 }
1119                 CTX_DATA_END;
1120         }
1121
1122         if(islamp) reshadeall_displist(scene);  /* only frees */
1123         
1124         /* warning, active object may be hidden now */
1125         
1126         WM_event_add_notifier(C, NC_OBJECT|ND_DRAW, scene);
1127         WM_event_add_notifier(C, NC_SCENE|ND_LAYER_CONTENT, scene);
1128
1129         DAG_scene_sort(bmain, scene);
1130
1131         return OPERATOR_FINISHED;
1132 }
1133
1134 void OBJECT_OT_move_to_layer(wmOperatorType *ot)
1135 {
1136         /* identifiers */
1137         ot->name= "Move to Layer";
1138         ot->description = "Move the object to different layers";
1139         ot->idname= "OBJECT_OT_move_to_layer";
1140         
1141         /* api callbacks */
1142         ot->invoke= move_to_layer_invoke;
1143         ot->exec= move_to_layer_exec;
1144         ot->poll= ED_operator_objectmode;
1145         
1146         /* flags */
1147         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
1148         
1149         /* properties */
1150         RNA_def_boolean_layer_member(ot->srna, "layers", 20, NULL, "Layer", "");
1151 }
1152
1153 /************************** Link to Scene Operator *****************************/
1154
1155 void link_to_scene(Main *UNUSED(bmain), unsigned short UNUSED(nr))
1156 {
1157 #if 0
1158         Scene *sce= (Scene*) BLI_findlink(&bmain->scene, G.curscreen->scenenr-1);
1159         Base *base, *nbase;
1160         
1161         if(sce==0) return;
1162         if(sce->id.lib) return;
1163         
1164         for(base= FIRSTBASE; base; base= base->next) {
1165                 if(TESTBASE(v3d, base)) {
1166                         
1167                         nbase= MEM_mallocN( sizeof(Base), "newbase");
1168                         *nbase= *base;
1169                         BLI_addhead( &(sce->base), nbase);
1170                         id_us_plus((ID *)base->object);
1171                 }
1172         }
1173 #endif
1174 }
1175
1176 static int make_links_scene_exec(bContext *C, wmOperator *op)
1177 {
1178         Main *bmain= CTX_data_main(C);
1179         Scene *scene_to= BLI_findlink(&CTX_data_main(C)->scene, RNA_enum_get(op->ptr, "scene"));
1180
1181         if(scene_to==NULL) {
1182                 BKE_report(op->reports, RPT_ERROR, "Scene not found");
1183                 return OPERATOR_CANCELLED;
1184         }
1185
1186         if(scene_to == CTX_data_scene(C)) {
1187                 BKE_report(op->reports, RPT_ERROR, "Can't link objects into the same scene");
1188                 return OPERATOR_CANCELLED;
1189         }
1190
1191         if(scene_to->id.lib) {
1192                 BKE_report(op->reports, RPT_ERROR, "Can't link objects into a linked scene");
1193                 return OPERATOR_CANCELLED;
1194         }
1195
1196         CTX_DATA_BEGIN(C, Base*, base, selected_bases)
1197         {
1198                 if(!object_in_scene(base->object, scene_to)) {
1199                         Base *nbase= MEM_mallocN( sizeof(Base), "newbase");
1200                         *nbase= *base;
1201                         BLI_addhead( &(scene_to->base), nbase);
1202                         id_us_plus((ID *)base->object);
1203                 }
1204         }
1205         CTX_DATA_END;
1206
1207         DAG_ids_flush_update(bmain, 0);
1208
1209         /* one day multiple scenes will be visible, then we should have some update function for them */
1210         return OPERATOR_FINISHED;
1211 }
1212
1213 enum {
1214         MAKE_LINKS_OBDATA = 1,
1215         MAKE_LINKS_MATERIALS,
1216         MAKE_LINKS_ANIMDATA,
1217         MAKE_LINKS_DUPLIGROUP,
1218         MAKE_LINKS_MODIFIERS
1219 };
1220
1221 /* Return 1 if make link data is allow, zero otherwise */
1222 static int allow_make_links_data(int ev, Object *ob, Object *obt)
1223 {
1224         switch(ev) {
1225                 case MAKE_LINKS_OBDATA:
1226                         if (ob->type == obt->type && ob->type != OB_EMPTY)
1227                                 return 1;
1228                         break;
1229                 case MAKE_LINKS_MATERIALS:
1230                         if (ELEM5(ob->type, OB_MESH, OB_CURVE, OB_FONT, OB_SURF, OB_MBALL) &&
1231                                 ELEM5(obt->type, OB_MESH, OB_CURVE, OB_FONT, OB_SURF, OB_MBALL))
1232                                 return 1;
1233                         break;
1234                 case MAKE_LINKS_ANIMDATA:
1235                 case MAKE_LINKS_DUPLIGROUP:
1236                         return 1;
1237                 case MAKE_LINKS_MODIFIERS:
1238                         if (ob->type != OB_EMPTY && obt->type != OB_EMPTY)
1239                                 return 1;
1240                         break;
1241         }
1242         return 0;
1243 }
1244
1245 static int make_links_data_exec(bContext *C, wmOperator *op)
1246 {
1247         Main *bmain= CTX_data_main(C);
1248         int event = RNA_int_get(op->ptr, "type");
1249         Object *ob;
1250         ID *id;
1251         int a;
1252
1253         ob= ED_object_active_context(C);
1254
1255         CTX_DATA_BEGIN(C, Object*, obt, selected_editable_objects) {
1256                 if(ob != obt) {
1257                         if (allow_make_links_data(event, ob, obt)) {
1258                                 switch(event) {
1259                                 case MAKE_LINKS_OBDATA: /* obdata */
1260                                         id= obt->data;
1261                                         id->us--;
1262
1263                                         id= ob->data;
1264                                         id_us_plus(id);
1265                                         obt->data= id;
1266
1267                                         /* if amount of material indices changed: */
1268                                         test_object_materials(obt->data);
1269
1270                                         obt->recalc |= OB_RECALC_DATA;
1271                                         break;
1272                                 case MAKE_LINKS_MATERIALS:
1273                                         /* new approach, using functions from kernel */
1274                                         for(a=0; a<ob->totcol; a++) {
1275                                                 Material *ma= give_current_material(ob, a+1);
1276                                                 assign_material(obt, ma, a+1);  /* also works with ma==NULL */
1277                                         }
1278                                         break;
1279                                 case MAKE_LINKS_ANIMDATA:
1280                                         BKE_copy_animdata_id((ID *)obt, (ID *)ob, FALSE);
1281                                         BKE_copy_animdata_id((ID *)obt->data, (ID *)ob->data, FALSE);
1282                                         break;
1283                                 case MAKE_LINKS_DUPLIGROUP:
1284                                         obt->dup_group= ob->dup_group;
1285                                         if(obt->dup_group) {
1286                                                 id_lib_extern(&obt->dup_group->id);
1287                                                 obt->transflag |= OB_DUPLIGROUP;
1288                                         }
1289                                         break;
1290                                 case MAKE_LINKS_MODIFIERS:
1291                                         object_link_modifiers(obt, ob);
1292                                         obt->recalc |= OB_RECALC_OB|OB_RECALC_DATA|OB_RECALC_TIME;
1293                                         break;
1294                                 }
1295                         }
1296                 }
1297         }
1298         CTX_DATA_END;
1299
1300         DAG_ids_flush_update(bmain, 0);
1301         WM_event_add_notifier(C, NC_SPACE|ND_SPACE_VIEW3D, CTX_wm_view3d(C));
1302         return OPERATOR_FINISHED;
1303 }
1304
1305
1306 void OBJECT_OT_make_links_scene(wmOperatorType *ot)
1307 {
1308         PropertyRNA *prop;
1309
1310         /* identifiers */
1311         ot->name= "Link Objects to Scene";
1312         ot->description = "Make linked data local to each object";
1313         ot->idname= "OBJECT_OT_make_links_scene";
1314
1315         /* api callbacks */
1316         ot->invoke= WM_enum_search_invoke;
1317         ot->exec= make_links_scene_exec;
1318         /* better not run the poll check */
1319
1320         /* flags */
1321         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
1322
1323         /* properties */
1324         prop= RNA_def_enum(ot->srna, "scene", DummyRNA_NULL_items, 0, "Scene", "");
1325         RNA_def_enum_funcs(prop, RNA_scene_local_itemf);
1326         ot->prop= prop;
1327 }
1328
1329 void OBJECT_OT_make_links_data(wmOperatorType *ot)
1330 {
1331         static EnumPropertyItem make_links_items[]= {
1332                 {MAKE_LINKS_OBDATA,             "OBDATA", 0, "Object Data", ""},
1333                 {MAKE_LINKS_MATERIALS,  "MATERIAL", 0, "Materials", ""},
1334                 {MAKE_LINKS_ANIMDATA,   "ANIMATION", 0, "Animation Data", ""},
1335                 {MAKE_LINKS_DUPLIGROUP, "DUPLIGROUP", 0, "DupliGroup", ""},
1336                 {MAKE_LINKS_MODIFIERS,  "MODIFIERS", 0, "Modifiers", ""},
1337                 {0, NULL, 0, NULL, NULL}};
1338
1339         PropertyRNA *prop;
1340
1341         /* identifiers */
1342         ot->name= "Link Data";
1343         ot->description = "Make links from the active object to other selected objects";
1344         ot->idname= "OBJECT_OT_make_links_data";
1345
1346         /* api callbacks */
1347         ot->exec= make_links_data_exec;
1348         ot->poll= ED_operator_objectmode;
1349
1350         /* flags */
1351         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
1352
1353         /* properties */
1354         prop= RNA_def_enum(ot->srna, "type", make_links_items, 0, "Type", "");
1355 }
1356
1357
1358 /**************************** Make Single User ********************************/
1359
1360 static void single_object_users__forwardModifierLinks(void *UNUSED(userData), Object *UNUSED(ob), Object **obpoin)
1361 {
1362         ID_NEW(*obpoin);
1363 }
1364
1365 void single_object_users(Scene *scene, View3D *v3d, int flag)   
1366 {
1367         Base *base;
1368         Object *ob, *obn;
1369         
1370         clear_sca_new_poins();  /* sensor/contr/act */
1371
1372         /* duplicate (must set newid) */
1373         for(base= FIRSTBASE; base; base= base->next) {
1374                 ob= base->object;
1375                 
1376                 if( (base->flag & flag)==flag ) {
1377                         if(ob->id.lib==NULL && ob->id.us>1) {
1378                                 /* base gets copy of object */
1379                                 obn= copy_object(ob);
1380                                 base->object= obn;
1381                                 ob->id.us--;
1382                         }
1383                 }
1384         }
1385         
1386         ID_NEW(scene->camera);
1387         if(v3d) ID_NEW(v3d->camera);
1388         
1389         /* object pointers */
1390         for(base= FIRSTBASE; base; base= base->next) {
1391                 ob= base->object;
1392                 if(ob->id.lib==NULL) {
1393                         relink_constraints(&base->object->constraints);
1394                         if (base->object->pose){
1395                                 bPoseChannel *chan;
1396                                 for (chan = base->object->pose->chanbase.first; chan; chan=chan->next){
1397                                         relink_constraints(&chan->constraints);
1398                                 }
1399                         }
1400                         modifiers_foreachObjectLink(base->object, single_object_users__forwardModifierLinks, NULL);
1401                         
1402                         ID_NEW(ob->parent);
1403                 }
1404         }
1405
1406         set_sca_new_poins();
1407 }
1408
1409 void new_id_matar(Material **matar, int totcol)
1410 {
1411         ID *id;
1412         int a;
1413         
1414         for(a=0; a<totcol; a++) {
1415                 id= (ID *)matar[a];
1416                 if(id && id->lib==0) {
1417                         if(id->newid) {
1418                                 matar[a]= (Material *)id->newid;
1419                                 id_us_plus(id->newid);
1420                                 id->us--;
1421                         }
1422                         else if(id->us>1) {
1423                                 matar[a]= copy_material(matar[a]);
1424                                 id->us--;
1425                                 id->newid= (ID *)matar[a];
1426                         }
1427                 }
1428         }
1429 }
1430
1431 void single_obdata_users(Main *bmain, Scene *scene, int flag)
1432 {
1433         Object *ob;
1434         Lamp *la;
1435         Curve *cu;
1436         //Camera *cam;
1437         Base *base;
1438         Mesh *me;
1439         ID *id;
1440         int a;
1441
1442         for(base= FIRSTBASE; base; base= base->next) {
1443                 ob= base->object;
1444                 if(ob->id.lib==NULL && (base->flag & flag)==flag ) {
1445                         id= ob->data;
1446                         
1447                         if(id && id->us>1 && id->lib==NULL) {
1448                                 ob->recalc= OB_RECALC_DATA;
1449                                 
1450                                 BKE_copy_animdata_id_action(id);
1451                                 
1452                                 switch(ob->type) {
1453                                 case OB_LAMP:
1454                                         ob->data= la= copy_lamp(ob->data);
1455                                         for(a=0; a<MAX_MTEX; a++) {
1456                                                 if(la->mtex[a]) {
1457                                                         ID_NEW(la->mtex[a]->object);
1458                                                 }
1459                                         }
1460                                         break;
1461                                 case OB_CAMERA:
1462                                         ob->data= copy_camera(ob->data);
1463                                         break;
1464                                 case OB_MESH:
1465                                         me= ob->data= copy_mesh(ob->data);
1466                                         //if(me && me->key)
1467                                         //      ipo_idnew(me->key->ipo);        /* drivers */
1468                                         break;
1469                                 case OB_MBALL:
1470                                         ob->data= copy_mball(ob->data);
1471                                         break;
1472                                 case OB_CURVE:
1473                                 case OB_SURF:
1474                                 case OB_FONT:
1475                                         ob->data= cu= copy_curve(ob->data);
1476                                         ID_NEW(cu->bevobj);
1477                                         ID_NEW(cu->taperobj);
1478                                         break;
1479                                 case OB_LATTICE:
1480                                         ob->data= copy_lattice(ob->data);
1481                                         break;
1482                                 case OB_ARMATURE:
1483                                         ob->recalc |= OB_RECALC_DATA;
1484                                         ob->data= copy_armature(ob->data);
1485                                         armature_rebuild_pose(ob, ob->data);
1486                                         break;
1487                                 default:
1488                                         if (G.f & G_DEBUG)
1489                                                 printf("ERROR single_obdata_users: can't copy %s\n", id->name);
1490                                         return;
1491                                 }
1492                                 
1493                                 id->us--;
1494                                 id->newid= ob->data;
1495                                 
1496                         }
1497                         
1498                 }
1499         }
1500         
1501         me= bmain->mesh.first;
1502         while(me) {
1503                 ID_NEW(me->texcomesh);
1504                 me= me->id.next;
1505         }
1506 }
1507
1508 static void single_object_action_users(Scene *scene, int flag)
1509 {
1510         Object *ob;
1511         Base *base;
1512         
1513         for(base= FIRSTBASE; base; base= base->next) {
1514                 ob= base->object;
1515                 if(ob->id.lib==NULL && (flag==0 || (base->flag & SELECT)) ) {
1516                         ob->recalc= OB_RECALC_DATA;
1517                         BKE_copy_animdata_id_action(&ob->id);
1518                 }
1519         }
1520 }
1521
1522 static void single_mat_users(Scene *scene, int flag, int do_textures)
1523 {
1524         Object *ob;
1525         Base *base;
1526         Material *ma, *man;
1527         Tex *tex;
1528         int a, b;
1529         
1530         for(base= FIRSTBASE; base; base= base->next) {
1531                 ob= base->object;
1532                 if(ob->id.lib==NULL && (flag==0 || (base->flag & SELECT)) ) {
1533         
1534                         for(a=1; a<=ob->totcol; a++) {
1535                                 ma= give_current_material(ob, a);
1536                                 if(ma) {
1537                                         /* do not test for LIB_NEW: this functions guaranteed delivers single_users! */
1538                                         
1539                                         if(ma->id.us>1) {
1540                                                 man= copy_material(ma);
1541                                                 BKE_copy_animdata_id_action(&man->id);
1542                                                 
1543                                                 man->id.us= 0;
1544                                                 assign_material(ob, man, a);
1545
1546                                                 if(do_textures) {
1547                                                         for(b=0; b<MAX_MTEX; b++) {
1548                                                                 if(ma->mtex[b] && ma->mtex[b]->tex) {
1549                                                                         tex= ma->mtex[b]->tex;
1550                                                                         if(tex->id.us>1) {
1551                                                                                 ma->mtex[b]->tex= copy_texture(tex);
1552                                                                                 tex->id.us--;
1553                                                                         }
1554                                                                 }
1555                                                         }
1556                                                 }
1557                                         }
1558                                 }
1559                         }
1560                 }
1561         }
1562 }
1563
1564 void do_single_tex_user(Tex **from)
1565 {
1566         Tex *tex, *texn;
1567         
1568         tex= *from;
1569         if(tex==0) return;
1570         
1571         if(tex->id.newid) {
1572                 *from= (Tex *)tex->id.newid;
1573                 id_us_plus(tex->id.newid);
1574                 tex->id.us--;
1575         }
1576         else if(tex->id.us>1) {
1577                 texn= copy_texture(tex);
1578                 BKE_copy_animdata_id_action(&texn->id);
1579                 tex->id.newid= (ID *)texn;
1580                 tex->id.us--;
1581                 *from= texn;
1582         }
1583 }
1584
1585 void single_tex_users_expand(Main *bmain)
1586 {
1587         /* only when 'parent' blocks are LIB_NEW */
1588         Material *ma;
1589         Lamp *la;
1590         World *wo;
1591         int b;
1592                 
1593         for(ma= bmain->mat.first; ma; ma=ma->id.next) {
1594                 if(ma->id.flag & LIB_NEW) {
1595                         for(b=0; b<MAX_MTEX; b++) {
1596                                 if(ma->mtex[b] && ma->mtex[b]->tex) {
1597                                         do_single_tex_user( &(ma->mtex[b]->tex) );
1598                                 }
1599                         }
1600                 }
1601         }
1602
1603         for(la= bmain->lamp.first; la; la=la->id.next) {
1604                 if(la->id.flag & LIB_NEW) {
1605                         for(b=0; b<MAX_MTEX; b++) {
1606                                 if(la->mtex[b] && la->mtex[b]->tex) {
1607                                         do_single_tex_user( &(la->mtex[b]->tex) );
1608                                 }
1609                         }
1610                 }
1611         }
1612
1613         for(wo= bmain->world.first; wo; wo=wo->id.next) {
1614                 if(wo->id.flag & LIB_NEW) {
1615                         for(b=0; b<MAX_MTEX; b++) {
1616                                 if(wo->mtex[b] && wo->mtex[b]->tex) {
1617                                         do_single_tex_user( &(wo->mtex[b]->tex) );
1618                                 }
1619                         }
1620                 }
1621         }
1622 }
1623
1624 static void single_mat_users_expand(Main *bmain)
1625 {
1626         /* only when 'parent' blocks are LIB_NEW */
1627         Object *ob;
1628         Mesh *me;
1629         Curve *cu;
1630         MetaBall *mb;
1631         Material *ma;
1632         int a;
1633         
1634         for(ob=bmain->object.first; ob; ob=ob->id.next)
1635                 if(ob->id.flag & LIB_NEW)
1636                         new_id_matar(ob->mat, ob->totcol);
1637
1638         for(me=bmain->mesh.first; me; me=me->id.next)
1639                 if(me->id.flag & LIB_NEW)
1640                         new_id_matar(me->mat, me->totcol);
1641
1642         for(cu=bmain->curve.first; cu; cu=cu->id.next)
1643                 if(cu->id.flag & LIB_NEW)
1644                         new_id_matar(cu->mat, cu->totcol);
1645
1646         for(mb=bmain->mball.first; mb; mb=mb->id.next)
1647                 if(mb->id.flag & LIB_NEW)
1648                         new_id_matar(mb->mat, mb->totcol);
1649
1650         /* material imats  */
1651         for(ma=bmain->mat.first; ma; ma=ma->id.next)
1652                 if(ma->id.flag & LIB_NEW)
1653                         for(a=0; a<MAX_MTEX; a++)
1654                                 if(ma->mtex[a])
1655                                         ID_NEW(ma->mtex[a]->object);
1656 }
1657
1658 /* used for copying scenes */
1659 void ED_object_single_users(Main *bmain, Scene *scene, int full)
1660 {
1661         single_object_users(scene, NULL, 0);
1662
1663         if(full) {
1664                 single_obdata_users(bmain, scene, 0);
1665                 single_mat_users_expand(bmain);
1666                 single_tex_users_expand(bmain);
1667         }
1668
1669         clear_id_newpoins();
1670 }
1671
1672 /******************************* Make Local ***********************************/
1673
1674 /* helper for below, ma was checked to be not NULL */
1675 static void make_local_makelocalmaterial(Material *ma)
1676 {
1677         AnimData *adt;
1678         int b;
1679         
1680         id_make_local(&ma->id, 0);
1681         
1682         for(b=0; b<MAX_MTEX; b++)
1683                 if(ma->mtex[b] && ma->mtex[b]->tex)
1684                         id_make_local(&ma->mtex[b]->tex->id, 0);
1685         
1686         adt= BKE_animdata_from_id(&ma->id);
1687         if(adt) BKE_animdata_make_local(adt);
1688
1689         /* nodetree? XXX */
1690 }
1691
1692 static int make_local_exec(bContext *C, wmOperator *op)
1693 {
1694         AnimData *adt;
1695         ParticleSystem *psys;
1696         Material *ma, ***matarar;
1697         Lamp *la;
1698         ID *id;
1699         int a, b, mode= RNA_enum_get(op->ptr, "type");
1700         
1701         if(mode==3) {
1702                 all_local(NULL, 0);     /* NULL is all libs */
1703                 WM_event_add_notifier(C, NC_WINDOW, NULL);
1704                 return OPERATOR_FINISHED;
1705         }
1706
1707         clear_id_newpoins();
1708         
1709         CTX_DATA_BEGIN(C, Object*, ob, selected_objects) {
1710                 if(ob->id.lib)
1711                         id_make_local(&ob->id, 0);
1712         }
1713         CTX_DATA_END;
1714         
1715         /* maybe object pointers */
1716         CTX_DATA_BEGIN(C, Object*, ob, selected_objects) {
1717                 if(ob->id.lib==NULL) {
1718                         ID_NEW(ob->parent);
1719                 }
1720         }
1721         CTX_DATA_END;
1722
1723         CTX_DATA_BEGIN(C, Object*, ob, selected_objects) {
1724                 id= ob->data;
1725                         
1726                 if(id && mode>1) {
1727                         id_make_local(id, 0);
1728                         adt= BKE_animdata_from_id(id);
1729                         if(adt) BKE_animdata_make_local(adt);
1730                         
1731                         /* tag indirect data direct */
1732                         matarar= (Material ***)give_matarar(ob);
1733                         if(matarar) {
1734                                 for(a=0; a<ob->totcol; a++) {
1735                                         ma= (*matarar)[a];
1736                                         if(ma)
1737                                                 id_lib_extern(&ma->id);
1738                                 }
1739                         }
1740                 }
1741
1742                 for(psys=ob->particlesystem.first; psys; psys=psys->next)
1743                         id_make_local(&psys->part->id, 0);
1744
1745                 adt= BKE_animdata_from_id(&ob->id);
1746                 if(adt) BKE_animdata_make_local(adt);
1747         }
1748         CTX_DATA_END;
1749
1750         if(mode>1) {
1751                 CTX_DATA_BEGIN(C, Object*, ob, selected_objects) {
1752                         if(ob->type==OB_LAMP) {
1753                                 la= ob->data;
1754
1755                                 for(b=0; b<MAX_MTEX; b++)
1756                                         if(la->mtex[b] && la->mtex[b]->tex)
1757                                                 id_make_local(&la->mtex[b]->tex->id, 0);
1758                         }
1759                         else {
1760                                 for(a=0; a<ob->totcol; a++) {
1761                                         ma= ob->mat[a];
1762                                         if(ma)
1763                                                 make_local_makelocalmaterial(ma);
1764                                 }
1765                                 
1766                                 matarar= (Material ***)give_matarar(ob);
1767                                 if(matarar) {
1768                                         for(a=0; a<ob->totcol; a++) {
1769                                                 ma= (*matarar)[a];
1770                                                 if(ma)
1771                                                         make_local_makelocalmaterial(ma);
1772                                         }
1773                                 }
1774                         }
1775                 }
1776                 CTX_DATA_END;
1777         }
1778
1779         WM_event_add_notifier(C, NC_WINDOW, NULL);
1780
1781         return OPERATOR_FINISHED;
1782 }
1783
1784 void OBJECT_OT_make_local(wmOperatorType *ot)
1785 {
1786         static EnumPropertyItem type_items[]= {
1787                 {1, "SELECTED_OBJECTS", 0, "Selected Objects", ""},
1788                 {2, "SELECTED_OBJECTS_DATA", 0, "Selected Objects and Data", ""},
1789                 {3, "ALL", 0, "All", ""},
1790                 {0, NULL, 0, NULL, NULL}};
1791
1792         /* identifiers */
1793         ot->name= "Make Local";
1794         ot->description = "Make library linked datablocks local to this file";
1795         ot->idname= "OBJECT_OT_make_local";
1796         
1797         /* api callbacks */
1798         ot->invoke= WM_menu_invoke;
1799         ot->exec= make_local_exec;
1800         ot->poll= ED_operator_objectmode;
1801         
1802         /* flags */
1803         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
1804         
1805         /* properties */
1806         ot->prop= RNA_def_enum(ot->srna, "type", type_items, 0, "Type", "");
1807 }
1808
1809 static int make_single_user_exec(bContext *C, wmOperator *op)
1810 {
1811         Main *bmain= CTX_data_main(C);
1812         Scene *scene= CTX_data_scene(C);
1813         View3D *v3d= CTX_wm_view3d(C); /* ok if this is NULL */
1814         int flag= RNA_enum_get(op->ptr, "type"); /* 0==ALL, SELECTED==selected objecs */
1815
1816         if(RNA_boolean_get(op->ptr, "object"))
1817                 single_object_users(scene, v3d, flag);
1818
1819         if(RNA_boolean_get(op->ptr, "obdata"))
1820                 single_obdata_users(bmain, scene, flag);
1821
1822         if(RNA_boolean_get(op->ptr, "material"))
1823                 single_mat_users(scene, flag, FALSE);
1824
1825         if(RNA_boolean_get(op->ptr, "texture"))
1826                 single_mat_users(scene, flag, TRUE);
1827
1828         if(RNA_boolean_get(op->ptr, "animation"))
1829                 single_object_action_users(scene, flag);
1830
1831         clear_id_newpoins();
1832
1833         WM_event_add_notifier(C, NC_WINDOW, NULL);
1834         return OPERATOR_FINISHED;
1835 }
1836
1837 void OBJECT_OT_make_single_user(wmOperatorType *ot)
1838 {
1839         static EnumPropertyItem type_items[]= {
1840                 {SELECT, "SELECTED_OBJECTS", 0, "Selected Objects", ""},
1841                 {0, "ALL", 0, "All", ""},
1842                 {0, NULL, 0, NULL, NULL}};
1843
1844         /* identifiers */
1845         ot->name= "Make Single User";
1846         ot->description = "Make linked data local to each object";
1847         ot->idname= "OBJECT_OT_make_single_user";
1848
1849         /* api callbacks */
1850         ot->invoke= WM_menu_invoke;
1851         ot->exec= make_single_user_exec;
1852         ot->poll= ED_operator_objectmode;
1853
1854         /* flags */
1855         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
1856
1857         /* properties */
1858         ot->prop= RNA_def_enum(ot->srna, "type", type_items, SELECT, "Type", "");
1859
1860         RNA_def_boolean(ot->srna, "object", 0, "Object", "Make single user objects");
1861         RNA_def_boolean(ot->srna, "obdata", 0, "Object Data", "Make single user object data");
1862         RNA_def_boolean(ot->srna, "material", 0, "Materials", "Make materials local to each datablock");
1863         RNA_def_boolean(ot->srna, "texture", 0, "Textures", "Make textures local to each material");
1864         RNA_def_boolean(ot->srna, "animation", 0, "Object Animation", "Make animation data local to each object");
1865 }
1866
1867 static int drop_named_material_invoke(bContext *C, wmOperator *op, wmEvent *event)
1868 {
1869         Main *bmain= CTX_data_main(C);
1870         Base *base= ED_view3d_give_base_under_cursor(C, event->mval);
1871         Material *ma;
1872         char name[32];
1873         
1874         RNA_string_get(op->ptr, "name", name);
1875         ma= (Material *)find_id("MA", name);
1876         if(base==NULL || ma==NULL) 
1877                 return OPERATOR_CANCELLED;
1878         
1879         assign_material(base->object, ma, 1);
1880         
1881         DAG_ids_flush_update(bmain, 0);
1882         WM_event_add_notifier(C, NC_SPACE|ND_SPACE_VIEW3D, CTX_wm_view3d(C));
1883         
1884         return OPERATOR_FINISHED;
1885 }
1886
1887 /* used for dropbox */
1888 /* assigns to object under cursor, only first material slot */
1889 void OBJECT_OT_drop_named_material(wmOperatorType *ot)
1890 {
1891
1892         /* identifiers */
1893         ot->name= "Drop Named Material on Object";
1894         ot->description = "";
1895         ot->idname= "OBJECT_OT_drop_named_material";
1896         
1897         /* api callbacks */
1898         ot->invoke= drop_named_material_invoke;
1899         ot->poll= ED_operator_objectmode;
1900         
1901         /* flags */
1902         ot->flag= OPTYPE_UNDO;
1903         
1904         /* properties */
1905         RNA_def_string(ot->srna, "name", "Material", 24, "Name", "Material name to assign.");
1906 }