2 * ***** BEGIN GPL LICENSE BLOCK *****
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.
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.
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.
18 * The Original Code is Copyright (C) 2001-2002 by NaN Holding BV.
19 * All rights reserved.
21 * Contributor(s): Blender Foundation, 2002-2008 full recode
23 * ***** END GPL LICENSE BLOCK *****
26 /** \file blender/editors/object/object_relations.c
35 #include "MEM_guardedalloc.h"
37 #include "DNA_anim_types.h"
38 #include "DNA_armature_types.h"
39 #include "DNA_mesh_types.h"
40 #include "DNA_collection_types.h"
41 #include "DNA_constraint_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_world_types.h"
49 #include "DNA_object_types.h"
50 #include "DNA_vfont_types.h"
51 #include "DNA_gpencil_types.h"
54 #include "BLI_listbase.h"
55 #include "BLI_linklist.h"
56 #include "BLI_string.h"
57 #include "BLI_kdtree.h"
58 #include "BLI_utildefines.h"
60 #include "BLT_translation.h"
62 #include "BKE_action.h"
63 #include "BKE_animsys.h"
64 #include "BKE_armature.h"
65 #include "BKE_camera.h"
66 #include "BKE_collection.h"
67 #include "BKE_context.h"
68 #include "BKE_constraint.h"
69 #include "BKE_curve.h"
70 #include "BKE_DerivedMesh.h"
71 #include "BKE_displist.h"
72 #include "BKE_editmesh.h"
73 #include "BKE_global.h"
74 #include "BKE_gpencil.h"
75 #include "BKE_fcurve.h"
76 #include "BKE_idprop.h"
78 #include "BKE_lattice.h"
79 #include "BKE_layer.h"
80 #include "BKE_library.h"
81 #include "BKE_library_override.h"
82 #include "BKE_library_query.h"
83 #include "BKE_library_remap.h"
84 #include "BKE_lightprobe.h"
86 #include "BKE_material.h"
87 #include "BKE_mball.h"
89 #include "BKE_modifier.h"
91 #include "BKE_object.h"
92 #include "BKE_report.h"
93 #include "BKE_scene.h"
94 #include "BKE_speaker.h"
95 #include "BKE_texture.h"
97 #include "DEG_depsgraph.h"
98 #include "DEG_depsgraph_build.h"
101 #include "WM_types.h"
103 #include "UI_interface.h"
104 #include "UI_resources.h"
106 #include "RNA_access.h"
107 #include "RNA_define.h"
108 #include "RNA_enum_types.h"
110 #include "ED_armature.h"
111 #include "ED_curve.h"
112 #include "ED_gpencil.h"
113 #include "ED_keyframing.h"
114 #include "ED_object.h"
116 #include "ED_screen.h"
117 #include "ED_view3d.h"
119 #include "object_intern.h"
121 /*********************** Make Vertex Parent Operator ************************/
123 static bool vertex_parent_set_poll(bContext *C)
125 return ED_operator_editmesh(C) || ED_operator_editsurfcurve(C) || ED_operator_editlattice(C);
128 static int vertex_parent_set_exec(bContext *C, wmOperator *op)
130 Main *bmain = CTX_data_main(C);
131 Scene *scene = CTX_data_scene(C);
132 View3D *v3d = CTX_wm_view3d(C);
133 Depsgraph *depsgraph = CTX_data_depsgraph(C);
134 ViewLayer *view_layer = CTX_data_view_layer(C);
135 Object *obedit = CTX_data_edit_object(C);
142 int a, v1 = 0, v2 = 0, v3 = 0, v4 = 0, nr = 1;
144 /* we need 1 to 3 selected vertices */
146 if (obedit->type == OB_MESH) {
147 Mesh *me = obedit->data;
150 EDBM_mesh_load(bmain, obedit);
151 EDBM_mesh_make(obedit, scene->toolsettings->selectmode, true);
153 DEG_id_tag_update(obedit->data, 0);
155 em = me->edit_btmesh;
157 EDBM_mesh_normals_update(em);
158 BKE_editmesh_tessface_calc(em);
160 /* derivedMesh might be needed for solving parenting,
161 * so re-create it here */
162 makeDerivedMesh(depsgraph, scene, obedit, em, CD_MASK_BAREMESH | CD_MASK_ORIGINDEX, false);
164 BM_ITER_MESH (eve, &iter, em->bm, BM_VERTS_OF_MESH) {
165 if (BM_elem_flag_test(eve, BM_ELEM_SELECT)) {
166 if (v1 == 0) v1 = nr;
167 else if (v2 == 0) v2 = nr;
168 else if (v3 == 0) v3 = nr;
169 else if (v4 == 0) v4 = nr;
175 else if (ELEM(obedit->type, OB_SURF, OB_CURVE)) {
176 ListBase *editnurb = object_editcurve_get(obedit);
178 nu = editnurb->first;
180 if (nu->type == CU_BEZIER) {
184 if (BEZT_ISSEL_ANY_HIDDENHANDLES(v3d, bezt)) {
185 if (v1 == 0) v1 = nr;
186 else if (v2 == 0) v2 = nr;
187 else if (v3 == 0) v3 = nr;
188 else if (v4 == 0) v4 = nr;
197 a = nu->pntsu * nu->pntsv;
199 if (bp->f1 & SELECT) {
200 if (v1 == 0) v1 = nr;
201 else if (v2 == 0) v2 = nr;
202 else if (v3 == 0) v3 = nr;
203 else if (v4 == 0) v4 = nr;
213 else if (obedit->type == OB_LATTICE) {
214 Lattice *lt = obedit->data;
216 a = lt->editlatt->latt->pntsu * lt->editlatt->latt->pntsv * lt->editlatt->latt->pntsw;
217 bp = lt->editlatt->latt->def;
219 if (bp->f1 & SELECT) {
220 if (v1 == 0) v1 = nr;
221 else if (v2 == 0) v2 = nr;
222 else if (v3 == 0) v3 = nr;
223 else if (v4 == 0) v4 = nr;
231 if (v4 || !((v1 && v2 == 0 && v3 == 0) || (v1 && v2 && v3))) {
232 BKE_report(op->reports, RPT_ERROR, "Select either 1 or 3 vertices to parent to");
233 return OPERATOR_CANCELLED;
236 CTX_DATA_BEGIN (C, Object *, ob, selected_editable_objects)
239 DEG_id_tag_update(&ob->id, ID_RECALC_TRANSFORM | ID_RECALC_GEOMETRY | ID_RECALC_ANIMATION);
240 par = obedit->parent;
242 if (BKE_object_parent_loop_check(par, ob)) {
243 BKE_report(op->reports, RPT_ERROR, "Loop in parents");
248 ob->parent = BASACT(view_layer)->object;
250 ob->partype = PARVERT3;
255 /* inverse parent matrix */
256 BKE_object_workob_calc_parent(depsgraph, scene, ob, &workob);
257 invert_m4_m4(ob->parentinv, workob.obmat);
260 ob->partype = PARVERT1;
263 /* inverse parent matrix */
264 BKE_object_workob_calc_parent(depsgraph, scene, ob, &workob);
265 invert_m4_m4(ob->parentinv, workob.obmat);
272 DEG_relations_tag_update(bmain);
274 WM_event_add_notifier(C, NC_OBJECT, NULL);
276 return OPERATOR_FINISHED;
279 void OBJECT_OT_vertex_parent_set(wmOperatorType *ot)
282 ot->name = "Make Vertex Parent";
283 ot->description = "Parent selected objects to the selected vertices";
284 ot->idname = "OBJECT_OT_vertex_parent_set";
287 ot->invoke = WM_operator_confirm;
288 ot->poll = vertex_parent_set_poll;
289 ot->exec = vertex_parent_set_exec;
292 ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
295 /********************** Make Proxy Operator *************************/
297 /* set the object to proxify */
298 static int make_proxy_invoke(bContext *C, wmOperator *op, const wmEvent *event)
300 Scene *scene = CTX_data_scene(C);
301 Object *ob = ED_object_active_context(C);
304 if (!scene || ID_IS_LINKED(scene) || !ob)
305 return OPERATOR_CANCELLED;
307 /* Get object to work on - use a menu if we need to... */
308 if (ob->dup_group && ID_IS_LINKED(ob->dup_group)) {
309 /* gives menu with list of objects in group */
310 /* proxy_group_objects_menu(C, op, ob, ob->dup_group); */
311 WM_enum_search_invoke(C, op, event);
312 return OPERATOR_CANCELLED;
314 else if (ID_IS_LINKED(ob)) {
315 uiPopupMenu *pup = UI_popup_menu_begin(C, IFACE_("OK?"), ICON_QUESTION);
316 uiLayout *layout = UI_popup_menu_layout(pup);
318 /* create operator menu item with relevant properties filled in */
319 PointerRNA opptr_dummy;
320 uiItemFullO_ptr(layout, op->type, op->type->name, ICON_NONE, NULL,
321 WM_OP_EXEC_REGION_WIN, 0, &opptr_dummy);
323 /* present the menu and be done... */
324 UI_popup_menu_end(C, pup);
326 /* this invoke just calls another instance of this operator... */
327 return OPERATOR_INTERFACE;
330 /* error.. cannot continue */
331 BKE_report(op->reports, RPT_ERROR, "Can only make proxy for a referenced object or collection");
332 return OPERATOR_CANCELLED;
337 static int make_proxy_exec(bContext *C, wmOperator *op)
339 Main *bmain = CTX_data_main(C);
340 Object *ob, *gob = ED_object_active_context(C);
341 Scene *scene = CTX_data_scene(C);
342 ViewLayer *view_layer = CTX_data_view_layer(C);
344 if (gob->dup_group != NULL) {
345 const ListBase dup_group_objects = BKE_collection_object_cache_get(gob->dup_group);
346 Base *base = BLI_findlink(&dup_group_objects, RNA_enum_get(op->ptr, "object"));
355 char name[MAX_ID_NAME + 4];
357 BLI_snprintf(name, sizeof(name), "%s_proxy", ((ID *)(gob ? gob : ob))->name + 2);
359 /* Add new object for the proxy */
360 newob = BKE_object_add_from(bmain, scene, view_layer, OB_EMPTY, name, gob ? gob : ob);
363 BKE_object_make_proxy(bmain, newob, ob, gob);
365 /* Set back pointer immediately so dependency graph knows that this is
366 * is a proxy and will act accordingly. Otherwise correctness of graph
367 * will depend on order of bases.
369 * TODO(sergey): We really need to get rid of this bi-directional links
370 * in proxies with something like static overrides.
372 newob->proxy->proxy_from = newob;
374 /* depsgraph flushes are needed for the new data */
375 DEG_relations_tag_update(bmain);
376 DEG_id_tag_update(&newob->id, ID_RECALC_TRANSFORM | ID_RECALC_GEOMETRY | ID_RECALC_ANIMATION);
377 WM_event_add_notifier(C, NC_OBJECT | ND_DRAW, newob);
380 BKE_report(op->reports, RPT_ERROR, "No object to make proxy for");
381 return OPERATOR_CANCELLED;
384 return OPERATOR_FINISHED;
387 /* Generic itemf's for operators that take library args */
388 static const EnumPropertyItem *proxy_collection_object_itemf(bContext *C, PointerRNA *UNUSED(ptr),
389 PropertyRNA *UNUSED(prop), bool *r_free)
391 EnumPropertyItem item_tmp = {0}, *item = NULL;
394 Object *ob = ED_object_active_context(C);
396 if (!ob || !ob->dup_group)
397 return DummyRNA_DEFAULT_items;
399 /* find the object to affect */
400 FOREACH_COLLECTION_OBJECT_RECURSIVE_BEGIN(ob->dup_group, object)
402 item_tmp.identifier = item_tmp.name = object->id.name + 2;
403 item_tmp.value = i++;
404 RNA_enum_item_add(&item, &totitem, &item_tmp);
406 FOREACH_COLLECTION_OBJECT_RECURSIVE_END;
408 RNA_enum_item_end(&item, &totitem);
414 void OBJECT_OT_proxy_make(wmOperatorType *ot)
419 ot->name = "Make Proxy";
420 ot->idname = "OBJECT_OT_proxy_make";
421 ot->description = "Add empty object to become local replacement data of a library-linked object";
424 ot->invoke = make_proxy_invoke;
425 ot->exec = make_proxy_exec;
426 ot->poll = ED_operator_object_active;
429 ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
432 /* XXX, relies on hard coded ID at the moment */
433 prop = RNA_def_enum(ot->srna, "object", DummyRNA_DEFAULT_items, 0, "Proxy Object",
434 "Name of lib-linked/collection object to make a proxy for");
435 RNA_def_enum_funcs(prop, proxy_collection_object_itemf);
436 RNA_def_property_flag(prop, PROP_ENUM_NO_TRANSLATE);
440 /********************** Clear Parent Operator ******************* */
442 typedef enum eObClearParentTypes {
443 CLEAR_PARENT_ALL = 0,
444 CLEAR_PARENT_KEEP_TRANSFORM,
446 } eObClearParentTypes;
448 EnumPropertyItem prop_clear_parent_types[] = {
449 {CLEAR_PARENT_ALL, "CLEAR", 0, "Clear Parent",
450 "Completely clear the parenting relationship, including involved modifiers if any"},
451 {CLEAR_PARENT_KEEP_TRANSFORM, "CLEAR_KEEP_TRANSFORM", 0, "Clear and Keep Transformation",
452 "As 'Clear Parent', but keep the current visual transformations of the object"},
453 {CLEAR_PARENT_INVERSE, "CLEAR_INVERSE", 0, "Clear Parent Inverse",
454 "Reset the transform corrections applied to the parenting relationship, does not remove parenting itself"},
455 {0, NULL, 0, NULL, NULL}
458 /* Helper for ED_object_parent_clear() - Remove deform-modifiers associated with parent */
459 static void object_remove_parent_deform_modifiers(Object *ob, const Object *par)
461 if (ELEM(par->type, OB_ARMATURE, OB_LATTICE, OB_CURVE)) {
462 ModifierData *md, *mdn;
464 /* assume that we only need to remove the first instance of matching deform modifier here */
465 for (md = ob->modifiers.first; md; md = mdn) {
470 /* need to match types (modifier + parent) and references */
471 if ((md->type == eModifierType_Armature) && (par->type == OB_ARMATURE)) {
472 ArmatureModifierData *amd = (ArmatureModifierData *)md;
473 if (amd->object == par) {
477 else if ((md->type == eModifierType_Lattice) && (par->type == OB_LATTICE)) {
478 LatticeModifierData *lmd = (LatticeModifierData *)md;
479 if (lmd->object == par) {
483 else if ((md->type == eModifierType_Curve) && (par->type == OB_CURVE)) {
484 CurveModifierData *cmd = (CurveModifierData *)md;
485 if (cmd->object == par) {
490 /* free modifier if match */
492 BLI_remlink(&ob->modifiers, md);
499 void ED_object_parent_clear(Object *ob, const int type)
501 if (ob->parent == NULL)
505 case CLEAR_PARENT_ALL:
507 /* for deformers, remove corresponding modifiers to prevent a large number of modifiers building up */
508 object_remove_parent_deform_modifiers(ob, ob->parent);
510 /* clear parenting relationship completely */
514 case CLEAR_PARENT_KEEP_TRANSFORM:
516 /* remove parent, and apply the parented transform result as object's local transforms */
518 BKE_object_apply_mat4(ob, ob->obmat, true, false);
521 case CLEAR_PARENT_INVERSE:
523 /* object stays parented, but the parent inverse (i.e. offset from parent to retain binding state)
524 * is cleared. In other words: nothing to do here! */
529 /* Always clear parentinv matrix for sake of consistency, see T41950. */
530 unit_m4(ob->parentinv);
532 DEG_id_tag_update(&ob->id, ID_RECALC_TRANSFORM | ID_RECALC_GEOMETRY | ID_RECALC_ANIMATION);
535 /* note, poll should check for editable scene */
536 static int parent_clear_exec(bContext *C, wmOperator *op)
538 Main *bmain = CTX_data_main(C);
539 const int type = RNA_enum_get(op->ptr, "type");
541 CTX_DATA_BEGIN (C, Object *, ob, selected_editable_objects)
543 ED_object_parent_clear(ob, type);
547 DEG_relations_tag_update(bmain);
548 WM_event_add_notifier(C, NC_OBJECT | ND_TRANSFORM, NULL);
549 WM_event_add_notifier(C, NC_OBJECT | ND_PARENT, NULL);
550 return OPERATOR_FINISHED;
553 void OBJECT_OT_parent_clear(wmOperatorType *ot)
556 ot->name = "Clear Parent";
557 ot->description = "Clear the object's parenting";
558 ot->idname = "OBJECT_OT_parent_clear";
561 ot->invoke = WM_menu_invoke;
562 ot->exec = parent_clear_exec;
564 ot->poll = ED_operator_object_active_editable;
567 ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
569 ot->prop = RNA_def_enum(ot->srna, "type", prop_clear_parent_types, CLEAR_PARENT_ALL, "Type", "");
572 /* ******************** Make Parent Operator *********************** */
574 void ED_object_parent(Object *ob, Object *par, const int type, const char *substr)
576 /* Always clear parentinv matrix for sake of consistency, see T41950. */
577 unit_m4(ob->parentinv);
579 if (!par || BKE_object_parent_loop_check(par, ob)) {
581 ob->partype = PAROBJECT;
582 ob->parsubstr[0] = 0;
586 /* Other partypes are deprecated, do not use here! */
587 BLI_assert(ELEM(type & PARTYPE, PAROBJECT, PARSKEL, PARVERT1, PARVERT3, PARBONE));
589 /* this could use some more checks */
592 ob->partype &= ~PARTYPE;
594 BLI_strncpy(ob->parsubstr, substr, sizeof(ob->parsubstr));
597 /* Operator Property */
598 EnumPropertyItem prop_make_parent_types[] = {
599 {PAR_OBJECT, "OBJECT", 0, "Object", ""},
600 {PAR_ARMATURE, "ARMATURE", 0, "Armature Deform", ""},
601 {PAR_ARMATURE_NAME, "ARMATURE_NAME", 0, " With Empty Groups", ""},
602 {PAR_ARMATURE_AUTO, "ARMATURE_AUTO", 0, " With Automatic Weights", ""},
603 {PAR_ARMATURE_ENVELOPE, "ARMATURE_ENVELOPE", 0, " With Envelope Weights", ""},
604 {PAR_BONE, "BONE", 0, "Bone", ""},
605 {PAR_BONE_RELATIVE, "BONE_RELATIVE", 0, "Bone Relative", ""},
606 {PAR_CURVE, "CURVE", 0, "Curve Deform", ""},
607 {PAR_FOLLOW, "FOLLOW", 0, "Follow Path", ""},
608 {PAR_PATH_CONST, "PATH_CONST", 0, "Path Constraint", ""},
609 {PAR_LATTICE, "LATTICE", 0, "Lattice Deform", ""},
610 {PAR_VERTEX, "VERTEX", 0, "Vertex", ""},
611 {PAR_VERTEX_TRI, "VERTEX_TRI", 0, "Vertex (Triangle)", ""},
612 {0, NULL, 0, NULL, NULL}
615 bool ED_object_parent_set(ReportList *reports, const bContext *C, Scene *scene, Object *ob, Object *par,
616 int partype, const bool xmirror, const bool keep_transform, const int vert_par[3])
618 Main *bmain = CTX_data_main(C);
619 Depsgraph *depsgraph = CTX_data_depsgraph(C);
620 bPoseChannel *pchan = NULL;
621 const bool pararm = ELEM(partype, PAR_ARMATURE, PAR_ARMATURE_NAME, PAR_ARMATURE_ENVELOPE, PAR_ARMATURE_AUTO);
623 DEG_id_tag_update(&par->id, ID_RECALC_TRANSFORM);
626 if (partype == PAR_FOLLOW || partype == PAR_PATH_CONST) {
627 if (par->type != OB_CURVE)
630 Curve *cu = par->data;
632 if ((cu->flag & CU_PATH) == 0) {
633 cu->flag |= CU_PATH | CU_FOLLOW;
634 BKE_displist_make_curveTypes(depsgraph, scene, par, false, false); /* force creation of path data */
637 cu->flag |= CU_FOLLOW;
640 /* if follow, add F-Curve for ctime (i.e. "eval_time") so that path-follow works */
641 if (partype == PAR_FOLLOW) {
642 /* get or create F-Curve */
643 bAction *act = verify_adt_action(bmain, &cu->id, 1);
644 FCurve *fcu = verify_fcurve(bmain, act, NULL, NULL, "eval_time", 0, 1);
646 /* setup dummy 'generator' modifier here to get 1-1 correspondence still working */
647 if (!fcu->bezt && !fcu->fpt && !fcu->modifiers.first)
648 add_fmodifier(&fcu->modifiers, FMODIFIER_TYPE_GENERATOR, fcu);
651 /* fall back on regular parenting now (for follow only) */
652 if (partype == PAR_FOLLOW)
653 partype = PAR_OBJECT;
656 else if (ELEM(partype, PAR_BONE, PAR_BONE_RELATIVE)) {
657 pchan = BKE_pose_channel_active(par);
660 BKE_report(reports, RPT_ERROR, "No active bone");
666 if (BKE_object_parent_loop_check(par, ob)) {
667 BKE_report(reports, RPT_ERROR, "Loop in parents");
673 /* apply transformation of previous parenting */
674 if (keep_transform) {
675 /* was removed because of bug [#23577],
676 * but this can be handy in some cases too [#32616], so make optional */
677 BKE_object_apply_mat4(ob, ob->obmat, false, false);
680 /* set the parent (except for follow-path constraint option) */
681 if (partype != PAR_PATH_CONST) {
683 /* Always clear parentinv matrix for sake of consistency, see T41950. */
684 unit_m4(ob->parentinv);
689 BLI_strncpy(ob->parsubstr, pchan->name, sizeof(ob->parsubstr));
691 ob->parsubstr[0] = 0;
693 if (partype == PAR_PATH_CONST) {
694 /* don't do anything here, since this is not technically "parenting" */
696 else if (ELEM(partype, PAR_CURVE, PAR_LATTICE) || (pararm)) {
697 /* partype is now set to PAROBJECT so that invisible 'virtual' modifiers don't need to be created
698 * NOTE: the old (2.4x) method was to set ob->partype = PARSKEL, creating the virtual modifiers
700 ob->partype = PAROBJECT; /* note, dna define, not operator property */
701 /* ob->partype = PARSKEL; */ /* note, dna define, not operator property */
703 /* BUT, to keep the deforms, we need a modifier, and then we need to set the object that it uses
704 * - We need to ensure that the modifier we're adding doesn't already exist, so we check this by
705 * assuming that the parent is selected too...
707 /* XXX currently this should only happen for meshes, curves, surfaces,
708 * and lattices - this stuff isn't available for metas yet */
709 if (ELEM(ob->type, OB_MESH, OB_CURVE, OB_SURF, OB_FONT, OB_LATTICE)) {
713 case PAR_CURVE: /* curve deform */
714 if (modifiers_isDeformedByCurve(ob) != par) {
715 md = ED_object_modifier_add(reports, bmain, scene, ob, NULL, eModifierType_Curve);
717 ((CurveModifierData *)md)->object = par;
719 if (par->runtime.curve_cache && par->runtime.curve_cache->path == NULL) {
720 DEG_id_tag_update(&par->id, ID_RECALC_GEOMETRY);
724 case PAR_LATTICE: /* lattice deform */
725 if (modifiers_isDeformedByLattice(ob) != par) {
726 md = ED_object_modifier_add(reports, bmain, scene, ob, NULL, eModifierType_Lattice);
728 ((LatticeModifierData *)md)->object = par;
732 default: /* armature deform */
733 if (modifiers_isDeformedByArmature(ob) != par) {
734 md = ED_object_modifier_add(reports, bmain, scene, ob, NULL, eModifierType_Armature);
736 ((ArmatureModifierData *)md)->object = par;
743 else if (partype == PAR_BONE) {
744 ob->partype = PARBONE; /* note, dna define, not operator property */
746 pchan->bone->flag &= ~BONE_RELATIVE_PARENTING;
748 else if (partype == PAR_BONE_RELATIVE) {
749 ob->partype = PARBONE; /* note, dna define, not operator property */
751 pchan->bone->flag |= BONE_RELATIVE_PARENTING;
753 else if (partype == PAR_VERTEX) {
754 ob->partype = PARVERT1;
755 ob->par1 = vert_par[0];
757 else if (partype == PAR_VERTEX_TRI) {
758 ob->partype = PARVERT3;
759 copy_v3_v3_int(&ob->par1, vert_par);
762 ob->partype = PAROBJECT; /* note, dna define, not operator property */
766 if (partype == PAR_PATH_CONST) {
768 bFollowPathConstraint *data;
769 float cmat[4][4], vec[3];
771 con = BKE_constraint_add_for_object(ob, "AutoPath", CONSTRAINT_TYPE_FOLLOWPATH);
776 BKE_constraint_target_matrix_get(depsgraph, scene, con, 0, CONSTRAINT_OBTYPE_OBJECT, NULL, cmat, scene->r.cfra);
777 sub_v3_v3v3(vec, ob->obmat[3], cmat[3]);
779 copy_v3_v3(ob->loc, vec);
781 else if (pararm && (ob->type == OB_MESH) && (par->type == OB_ARMATURE)) {
782 if (partype == PAR_ARMATURE_NAME) {
783 ED_object_vgroup_calc_from_armature(reports, depsgraph, scene, ob, par, ARM_GROUPS_NAME, false);
785 else if (partype == PAR_ARMATURE_ENVELOPE) {
786 ED_object_vgroup_calc_from_armature(reports, depsgraph, scene, ob, par, ARM_GROUPS_ENVELOPE, xmirror);
788 else if (partype == PAR_ARMATURE_AUTO) {
790 ED_object_vgroup_calc_from_armature(reports, depsgraph, scene, ob, par, ARM_GROUPS_AUTO, xmirror);
793 /* get corrected inverse */
794 ob->partype = PAROBJECT;
795 BKE_object_workob_calc_parent(depsgraph, scene, ob, &workob);
797 invert_m4_m4(ob->parentinv, workob.obmat);
799 else if (pararm && (ob->type == OB_GPENCIL) && (par->type == OB_ARMATURE)) {
800 if (partype == PAR_ARMATURE_NAME) {
801 ED_gpencil_add_armature_weights(C, reports, ob, par, GP_PAR_ARMATURE_NAME);
803 else if ((partype == PAR_ARMATURE_AUTO) ||
804 (partype == PAR_ARMATURE_ENVELOPE))
807 ED_gpencil_add_armature_weights(C, reports, ob, par, GP_PAR_ARMATURE_AUTO);
810 /* get corrected inverse */
811 ob->partype = PAROBJECT;
812 BKE_object_workob_calc_parent(depsgraph, scene, ob, &workob);
814 invert_m4_m4(ob->parentinv, workob.obmat);
817 /* calculate inverse parent matrix */
818 BKE_object_workob_calc_parent(depsgraph, scene, ob, &workob);
819 invert_m4_m4(ob->parentinv, workob.obmat);
822 DEG_id_tag_update(&ob->id, ID_RECALC_TRANSFORM | ID_RECALC_GEOMETRY);
831 static void parent_set_vert_find(KDTree *tree, Object *child, int vert_par[3], bool is_tri)
833 const float *co_find = child->obmat[3];
835 KDTreeNearest nearest[3];
838 tot = BLI_kdtree_find_nearest_n(tree, co_find, nearest, 3);
839 BLI_assert(tot == 3);
842 vert_par[0] = nearest[0].index;
843 vert_par[1] = nearest[1].index;
844 vert_par[2] = nearest[2].index;
846 BLI_assert(min_iii(UNPACK3(vert_par)) >= 0);
849 vert_par[0] = BLI_kdtree_find_nearest(tree, co_find, NULL);
850 BLI_assert(vert_par[0] >= 0);
856 static int parent_set_exec(bContext *C, wmOperator *op)
858 Main *bmain = CTX_data_main(C);
859 Scene *scene = CTX_data_scene(C);
860 Object *par = ED_object_active_context(C);
861 int partype = RNA_enum_get(op->ptr, "type");
862 const bool xmirror = RNA_boolean_get(op->ptr, "xmirror");
863 const bool keep_transform = RNA_boolean_get(op->ptr, "keep_transform");
866 /* vertex parent (kdtree) */
867 const bool is_vert_par = ELEM(partype, PAR_VERTEX, PAR_VERTEX_TRI);
868 const bool is_tri = partype == PAR_VERTEX_TRI;
870 struct KDTree *tree = NULL;
871 int vert_par[3] = {0, 0, 0};
872 const int *vert_par_p = is_vert_par ? vert_par : NULL;
876 tree = BKE_object_as_kdtree(par, &tree_tot);
877 BLI_assert(tree != NULL);
879 if (tree_tot < (is_tri ? 3 : 1)) {
880 BKE_report(op->reports, RPT_ERROR, "Not enough vertices for vertex-parent");
886 /* Non vertex-parent */
887 CTX_DATA_BEGIN (C, Object *, ob, selected_editable_objects)
890 parent_set_vert_find(tree, ob, vert_par, is_tri);
893 if (!ED_object_parent_set(op->reports, C, scene, ob, par, partype, xmirror, keep_transform, vert_par_p)) {
902 BLI_kdtree_free(tree);
906 return OPERATOR_CANCELLED;
908 DEG_relations_tag_update(bmain);
909 WM_event_add_notifier(C, NC_OBJECT | ND_TRANSFORM, NULL);
910 WM_event_add_notifier(C, NC_OBJECT | ND_PARENT, NULL);
912 return OPERATOR_FINISHED;
916 static int parent_set_invoke(bContext *C, wmOperator *UNUSED(op), const wmEvent *UNUSED(event))
918 Object *ob = ED_object_active_context(C);
919 uiPopupMenu *pup = UI_popup_menu_begin(C, IFACE_("Set Parent To"), ICON_NONE);
920 uiLayout *layout = UI_popup_menu_layout(pup);
922 wmOperatorType *ot = WM_operatortype_find("OBJECT_OT_parent_set", true);
926 uiItemEnumO_ptr(layout, ot, NULL, 0, "type", PAR_OBJECT);
928 uiItemFullO_ptr(layout, ot, IFACE_("Object"), ICON_NONE, NULL, WM_OP_EXEC_DEFAULT, 0, &opptr);
929 RNA_enum_set(&opptr, "type", PAR_OBJECT);
930 RNA_boolean_set(&opptr, "keep_transform", false);
933 layout, ot, IFACE_("Object (Keep Transform)"), ICON_NONE,
934 NULL, WM_OP_EXEC_DEFAULT, 0, &opptr);
935 RNA_enum_set(&opptr, "type", PAR_OBJECT);
936 RNA_boolean_set(&opptr, "keep_transform", true);
938 /* ob becomes parent, make the associated menus */
939 if (ob->type == OB_ARMATURE) {
940 uiItemEnumO_ptr(layout, ot, NULL, 0, "type", PAR_ARMATURE);
941 uiItemEnumO_ptr(layout, ot, NULL, 0, "type", PAR_ARMATURE_NAME);
942 uiItemEnumO_ptr(layout, ot, NULL, 0, "type", PAR_ARMATURE_ENVELOPE);
943 uiItemEnumO_ptr(layout, ot, NULL, 0, "type", PAR_ARMATURE_AUTO);
944 uiItemEnumO_ptr(layout, ot, NULL, 0, "type", PAR_BONE);
945 uiItemEnumO_ptr(layout, ot, NULL, 0, "type", PAR_BONE_RELATIVE);
947 else if (ob->type == OB_CURVE) {
948 uiItemEnumO_ptr(layout, ot, NULL, 0, "type", PAR_CURVE);
949 uiItemEnumO_ptr(layout, ot, NULL, 0, "type", PAR_FOLLOW);
950 uiItemEnumO_ptr(layout, ot, NULL, 0, "type", PAR_PATH_CONST);
952 else if (ob->type == OB_LATTICE) {
953 uiItemEnumO_ptr(layout, ot, NULL, 0, "type", PAR_LATTICE);
956 /* vertex parenting */
957 if (OB_TYPE_SUPPORT_PARVERT(ob->type)) {
958 uiItemEnumO_ptr(layout, ot, NULL, 0, "type", PAR_VERTEX);
959 uiItemEnumO_ptr(layout, ot, NULL, 0, "type", PAR_VERTEX_TRI);
962 UI_popup_menu_end(C, pup);
964 return OPERATOR_INTERFACE;
967 static bool parent_set_poll_property(const bContext *UNUSED(C), wmOperator *op, const PropertyRNA *prop)
969 const char *prop_id = RNA_property_identifier(prop);
971 /* Only show XMirror for PAR_ARMATURE_ENVELOPE and PAR_ARMATURE_AUTO! */
972 if (STREQ(prop_id, "xmirror")) {
973 const int type = RNA_enum_get(op->ptr, "type");
974 if (ELEM(type, PAR_ARMATURE_ENVELOPE, PAR_ARMATURE_AUTO))
983 void OBJECT_OT_parent_set(wmOperatorType *ot)
986 ot->name = "Make Parent";
987 ot->description = "Set the object's parenting";
988 ot->idname = "OBJECT_OT_parent_set";
991 ot->invoke = parent_set_invoke;
992 ot->exec = parent_set_exec;
993 ot->poll = ED_operator_object_active;
994 ot->poll_property = parent_set_poll_property;
997 ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
999 ot->prop = RNA_def_enum(ot->srna, "type", prop_make_parent_types, 0, "Type", "");
1000 RNA_def_boolean(ot->srna, "xmirror", false, "X Mirror",
1001 "Apply weights symmetrically along X axis, for Envelope/Automatic vertex groups creation");
1002 RNA_def_boolean(ot->srna, "keep_transform", false, "Keep Transform",
1003 "Apply transformation before parenting");
1006 /* ************ Make Parent Without Inverse Operator ******************* */
1008 static int parent_noinv_set_exec(bContext *C, wmOperator *op)
1010 Main *bmain = CTX_data_main(C);
1011 Object *par = ED_object_active_context(C);
1013 DEG_id_tag_update(&par->id, ID_RECALC_TRANSFORM);
1015 /* context iterator */
1016 CTX_DATA_BEGIN (C, Object *, ob, selected_editable_objects)
1019 if (BKE_object_parent_loop_check(par, ob)) {
1020 BKE_report(op->reports, RPT_ERROR, "Loop in parents");
1023 /* clear inverse matrix and also the object location */
1024 unit_m4(ob->parentinv);
1025 memset(ob->loc, 0, 3 * sizeof(float));
1027 /* set recalc flags */
1028 DEG_id_tag_update(&ob->id, ID_RECALC_TRANSFORM | ID_RECALC_GEOMETRY);
1030 /* set parenting type for object - object only... */
1032 ob->partype = PAROBJECT; /* note, dna define, not operator property */
1038 DEG_relations_tag_update(bmain);
1039 WM_event_add_notifier(C, NC_OBJECT | ND_TRANSFORM, NULL);
1041 return OPERATOR_FINISHED;
1044 void OBJECT_OT_parent_no_inverse_set(wmOperatorType *ot)
1047 ot->name = "Make Parent without Inverse";
1048 ot->description = "Set the object's parenting without setting the inverse parent correction";
1049 ot->idname = "OBJECT_OT_parent_no_inverse_set";
1052 ot->invoke = WM_operator_confirm;
1053 ot->exec = parent_noinv_set_exec;
1054 ot->poll = ED_operator_object_active_editable;
1057 ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
1060 /************************ Clear Slow Parent Operator *********************/
1062 static int object_slow_parent_clear_exec(bContext *C, wmOperator *UNUSED(op))
1064 Depsgraph *depsgraph = CTX_data_depsgraph(C);
1065 Scene *scene = CTX_data_scene(C);
1067 CTX_DATA_BEGIN (C, Object *, ob, selected_editable_objects)
1070 if (ob->partype & PARSLOW) {
1071 ob->partype -= PARSLOW;
1072 BKE_object_where_is_calc(depsgraph, scene, ob);
1073 ob->partype |= PARSLOW;
1074 DEG_id_tag_update(&ob->id, ID_RECALC_TRANSFORM);
1080 WM_event_add_notifier(C, NC_SCENE, scene);
1082 return OPERATOR_FINISHED;
1085 void OBJECT_OT_slow_parent_clear(wmOperatorType *ot)
1088 ot->name = "Clear Slow Parent";
1089 ot->description = "Clear the object's slow parent";
1090 ot->idname = "OBJECT_OT_slow_parent_clear";
1093 ot->invoke = WM_operator_confirm;
1094 ot->exec = object_slow_parent_clear_exec;
1095 ot->poll = ED_operator_view3d_active;
1098 ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
1101 /********************** Make Slow Parent Operator *********************/
1103 static int object_slow_parent_set_exec(bContext *C, wmOperator *UNUSED(op))
1105 Scene *scene = CTX_data_scene(C);
1107 CTX_DATA_BEGIN (C, Object *, ob, selected_editable_objects)
1110 ob->partype |= PARSLOW;
1112 DEG_id_tag_update(&ob->id, ID_RECALC_TRANSFORM);
1116 WM_event_add_notifier(C, NC_SCENE, scene);
1118 return OPERATOR_FINISHED;
1121 void OBJECT_OT_slow_parent_set(wmOperatorType *ot)
1124 ot->name = "Set Slow Parent";
1125 ot->description = "Set the object's slow parent";
1126 ot->idname = "OBJECT_OT_slow_parent_set";
1129 ot->invoke = WM_operator_confirm;
1130 ot->exec = object_slow_parent_set_exec;
1131 ot->poll = ED_operator_view3d_active;
1134 ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
1137 /* ******************** Clear Track Operator ******************* */
1141 CLEAR_TRACK_KEEP_TRANSFORM = 2,
1144 static const EnumPropertyItem prop_clear_track_types[] = {
1145 {CLEAR_TRACK, "CLEAR", 0, "Clear Track", ""},
1146 {CLEAR_TRACK_KEEP_TRANSFORM, "CLEAR_KEEP_TRANSFORM", 0, "Clear and Keep Transformation (Clear Track)", ""},
1147 {0, NULL, 0, NULL, NULL}
1150 /* note, poll should check for editable scene */
1151 static int object_track_clear_exec(bContext *C, wmOperator *op)
1153 Main *bmain = CTX_data_main(C);
1154 const int type = RNA_enum_get(op->ptr, "type");
1156 if (CTX_data_edit_object(C)) {
1157 BKE_report(op->reports, RPT_ERROR, "Operation cannot be performed in edit mode");
1158 return OPERATOR_CANCELLED;
1160 CTX_DATA_BEGIN (C, Object *, ob, selected_editable_objects)
1162 bConstraint *con, *pcon;
1164 /* remove track-object for old track */
1166 DEG_id_tag_update(&ob->id, ID_RECALC_TRANSFORM | ID_RECALC_GEOMETRY | ID_RECALC_ANIMATION);
1168 /* also remove all tracking constraints */
1169 for (con = ob->constraints.last; con; con = pcon) {
1171 if (ELEM(con->type, CONSTRAINT_TYPE_TRACKTO, CONSTRAINT_TYPE_LOCKTRACK, CONSTRAINT_TYPE_DAMPTRACK))
1172 BKE_constraint_remove(&ob->constraints, con);
1175 if (type == CLEAR_TRACK_KEEP_TRANSFORM)
1176 BKE_object_apply_mat4(ob, ob->obmat, true, true);
1180 DEG_relations_tag_update(bmain);
1181 WM_event_add_notifier(C, NC_OBJECT | ND_TRANSFORM, NULL);
1183 return OPERATOR_FINISHED;
1186 void OBJECT_OT_track_clear(wmOperatorType *ot)
1189 ot->name = "Clear Track";
1190 ot->description = "Clear tracking constraint or flag from object";
1191 ot->idname = "OBJECT_OT_track_clear";
1194 ot->invoke = WM_menu_invoke;
1195 ot->exec = object_track_clear_exec;
1197 ot->poll = ED_operator_objectmode;
1200 ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
1202 ot->prop = RNA_def_enum(ot->srna, "type", prop_clear_track_types, 0, "Type", "");
1205 /************************** Make Track Operator *****************************/
1208 CREATE_TRACK_DAMPTRACK = 1,
1209 CREATE_TRACK_TRACKTO = 2,
1210 CREATE_TRACK_LOCKTRACK = 3,
1213 static const EnumPropertyItem prop_make_track_types[] = {
1214 {CREATE_TRACK_DAMPTRACK, "DAMPTRACK", 0, "Damped Track Constraint", ""},
1215 {CREATE_TRACK_TRACKTO, "TRACKTO", 0, "Track To Constraint", ""},
1216 {CREATE_TRACK_LOCKTRACK, "LOCKTRACK", 0, "Lock Track Constraint", ""},
1217 {0, NULL, 0, NULL, NULL}
1220 static int track_set_exec(bContext *C, wmOperator *op)
1222 Main *bmain = CTX_data_main(C);
1223 Object *obact = ED_object_active_context(C);
1225 const int type = RNA_enum_get(op->ptr, "type");
1228 case CREATE_TRACK_DAMPTRACK:
1231 bDampTrackConstraint *data;
1233 CTX_DATA_BEGIN (C, Object *, ob, selected_editable_objects)
1236 con = BKE_constraint_add_for_object(ob, "AutoTrack", CONSTRAINT_TYPE_DAMPTRACK);
1240 DEG_id_tag_update(&ob->id, ID_RECALC_TRANSFORM | ID_RECALC_GEOMETRY | ID_RECALC_ANIMATION);
1242 /* Lamp, Camera and Speaker track differently by default */
1243 if (ELEM(ob->type, OB_LAMP, OB_CAMERA, OB_SPEAKER)) {
1244 data->trackflag = TRACK_nZ;
1251 case CREATE_TRACK_TRACKTO:
1254 bTrackToConstraint *data;
1256 CTX_DATA_BEGIN (C, Object *, ob, selected_editable_objects)
1259 con = BKE_constraint_add_for_object(ob, "AutoTrack", CONSTRAINT_TYPE_TRACKTO);
1263 DEG_id_tag_update(&ob->id, ID_RECALC_TRANSFORM | ID_RECALC_GEOMETRY | ID_RECALC_ANIMATION);
1265 /* Lamp, Camera and Speaker track differently by default */
1266 if (ELEM(ob->type, OB_LAMP, OB_CAMERA, OB_SPEAKER)) {
1267 data->reserved1 = TRACK_nZ;
1268 data->reserved2 = UP_Y;
1275 case CREATE_TRACK_LOCKTRACK:
1278 bLockTrackConstraint *data;
1280 CTX_DATA_BEGIN (C, Object *, ob, selected_editable_objects)
1283 con = BKE_constraint_add_for_object(ob, "AutoTrack", CONSTRAINT_TYPE_LOCKTRACK);
1287 DEG_id_tag_update(&ob->id, ID_RECALC_TRANSFORM | ID_RECALC_GEOMETRY | ID_RECALC_ANIMATION);
1289 /* Lamp, Camera and Speaker track differently by default */
1290 if (ELEM(ob->type, OB_LAMP, OB_CAMERA, OB_SPEAKER)) {
1291 data->trackflag = TRACK_nZ;
1292 data->lockflag = LOCK_Y;
1301 DEG_relations_tag_update(bmain);
1302 WM_event_add_notifier(C, NC_OBJECT | ND_TRANSFORM, NULL);
1304 return OPERATOR_FINISHED;
1307 void OBJECT_OT_track_set(wmOperatorType *ot)
1310 ot->name = "Make Track";
1311 ot->description = "Make the object track another object, using various methods/constraints";
1312 ot->idname = "OBJECT_OT_track_set";
1315 ot->invoke = WM_menu_invoke;
1316 ot->exec = track_set_exec;
1318 ot->poll = ED_operator_objectmode;
1321 ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
1324 ot->prop = RNA_def_enum(ot->srna, "type", prop_make_track_types, 0, "Type", "");
1327 /************************** Link to Scene Operator *****************************/
1330 static void link_to_scene(Main *UNUSED(bmain), unsigned short UNUSED(nr))
1332 Scene *sce = (Scene *) BLI_findlink(&bmain->scene, G.curscreen->scenenr - 1);
1335 if (sce == NULL) return;
1336 if (sce->id.lib) return;
1338 for (base = FIRSTBASE; base; base = base->next) {
1339 if (TESTBASE(v3d, base)) {
1340 nbase = MEM_mallocN(sizeof(Base), "newbase");
1342 BLI_addhead(&(sce->base), nbase);
1343 id_us_plus((ID *)base->object);
1349 static int make_links_scene_exec(bContext *C, wmOperator *op)
1351 Main *bmain = CTX_data_main(C);
1352 Scene *scene_to = BLI_findlink(&bmain->scene, RNA_enum_get(op->ptr, "scene"));
1354 if (scene_to == NULL) {
1355 BKE_report(op->reports, RPT_ERROR, "Could not find scene");
1356 return OPERATOR_CANCELLED;
1359 if (scene_to == CTX_data_scene(C)) {
1360 BKE_report(op->reports, RPT_ERROR, "Cannot link objects into the same scene");
1361 return OPERATOR_CANCELLED;
1364 if (ID_IS_LINKED(scene_to)) {
1365 BKE_report(op->reports, RPT_ERROR, "Cannot link objects into a linked scene");
1366 return OPERATOR_CANCELLED;
1369 Collection *collection_to = BKE_collection_master(scene_to);
1370 CTX_DATA_BEGIN (C, Base *, base, selected_bases)
1372 BKE_collection_object_add(bmain, collection_to, base->object);
1376 /* redraw the 3D view because the object center points are colored differently */
1377 WM_event_add_notifier(C, NC_OBJECT | ND_DRAW, NULL);
1379 /* one day multiple scenes will be visible, then we should have some update function for them */
1380 return OPERATOR_FINISHED;
1384 MAKE_LINKS_OBDATA = 1,
1385 MAKE_LINKS_MATERIALS = 2,
1386 MAKE_LINKS_ANIMDATA = 3,
1387 MAKE_LINKS_GROUP = 4,
1388 MAKE_LINKS_DUPLICOLLECTION = 5,
1389 MAKE_LINKS_MODIFIERS = 6,
1390 MAKE_LINKS_FONTS = 7,
1393 /* Return true if make link data is allowed, false otherwise */
1394 static bool allow_make_links_data(const int type, Object *ob_src, Object *ob_dst)
1397 case MAKE_LINKS_OBDATA:
1398 if (ob_src->type == ob_dst->type && ob_src->type != OB_EMPTY) {
1402 case MAKE_LINKS_MATERIALS:
1403 if (OB_TYPE_SUPPORT_MATERIAL(ob_src->type) && OB_TYPE_SUPPORT_MATERIAL(ob_dst->type)) {
1407 case MAKE_LINKS_ANIMDATA:
1408 case MAKE_LINKS_GROUP:
1409 case MAKE_LINKS_DUPLICOLLECTION:
1411 case MAKE_LINKS_MODIFIERS:
1412 if (!ELEM(OB_EMPTY, ob_src->type, ob_dst->type)) {
1416 case MAKE_LINKS_FONTS:
1417 if ((ob_src->data != ob_dst->data) && (ob_src->type == OB_FONT) && (ob_dst->type == OB_FONT)) {
1425 static int make_links_data_exec(bContext *C, wmOperator *op)
1427 Scene *scene = CTX_data_scene(C);
1428 Main *bmain = CTX_data_main(C);
1429 const int type = RNA_enum_get(op->ptr, "type");
1435 LinkNode *ob_collections = NULL;
1436 bool is_cycle = false;
1437 bool is_lib = false;
1439 ob_src = ED_object_active_context(C);
1441 /* avoid searching all collections in source object each time */
1442 if (type == MAKE_LINKS_GROUP) {
1443 ob_collections = BKE_object_groups(bmain, ob_src);
1446 CTX_DATA_BEGIN (C, Base *, base_dst, selected_editable_bases)
1448 Object *ob_dst = base_dst->object;
1450 if (ob_src != ob_dst) {
1451 if (allow_make_links_data(type, ob_src, ob_dst)) {
1452 obdata_id = ob_dst->data;
1455 case MAKE_LINKS_OBDATA: /* obdata */
1456 id_us_min(obdata_id);
1458 obdata_id = ob_src->data;
1459 id_us_plus(obdata_id);
1460 ob_dst->data = obdata_id;
1462 /* if amount of material indices changed: */
1463 test_object_materials(bmain, ob_dst, ob_dst->data);
1465 DEG_id_tag_update(&ob_dst->id, ID_RECALC_GEOMETRY);
1467 case MAKE_LINKS_MATERIALS:
1468 /* new approach, using functions from kernel */
1469 for (a = 0; a < ob_src->totcol; a++) {
1470 Material *ma = give_current_material(ob_src, a + 1);
1471 assign_material(bmain, ob_dst, ma, a + 1, BKE_MAT_ASSIGN_USERPREF); /* also works with ma==NULL */
1473 DEG_id_tag_update(&ob_dst->id, ID_RECALC_GEOMETRY);
1475 case MAKE_LINKS_ANIMDATA:
1476 BKE_animdata_copy_id(bmain, (ID *)ob_dst, (ID *)ob_src, 0);
1477 if (ob_dst->data && ob_src->data) {
1478 if (ID_IS_LINKED(obdata_id)) {
1482 BKE_animdata_copy_id(bmain, (ID *)ob_dst->data, (ID *)ob_src->data, 0);
1484 DEG_id_tag_update(&ob_dst->id, ID_RECALC_TRANSFORM | ID_RECALC_GEOMETRY | ID_RECALC_ANIMATION);
1486 case MAKE_LINKS_GROUP:
1488 LinkNode *collection_node;
1490 /* first clear collections */
1491 BKE_object_groups_clear(bmain, ob_dst);
1493 /* now add in the collections from the link nodes */
1494 for (collection_node = ob_collections; collection_node; collection_node = collection_node->next) {
1495 if (ob_dst->dup_group != collection_node->link) {
1496 BKE_collection_object_add(bmain, collection_node->link, ob_dst);
1504 case MAKE_LINKS_DUPLICOLLECTION:
1505 ob_dst->dup_group = ob_src->dup_group;
1506 if (ob_dst->dup_group) {
1507 id_us_plus(&ob_dst->dup_group->id);
1508 ob_dst->transflag |= OB_DUPLICOLLECTION;
1511 case MAKE_LINKS_MODIFIERS:
1512 BKE_object_link_modifiers(scene, ob_dst, ob_src);
1513 DEG_id_tag_update(&ob_dst->id, ID_RECALC_TRANSFORM | ID_RECALC_GEOMETRY | ID_RECALC_ANIMATION);
1515 case MAKE_LINKS_FONTS:
1517 Curve *cu_src = ob_src->data;
1518 Curve *cu_dst = ob_dst->data;
1520 if (ID_IS_LINKED(obdata_id)) {
1526 id_us_min(&cu_dst->vfont->id);
1527 cu_dst->vfont = cu_src->vfont;
1528 id_us_plus((ID *)cu_dst->vfont);
1530 id_us_min(&cu_dst->vfontb->id);
1531 cu_dst->vfontb = cu_src->vfontb;
1532 id_us_plus((ID *)cu_dst->vfontb);
1534 id_us_min(&cu_dst->vfonti->id);
1535 cu_dst->vfonti = cu_src->vfonti;
1536 id_us_plus((ID *)cu_dst->vfonti);
1537 if (cu_dst->vfontbi)
1538 id_us_min(&cu_dst->vfontbi->id);
1539 cu_dst->vfontbi = cu_src->vfontbi;
1540 id_us_plus((ID *)cu_dst->vfontbi);
1542 DEG_id_tag_update(&ob_dst->id, ID_RECALC_TRANSFORM | ID_RECALC_GEOMETRY | ID_RECALC_ANIMATION);
1551 if (type == MAKE_LINKS_GROUP) {
1552 if (ob_collections) {
1553 BLI_linklist_free(ob_collections, NULL);
1557 BKE_report(op->reports, RPT_WARNING, "Skipped some collections because of cycle detected");
1562 BKE_report(op->reports, RPT_WARNING, "Skipped editing library object data");
1565 DEG_relations_tag_update(bmain);
1566 WM_event_add_notifier(C, NC_SPACE | ND_SPACE_VIEW3D, CTX_wm_view3d(C));
1567 WM_event_add_notifier(C, NC_ANIMATION | ND_NLA_ACTCHANGE, CTX_wm_view3d(C));
1568 WM_event_add_notifier(C, NC_OBJECT, NULL);
1570 return OPERATOR_FINISHED;
1574 void OBJECT_OT_make_links_scene(wmOperatorType *ot)
1579 ot->name = "Link Objects to Scene";
1580 ot->description = "Link selection to another scene";
1581 ot->idname = "OBJECT_OT_make_links_scene";
1584 ot->invoke = WM_enum_search_invoke;
1585 ot->exec = make_links_scene_exec;
1586 /* better not run the poll check */
1589 ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
1592 prop = RNA_def_enum(ot->srna, "scene", DummyRNA_NULL_items, 0, "Scene", "");
1593 RNA_def_enum_funcs(prop, RNA_scene_local_itemf);
1594 RNA_def_property_flag(prop, PROP_ENUM_NO_TRANSLATE);
1598 void OBJECT_OT_make_links_data(wmOperatorType *ot)
1600 static const EnumPropertyItem make_links_items[] = {
1601 {MAKE_LINKS_OBDATA, "OBDATA", 0, "Object Data", ""},
1602 {MAKE_LINKS_MATERIALS, "MATERIAL", 0, "Materials", ""},
1603 {MAKE_LINKS_ANIMDATA, "ANIMATION", 0, "Animation Data", ""},
1604 {MAKE_LINKS_GROUP, "GROUPS", 0, "Group", ""},
1605 {MAKE_LINKS_DUPLICOLLECTION, "DUPLICOLLECTION", 0, "DupliGroup", ""},
1606 {MAKE_LINKS_MODIFIERS, "MODIFIERS", 0, "Modifiers", ""},
1607 {MAKE_LINKS_FONTS, "FONTS", 0, "Fonts", ""},
1608 {0, NULL, 0, NULL, NULL}};
1611 ot->name = "Link Data";
1612 ot->description = "Apply active object links to other selected objects";
1613 ot->idname = "OBJECT_OT_make_links_data";
1616 ot->exec = make_links_data_exec;
1617 ot->poll = ED_operator_object_active;
1620 ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
1623 ot->prop = RNA_def_enum(ot->srna, "type", make_links_items, 0, "Type", "");
1627 /**************************** Make Single User ********************************/
1629 static void libblock_relink_collection(Collection *collection)
1631 BKE_libblock_relink_to_newid(&collection->id);
1633 for (CollectionObject *cob = collection->gobject.first; cob != NULL; cob = cob->next) {
1634 BKE_libblock_relink_to_newid(&cob->ob->id);
1637 for (CollectionChild *child = collection->children.first; child; child = child->next) {
1638 libblock_relink_collection(child->collection);
1642 static void single_object_users_collection(
1643 Main *bmain, Scene *scene, Collection *collection,
1644 const int flag, const bool copy_collections, const bool is_master_collection)
1646 /* Generate new copies for objects in given collection and all its children,
1647 * and optionnaly also copy collections themselves. */
1648 if (copy_collections && !is_master_collection) {
1649 collection = ID_NEW_SET(collection, BKE_collection_copy(bmain, NULL, collection));
1652 /* We do not remap to new objects here, this is done in separate step. */
1653 for (CollectionObject *cob = collection->gobject.first; cob; cob = cob->next) {
1654 Object *ob = cob->ob;
1655 /* an object may be in more than one collection */
1656 if ((ob->id.newid == NULL) && ((ob->flag & flag) == flag)) {
1657 if (!ID_IS_LINKED(ob) && ob->id.us > 1) {
1658 ID_NEW_SET(ob, BKE_object_copy(bmain, ob));
1663 for (CollectionChild *child = collection->children.first; child; child = child->next) {
1664 single_object_users_collection(bmain, scene, child->collection, flag, copy_collections, false);
1668 /* Warning, sets ID->newid pointers of objects and collections, but does not clear them. */
1669 static void single_object_users(Main *bmain, Scene *scene, View3D *v3d, const int flag, const bool copy_collections)
1671 /* duplicate all the objects of the scene (and matching collections, if required). */
1672 Collection *master_collection = BKE_collection_master(scene);
1673 single_object_users_collection(bmain, scene, master_collection, flag, copy_collections, true);
1675 /* duplicate collections that consist entirely of duplicated objects */
1676 /* XXX I guess that was designed for calls from 'make single user' operator... But since copy_collection is
1677 * always false then, was not doing anything. And that kind of behavior should be added at operator level,
1678 * not in a utility function also used by rather different code... */
1680 if (copy_collections) {
1681 Collection *collection, *collectionn;
1682 for (collection = bmain->collection.first; collection; collection = collection->id.next) {
1683 bool all_duplicated = true;
1684 bool any_duplicated = false;
1686 for (CollectionObject *cob = collection->gobject.first; cob; cob = cob->next) {
1687 any_duplicated = true;
1688 if (cob->ob->id.newid == NULL) {
1689 all_duplicated = false;
1694 if (any_duplicated && all_duplicated) {
1695 // TODO: test if this works, with child collections ..
1696 collectionn = ID_NEW_SET(collection, BKE_collection_copy(bmain, NULL, collection));
1698 for (CollectionObject *cob = collectionn->gobject.first; cob; cob = cob->next) {
1699 cob->ob = (Object *)cob->ob->id.newid;
1706 /* Collection and object pointers in collections */
1707 libblock_relink_collection(master_collection);
1709 /* collection pointers in scene */
1710 BKE_scene_groups_relink(scene);
1713 ID_NEW_REMAP(scene->camera);
1714 if (v3d) ID_NEW_REMAP(v3d->camera);
1716 BKE_scene_collection_sync(scene);
1719 /* not an especially efficient function, only added so the single user
1720 * button can be functional.*/
1721 void ED_object_single_user(Main *bmain, Scene *scene, Object *ob)
1723 FOREACH_SCENE_OBJECT_BEGIN(scene, ob_iter)
1725 ob_iter->flag &= ~OB_DONE;
1727 FOREACH_SCENE_OBJECT_END;
1729 /* tag only the one object */
1730 ob->flag |= OB_DONE;
1732 single_object_users(bmain, scene, NULL, OB_DONE, false);
1733 BKE_main_id_clear_newpoins(bmain);
1736 static void new_id_matar(Main *bmain, Material **matar, const int totcol)
1741 for (a = 0; a < totcol; a++) {
1742 id = (ID *)matar[a];
1743 if (id && !ID_IS_LINKED(id)) {
1745 matar[a] = (Material *)id->newid;
1746 id_us_plus(id->newid);
1749 else if (id->us > 1) {
1750 matar[a] = ID_NEW_SET(id, BKE_material_copy(bmain, matar[a]));
1757 static void single_obdata_users(Main *bmain, Scene *scene, ViewLayer *view_layer, View3D *v3d, const int flag)
1766 FOREACH_OBJECT_FLAG_BEGIN(scene, view_layer, v3d, flag, ob)
1768 if (!ID_IS_LINKED(ob)) {
1771 if (id && id->us > 1 && !ID_IS_LINKED(id)) {
1772 DEG_id_tag_update(&ob->id, ID_RECALC_GEOMETRY);
1776 ob->data = la = ID_NEW_SET(ob->data, BKE_lamp_copy(bmain, ob->data));
1779 ob->data = ID_NEW_SET(ob->data, BKE_camera_copy(bmain, ob->data));
1782 /* Needed to remap texcomesh below. */
1783 me = ob->data = ID_NEW_SET(ob->data, BKE_mesh_copy(bmain, ob->data));
1784 if (me->key) /* We do not need to set me->key->id.newid here... */
1785 BKE_animdata_copy_id_action(bmain, (ID *)me->key, false);
1788 ob->data = ID_NEW_SET(ob->data, BKE_mball_copy(bmain, ob->data));
1793 ob->data = cu = ID_NEW_SET(ob->data, BKE_curve_copy(bmain, ob->data));
1794 ID_NEW_REMAP(cu->bevobj);
1795 ID_NEW_REMAP(cu->taperobj);
1796 if (cu->key) /* We do not need to set cu->key->id.newid here... */
1797 BKE_animdata_copy_id_action(bmain, (ID *)cu->key, false);
1800 ob->data = lat = ID_NEW_SET(ob->data, BKE_lattice_copy(bmain, ob->data));
1801 if (lat->key) /* We do not need to set lat->key->id.newid here... */
1802 BKE_animdata_copy_id_action(bmain, (ID *)lat->key, false);
1805 DEG_id_tag_update(&ob->id, ID_RECALC_GEOMETRY);
1806 ob->data = ID_NEW_SET(ob->data, BKE_armature_copy(bmain, ob->data));
1807 BKE_pose_rebuild(bmain, ob, ob->data, true);
1810 ob->data = ID_NEW_SET(ob->data, BKE_speaker_copy(bmain, ob->data));
1813 ob->data = ID_NEW_SET(ob->data, BKE_lightprobe_copy(bmain, ob->data));
1816 ob->data = ID_NEW_SET(ob->data, BKE_gpencil_copy(bmain, ob->data));
1819 printf("ERROR %s: can't copy %s\n", __func__, id->name);
1820 BLI_assert(!"This should never happen.");
1822 /* We need to end the FOREACH_OBJECT_FLAG_BEGIN iterator to prevent memory leak. */
1823 BKE_scene_objects_iterator_end(&iter_macro);
1827 /* Copy animation data after object data became local,
1828 * otherwise old and new object data will share the same
1829 * AnimData structure, which is not what we want.
1832 BKE_animdata_copy_id_action(bmain, (ID *)ob->data, false);
1838 FOREACH_OBJECT_FLAG_END;
1840 me = bmain->mesh.first;
1842 ID_NEW_REMAP(me->texcomesh);
1847 static void single_object_action_users(Main *bmain, Scene *scene, ViewLayer *view_layer, View3D *v3d, const int flag)
1849 FOREACH_OBJECT_FLAG_BEGIN(scene, view_layer, v3d, flag, ob)
1851 if (!ID_IS_LINKED(ob)) {
1852 DEG_id_tag_update(&ob->id, ID_RECALC_GEOMETRY);
1853 BKE_animdata_copy_id_action(bmain, &ob->id, false);
1856 FOREACH_OBJECT_FLAG_END;
1859 static void single_mat_users(Main *bmain, Scene *scene, ViewLayer *view_layer, View3D *v3d, const int flag)
1864 FOREACH_OBJECT_FLAG_BEGIN(scene, view_layer, v3d, flag, ob)
1866 if (!ID_IS_LINKED(ob)) {
1867 for (a = 1; a <= ob->totcol; a++) {
1868 ma = give_current_material(ob, a);
1870 /* do not test for LIB_TAG_NEW or use newid: this functions guaranteed delivers single_users! */
1872 if (ma->id.us > 1) {
1873 man = BKE_material_copy(bmain, ma);
1874 BKE_animdata_copy_id_action(bmain, &man->id, false);
1875 if (man->nodetree != NULL) {
1876 BKE_animdata_copy_id_action(bmain, &man->nodetree->id, false);
1880 assign_material(bmain, ob, man, a, BKE_MAT_ASSIGN_USERPREF);
1886 FOREACH_OBJECT_FLAG_END;
1889 static void single_mat_users_expand(Main *bmain)
1891 /* only when 'parent' blocks are LIB_TAG_NEW */
1898 for (ob = bmain->object.first; ob; ob = ob->id.next)
1899 if (ob->id.tag & LIB_TAG_NEW)
1900 new_id_matar(bmain, ob->mat, ob->totcol);
1902 for (me = bmain->mesh.first; me; me = me->id.next)
1903 if (me->id.tag & LIB_TAG_NEW)
1904 new_id_matar(bmain, me->mat, me->totcol);
1906 for (cu = bmain->curve.first; cu; cu = cu->id.next)
1907 if (cu->id.tag & LIB_TAG_NEW)
1908 new_id_matar(bmain, cu->mat, cu->totcol);
1910 for (mb = bmain->mball.first; mb; mb = mb->id.next)
1911 if (mb->id.tag & LIB_TAG_NEW)
1912 new_id_matar(bmain, mb->mat, mb->totcol);
1914 for (gpd = bmain->gpencil.first; gpd; gpd = gpd->id.next)
1915 if (gpd->id.tag & LIB_TAG_NEW)
1916 new_id_matar(bmain, gpd->mat, gpd->totcol);
1919 /* used for copying scenes */
1920 void ED_object_single_users(Main *bmain, Scene *scene, const bool full, const bool copy_collections)
1922 single_object_users(bmain, scene, NULL, 0, copy_collections);
1925 single_obdata_users(bmain, scene, NULL, NULL, 0);
1926 single_object_action_users(bmain, scene, NULL, NULL, 0);
1927 single_mat_users_expand(bmain);
1930 /* Relink nodetrees' pointers that have been duplicated. */
1931 FOREACH_NODETREE_BEGIN(bmain, ntree, id)
1933 /* This is a bit convoluted, we want to root ntree of copied IDs and only those,
1934 * so we first check that old ID has been copied and that ntree is root tree of old ID,
1935 * then get root tree of new ID and remap its pointers to new ID... */
1936 if (id->newid && (&ntree->id != id)) {
1937 ntree = ntreeFromID(id->newid);
1938 BKE_libblock_relink_to_newid(&ntree->id);
1940 } FOREACH_NODETREE_END;
1942 /* Relink datablock pointer properties */
1944 IDP_RelinkProperty(scene->id.properties);
1946 FOREACH_SCENE_OBJECT_BEGIN(scene, ob)
1948 if (!ID_IS_LINKED(ob)) {
1949 IDP_RelinkProperty(ob->id.properties);
1952 FOREACH_SCENE_OBJECT_END;
1954 if (scene->nodetree) {
1955 IDP_RelinkProperty(scene->nodetree->id.properties);
1956 for (bNode *node = scene->nodetree->nodes.first; node; node = node->next) {
1957 IDP_RelinkProperty(node->prop);
1962 IDP_RelinkProperty(scene->world->id.properties);
1966 IDP_RelinkProperty(scene->clip->id.properties);
1969 BKE_main_id_clear_newpoins(bmain);
1970 DEG_relations_tag_update(bmain);
1973 /******************************* Make Local ***********************************/
1976 MAKE_LOCAL_SELECT_OB = 1,
1977 MAKE_LOCAL_SELECT_OBDATA = 2,
1978 MAKE_LOCAL_SELECT_OBDATA_MATERIAL = 3,
1982 static int tag_localizable_looper(
1983 void *UNUSED(user_data), ID *UNUSED(self_id), ID **id_pointer, const int UNUSED(cb_flag))
1986 (*id_pointer)->tag &= ~LIB_TAG_DOIT;
1989 return IDWALK_RET_NOP;
1992 static void tag_localizable_objects(bContext *C, const int mode)
1994 Main *bmain = CTX_data_main(C);
1996 BKE_main_id_tag_all(bmain, LIB_TAG_DOIT, false);
1998 /* Set LIB_TAG_DOIT flag for all selected objects, so next we can check whether
1999 * object is gonna to become local or not.
2001 CTX_DATA_BEGIN (C, Object *, object, selected_objects)
2003 object->id.tag |= LIB_TAG_DOIT;
2005 /* If data is also gonna to become local, mark data we're interested in
2006 * as gonna-to-be-local.
2008 if (mode == MAKE_LOCAL_SELECT_OBDATA && object->data) {
2009 ID *data_id = (ID *) object->data;
2010 data_id->tag |= LIB_TAG_DOIT;
2015 /* Also forbid making objects local if other library objects are using
2016 * them for modifiers or constraints.
2018 for (Object *object = bmain->object.first; object; object = object->id.next) {
2019 if ((object->id.tag & LIB_TAG_DOIT) == 0) {
2020 BKE_library_foreach_ID_link(NULL, &object->id, tag_localizable_looper, NULL, IDWALK_READONLY);
2023 ID *data_id = (ID *) object->data;
2024 if ((data_id->tag & LIB_TAG_DOIT) == 0) {
2025 BKE_library_foreach_ID_link(NULL, data_id, tag_localizable_looper, NULL, IDWALK_READONLY);
2030 /* TODO(sergey): Drivers targets? */
2034 * Instance indirectly referenced zero user objects,
2035 * otherwise they're lost on reload, see T40595.
2037 static bool make_local_all__instance_indirect_unused(Main *bmain, ViewLayer *view_layer, Collection *collection)
2040 bool changed = false;
2042 for (ob = bmain->object.first; ob; ob = ob->id.next) {
2043 if (ID_IS_LINKED(ob) && (ob->id.us == 0)) {
2046 id_us_plus(&ob->id);
2048 BKE_collection_object_add(bmain, collection, ob);
2049 base = BKE_view_layer_base_find(view_layer, ob);
2050 base->flag |= BASE_SELECTED;
2051 BKE_scene_object_base_flag_sync_from_base(base);
2052 DEG_id_tag_update(&ob->id, ID_RECALC_TRANSFORM | ID_RECALC_GEOMETRY | ID_RECALC_ANIMATION);
2061 static void make_local_animdata_tag_strips(ListBase *strips)
2065 for (strip = strips->first; strip; strip = strip->next) {
2067 strip->act->id.tag &= ~LIB_TAG_PRE_EXISTING;
2070 make_local_animdata_tag_strips(&strip->strips);
2074 /* Tag all actions used by given animdata to be made local. */
2075 static void make_local_animdata_tag(AnimData *adt)
2078 /* Actions - Active and Temp */
2080 adt->action->id.tag &= ~LIB_TAG_PRE_EXISTING;
2083 adt->tmpact->id.tag &= ~LIB_TAG_PRE_EXISTING;
2087 /* TODO: need to handle the ID-targets too? */
2090 for (NlaTrack *nlt = adt->nla_tracks.first; nlt; nlt = nlt->next) {
2091 make_local_animdata_tag_strips(&nlt->strips);
2096 static void make_local_material_tag(Material *ma)
2099 ma->id.tag &= ~LIB_TAG_PRE_EXISTING;
2100 make_local_animdata_tag(BKE_animdata_from_id(&ma->id));
2102 /* About nodetrees: root one is made local together with material, others we keep linked for now... */
2106 static int make_local_exec(bContext *C, wmOperator *op)
2108 Main *bmain = CTX_data_main(C);
2109 ParticleSystem *psys;
2110 Material *ma, ***matarar;
2111 const int mode = RNA_enum_get(op->ptr, "type");
2114 /* Note: we (ab)use LIB_TAG_PRE_EXISTING to cherry pick which ID to make local... */
2115 if (mode == MAKE_LOCAL_ALL) {
2116 ViewLayer *view_layer = CTX_data_view_layer(C);
2117 Collection *collection = CTX_data_collection(C);
2119 BKE_main_id_tag_all(bmain, LIB_TAG_PRE_EXISTING, false);
2121 /* De-select so the user can differentiate newly instanced from existing objects. */
2122 BKE_view_layer_base_deselect_all(view_layer);
2124 if (make_local_all__instance_indirect_unused(bmain, view_layer, collection)) {
2125 BKE_report(op->reports, RPT_INFO, "Orphan library objects added to the current scene to avoid loss");
2129 BKE_main_id_tag_all(bmain, LIB_TAG_PRE_EXISTING, true);
2130 tag_localizable_objects(C, mode);
2132 CTX_DATA_BEGIN (C, Object *, ob, selected_objects)
2134 if ((ob->id.tag & LIB_TAG_DOIT) == 0) {
2138 ob->id.tag &= ~LIB_TAG_PRE_EXISTING;
2139 make_local_animdata_tag(BKE_animdata_from_id(&ob->id));
2140 for (psys = ob->particlesystem.first; psys; psys = psys->next) {
2141 psys->part->id.tag &= ~LIB_TAG_PRE_EXISTING;
2144 if (mode == MAKE_LOCAL_SELECT_OBDATA_MATERIAL) {
2145 for (a = 0; a < ob->totcol; a++) {
2148 make_local_material_tag(ma);
2152 matarar = (Material ***)give_matarar(ob);
2154 for (a = 0; a < ob->totcol; a++) {
2157 make_local_material_tag(ma);
2163 if (ELEM(mode, MAKE_LOCAL_SELECT_OBDATA, MAKE_LOCAL_SELECT_OBDATA_MATERIAL) && ob->data != NULL) {
2164 ID *ob_data = ob->data;
2165 ob_data->tag &= ~LIB_TAG_PRE_EXISTING;
2166 make_local_animdata_tag(BKE_animdata_from_id(ob_data));
2172 BKE_library_make_local(bmain, NULL, NULL, true, false); /* NULL is all libs */
2174 WM_event_add_notifier(C, NC_WINDOW, NULL);
2175 return OPERATOR_FINISHED;
2178 void OBJECT_OT_make_local(wmOperatorType *ot)
2180 static const EnumPropertyItem type_items[] = {
2181 {MAKE_LOCAL_SELECT_OB, "SELECT_OBJECT", 0, "Selected Objects", ""},
2182 {MAKE_LOCAL_SELECT_OBDATA, "SELECT_OBDATA", 0, "Selected Objects and Data", ""},
2183 {MAKE_LOCAL_SELECT_OBDATA_MATERIAL, "SELECT_OBDATA_MATERIAL", 0, "Selected Objects, Data and Materials", ""},
2184 {MAKE_LOCAL_ALL, "ALL", 0, "All", ""},
2185 {0, NULL, 0, NULL, NULL}
2189 ot->name = "Make Local";
2190 ot->description = "Make library linked data-blocks local to this file";
2191 ot->idname = "OBJECT_OT_make_local";
2194 ot->invoke = WM_menu_invoke;
2195 ot->exec = make_local_exec;
2196 ot->poll = ED_operator_objectmode;
2199 ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
2202 ot->prop = RNA_def_enum(ot->srna, "type", type_items, 0, "Type", "");
2206 static void make_override_static_tag_object(Object *obact, Object *ob)
2212 if (!ID_IS_LINKED(ob)) {
2216 /* Note: all this is very case-by-case bad handling, ultimately we'll want a real full 'automatic', generic
2217 * handling of all this, will probably require adding some override-aware stuff to library_query code... */
2219 if (obact->type == OB_ARMATURE && ob->modifiers.first != NULL) {
2220 for (ModifierData *md = ob->modifiers.first; md != NULL; md = md->next) {
2221 if (md->type == eModifierType_Armature) {
2222 ArmatureModifierData *amd = (ArmatureModifierData *)md;
2223 if (amd->object == obact) {
2224 ob->id.tag |= LIB_TAG_DOIT;
2230 else if (ob->parent == obact) {
2231 ob->id.tag |= LIB_TAG_DOIT;
2234 if (ob->id.tag & LIB_TAG_DOIT) {
2235 printf("Indirectly overriding %s for %s\n", ob->id.name, obact->id.name);
2239 static void make_override_static_tag_collections(Collection *collection)
2241 collection->id.tag |= LIB_TAG_DOIT;
2242 for (CollectionChild *coll_child = collection->children.first; coll_child != NULL; coll_child = coll_child->next) {
2243 make_override_static_tag_collections(coll_child->collection);
2247 /* Set the object to override. */
2248 static int make_override_static_invoke(bContext *C, wmOperator *op, const wmEvent *event)
2250 Scene *scene = CTX_data_scene(C);
2251 Object *obact = ED_object_active_context(C);
2253 /* Sanity checks. */
2254 if (!scene || ID_IS_LINKED(scene) || !obact) {
2255 return OPERATOR_CANCELLED;
2258 /* Get object to work on - use a menu if we need to... */
2259 if (!ID_IS_LINKED(obact) && obact->dup_group != NULL && ID_IS_LINKED(obact->dup_group)) {
2260 /* Gives menu with list of objects in group. */
2261 WM_enum_search_invoke(C, op, event);
2262 return OPERATOR_CANCELLED;
2264 else if (ID_IS_LINKED(obact)) {
2265 uiPopupMenu *pup = UI_popup_menu_begin(C, IFACE_("OK?"), ICON_QUESTION);
2266 uiLayout *layout = UI_popup_menu_layout(pup);
2268 /* Create operator menu item with relevant properties filled in. */
2269 PointerRNA opptr_dummy;
2270 uiItemFullO_ptr(layout, op->type, op->type->name, ICON_NONE, NULL,
2271 WM_OP_EXEC_REGION_WIN, 0, &opptr_dummy);
2273 /* Present the menu and be done... */
2274 UI_popup_menu_end(C, pup);
2276 /* This invoke just calls another instance of this operator... */
2277 return OPERATOR_INTERFACE;
2280 /* Error.. cannot continue. */
2281 BKE_report(op->reports, RPT_ERROR, "Can only make static override for a referenced object or collection");
2282 return OPERATOR_CANCELLED;
2287 static int make_override_static_exec(bContext *C, wmOperator *op)
2289 Main *bmain = CTX_data_main(C);
2290 Object *obact = CTX_data_active_object(C);
2292 bool success = false;
2294 if (!ID_IS_LINKED(obact) && obact->dup_group != NULL && ID_IS_LINKED(obact->dup_group)) {
2295 Object *obcollection = obact;
2296 Collection *collection = obcollection->dup_group;
2298 const ListBase dup_collection_objects = BKE_collection_object_cache_get(collection);
2299 Base *base = BLI_findlink(&dup_collection_objects, RNA_enum_get(op->ptr, "object"));
2300 obact = base->object;
2302 /* First, we make a static override of the linked collection itself, and all its children. */
2303 make_override_static_tag_collections(collection);
2305 /* Then, we make static override of the whole set of objects in the Collection. */
2306 FOREACH_COLLECTION_OBJECT_RECURSIVE_BEGIN(collection, ob)
2308 ob->id.tag |= LIB_TAG_DOIT;
2310 FOREACH_COLLECTION_OBJECT_RECURSIVE_END;
2312 /* Then, we remove (untag) bone shape objects, you shall never want to override those (hopefully)... */
2313 FOREACH_COLLECTION_OBJECT_RECURSIVE_BEGIN(collection, ob)
2315 if (ob->type == OB_ARMATURE && ob->pose != NULL) {
2316 for (bPoseChannel *pchan = ob->pose->chanbase.first; pchan != NULL; pchan = pchan->next) {
2317 if (pchan->custom != NULL) {
2318 pchan->custom->id.tag &= ~ LIB_TAG_DOIT;
2323 FOREACH_COLLECTION_OBJECT_RECURSIVE_END;
2325 success = BKE_override_static_create_from_tag(bmain);
2327 /* Instantiate our newly overridden objects in scene, if not yet done. */
2328 Scene *scene = CTX_data_scene(C);
2329 ViewLayer *view_layer = CTX_data_view_layer(C);
2330 Collection *new_collection = (Collection *)collection->id.newid;
2332 BKE_collection_child_add(bmain, scene->master_collection, new_collection);
2333 FOREACH_COLLECTION_OBJECT_RECURSIVE_BEGIN(new_collection, new_ob)
2335 if (new_ob != NULL && new_ob->id.override_static != NULL) {
2336 if ((base = BKE_view_layer_base_find(view_layer, new_ob)) == NULL) {
2337 BKE_collection_object_add_from(bmain, scene, obcollection, new_ob);
2338 base = BKE_view_layer_base_find(view_layer, new_ob);
2339 DEG_id_tag_update_ex(bmain, &new_ob->id, ID_RECALC_TRANSFORM | ID_RECALC_BASE_FLAGS);
2341 /* parent to 'collection' empty */
2342 if (new_ob->parent == NULL) {
2343 new_ob->parent = obcollection;
2345 if (new_ob == (Object *)obact->id.newid) {
2346 /* TODO: is setting active needed? */
2347 BKE_view_layer_base_select_and_set_active(view_layer, base);
2350 /* Disable auto-override tags for non-active objects, will help with performaces... */
2351 new_ob->id.override_static->flag &= ~STATICOVERRIDE_AUTO;
2353 /* We still want to store all objects' current override status (i.e. change of parent). */
2354 BKE_override_static_operations_create(bmain, &new_ob->id, true);
2357 FOREACH_COLLECTION_OBJECT_RECURSIVE_END;
2359 /* obcollection is no more duplicollection-ing, it merely parents whole collection of overriding instantiated objects. */
2360 obcollection->dup_group = NULL;
2362 /* Also, we'd likely want to lock by default things like transformations of implicitly overridden objects? */
2364 DEG_id_tag_update(&scene->id, 0);
2367 BKE_main_id_clear_newpoins(bmain);
2368 BKE_main_id_tag_listbase(&bmain->object, LIB_TAG_DOIT, false);
2370 /* Else, poll func ensures us that ID_IS_LINKED(obact) is true. */
2371 else if (obact->type == OB_ARMATURE) {
2372 BKE_main_id_tag_all(bmain, LIB_TAG_DOIT, false);
2374 obact->id.tag |= LIB_TAG_DOIT;
2376 for (Object *ob = bmain->object.first; ob != NULL; ob = ob->id.next) {
2377 make_override_static_tag_object(obact, ob);
2380 success = BKE_override_static_create_from_tag(bmain);
2382 /* Also, we'd likely want to lock by default things like transformations of implicitly overridden objects? */
2385 BKE_main_id_clear_newpoins(bmain);
2386 BKE_main_id_tag_listbase(&bmain->object, LIB_TAG_DOIT, false);
2388 /* TODO: probably more cases where we want to do automated smart things in the future! */
2390 success = (BKE_override_static_create_from_id(bmain, &obact->id) != NULL);
2393 WM_event_add_notifier(C, NC_WINDOW, NULL);
2395 return success ? OPERATOR_FINISHED : OPERATOR_CANCELLED;
2398 static bool make_override_static_poll(bContext *C)
2400 Object *obact = CTX_data_active_object(C);
2402 /* Object must be directly linked to be overridable. */
2403 return (BKE_override_static_is_enabled() &&
2404 ED_operator_objectmode(C) && obact != NULL &&
2405 ((ID_IS_LINKED(obact) && obact->id.tag & LIB_TAG_EXTERN) ||
2406 (!ID_IS_LINKED(obact) && obact->dup_group != NULL && ID_IS_LINKED(obact->dup_group))));
2409 void OBJECT_OT_make_override_static(wmOperatorType *ot)
2412 ot->name = "Make Static Override";
2413 ot->description = "Make local override of this library linked data-block";
2414 ot->idname = "OBJECT_OT_make_override_static";
2417 ot->invoke = make_override_static_invoke;
2418 ot->exec = make_override_static_exec;
2419 ot->poll = make_override_static_poll;
2422 ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
2426 prop = RNA_def_enum(ot->srna, "object", DummyRNA_DEFAULT_items, 0, "Override Object",
2427 "Name of lib-linked/collection object to make an override from");
2428 RNA_def_enum_funcs(prop, proxy_collection_object_itemf);
2429 RNA_def_property_flag(prop, PROP_ENUM_NO_TRANSLATE);
2434 MAKE_SINGLE_USER_ALL = 1,
2435 MAKE_SINGLE_USER_SELECTED = 2,
2438 static int make_single_user_exec(bContext *C, wmOperator *op)
2440 Main *bmain = CTX_data_main(C);
2441 Scene *scene = CTX_data_scene(C);
2442 ViewLayer *view_layer = CTX_data_view_layer(C);
2443 View3D *v3d = CTX_wm_view3d(C); /* ok if this is NULL */
2444 const int flag = (RNA_enum_get(op->ptr, "type") == MAKE_SINGLE_USER_SELECTED) ? SELECT : 0;
2445 const bool copy_collections = false;
2446 bool update_deps = false;
2448 if (RNA_boolean_get(op->ptr, "object")) {
2449 if (flag == SELECT) {
2450 BKE_view_layer_selected_objects_tag(view_layer, OB_DONE);
2451 single_object_users(bmain, scene, v3d, OB_DONE, copy_collections);
2454 single_object_users(bmain, scene, v3d, 0, copy_collections);
2457 /* needed since object relationships may have changed */
2461 if (RNA_boolean_get(op->ptr, "obdata")) {
2462 single_obdata_users(bmain, scene, view_layer, v3d, flag);
2465 if (RNA_boolean_get(op->ptr, "material")) {
2466 single_mat_users(bmain, scene, view_layer, v3d, flag);
2469 if (RNA_boolean_get(op->ptr, "animation")) {
2470 single_object_action_users(bmain, scene, view_layer, v3d, flag);
2473 BKE_main_id_clear_newpoins(bmain);
2475 WM_event_add_notifier(C, NC_WINDOW, NULL);
2478 DEG_relations_tag_update(bmain);
2481 return OPERATOR_FINISHED;
2484 void OBJECT_OT_make_single_user(wmOperatorType *ot)
2486 static const EnumPropertyItem type_items[] = {
2487 {MAKE_SINGLE_USER_SELECTED, "SELECTED_OBJECTS", 0, "Selected Objects", ""},
2488 {MAKE_SINGLE_USER_ALL, "ALL", 0, "All", ""},
2489 {0, NULL, 0, NULL, NULL}};
2492 ot->name = "Make Single User";
2493 ot->description = "Make linked data local to each object";
2494 ot->idname = "OBJECT_OT_make_single_user";
2497 ot->invoke = WM_menu_invoke;
2498 ot->exec = make_single_user_exec;
2499 ot->poll = ED_operator_objectmode;
2502 ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
2505 ot->prop = RNA_def_enum(ot->srna, "type", type_items, MAKE_SINGLE_USER_SELECTED, "Type", "");
2507 RNA_def_boolean(ot->srna, "object", 0, "Object", "Make single user objects");
2508 RNA_def_boolean(ot->srna, "obdata", 0, "Object Data", "Make single user object data");
2509 RNA_def_boolean(ot->srna, "material", 0, "Materials", "Make materials local to each data-block");
2510 RNA_def_boolean(ot->srna, "animation", 0, "Object Animation", "Make animation data local to each object");
2513 static int drop_named_material_invoke(bContext *C, wmOperator *op, const wmEvent *event)
2515 Main *bmain = CTX_data_main(C);
2516 Base *base = ED_view3d_give_base_under_cursor(C, event->mval);
2518 char name[MAX_ID_NAME - 2];
2520 RNA_string_get(op->ptr, "name", name);
2521 ma = (Material *)BKE_libblock_find_name(bmain, ID_MA, name);
2522 if (base == NULL || ma == NULL)
2523 return OPERATOR_CANCELLED;
2525 assign_material(CTX_data_main(C), base->object, ma, 1, BKE_MAT_ASSIGN_USERPREF);
2527 DEG_id_tag_update(&base->object->id, ID_RECALC_TRANSFORM);
2529 WM_event_add_notifier(C, NC_OBJECT | ND_OB_SHADING, base->object);
2530 WM_event_add_notifier(C, NC_SPACE | ND_SPACE_VIEW3D, CTX_wm_view3d(C));
2531 WM_event_add_notifier(C, NC_MATERIAL | ND_SHADING_LINKS, ma);
2533 return OPERATOR_FINISHED;
2536 /* used for dropbox */
2537 /* assigns to object under cursor, only first material slot */
2538 void OBJECT_OT_drop_named_material(wmOperatorType *ot)
2541 ot->name = "Drop Named Material on Object";
2542 ot->idname = "OBJECT_OT_drop_named_material";
2545 ot->invoke = drop_named_material_invoke;
2546 ot->poll = ED_operator_objectmode;
2549 ot->flag = OPTYPE_UNDO | OPTYPE_INTERNAL;
2552 RNA_def_string(ot->srna, "name", "Material", MAX_ID_NAME - 2, "Name", "Material name to assign");
2555 static int object_unlink_data_exec(bContext *C, wmOperator *op)
2558 PropertyPointerRNA pprop;
2560 UI_context_active_but_prop_get_templateID(C, &pprop.ptr, &pprop.prop);
2562 if (pprop.prop == NULL) {
2563 BKE_report(op->reports, RPT_ERROR, "Incorrect context for running object data unlink");
2564 return OPERATOR_CANCELLED;
2567 id = pprop.ptr.id.data;
2569 if (GS(id->name) == ID_OB) {
2570 Object *ob = (Object *)id;
2572 ID *id_data = ob->data;
2574 if (GS(id_data->name) == ID_IM) {
2579 BKE_report(op->reports, RPT_ERROR, "Can't unlink this object data");
2580 return OPERATOR_CANCELLED;
2585 RNA_property_update(C, &pprop.ptr, pprop.prop);
2587 return OPERATOR_FINISHED;
2591 * \note Only for empty-image objects, this operator is needed
2593 void OBJECT_OT_unlink_data(wmOperatorType *ot)
2596 ot->name = "Unlink";
2597 ot->idname = "OBJECT_OT_unlink_data";
2600 ot->exec = object_unlink_data_exec;
2603 ot->flag = OPTYPE_INTERNAL;