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