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