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