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_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_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_depsgraph.h"
71 #include "BKE_DerivedMesh.h"
72 #include "BKE_displist.h"
73 #include "BKE_global.h"
74 #include "BKE_group.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_query.h"
82 #include "BKE_library_remap.h"
84 #include "BKE_material.h"
85 #include "BKE_mball.h"
87 #include "BKE_modifier.h"
89 #include "BKE_object.h"
90 #include "BKE_report.h"
92 #include "BKE_scene.h"
93 #include "BKE_speaker.h"
94 #include "BKE_texture.h"
95 #include "BKE_editmesh.h"
100 #include "UI_interface.h"
101 #include "UI_resources.h"
103 #include "RNA_access.h"
104 #include "RNA_define.h"
105 #include "RNA_enum_types.h"
107 #include "ED_armature.h"
108 #include "ED_curve.h"
109 #include "ED_keyframing.h"
110 #include "ED_object.h"
112 #include "ED_screen.h"
113 #include "ED_view3d.h"
115 #include "object_intern.h"
117 /*********************** Make Vertex Parent Operator ************************/
119 static int vertex_parent_set_poll(bContext *C)
121 return ED_operator_editmesh(C) || ED_operator_editsurfcurve(C) || ED_operator_editlattice(C);
124 static int vertex_parent_set_exec(bContext *C, wmOperator *op)
126 Main *bmain = CTX_data_main(C);
127 Scene *scene = CTX_data_scene(C);
128 SceneLayer *sl = CTX_data_scene_layer(C);
129 Object *obedit = CTX_data_edit_object(C);
137 int a, v1 = 0, v2 = 0, v3 = 0, v4 = 0, nr = 1;
139 /* we need 1 to 3 selected vertices */
141 if (obedit->type == OB_MESH) {
142 Mesh *me = obedit->data;
145 EDBM_mesh_load(obedit);
146 EDBM_mesh_make(scene->toolsettings, obedit, true);
148 DAG_id_tag_update(obedit->data, 0);
150 em = me->edit_btmesh;
152 EDBM_mesh_normals_update(em);
153 BKE_editmesh_tessface_calc(em);
155 /* derivedMesh might be needed for solving parenting,
156 * so re-create it here */
157 makeDerivedMesh(scene, obedit, em, CD_MASK_BAREMESH | CD_MASK_ORIGINDEX, false);
159 BM_ITER_MESH (eve, &iter, em->bm, BM_VERTS_OF_MESH) {
160 if (BM_elem_flag_test(eve, BM_ELEM_SELECT)) {
161 if (v1 == 0) v1 = nr;
162 else if (v2 == 0) v2 = nr;
163 else if (v3 == 0) v3 = nr;
164 else if (v4 == 0) v4 = nr;
170 else if (ELEM(obedit->type, OB_SURF, OB_CURVE)) {
171 ListBase *editnurb = object_editcurve_get(obedit);
175 nu = editnurb->first;
177 if (nu->type == CU_BEZIER) {
181 if (BEZT_ISSEL_ANY_HIDDENHANDLES(cu, bezt)) {
182 if (v1 == 0) v1 = nr;
183 else if (v2 == 0) v2 = nr;
184 else if (v3 == 0) v3 = nr;
185 else if (v4 == 0) v4 = nr;
194 a = nu->pntsu * nu->pntsv;
196 if (bp->f1 & SELECT) {
197 if (v1 == 0) v1 = nr;
198 else if (v2 == 0) v2 = nr;
199 else if (v3 == 0) v3 = nr;
200 else if (v4 == 0) v4 = nr;
210 else if (obedit->type == OB_LATTICE) {
211 Lattice *lt = obedit->data;
213 a = lt->editlatt->latt->pntsu * lt->editlatt->latt->pntsv * lt->editlatt->latt->pntsw;
214 bp = lt->editlatt->latt->def;
216 if (bp->f1 & SELECT) {
217 if (v1 == 0) v1 = nr;
218 else if (v2 == 0) v2 = nr;
219 else if (v3 == 0) v3 = nr;
220 else if (v4 == 0) v4 = nr;
228 if (v4 || !((v1 && v2 == 0 && v3 == 0) || (v1 && v2 && v3))) {
229 BKE_report(op->reports, RPT_ERROR, "Select either 1 or 3 vertices to parent to");
230 return OPERATOR_CANCELLED;
233 CTX_DATA_BEGIN (C, Object *, ob, selected_editable_objects)
236 DAG_id_tag_update(&ob->id, OB_RECALC_OB | OB_RECALC_DATA | OB_RECALC_TIME);
237 par = obedit->parent;
239 if (BKE_object_parent_loop_check(par, ob)) {
240 BKE_report(op->reports, RPT_ERROR, "Loop in parents");
245 ob->parent = BASACT_NEW->object;
247 ob->partype = PARVERT3;
252 /* inverse parent matrix */
253 BKE_object_workob_calc_parent(scene, ob, &workob);
254 invert_m4_m4(ob->parentinv, workob.obmat);
257 ob->partype = PARVERT1;
260 /* inverse parent matrix */
261 BKE_object_workob_calc_parent(scene, ob, &workob);
262 invert_m4_m4(ob->parentinv, workob.obmat);
269 DAG_relations_tag_update(bmain);
271 WM_event_add_notifier(C, NC_OBJECT, NULL);
273 return OPERATOR_FINISHED;
276 void OBJECT_OT_vertex_parent_set(wmOperatorType *ot)
279 ot->name = "Make Vertex Parent";
280 ot->description = "Parent selected objects to the selected vertices";
281 ot->idname = "OBJECT_OT_vertex_parent_set";
284 ot->invoke = WM_operator_confirm;
285 ot->poll = vertex_parent_set_poll;
286 ot->exec = vertex_parent_set_exec;
289 ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
292 /********************** Make Proxy Operator *************************/
294 /* set the object to proxify */
295 static int make_proxy_invoke(bContext *C, wmOperator *op, const wmEvent *event)
297 Scene *scene = CTX_data_scene(C);
298 Object *ob = ED_object_active_context(C);
301 if (!scene || ID_IS_LINKED_DATABLOCK(scene) || !ob)
302 return OPERATOR_CANCELLED;
304 /* Get object to work on - use a menu if we need to... */
305 if (ob->dup_group && ID_IS_LINKED_DATABLOCK(ob->dup_group)) {
306 /* gives menu with list of objects in group */
307 /* proxy_group_objects_menu(C, op, ob, ob->dup_group); */
308 WM_enum_search_invoke(C, op, event);
309 return OPERATOR_CANCELLED;
311 else if (ID_IS_LINKED_DATABLOCK(ob)) {
312 uiPopupMenu *pup = UI_popup_menu_begin(C, IFACE_("OK?"), ICON_QUESTION);
313 uiLayout *layout = UI_popup_menu_layout(pup);
315 /* create operator menu item with relevant properties filled in */
316 uiItemFullO_ptr(layout, op->type, op->type->name, ICON_NONE, NULL,
317 WM_OP_EXEC_REGION_WIN, UI_ITEM_O_RETURN_PROPS);
319 /* present the menu and be done... */
320 UI_popup_menu_end(C, pup);
322 /* this invoke just calls another instance of this operator... */
323 return OPERATOR_INTERFACE;
326 /* error.. cannot continue */
327 BKE_report(op->reports, RPT_ERROR, "Can only make proxy for a referenced object or group");
328 return OPERATOR_CANCELLED;
333 static int make_proxy_exec(bContext *C, wmOperator *op)
335 Main *bmain = CTX_data_main(C);
336 Object *ob, *gob = ED_object_active_context(C);
338 Scene *scene = CTX_data_scene(C);
339 SceneLayer *sl = CTX_data_scene_layer(C);
341 if (gob->dup_group != NULL) {
342 go = BLI_findlink(&gob->dup_group->gobject, RNA_enum_get(op->ptr, "object"));
352 BaseLegacy *newbase, *oldbase = BASACT_NEW;
353 char name[MAX_ID_NAME + 4];
355 BLI_snprintf(name, sizeof(name), "%s_proxy", ((ID *)(gob ? gob : ob))->name + 2);
357 /* Add new object for the proxy */
358 newob = BKE_object_add(bmain, scene, sl, OB_EMPTY, name);
361 newbase = BASACT_NEW; /* BKE_object_add sets active... */
362 newbase->lay = oldbase->lay;
363 newob->lay = newbase->lay;
365 /* remove base, leave user count of object, it gets linked in BKE_object_make_proxy */
367 BKE_scene_base_unlink(scene, oldbase);
371 BKE_object_make_proxy(newob, ob, gob);
373 /* depsgraph flushes are needed for the new data */
374 DAG_relations_tag_update(bmain);
375 DAG_id_tag_update(&newob->id, OB_RECALC_OB | OB_RECALC_DATA | OB_RECALC_TIME);
376 WM_event_add_notifier(C, NC_OBJECT | ND_DRAW, newob);
379 BKE_report(op->reports, RPT_ERROR, "No object to make proxy for");
380 return OPERATOR_CANCELLED;
383 return OPERATOR_FINISHED;
386 /* Generic itemf's for operators that take library args */
387 static EnumPropertyItem *proxy_group_object_itemf(bContext *C, PointerRNA *UNUSED(ptr),
388 PropertyRNA *UNUSED(prop), bool *r_free)
390 EnumPropertyItem item_tmp = {0}, *item = NULL;
393 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 for (go = ob->dup_group->gobject.first; go; go = go->next) {
401 item_tmp.identifier = item_tmp.name = go->ob->id.name + 2;
402 item_tmp.value = i++;
403 RNA_enum_item_add(&item, &totitem, &item_tmp);
406 RNA_enum_item_end(&item, &totitem);
412 void OBJECT_OT_proxy_make(wmOperatorType *ot)
417 ot->name = "Make Proxy";
418 ot->idname = "OBJECT_OT_proxy_make";
419 ot->description = "Add empty object to become local replacement data of a library-linked object";
422 ot->invoke = make_proxy_invoke;
423 ot->exec = make_proxy_exec;
424 ot->poll = ED_operator_object_active;
427 ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
430 /* XXX, relies on hard coded ID at the moment */
431 prop = RNA_def_enum(ot->srna, "object", DummyRNA_DEFAULT_items, 0, "Proxy Object",
432 "Name of lib-linked/grouped object to make a proxy for");
433 RNA_def_enum_funcs(prop, proxy_group_object_itemf);
434 RNA_def_property_flag(prop, PROP_ENUM_NO_TRANSLATE);
438 /********************** Clear Parent Operator ******************* */
440 typedef enum eObClearParentTypes {
441 CLEAR_PARENT_ALL = 0,
442 CLEAR_PARENT_KEEP_TRANSFORM,
444 } eObClearParentTypes;
446 EnumPropertyItem prop_clear_parent_types[] = {
447 {CLEAR_PARENT_ALL, "CLEAR", 0, "Clear Parent",
448 "Completely clear the parenting relationship, including involved modifiers if any"},
449 {CLEAR_PARENT_KEEP_TRANSFORM, "CLEAR_KEEP_TRANSFORM", 0, "Clear and Keep Transformation",
450 "As 'Clear Parent', but keep the current visual transformations of the object"},
451 {CLEAR_PARENT_INVERSE, "CLEAR_INVERSE", 0, "Clear Parent Inverse",
452 "Reset the transform corrections applied to the parenting relationship, does not remove parenting itself"},
453 {0, NULL, 0, NULL, NULL}
456 /* Helper for ED_object_parent_clear() - Remove deform-modifiers associated with parent */
457 static void object_remove_parent_deform_modifiers(Object *ob, const Object *par)
459 if (ELEM(par->type, OB_ARMATURE, OB_LATTICE, OB_CURVE)) {
460 ModifierData *md, *mdn;
462 /* assume that we only need to remove the first instance of matching deform modifier here */
463 for (md = ob->modifiers.first; md; md = mdn) {
468 /* need to match types (modifier + parent) and references */
469 if ((md->type == eModifierType_Armature) && (par->type == OB_ARMATURE)) {
470 ArmatureModifierData *amd = (ArmatureModifierData *)md;
471 if (amd->object == par) {
475 else if ((md->type == eModifierType_Lattice) && (par->type == OB_LATTICE)) {
476 LatticeModifierData *lmd = (LatticeModifierData *)md;
477 if (lmd->object == par) {
481 else if ((md->type == eModifierType_Curve) && (par->type == OB_CURVE)) {
482 CurveModifierData *cmd = (CurveModifierData *)md;
483 if (cmd->object == par) {
488 /* free modifier if match */
490 BLI_remlink(&ob->modifiers, md);
497 void ED_object_parent_clear(Object *ob, const int type)
499 if (ob->parent == NULL)
503 case CLEAR_PARENT_ALL:
505 /* for deformers, remove corresponding modifiers to prevent a large number of modifiers building up */
506 object_remove_parent_deform_modifiers(ob, ob->parent);
508 /* clear parenting relationship completely */
512 case CLEAR_PARENT_KEEP_TRANSFORM:
514 /* remove parent, and apply the parented transform result as object's local transforms */
516 BKE_object_apply_mat4(ob, ob->obmat, true, false);
519 case CLEAR_PARENT_INVERSE:
521 /* object stays parented, but the parent inverse (i.e. offset from parent to retain binding state)
522 * is cleared. In other words: nothing to do here! */
527 /* Always clear parentinv matrix for sake of consistency, see T41950. */
528 unit_m4(ob->parentinv);
530 DAG_id_tag_update(&ob->id, OB_RECALC_OB | OB_RECALC_DATA | OB_RECALC_TIME);
533 /* note, poll should check for editable scene */
534 static int parent_clear_exec(bContext *C, wmOperator *op)
536 Main *bmain = CTX_data_main(C);
537 const int type = RNA_enum_get(op->ptr, "type");
539 CTX_DATA_BEGIN (C, Object *, ob, selected_editable_objects)
541 ED_object_parent_clear(ob, type);
545 DAG_relations_tag_update(bmain);
546 WM_event_add_notifier(C, NC_OBJECT | ND_TRANSFORM, NULL);
547 WM_event_add_notifier(C, NC_OBJECT | ND_PARENT, NULL);
548 return OPERATOR_FINISHED;
551 void OBJECT_OT_parent_clear(wmOperatorType *ot)
554 ot->name = "Clear Parent";
555 ot->description = "Clear the object's parenting";
556 ot->idname = "OBJECT_OT_parent_clear";
559 ot->invoke = WM_menu_invoke;
560 ot->exec = parent_clear_exec;
562 ot->poll = ED_operator_object_active_editable;
565 ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
567 ot->prop = RNA_def_enum(ot->srna, "type", prop_clear_parent_types, CLEAR_PARENT_ALL, "Type", "");
570 /* ******************** Make Parent Operator *********************** */
572 void ED_object_parent(Object *ob, Object *par, const int type, const char *substr)
574 /* Always clear parentinv matrix for sake of consistency, see T41950. */
575 unit_m4(ob->parentinv);
577 if (!par || BKE_object_parent_loop_check(par, ob)) {
579 ob->partype = PAROBJECT;
580 ob->parsubstr[0] = 0;
584 /* Other partypes are deprecated, do not use here! */
585 BLI_assert(ELEM(type & PARTYPE, PAROBJECT, PARSKEL, PARVERT1, PARVERT3, PARBONE));
587 /* this could use some more checks */
590 ob->partype &= ~PARTYPE;
592 BLI_strncpy(ob->parsubstr, substr, sizeof(ob->parsubstr));
595 /* Operator Property */
596 EnumPropertyItem prop_make_parent_types[] = {
597 {PAR_OBJECT, "OBJECT", 0, "Object", ""},
598 {PAR_ARMATURE, "ARMATURE", 0, "Armature Deform", ""},
599 {PAR_ARMATURE_NAME, "ARMATURE_NAME", 0, " With Empty Groups", ""},
600 {PAR_ARMATURE_AUTO, "ARMATURE_AUTO", 0, " With Automatic Weights", ""},
601 {PAR_ARMATURE_ENVELOPE, "ARMATURE_ENVELOPE", 0, " With Envelope Weights", ""},
602 {PAR_BONE, "BONE", 0, "Bone", ""},
603 {PAR_BONE_RELATIVE, "BONE_RELATIVE", 0, "Bone Relative", ""},
604 {PAR_CURVE, "CURVE", 0, "Curve Deform", ""},
605 {PAR_FOLLOW, "FOLLOW", 0, "Follow Path", ""},
606 {PAR_PATH_CONST, "PATH_CONST", 0, "Path Constraint", ""},
607 {PAR_LATTICE, "LATTICE", 0, "Lattice Deform", ""},
608 {PAR_VERTEX, "VERTEX", 0, "Vertex", ""},
609 {PAR_VERTEX_TRI, "VERTEX_TRI", 0, "Vertex (Triangle)", ""},
610 {0, NULL, 0, NULL, NULL}
613 bool ED_object_parent_set(ReportList *reports, Main *bmain, Scene *scene, Object *ob, Object *par,
614 int partype, const bool xmirror, const bool keep_transform, const int vert_par[3])
616 bPoseChannel *pchan = NULL;
617 const bool pararm = ELEM(partype, PAR_ARMATURE, PAR_ARMATURE_NAME, PAR_ARMATURE_ENVELOPE, PAR_ARMATURE_AUTO);
619 DAG_id_tag_update(&par->id, OB_RECALC_OB);
622 if (partype == PAR_FOLLOW || partype == PAR_PATH_CONST) {
623 if (par->type != OB_CURVE)
626 Curve *cu = par->data;
628 if ((cu->flag & CU_PATH) == 0) {
629 cu->flag |= CU_PATH | CU_FOLLOW;
630 BKE_displist_make_curveTypes(scene, par, 0); /* force creation of path data */
633 cu->flag |= CU_FOLLOW;
636 /* if follow, add F-Curve for ctime (i.e. "eval_time") so that path-follow works */
637 if (partype == PAR_FOLLOW) {
638 /* get or create F-Curve */
639 bAction *act = verify_adt_action(&cu->id, 1);
640 FCurve *fcu = verify_fcurve(act, NULL, NULL, "eval_time", 0, 1);
642 /* setup dummy 'generator' modifier here to get 1-1 correspondence still working */
643 if (!fcu->bezt && !fcu->fpt && !fcu->modifiers.first)
644 add_fmodifier(&fcu->modifiers, FMODIFIER_TYPE_GENERATOR);
647 /* fall back on regular parenting now (for follow only) */
648 if (partype == PAR_FOLLOW)
649 partype = PAR_OBJECT;
652 else if (ELEM(partype, PAR_BONE, PAR_BONE_RELATIVE)) {
653 pchan = BKE_pose_channel_active(par);
656 BKE_report(reports, RPT_ERROR, "No active bone");
662 if (BKE_object_parent_loop_check(par, ob)) {
663 BKE_report(reports, RPT_ERROR, "Loop in parents");
669 /* apply transformation of previous parenting */
670 if (keep_transform) {
671 /* was removed because of bug [#23577],
672 * but this can be handy in some cases too [#32616], so make optional */
673 BKE_object_apply_mat4(ob, ob->obmat, false, false);
676 /* set the parent (except for follow-path constraint option) */
677 if (partype != PAR_PATH_CONST) {
679 /* Always clear parentinv matrix for sake of consistency, see T41950. */
680 unit_m4(ob->parentinv);
685 BLI_strncpy(ob->parsubstr, pchan->name, sizeof(ob->parsubstr));
687 ob->parsubstr[0] = 0;
689 if (partype == PAR_PATH_CONST) {
690 /* don't do anything here, since this is not technically "parenting" */
692 else if (ELEM(partype, PAR_CURVE, PAR_LATTICE) || (pararm)) {
693 /* partype is now set to PAROBJECT so that invisible 'virtual' modifiers don't need to be created
694 * NOTE: the old (2.4x) method was to set ob->partype = PARSKEL, creating the virtual modifiers
696 ob->partype = PAROBJECT; /* note, dna define, not operator property */
697 /* ob->partype = PARSKEL; */ /* note, dna define, not operator property */
699 /* BUT, to keep the deforms, we need a modifier, and then we need to set the object that it uses
700 * - We need to ensure that the modifier we're adding doesn't already exist, so we check this by
701 * assuming that the parent is selected too...
703 /* XXX currently this should only happen for meshes, curves, surfaces,
704 * and lattices - this stuff isn't available for metas yet */
705 if (ELEM(ob->type, OB_MESH, OB_CURVE, OB_SURF, OB_FONT, OB_LATTICE)) {
709 case PAR_CURVE: /* curve deform */
710 if (modifiers_isDeformedByCurve(ob) != par) {
711 md = ED_object_modifier_add(reports, bmain, scene, ob, NULL, eModifierType_Curve);
713 ((CurveModifierData *)md)->object = par;
715 if (par->curve_cache && par->curve_cache->path == NULL) {
716 DAG_id_tag_update(&par->id, OB_RECALC_DATA);
720 case PAR_LATTICE: /* lattice deform */
721 if (modifiers_isDeformedByLattice(ob) != par) {
722 md = ED_object_modifier_add(reports, bmain, scene, ob, NULL, eModifierType_Lattice);
724 ((LatticeModifierData *)md)->object = par;
728 default: /* armature deform */
729 if (modifiers_isDeformedByArmature(ob) != par) {
730 md = ED_object_modifier_add(reports, bmain, scene, ob, NULL, eModifierType_Armature);
732 ((ArmatureModifierData *)md)->object = par;
739 else if (partype == PAR_BONE) {
740 ob->partype = PARBONE; /* note, dna define, not operator property */
742 pchan->bone->flag &= ~BONE_RELATIVE_PARENTING;
744 else if (partype == PAR_BONE_RELATIVE) {
745 ob->partype = PARBONE; /* note, dna define, not operator property */
747 pchan->bone->flag |= BONE_RELATIVE_PARENTING;
749 else if (partype == PAR_VERTEX) {
750 ob->partype = PARVERT1;
751 ob->par1 = vert_par[0];
753 else if (partype == PAR_VERTEX_TRI) {
754 ob->partype = PARVERT3;
755 copy_v3_v3_int(&ob->par1, vert_par);
758 ob->partype = PAROBJECT; /* note, dna define, not operator property */
762 if (partype == PAR_PATH_CONST) {
764 bFollowPathConstraint *data;
765 float cmat[4][4], vec[3];
767 con = BKE_constraint_add_for_object(ob, "AutoPath", CONSTRAINT_TYPE_FOLLOWPATH);
772 BKE_constraint_target_matrix_get(scene, con, 0, CONSTRAINT_OBTYPE_OBJECT, NULL, cmat, scene->r.cfra);
773 sub_v3_v3v3(vec, ob->obmat[3], cmat[3]);
775 copy_v3_v3(ob->loc, vec);
777 else if (pararm && (ob->type == OB_MESH) && (par->type == OB_ARMATURE)) {
778 if (partype == PAR_ARMATURE_NAME)
779 create_vgroups_from_armature(reports, scene, ob, par, ARM_GROUPS_NAME, false);
780 else if (partype == PAR_ARMATURE_ENVELOPE)
781 create_vgroups_from_armature(reports, scene, ob, par, ARM_GROUPS_ENVELOPE, xmirror);
782 else if (partype == PAR_ARMATURE_AUTO) {
784 create_vgroups_from_armature(reports, scene, ob, par, ARM_GROUPS_AUTO, xmirror);
787 /* get corrected inverse */
788 ob->partype = PAROBJECT;
789 BKE_object_workob_calc_parent(scene, ob, &workob);
791 invert_m4_m4(ob->parentinv, workob.obmat);
794 /* calculate inverse parent matrix */
795 BKE_object_workob_calc_parent(scene, ob, &workob);
796 invert_m4_m4(ob->parentinv, workob.obmat);
799 DAG_id_tag_update(&ob->id, OB_RECALC_OB | OB_RECALC_DATA);
808 static void parent_set_vert_find(KDTree *tree, Object *child, int vert_par[3], bool is_tri)
810 const float *co_find = child->obmat[3];
812 KDTreeNearest nearest[3];
815 tot = BLI_kdtree_find_nearest_n(tree, co_find, nearest, 3);
816 BLI_assert(tot == 3);
819 vert_par[0] = nearest[0].index;
820 vert_par[1] = nearest[1].index;
821 vert_par[2] = nearest[2].index;
823 BLI_assert(min_iii(UNPACK3(vert_par)) >= 0);
826 vert_par[0] = BLI_kdtree_find_nearest(tree, co_find, NULL);
827 BLI_assert(vert_par[0] >= 0);
833 static int parent_set_exec(bContext *C, wmOperator *op)
835 Main *bmain = CTX_data_main(C);
836 Scene *scene = CTX_data_scene(C);
837 Object *par = ED_object_active_context(C);
838 int partype = RNA_enum_get(op->ptr, "type");
839 const bool xmirror = RNA_boolean_get(op->ptr, "xmirror");
840 const bool keep_transform = RNA_boolean_get(op->ptr, "keep_transform");
843 /* vertex parent (kdtree) */
844 const bool is_vert_par = ELEM(partype, PAR_VERTEX, PAR_VERTEX_TRI);
845 const bool is_tri = partype == PAR_VERTEX_TRI;
847 struct KDTree *tree = NULL;
848 int vert_par[3] = {0, 0, 0};
849 const int *vert_par_p = is_vert_par ? vert_par : NULL;
853 tree = BKE_object_as_kdtree(par, &tree_tot);
854 BLI_assert(tree != NULL);
856 if (tree_tot < (is_tri ? 3 : 1)) {
857 BKE_report(op->reports, RPT_ERROR, "Not enough vertices for vertex-parent");
863 /* Non vertex-parent */
864 CTX_DATA_BEGIN (C, Object *, ob, selected_editable_objects)
867 parent_set_vert_find(tree, ob, vert_par, is_tri);
870 if (!ED_object_parent_set(op->reports, bmain, scene, ob, par, partype, xmirror, keep_transform, vert_par_p)) {
879 BLI_kdtree_free(tree);
883 return OPERATOR_CANCELLED;
885 DAG_relations_tag_update(bmain);
886 WM_event_add_notifier(C, NC_OBJECT | ND_TRANSFORM, NULL);
887 WM_event_add_notifier(C, NC_OBJECT | ND_PARENT, NULL);
889 return OPERATOR_FINISHED;
893 static int parent_set_invoke(bContext *C, wmOperator *UNUSED(op), const wmEvent *UNUSED(event))
895 Object *ob = ED_object_active_context(C);
896 uiPopupMenu *pup = UI_popup_menu_begin(C, IFACE_("Set Parent To"), ICON_NONE);
897 uiLayout *layout = UI_popup_menu_layout(pup);
899 wmOperatorType *ot = WM_operatortype_find("OBJECT_OT_parent_set", true);
903 uiItemEnumO_ptr(layout, ot, NULL, 0, "type", PAR_OBJECT);
905 opptr = uiItemFullO_ptr(layout, ot, IFACE_("Object"), ICON_NONE, NULL, WM_OP_EXEC_DEFAULT, UI_ITEM_O_RETURN_PROPS);
906 RNA_enum_set(&opptr, "type", PAR_OBJECT);
907 RNA_boolean_set(&opptr, "keep_transform", false);
909 opptr = uiItemFullO_ptr(layout, ot, IFACE_("Object (Keep Transform)"), ICON_NONE, NULL, WM_OP_EXEC_DEFAULT,
910 UI_ITEM_O_RETURN_PROPS);
911 RNA_enum_set(&opptr, "type", PAR_OBJECT);
912 RNA_boolean_set(&opptr, "keep_transform", true);
914 /* ob becomes parent, make the associated menus */
915 if (ob->type == OB_ARMATURE) {
916 uiItemEnumO_ptr(layout, ot, NULL, 0, "type", PAR_ARMATURE);
917 uiItemEnumO_ptr(layout, ot, NULL, 0, "type", PAR_ARMATURE_NAME);
918 uiItemEnumO_ptr(layout, ot, NULL, 0, "type", PAR_ARMATURE_ENVELOPE);
919 uiItemEnumO_ptr(layout, ot, NULL, 0, "type", PAR_ARMATURE_AUTO);
920 uiItemEnumO_ptr(layout, ot, NULL, 0, "type", PAR_BONE);
921 uiItemEnumO_ptr(layout, ot, NULL, 0, "type", PAR_BONE_RELATIVE);
923 else if (ob->type == OB_CURVE) {
924 uiItemEnumO_ptr(layout, ot, NULL, 0, "type", PAR_CURVE);
925 uiItemEnumO_ptr(layout, ot, NULL, 0, "type", PAR_FOLLOW);
926 uiItemEnumO_ptr(layout, ot, NULL, 0, "type", PAR_PATH_CONST);
928 else if (ob->type == OB_LATTICE) {
929 uiItemEnumO_ptr(layout, ot, NULL, 0, "type", PAR_LATTICE);
932 /* vertex parenting */
933 if (OB_TYPE_SUPPORT_PARVERT(ob->type)) {
934 uiItemEnumO_ptr(layout, ot, NULL, 0, "type", PAR_VERTEX);
935 uiItemEnumO_ptr(layout, ot, NULL, 0, "type", PAR_VERTEX_TRI);
938 UI_popup_menu_end(C, pup);
940 return OPERATOR_INTERFACE;
943 static bool parent_set_draw_check_prop(PointerRNA *ptr, PropertyRNA *prop)
945 const char *prop_id = RNA_property_identifier(prop);
946 const int type = RNA_enum_get(ptr, "type");
948 /* Only show XMirror for PAR_ARMATURE_ENVELOPE and PAR_ARMATURE_AUTO! */
949 if (STREQ(prop_id, "xmirror")) {
950 if (ELEM(type, PAR_ARMATURE_ENVELOPE, PAR_ARMATURE_AUTO))
959 static void parent_set_ui(bContext *C, wmOperator *op)
961 uiLayout *layout = op->layout;
962 wmWindowManager *wm = CTX_wm_manager(C);
965 RNA_pointer_create(&wm->id, op->type->srna, op->properties, &ptr);
967 /* Main auto-draw call. */
968 uiDefAutoButsRNA(layout, &ptr, parent_set_draw_check_prop, '\0');
971 void OBJECT_OT_parent_set(wmOperatorType *ot)
974 ot->name = "Make Parent";
975 ot->description = "Set the object's parenting";
976 ot->idname = "OBJECT_OT_parent_set";
979 ot->invoke = parent_set_invoke;
980 ot->exec = parent_set_exec;
981 ot->poll = ED_operator_object_active;
982 ot->ui = parent_set_ui;
985 ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
987 ot->prop = RNA_def_enum(ot->srna, "type", prop_make_parent_types, 0, "Type", "");
988 RNA_def_boolean(ot->srna, "xmirror", false, "X Mirror",
989 "Apply weights symmetrically along X axis, for Envelope/Automatic vertex groups creation");
990 RNA_def_boolean(ot->srna, "keep_transform", false, "Keep Transform",
991 "Apply transformation before parenting");
994 /* ************ Make Parent Without Inverse Operator ******************* */
996 static int parent_noinv_set_exec(bContext *C, wmOperator *op)
998 Main *bmain = CTX_data_main(C);
999 Object *par = ED_object_active_context(C);
1001 DAG_id_tag_update(&par->id, OB_RECALC_OB);
1003 /* context iterator */
1004 CTX_DATA_BEGIN (C, Object *, ob, selected_editable_objects)
1007 if (BKE_object_parent_loop_check(par, ob)) {
1008 BKE_report(op->reports, RPT_ERROR, "Loop in parents");
1011 /* clear inverse matrix and also the object location */
1012 unit_m4(ob->parentinv);
1013 memset(ob->loc, 0, 3 * sizeof(float));
1015 /* set recalc flags */
1016 DAG_id_tag_update(&ob->id, OB_RECALC_OB | OB_RECALC_DATA);
1018 /* set parenting type for object - object only... */
1020 ob->partype = PAROBJECT; /* note, dna define, not operator property */
1026 DAG_relations_tag_update(bmain);
1027 WM_event_add_notifier(C, NC_OBJECT | ND_TRANSFORM, NULL);
1029 return OPERATOR_FINISHED;
1032 void OBJECT_OT_parent_no_inverse_set(wmOperatorType *ot)
1035 ot->name = "Make Parent without Inverse";
1036 ot->description = "Set the object's parenting without setting the inverse parent correction";
1037 ot->idname = "OBJECT_OT_parent_no_inverse_set";
1040 ot->invoke = WM_operator_confirm;
1041 ot->exec = parent_noinv_set_exec;
1042 ot->poll = ED_operator_object_active_editable;
1045 ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
1048 /************************ Clear Slow Parent Operator *********************/
1050 static int object_slow_parent_clear_exec(bContext *C, wmOperator *UNUSED(op))
1052 Scene *scene = CTX_data_scene(C);
1054 CTX_DATA_BEGIN (C, Object *, ob, selected_editable_objects)
1057 if (ob->partype & PARSLOW) {
1058 ob->partype -= PARSLOW;
1059 BKE_object_where_is_calc(scene, ob);
1060 ob->partype |= PARSLOW;
1061 DAG_id_tag_update(&ob->id, OB_RECALC_OB);
1067 WM_event_add_notifier(C, NC_SCENE, scene);
1069 return OPERATOR_FINISHED;
1072 void OBJECT_OT_slow_parent_clear(wmOperatorType *ot)
1075 ot->name = "Clear Slow Parent";
1076 ot->description = "Clear the object's slow parent";
1077 ot->idname = "OBJECT_OT_slow_parent_clear";
1080 ot->invoke = WM_operator_confirm;
1081 ot->exec = object_slow_parent_clear_exec;
1082 ot->poll = ED_operator_view3d_active;
1085 ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
1088 /********************** Make Slow Parent Operator *********************/
1090 static int object_slow_parent_set_exec(bContext *C, wmOperator *UNUSED(op))
1092 Scene *scene = CTX_data_scene(C);
1094 CTX_DATA_BEGIN (C, Object *, ob, selected_editable_objects)
1097 ob->partype |= PARSLOW;
1099 DAG_id_tag_update(&ob->id, OB_RECALC_OB);
1103 WM_event_add_notifier(C, NC_SCENE, scene);
1105 return OPERATOR_FINISHED;
1108 void OBJECT_OT_slow_parent_set(wmOperatorType *ot)
1111 ot->name = "Set Slow Parent";
1112 ot->description = "Set the object's slow parent";
1113 ot->idname = "OBJECT_OT_slow_parent_set";
1116 ot->invoke = WM_operator_confirm;
1117 ot->exec = object_slow_parent_set_exec;
1118 ot->poll = ED_operator_view3d_active;
1121 ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
1124 /* ******************** Clear Track Operator ******************* */
1128 CLEAR_TRACK_KEEP_TRANSFORM = 2,
1131 static EnumPropertyItem prop_clear_track_types[] = {
1132 {CLEAR_TRACK, "CLEAR", 0, "Clear Track", ""},
1133 {CLEAR_TRACK_KEEP_TRANSFORM, "CLEAR_KEEP_TRANSFORM", 0, "Clear and Keep Transformation (Clear Track)", ""},
1134 {0, NULL, 0, NULL, NULL}
1137 /* note, poll should check for editable scene */
1138 static int object_track_clear_exec(bContext *C, wmOperator *op)
1140 Main *bmain = CTX_data_main(C);
1141 const int type = RNA_enum_get(op->ptr, "type");
1143 if (CTX_data_edit_object(C)) {
1144 BKE_report(op->reports, RPT_ERROR, "Operation cannot be performed in edit mode");
1145 return OPERATOR_CANCELLED;
1147 CTX_DATA_BEGIN (C, Object *, ob, selected_editable_objects)
1149 bConstraint *con, *pcon;
1151 /* remove track-object for old track */
1153 DAG_id_tag_update(&ob->id, OB_RECALC_OB | OB_RECALC_DATA | OB_RECALC_TIME);
1155 /* also remove all tracking constraints */
1156 for (con = ob->constraints.last; con; con = pcon) {
1158 if (ELEM(con->type, CONSTRAINT_TYPE_TRACKTO, CONSTRAINT_TYPE_LOCKTRACK, CONSTRAINT_TYPE_DAMPTRACK))
1159 BKE_constraint_remove(&ob->constraints, con);
1162 if (type == CLEAR_TRACK_KEEP_TRANSFORM)
1163 BKE_object_apply_mat4(ob, ob->obmat, true, true);
1167 DAG_relations_tag_update(bmain);
1168 WM_event_add_notifier(C, NC_OBJECT | ND_TRANSFORM, NULL);
1170 return OPERATOR_FINISHED;
1173 void OBJECT_OT_track_clear(wmOperatorType *ot)
1176 ot->name = "Clear Track";
1177 ot->description = "Clear tracking constraint or flag from object";
1178 ot->idname = "OBJECT_OT_track_clear";
1181 ot->invoke = WM_menu_invoke;
1182 ot->exec = object_track_clear_exec;
1184 ot->poll = ED_operator_objectmode;
1187 ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
1189 ot->prop = RNA_def_enum(ot->srna, "type", prop_clear_track_types, 0, "Type", "");
1192 /************************** Make Track Operator *****************************/
1195 CREATE_TRACK_DAMPTRACK = 1,
1196 CREATE_TRACK_TRACKTO = 2,
1197 CREATE_TRACK_LOCKTRACK = 3,
1200 static EnumPropertyItem prop_make_track_types[] = {
1201 {CREATE_TRACK_DAMPTRACK, "DAMPTRACK", 0, "Damped Track Constraint", ""},
1202 {CREATE_TRACK_TRACKTO, "TRACKTO", 0, "Track To Constraint", ""},
1203 {CREATE_TRACK_LOCKTRACK, "LOCKTRACK", 0, "Lock Track Constraint", ""},
1204 {0, NULL, 0, NULL, NULL}
1207 static int track_set_exec(bContext *C, wmOperator *op)
1209 Main *bmain = CTX_data_main(C);
1210 Object *obact = ED_object_active_context(C);
1212 const int type = RNA_enum_get(op->ptr, "type");
1215 case CREATE_TRACK_DAMPTRACK:
1218 bDampTrackConstraint *data;
1220 CTX_DATA_BEGIN (C, Object *, ob, selected_editable_objects)
1223 con = BKE_constraint_add_for_object(ob, "AutoTrack", CONSTRAINT_TYPE_DAMPTRACK);
1227 DAG_id_tag_update(&ob->id, OB_RECALC_OB | OB_RECALC_DATA | OB_RECALC_TIME);
1229 /* Lamp, Camera and Speaker track differently by default */
1230 if (ELEM(ob->type, OB_LAMP, OB_CAMERA, OB_SPEAKER)) {
1231 data->trackflag = TRACK_nZ;
1238 case CREATE_TRACK_TRACKTO:
1241 bTrackToConstraint *data;
1243 CTX_DATA_BEGIN (C, Object *, ob, selected_editable_objects)
1246 con = BKE_constraint_add_for_object(ob, "AutoTrack", CONSTRAINT_TYPE_TRACKTO);
1250 DAG_id_tag_update(&ob->id, OB_RECALC_OB | OB_RECALC_DATA | OB_RECALC_TIME);
1252 /* Lamp, Camera and Speaker track differently by default */
1253 if (ELEM(ob->type, OB_LAMP, OB_CAMERA, OB_SPEAKER)) {
1254 data->reserved1 = TRACK_nZ;
1255 data->reserved2 = UP_Y;
1262 case CREATE_TRACK_LOCKTRACK:
1265 bLockTrackConstraint *data;
1267 CTX_DATA_BEGIN (C, Object *, ob, selected_editable_objects)
1270 con = BKE_constraint_add_for_object(ob, "AutoTrack", CONSTRAINT_TYPE_LOCKTRACK);
1274 DAG_id_tag_update(&ob->id, OB_RECALC_OB | OB_RECALC_DATA | OB_RECALC_TIME);
1276 /* Lamp, Camera and Speaker track differently by default */
1277 if (ELEM(ob->type, OB_LAMP, OB_CAMERA, OB_SPEAKER)) {
1278 data->trackflag = TRACK_nZ;
1279 data->lockflag = LOCK_Y;
1288 DAG_relations_tag_update(bmain);
1289 WM_event_add_notifier(C, NC_OBJECT | ND_TRANSFORM, NULL);
1291 return OPERATOR_FINISHED;
1294 void OBJECT_OT_track_set(wmOperatorType *ot)
1297 ot->name = "Make Track";
1298 ot->description = "Make the object track another object, using various methods/constraints";
1299 ot->idname = "OBJECT_OT_track_set";
1302 ot->invoke = WM_menu_invoke;
1303 ot->exec = track_set_exec;
1305 ot->poll = ED_operator_objectmode;
1308 ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
1311 ot->prop = RNA_def_enum(ot->srna, "type", prop_make_track_types, 0, "Type", "");
1314 /************************** Link to Scene Operator *****************************/
1317 static void link_to_scene(Main *UNUSED(bmain), unsigned short UNUSED(nr))
1319 Scene *sce = (Scene *) BLI_findlink(&bmain->scene, G.curscreen->scenenr - 1);
1322 if (sce == NULL) return;
1323 if (sce->id.lib) return;
1325 for (base = FIRSTBASE; base; base = base->next) {
1326 if (TESTBASE(v3d, base)) {
1327 nbase = MEM_mallocN(sizeof(Base), "newbase");
1329 BLI_addhead(&(sce->base), nbase);
1330 id_us_plus((ID *)base->object);
1336 static int make_links_scene_exec(bContext *C, wmOperator *op)
1338 Scene *scene_to = BLI_findlink(&CTX_data_main(C)->scene, RNA_enum_get(op->ptr, "scene"));
1340 if (scene_to == NULL) {
1341 BKE_report(op->reports, RPT_ERROR, "Could not find scene");
1342 return OPERATOR_CANCELLED;
1345 if (scene_to == CTX_data_scene(C)) {
1346 BKE_report(op->reports, RPT_ERROR, "Cannot link objects into the same scene");
1347 return OPERATOR_CANCELLED;
1350 if (ID_IS_LINKED_DATABLOCK(scene_to)) {
1351 BKE_report(op->reports, RPT_ERROR, "Cannot link objects into a linked scene");
1352 return OPERATOR_CANCELLED;
1355 SceneCollection *sc_to = BKE_collection_master(scene_to);
1356 CTX_DATA_BEGIN (C, Base *, base, selected_bases)
1358 BKE_collection_object_add(scene_to, sc_to, base->object);
1362 /* redraw the 3D view because the object center points are colored differently */
1363 WM_event_add_notifier(C, NC_OBJECT | ND_DRAW, NULL);
1365 /* one day multiple scenes will be visible, then we should have some update function for them */
1366 return OPERATOR_FINISHED;
1370 MAKE_LINKS_OBDATA = 1,
1371 MAKE_LINKS_MATERIALS = 2,
1372 MAKE_LINKS_ANIMDATA = 3,
1373 MAKE_LINKS_GROUP = 4,
1374 MAKE_LINKS_DUPLIGROUP = 5,
1375 MAKE_LINKS_MODIFIERS = 6,
1376 MAKE_LINKS_FONTS = 7,
1379 /* Return true if make link data is allowed, false otherwise */
1380 static bool allow_make_links_data(const int type, Object *ob_src, Object *ob_dst)
1383 case MAKE_LINKS_OBDATA:
1384 if (ob_src->type == ob_dst->type && ob_src->type != OB_EMPTY) {
1388 case MAKE_LINKS_MATERIALS:
1389 if (OB_TYPE_SUPPORT_MATERIAL(ob_src->type) && OB_TYPE_SUPPORT_MATERIAL(ob_dst->type)) {
1393 case MAKE_LINKS_ANIMDATA:
1394 case MAKE_LINKS_GROUP:
1395 case MAKE_LINKS_DUPLIGROUP:
1397 case MAKE_LINKS_MODIFIERS:
1398 if (!ELEM(OB_EMPTY, ob_src->type, ob_dst->type)) {
1402 case MAKE_LINKS_FONTS:
1403 if ((ob_src->data != ob_dst->data) && (ob_src->type == OB_FONT) && (ob_dst->type == OB_FONT)) {
1411 static int make_links_data_exec(bContext *C, wmOperator *op)
1413 Main *bmain = CTX_data_main(C);
1414 const int type = RNA_enum_get(op->ptr, "type");
1420 LinkNode *ob_groups = NULL;
1421 bool is_cycle = false;
1422 bool is_lib = false;
1424 ob_src = ED_object_active_context(C);
1426 /* avoid searching all groups in source object each time */
1427 if (type == MAKE_LINKS_GROUP) {
1428 ob_groups = BKE_object_groups(ob_src);
1431 CTX_DATA_BEGIN (C, Base *, base_dst, selected_editable_bases)
1433 Object *ob_dst = base_dst->object;
1435 if (ob_src != ob_dst) {
1436 if (allow_make_links_data(type, ob_src, ob_dst)) {
1437 obdata_id = ob_dst->data;
1440 case MAKE_LINKS_OBDATA: /* obdata */
1441 id_us_min(obdata_id);
1443 obdata_id = ob_src->data;
1444 id_us_plus(obdata_id);
1445 ob_dst->data = obdata_id;
1447 /* if amount of material indices changed: */
1448 test_object_materials(ob_dst, ob_dst->data);
1450 DAG_id_tag_update(&ob_dst->id, OB_RECALC_DATA);
1452 case MAKE_LINKS_MATERIALS:
1453 /* new approach, using functions from kernel */
1454 for (a = 0; a < ob_src->totcol; a++) {
1455 Material *ma = give_current_material(ob_src, a + 1);
1456 assign_material(ob_dst, ma, a + 1, BKE_MAT_ASSIGN_USERPREF); /* also works with ma==NULL */
1458 DAG_id_tag_update(&ob_dst->id, OB_RECALC_DATA);
1460 case MAKE_LINKS_ANIMDATA:
1461 BKE_animdata_copy_id((ID *)ob_dst, (ID *)ob_src, false);
1462 if (ob_dst->data && ob_src->data) {
1463 if (ID_IS_LINKED_DATABLOCK(obdata_id)) {
1467 BKE_animdata_copy_id((ID *)ob_dst->data, (ID *)ob_src->data, false);
1469 DAG_id_tag_update(&ob_dst->id, OB_RECALC_OB | OB_RECALC_DATA | OB_RECALC_TIME);
1471 case MAKE_LINKS_GROUP:
1473 LinkNode *group_node;
1475 /* first clear groups */
1476 BKE_object_groups_clear(ob_dst);
1478 /* now add in the groups from the link nodes */
1479 for (group_node = ob_groups; group_node; group_node = group_node->next) {
1480 if (ob_dst->dup_group != group_node->link) {
1481 BKE_group_object_add(group_node->link, ob_dst);
1489 case MAKE_LINKS_DUPLIGROUP:
1490 ob_dst->dup_group = ob_src->dup_group;
1491 if (ob_dst->dup_group) {
1492 id_us_plus(&ob_dst->dup_group->id);
1493 ob_dst->transflag |= OB_DUPLIGROUP;
1496 case MAKE_LINKS_MODIFIERS:
1497 BKE_object_link_modifiers(ob_dst, ob_src);
1498 DAG_id_tag_update(&ob_dst->id, OB_RECALC_OB | OB_RECALC_DATA | OB_RECALC_TIME);
1500 case MAKE_LINKS_FONTS:
1502 Curve *cu_src = ob_src->data;
1503 Curve *cu_dst = ob_dst->data;
1505 if (ID_IS_LINKED_DATABLOCK(obdata_id)) {
1511 id_us_min(&cu_dst->vfont->id);
1512 cu_dst->vfont = cu_src->vfont;
1513 id_us_plus((ID *)cu_dst->vfont);
1515 id_us_min(&cu_dst->vfontb->id);
1516 cu_dst->vfontb = cu_src->vfontb;
1517 id_us_plus((ID *)cu_dst->vfontb);
1519 id_us_min(&cu_dst->vfonti->id);
1520 cu_dst->vfonti = cu_src->vfonti;
1521 id_us_plus((ID *)cu_dst->vfonti);
1522 if (cu_dst->vfontbi)
1523 id_us_min(&cu_dst->vfontbi->id);
1524 cu_dst->vfontbi = cu_src->vfontbi;
1525 id_us_plus((ID *)cu_dst->vfontbi);
1527 DAG_id_tag_update(&ob_dst->id, OB_RECALC_OB | OB_RECALC_DATA | OB_RECALC_TIME);
1536 if (type == MAKE_LINKS_GROUP) {
1538 BLI_linklist_free(ob_groups, NULL);
1542 BKE_report(op->reports, RPT_WARNING, "Skipped some groups because of cycle detected");
1547 BKE_report(op->reports, RPT_WARNING, "Skipped editing library object data");
1550 DAG_relations_tag_update(bmain);
1551 WM_event_add_notifier(C, NC_SPACE | ND_SPACE_VIEW3D, CTX_wm_view3d(C));
1552 WM_event_add_notifier(C, NC_ANIMATION | ND_NLA_ACTCHANGE, CTX_wm_view3d(C));
1553 WM_event_add_notifier(C, NC_OBJECT, NULL);
1555 return OPERATOR_FINISHED;
1559 void OBJECT_OT_make_links_scene(wmOperatorType *ot)
1564 ot->name = "Link Objects to Scene";
1565 ot->description = "Link selection to another scene";
1566 ot->idname = "OBJECT_OT_make_links_scene";
1569 ot->invoke = WM_enum_search_invoke;
1570 ot->exec = make_links_scene_exec;
1571 /* better not run the poll check */
1574 ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
1577 prop = RNA_def_enum(ot->srna, "scene", DummyRNA_NULL_items, 0, "Scene", "");
1578 RNA_def_enum_funcs(prop, RNA_scene_local_itemf);
1579 RNA_def_property_flag(prop, PROP_ENUM_NO_TRANSLATE);
1583 void OBJECT_OT_make_links_data(wmOperatorType *ot)
1585 static EnumPropertyItem make_links_items[] = {
1586 {MAKE_LINKS_OBDATA, "OBDATA", 0, "Object Data", ""},
1587 {MAKE_LINKS_MATERIALS, "MATERIAL", 0, "Materials", ""},
1588 {MAKE_LINKS_ANIMDATA, "ANIMATION", 0, "Animation Data", ""},
1589 {MAKE_LINKS_GROUP, "GROUPS", 0, "Group", ""},
1590 {MAKE_LINKS_DUPLIGROUP, "DUPLIGROUP", 0, "DupliGroup", ""},
1591 {MAKE_LINKS_MODIFIERS, "MODIFIERS", 0, "Modifiers", ""},
1592 {MAKE_LINKS_FONTS, "FONTS", 0, "Fonts", ""},
1593 {0, NULL, 0, NULL, NULL}};
1596 ot->name = "Link Data";
1597 ot->description = "Apply active object links to other selected objects";
1598 ot->idname = "OBJECT_OT_make_links_data";
1601 ot->exec = make_links_data_exec;
1602 ot->poll = ED_operator_object_active;
1605 ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
1608 ot->prop = RNA_def_enum(ot->srna, "type", make_links_items, 0, "Type", "");
1612 /**************************** Make Single User ********************************/
1614 static Object *single_object_users_object(Main *bmain, Scene *scene, Object *ob, const bool copy_groups)
1616 if (!ID_IS_LINKED_DATABLOCK(ob) && ob->id.us > 1) {
1617 /* base gets copy of object */
1618 Object *obn = ID_NEW_SET(ob, BKE_object_copy(bmain, ob));
1621 if (ob->flag & OB_FROMGROUP) {
1622 obn->flag |= OB_FROMGROUP;
1626 /* copy already clears */
1628 /* remap gpencil parenting */
1631 bGPdata *gpd = scene->gpd;
1632 for (bGPDlayer *gpl = gpd->layers.first; gpl; gpl = gpl->next) {
1633 if (gpl->parent == ob) {
1645 static void libblock_relink_scene_collection(SceneCollection *sc)
1647 for (LinkData *link = sc->objects.first; link; link = link->next) {
1648 BKE_libblock_relink_to_newid(link->data);
1651 for (SceneCollection *nsc = sc->scene_collections.first; nsc; nsc = nsc->next) {
1652 libblock_relink_scene_collection(nsc);
1656 static void single_object_users_scene_collection(Main *bmain, Scene *scene, SceneCollection *sc, const int flag, const bool copy_groups)
1658 for (LinkData *link = sc->objects.first; link; link = link->next) {
1659 Object *ob = link->data;
1660 /* an object may be in more than one collection */
1661 if ((ob->id.newid == NULL) && ((ob->flag & flag) == flag)) {
1662 link->data = single_object_users_object(bmain, scene, link->data, copy_groups);
1666 /* we reset filter objects because they should be regenerated after this */
1667 BLI_freelistN(&sc->filter_objects);
1669 for (SceneCollection *nsc = sc->scene_collections.first; nsc; nsc = nsc->next) {
1670 single_object_users_scene_collection(bmain, scene, nsc, flag, copy_groups);
1674 /* Warning, sets ID->newid pointers of objects and groups, but does not clear them. */
1675 static void single_object_users(Main *bmain, Scene *scene, View3D *v3d, const int flag, const bool copy_groups)
1677 Group *group, *groupn;
1680 clear_sca_new_poins(); /* BGE logic */
1682 /* duplicate all the objects of the scene */
1683 SceneCollection *msc = BKE_collection_master(scene);
1684 single_object_users_scene_collection(bmain, scene, msc, flag, copy_groups);
1686 /* loop over SceneLayers and assign the pointers accordingly */
1687 for (SceneLayer *sl = scene->render_layers.first; sl; sl = sl->next) {
1688 for (Base *base = sl->object_bases.first; base; base = base->next) {
1689 ID_NEW_REMAP(base->object);
1693 /* duplicate groups that consist entirely of duplicated objects */
1694 for (group = bmain->group.first; group; group = group->id.next) {
1695 if (copy_groups && group->gobject.first) {
1696 bool all_duplicated = true;
1698 for (go = group->gobject.first; go; go = go->next) {
1699 if (!(go->ob && (go->ob->id.newid))) {
1700 all_duplicated = false;
1705 if (all_duplicated) {
1706 groupn = ID_NEW_SET(group, BKE_group_copy(bmain, group));
1708 for (go = groupn->gobject.first; go; go = go->next) {
1709 go->ob = (Object *)go->ob->id.newid;
1715 /* group pointers in scene */
1716 BKE_scene_groups_relink(scene);
1718 ID_NEW_REMAP(scene->camera);
1719 if (v3d) ID_NEW_REMAP(v3d->camera);
1721 /* object and group pointers */
1722 libblock_relink_scene_collection(msc);
1724 set_sca_new_poins();
1726 /* TODO redo filter */
1727 TODO_LAYER_SYNC_FILTER
1730 /* not an especially efficient function, only added so the single user
1731 * button can be functional.*/
1732 void ED_object_single_user(Main *bmain, Scene *scene, Object *ob)
1734 FOREACH_SCENE_OBJECT(scene, ob_iter)
1736 ob_iter->flag &= ~OB_DONE;
1738 FOREACH_SCENE_OBJECT_END
1740 /* tag only the one object */
1741 ob->flag |= OB_DONE;
1743 single_object_users(bmain, scene, NULL, OB_DONE, false);
1744 BKE_main_id_clear_newpoins(bmain);
1747 static void new_id_matar(Main *bmain, Material **matar, const int totcol)
1752 for (a = 0; a < totcol; a++) {
1753 id = (ID *)matar[a];
1754 if (id && !ID_IS_LINKED_DATABLOCK(id)) {
1756 matar[a] = (Material *)id->newid;
1757 id_us_plus(id->newid);
1760 else if (id->us > 1) {
1761 matar[a] = ID_NEW_SET(id, BKE_material_copy(bmain, matar[a]));
1768 static void single_obdata_users(Main *bmain, Scene *scene, SceneLayer *sl, const int flag)
1778 FOREACH_OBJECT_FLAG(scene, sl, flag, ob)
1780 if (!ID_IS_LINKED_DATABLOCK(ob)) {
1783 if (id && id->us > 1 && !ID_IS_LINKED_DATABLOCK(id)) {
1784 DAG_id_tag_update(&ob->id, OB_RECALC_DATA);
1788 ob->data = la = ID_NEW_SET(ob->data, BKE_lamp_copy(bmain, ob->data));
1789 for (a = 0; a < MAX_MTEX; a++) {
1791 ID_NEW_REMAP(la->mtex[a]->object);
1796 ob->data = ID_NEW_SET(ob->data, BKE_camera_copy(bmain, ob->data));
1799 /* Needed to remap texcomesh below. */
1800 me = ob->data = ID_NEW_SET(ob->data, BKE_mesh_copy(bmain, ob->data));
1801 if (me->key) /* We do not need to set me->key->id.newid here... */
1802 BKE_animdata_copy_id_action((ID *)me->key, false);
1805 ob->data = ID_NEW_SET(ob->data, BKE_mball_copy(bmain, ob->data));
1810 ob->data = cu = ID_NEW_SET(ob->data, BKE_curve_copy(bmain, ob->data));
1811 ID_NEW_REMAP(cu->bevobj);
1812 ID_NEW_REMAP(cu->taperobj);
1813 if (cu->key) /* We do not need to set cu->key->id.newid here... */
1814 BKE_animdata_copy_id_action((ID *)cu->key, false);
1817 ob->data = lat = ID_NEW_SET(ob->data, BKE_lattice_copy(bmain, ob->data));
1818 if (lat->key) /* We do not need to set lat->key->id.newid here... */
1819 BKE_animdata_copy_id_action((ID *)lat->key, false);
1822 DAG_id_tag_update(&ob->id, OB_RECALC_DATA);
1823 ob->data = ID_NEW_SET(ob->data, BKE_armature_copy(bmain, ob->data));
1824 BKE_pose_rebuild(ob, ob->data);
1827 ob->data = ID_NEW_SET(ob->data, BKE_speaker_copy(bmain, ob->data));
1830 if (G.debug & G_DEBUG)
1831 printf("ERROR %s: can't copy %s\n", __func__, id->name);
1835 /* Copy animation data after object data became local,
1836 * otherwise old and new object data will share the same
1837 * AnimData structure, which is not what we want.
1840 BKE_animdata_copy_id_action((ID *)ob->data, false);
1846 FOREACH_OBJECT_FLAG_END
1848 me = bmain->mesh.first;
1850 ID_NEW_REMAP(me->texcomesh);
1855 static void single_object_action_users(Scene *scene, SceneLayer *sl, const int flag)
1857 FOREACH_OBJECT_FLAG(scene, sl, flag, ob)
1858 if (!ID_IS_LINKED_DATABLOCK(ob)) {
1859 DAG_id_tag_update(&ob->id, OB_RECALC_DATA);
1860 BKE_animdata_copy_id_action(&ob->id, false);
1862 FOREACH_OBJECT_FLAG_END
1865 static void single_mat_users(Main *bmain, Scene *scene, SceneLayer *sl, const int flag, const bool do_textures)
1871 FOREACH_OBJECT_FLAG(scene, sl, flag, ob)
1872 if (!ID_IS_LINKED_DATABLOCK(ob)) {
1873 for (a = 1; a <= ob->totcol; a++) {
1874 ma = give_current_material(ob, a);
1876 /* do not test for LIB_TAG_NEW or use newid: this functions guaranteed delivers single_users! */
1878 if (ma->id.us > 1) {
1879 man = BKE_material_copy(bmain, ma);
1880 BKE_animdata_copy_id_action(&man->id, false);
1883 assign_material(ob, man, a, BKE_MAT_ASSIGN_USERPREF);
1886 for (b = 0; b < MAX_MTEX; b++) {
1887 if (ma->mtex[b] && (tex = ma->mtex[b]->tex)) {
1888 if (tex->id.us > 1) {
1889 id_us_min(&tex->id);
1890 tex = BKE_texture_copy(bmain, tex);
1891 BKE_animdata_copy_id_action(&tex->id, false);
1892 man->mtex[b]->tex = tex;
1901 FOREACH_OBJECT_FLAG_END
1904 static void do_single_tex_user(Main *bmain, Tex **from)
1909 if (tex == NULL) return;
1911 if (tex->id.newid) {
1912 *from = (Tex *)tex->id.newid;
1913 id_us_plus(tex->id.newid);
1914 id_us_min(&tex->id);
1916 else if (tex->id.us > 1) {
1917 texn = ID_NEW_SET(tex, BKE_texture_copy(bmain, tex));
1918 BKE_animdata_copy_id_action(&texn->id, false);
1919 tex->id.newid = (ID *)texn;
1920 id_us_min(&tex->id);
1925 static void single_tex_users_expand(Main *bmain)
1927 /* only when 'parent' blocks are LIB_TAG_NEW */
1933 for (ma = bmain->mat.first; ma; ma = ma->id.next) {
1934 if (ma->id.tag & LIB_TAG_NEW) {
1935 for (b = 0; b < MAX_MTEX; b++) {
1936 if (ma->mtex[b] && ma->mtex[b]->tex) {
1937 do_single_tex_user(bmain, &(ma->mtex[b]->tex));
1943 for (la = bmain->lamp.first; la; la = la->id.next) {
1944 if (la->id.tag & LIB_TAG_NEW) {
1945 for (b = 0; b < MAX_MTEX; b++) {
1946 if (la->mtex[b] && la->mtex[b]->tex) {
1947 do_single_tex_user(bmain, &(la->mtex[b]->tex));
1953 for (wo = bmain->world.first; wo; wo = wo->id.next) {
1954 if (wo->id.tag & LIB_TAG_NEW) {
1955 for (b = 0; b < MAX_MTEX; b++) {
1956 if (wo->mtex[b] && wo->mtex[b]->tex) {
1957 do_single_tex_user(bmain, &(wo->mtex[b]->tex));
1964 static void single_mat_users_expand(Main *bmain)
1966 /* only when 'parent' blocks are LIB_TAG_NEW */
1974 for (ob = bmain->object.first; ob; ob = ob->id.next)
1975 if (ob->id.tag & LIB_TAG_NEW)
1976 new_id_matar(bmain, ob->mat, ob->totcol);
1978 for (me = bmain->mesh.first; me; me = me->id.next)
1979 if (me->id.tag & LIB_TAG_NEW)
1980 new_id_matar(bmain, me->mat, me->totcol);
1982 for (cu = bmain->curve.first; cu; cu = cu->id.next)
1983 if (cu->id.tag & LIB_TAG_NEW)
1984 new_id_matar(bmain, cu->mat, cu->totcol);
1986 for (mb = bmain->mball.first; mb; mb = mb->id.next)
1987 if (mb->id.tag & LIB_TAG_NEW)
1988 new_id_matar(bmain, mb->mat, mb->totcol);
1990 /* material imats */
1991 for (ma = bmain->mat.first; ma; ma = ma->id.next)
1992 if (ma->id.tag & LIB_TAG_NEW)
1993 for (a = 0; a < MAX_MTEX; a++)
1995 ID_NEW_REMAP(ma->mtex[a]->object);
1998 /* used for copying scenes */
1999 void ED_object_single_users(Main *bmain, Scene *scene, const bool full, const bool copy_groups)
2001 single_object_users(bmain, scene, NULL, 0, copy_groups);
2004 single_obdata_users(bmain, scene, NULL, 0);
2005 single_object_action_users(scene, NULL, 0);
2006 single_mat_users_expand(bmain);
2007 single_tex_users_expand(bmain);
2010 /* Relink nodetrees' pointers that have been duplicated. */
2011 FOREACH_NODETREE(bmain, ntree, id)
2013 /* This is a bit convoluted, we want to root ntree of copied IDs and only those,
2014 * so we first check that old ID has been copied and that ntree is root tree of old ID,
2015 * then get root tree of new ID and remap its pointers to new ID... */
2016 if (id->newid && (&ntree->id != id)) {
2017 ntree = ntreeFromID(id->newid);
2018 BKE_libblock_relink_to_newid(&ntree->id);
2020 } FOREACH_NODETREE_END
2022 /* Relink datablock pointer properties */
2024 IDP_RelinkProperty(scene->id.properties);
2026 for (Base *base = scene->base.first; base; base = base->next) {
2027 Object *ob = base->object;
2028 if (!ID_IS_LINKED_DATABLOCK(ob)) {
2029 IDP_RelinkProperty(ob->id.properties);
2033 if (scene->nodetree) {
2034 IDP_RelinkProperty(scene->nodetree->id.properties);
2035 for (bNode *node = scene->nodetree->nodes.first; node; node = node->next) {
2036 IDP_RelinkProperty(node->prop);
2041 IDP_RelinkProperty(scene->gpd->id.properties);
2044 IDP_RelinkProperty(scene->world->id.properties);
2047 IDP_RelinkProperty(scene->clip->id.properties);
2050 BKE_main_id_clear_newpoins(bmain);
2051 DAG_relations_tag_update(bmain);
2054 /******************************* Make Local ***********************************/
2056 /* helper for below, ma was checked to be not NULL */
2057 static void make_local_makelocalmaterial(Material *ma)
2062 id_make_local(G.main, &ma->id, false, false);
2064 for (b = 0; b < MAX_MTEX; b++)
2065 if (ma->mtex[b] && ma->mtex[b]->tex)
2066 id_make_local(G.main, &ma->mtex[b]->tex->id, false, false);
2068 adt = BKE_animdata_from_id(&ma->id);
2069 if (adt) BKE_animdata_make_local(adt);
2075 MAKE_LOCAL_SELECT_OB = 1,
2076 MAKE_LOCAL_SELECT_OBDATA = 2,
2077 MAKE_LOCAL_SELECT_OBDATA_MATERIAL = 3,
2081 static int tag_localizable_looper(
2082 void *UNUSED(user_data), ID *UNUSED(self_id), ID **id_pointer, const int UNUSED(cb_flag))
2085 (*id_pointer)->tag &= ~LIB_TAG_DOIT;
2088 return IDWALK_RET_NOP;
2091 static void tag_localizable_objects(bContext *C, const int mode)
2093 Main *bmain = CTX_data_main(C);
2095 BKE_main_id_tag_all(bmain, LIB_TAG_DOIT, false);
2097 /* Set LIB_TAG_DOIT flag for all selected objects, so next we can check whether
2098 * object is gonna to become local or not.
2100 CTX_DATA_BEGIN (C, Object *, object, selected_objects)
2102 object->id.tag |= LIB_TAG_DOIT;
2104 /* If data is also gonna to become local, mark data we're interested in
2105 * as gonna-to-be-local.
2107 if (mode == MAKE_LOCAL_SELECT_OBDATA && object->data) {
2108 ID *data_id = (ID *) object->data;
2109 data_id->tag |= LIB_TAG_DOIT;
2114 /* Also forbid making objects local if other library objects are using
2115 * them for modifiers or constraints.
2117 for (Object *object = bmain->object.first; object; object = object->id.next) {
2118 if ((object->id.tag & LIB_TAG_DOIT) == 0) {
2119 BKE_library_foreach_ID_link(NULL, &object->id, tag_localizable_looper, NULL, IDWALK_READONLY);
2122 ID *data_id = (ID *) object->data;
2123 if ((data_id->tag & LIB_TAG_DOIT) == 0) {
2124 BKE_library_foreach_ID_link(NULL, data_id, tag_localizable_looper, NULL, IDWALK_READONLY);
2129 /* TODO(sergey): Drivers targets? */
2133 * Instance indirectly referenced zero user objects,
2134 * otherwise they're lost on reload, see T40595.
2136 static bool make_local_all__instance_indirect_unused(Main *bmain, Scene *scene, SceneLayer *sl, SceneCollection *sc)
2139 bool changed = false;
2141 for (ob = bmain->object.first; ob; ob = ob->id.next) {
2142 if (ID_IS_LINKED_DATABLOCK(ob) && (ob->id.us == 0)) {
2145 id_us_plus(&ob->id);
2147 BKE_collection_object_add(scene, sc, ob);
2148 base = BKE_scene_layer_base_find(sl, ob);
2149 base->flag |= BASE_SELECTED;
2150 BKE_scene_object_base_flag_sync_from_base(base);
2151 DAG_id_tag_update(&ob->id, OB_RECALC_OB | OB_RECALC_DATA | OB_RECALC_TIME);
2160 static int make_local_exec(bContext *C, wmOperator *op)
2162 Main *bmain = CTX_data_main(C);
2163 Scene *scene = CTX_data_scene(C);
2164 SceneLayer *sl = CTX_data_scene_layer(C);
2165 SceneCollection *sc = CTX_data_scene_collection(C);
2167 ParticleSystem *psys;
2168 Material *ma, ***matarar;
2171 const int mode = RNA_enum_get(op->ptr, "type");
2174 if (mode == MAKE_LOCAL_ALL) {
2175 /* de-select so the user can differentiate newly instanced from existing objects */
2176 BKE_scene_base_deselect_all(scene);
2178 if (make_local_all__instance_indirect_unused(bmain, scene, sl, sc)) {
2179 BKE_report(op->reports, RPT_INFO,
2180 "Orphan library objects added to the current scene to avoid loss");
2183 BKE_library_make_local(bmain, NULL, NULL, false, false); /* NULL is all libs */
2184 WM_event_add_notifier(C, NC_WINDOW, NULL);
2185 return OPERATOR_FINISHED;
2188 tag_localizable_objects(C, mode);
2190 CTX_DATA_BEGIN (C, Object *, ob, selected_objects)
2192 if ((ob->id.tag & LIB_TAG_DOIT) == 0) {
2197 id_make_local(bmain, &ob->id, false, false);
2201 /* maybe object pointers */
2202 CTX_DATA_BEGIN (C, Object *, ob, selected_objects)
2204 if (ob->id.lib == NULL) {
2205 ID_NEW_REMAP(ob->parent);
2210 CTX_DATA_BEGIN (C, Object *, ob, selected_objects)
2212 if ((ob->id.tag & LIB_TAG_DOIT) == 0) {
2218 if (id && (ELEM(mode, MAKE_LOCAL_SELECT_OBDATA, MAKE_LOCAL_SELECT_OBDATA_MATERIAL))) {
2219 id_make_local(bmain, id, false, false);
2220 adt = BKE_animdata_from_id(id);
2221 if (adt) BKE_animdata_make_local(adt);
2223 /* tag indirect data direct */
2224 matarar = give_matarar(ob);
2226 for (a = 0; a < ob->totcol; a++) {
2229 id_lib_extern(&ma->id);
2234 for (psys = ob->particlesystem.first; psys; psys = psys->next)
2235 id_make_local(bmain, &psys->part->id, false, false);
2237 adt = BKE_animdata_from_id(&ob->id);
2238 if (adt) BKE_animdata_make_local(adt);
2242 if (mode == MAKE_LOCAL_SELECT_OBDATA_MATERIAL) {
2243 CTX_DATA_BEGIN (C, Object *, ob, selected_objects)
2245 if ((ob->id.tag & LIB_TAG_DOIT) == 0) {
2249 if (ob->type == OB_LAMP) {
2252 for (b = 0; b < MAX_MTEX; b++)
2253 if (la->mtex[b] && la->mtex[b]->tex)
2254 id_make_local(bmain, &la->mtex[b]->tex->id, false, false);
2257 for (a = 0; a < ob->totcol; a++) {
2260 make_local_makelocalmaterial(ma);
2263 matarar = (Material ***)give_matarar(ob);
2265 for (a = 0; a < ob->totcol; a++) {
2268 make_local_makelocalmaterial(ma);
2276 BKE_main_id_clear_newpoins(bmain);
2277 WM_event_add_notifier(C, NC_WINDOW, NULL);
2279 return OPERATOR_FINISHED;
2282 void OBJECT_OT_make_local(wmOperatorType *ot)
2284 static EnumPropertyItem type_items[] = {
2285 {MAKE_LOCAL_SELECT_OB, "SELECT_OBJECT", 0, "Selected Objects", ""},
2286 {MAKE_LOCAL_SELECT_OBDATA, "SELECT_OBDATA", 0, "Selected Objects and Data", ""},
2287 {MAKE_LOCAL_SELECT_OBDATA_MATERIAL, "SELECT_OBDATA_MATERIAL", 0, "Selected Objects, Data and Materials", ""},
2288 {MAKE_LOCAL_ALL, "ALL", 0, "All", ""},
2289 {0, NULL, 0, NULL, NULL}
2293 ot->name = "Make Local";
2294 ot->description = "Make library linked data-blocks local to this file";
2295 ot->idname = "OBJECT_OT_make_local";
2298 ot->invoke = WM_menu_invoke;
2299 ot->exec = make_local_exec;
2300 ot->poll = ED_operator_objectmode;
2303 ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
2306 ot->prop = RNA_def_enum(ot->srna, "type", type_items, 0, "Type", "");
2310 MAKE_SINGLE_USER_ALL = 1,
2311 MAKE_SINGLE_USER_SELECTED = 2,
2314 static int make_single_user_exec(bContext *C, wmOperator *op)
2316 Main *bmain = CTX_data_main(C);
2317 Scene *scene = CTX_data_scene(C);
2318 SceneLayer *sl = CTX_data_scene_layer(C);
2319 View3D *v3d = CTX_wm_view3d(C); /* ok if this is NULL */
2320 const int flag = (RNA_enum_get(op->ptr, "type") == MAKE_SINGLE_USER_SELECTED) ? SELECT : 0;
2321 const bool copy_groups = false;
2322 bool update_deps = false;
2324 if (RNA_boolean_get(op->ptr, "object")) {
2325 if (flag == SELECT) {
2326 BKE_scene_layer_selected_objects_tag(sl, OB_DONE);
2327 single_object_users(bmain, scene, v3d, OB_DONE, copy_groups);
2330 single_object_users(bmain, scene, v3d, 0, copy_groups);
2333 /* needed since object relationships may have changed */
2337 if (RNA_boolean_get(op->ptr, "obdata")) {
2338 single_obdata_users(bmain, scene, sl, flag);
2341 if (RNA_boolean_get(op->ptr, "material")) {
2342 single_mat_users(bmain, scene, sl, flag, RNA_boolean_get(op->ptr, "texture"));
2345 #if 0 /* can't do this separate from materials */
2346 if (RNA_boolean_get(op->ptr, "texture"))
2347 single_mat_users(scene, flag, true);
2349 if (RNA_boolean_get(op->ptr, "animation")) {
2350 single_object_action_users(scene, sl, flag);
2353 BKE_main_id_clear_newpoins(bmain);
2355 WM_event_add_notifier(C, NC_WINDOW, NULL);
2358 DAG_relations_tag_update(bmain);
2361 return OPERATOR_FINISHED;
2364 void OBJECT_OT_make_single_user(wmOperatorType *ot)
2366 static EnumPropertyItem type_items[] = {
2367 {MAKE_SINGLE_USER_SELECTED, "SELECTED_OBJECTS", 0, "Selected Objects", ""},
2368 {MAKE_SINGLE_USER_ALL, "ALL", 0, "All", ""},
2369 {0, NULL, 0, NULL, NULL}};
2372 ot->name = "Make Single User";
2373 ot->description = "Make linked data local to each object";
2374 ot->idname = "OBJECT_OT_make_single_user";
2377 ot->invoke = WM_menu_invoke;
2378 ot->exec = make_single_user_exec;
2379 ot->poll = ED_operator_objectmode;
2382 ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
2385 ot->prop = RNA_def_enum(ot->srna, "type", type_items, MAKE_SINGLE_USER_SELECTED, "Type", "");
2387 RNA_def_boolean(ot->srna, "object", 0, "Object", "Make single user objects");
2388 RNA_def_boolean(ot->srna, "obdata", 0, "Object Data", "Make single user object data");
2389 RNA_def_boolean(ot->srna, "material", 0, "Materials", "Make materials local to each data-block");
2390 RNA_def_boolean(ot->srna, "texture", 0, "Textures",
2391 "Make textures local to each material (needs 'Materials' to be set too)");
2392 RNA_def_boolean(ot->srna, "animation", 0, "Object Animation", "Make animation data local to each object");
2395 static int drop_named_material_invoke(bContext *C, wmOperator *op, const wmEvent *event)
2397 BaseLegacy *base = ED_view3d_give_base_under_cursor(C, event->mval);
2399 char name[MAX_ID_NAME - 2];
2401 RNA_string_get(op->ptr, "name", name);
2402 ma = (Material *)BKE_libblock_find_name(ID_MA, name);
2403 if (base == NULL || ma == NULL)
2404 return OPERATOR_CANCELLED;
2406 assign_material(base->object, ma, 1, BKE_MAT_ASSIGN_USERPREF);
2408 DAG_id_tag_update(&base->object->id, OB_RECALC_OB);
2410 WM_event_add_notifier(C, NC_OBJECT | ND_OB_SHADING, base->object);
2411 WM_event_add_notifier(C, NC_SPACE | ND_SPACE_VIEW3D, CTX_wm_view3d(C));
2412 WM_event_add_notifier(C, NC_MATERIAL | ND_SHADING_LINKS, ma);
2414 return OPERATOR_FINISHED;
2417 /* used for dropbox */
2418 /* assigns to object under cursor, only first material slot */
2419 void OBJECT_OT_drop_named_material(wmOperatorType *ot)
2422 ot->name = "Drop Named Material on Object";
2423 ot->description = "";
2424 ot->idname = "OBJECT_OT_drop_named_material";
2427 ot->invoke = drop_named_material_invoke;
2428 ot->poll = ED_operator_objectmode;
2431 ot->flag = OPTYPE_UNDO | OPTYPE_INTERNAL;
2434 RNA_def_string(ot->srna, "name", "Material", MAX_ID_NAME - 2, "Name", "Material name to assign");
2437 static int object_unlink_data_exec(bContext *C, wmOperator *op)
2440 PropertyPointerRNA pprop;
2442 UI_context_active_but_prop_get_templateID(C, &pprop.ptr, &pprop.prop);
2444 if (pprop.prop == NULL) {
2445 BKE_report(op->reports, RPT_ERROR, "Incorrect context for running object data unlink");
2446 return OPERATOR_CANCELLED;
2449 id = pprop.ptr.id.data;
2451 if (GS(id->name) == ID_OB) {
2452 Object *ob = (Object *)id;
2454 ID *id_data = ob->data;
2456 if (GS(id_data->name) == ID_IM) {
2461 BKE_report(op->reports, RPT_ERROR, "Can't unlink this object data");
2462 return OPERATOR_CANCELLED;
2467 RNA_property_update(C, &pprop.ptr, pprop.prop);
2469 return OPERATOR_FINISHED;
2473 * \note Only for empty-image objects, this operator is needed
2475 void OBJECT_OT_unlink_data(wmOperatorType *ot)
2478 ot->name = "Unlink";
2479 ot->idname = "OBJECT_OT_unlink_data";
2480 ot->description = "";
2483 ot->exec = object_unlink_data_exec;
2486 ot->flag = OPTYPE_INTERNAL;