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