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