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