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_scene_types.h"
47 #include "DNA_world_types.h"
48 #include "DNA_object_types.h"
49 #include "DNA_vfont_types.h"
50 #include "DNA_gpencil_types.h"
53 #include "BLI_listbase.h"
54 #include "BLI_linklist.h"
55 #include "BLI_string.h"
56 #include "BLI_kdtree.h"
57 #include "BLI_utildefines.h"
59 #include "BLT_translation.h"
61 #include "BKE_action.h"
62 #include "BKE_animsys.h"
63 #include "BKE_armature.h"
64 #include "BKE_camera.h"
65 #include "BKE_context.h"
66 #include "BKE_constraint.h"
67 #include "BKE_curve.h"
68 #include "BKE_depsgraph.h"
69 #include "BKE_DerivedMesh.h"
70 #include "BKE_displist.h"
71 #include "BKE_global.h"
72 #include "BKE_group.h"
73 #include "BKE_fcurve.h"
75 #include "BKE_lattice.h"
76 #include "BKE_library.h"
77 #include "BKE_library_query.h"
79 #include "BKE_material.h"
80 #include "BKE_mball.h"
82 #include "BKE_modifier.h"
83 #include "BKE_object.h"
84 #include "BKE_report.h"
86 #include "BKE_scene.h"
87 #include "BKE_speaker.h"
88 #include "BKE_texture.h"
89 #include "BKE_editmesh.h"
94 #include "UI_interface.h"
95 #include "UI_resources.h"
97 #include "RNA_access.h"
98 #include "RNA_define.h"
99 #include "RNA_enum_types.h"
101 #include "ED_armature.h"
102 #include "ED_curve.h"
103 #include "ED_keyframing.h"
104 #include "ED_object.h"
106 #include "ED_screen.h"
107 #include "ED_view3d.h"
109 #include "object_intern.h"
111 /*********************** Make Vertex Parent Operator ************************/
113 static int vertex_parent_set_poll(bContext *C)
115 return ED_operator_editmesh(C) || ED_operator_editsurfcurve(C) || ED_operator_editlattice(C);
118 static int vertex_parent_set_exec(bContext *C, wmOperator *op)
120 Main *bmain = CTX_data_main(C);
121 Scene *scene = CTX_data_scene(C);
122 Object *obedit = CTX_data_edit_object(C);
130 int a, v1 = 0, v2 = 0, v3 = 0, v4 = 0, nr = 1;
132 /* we need 1 to 3 selected vertices */
134 if (obedit->type == OB_MESH) {
135 Mesh *me = obedit->data;
138 EDBM_mesh_load(obedit);
139 EDBM_mesh_make(scene->toolsettings, obedit, true);
141 DAG_id_tag_update(obedit->data, 0);
143 em = me->edit_btmesh;
145 EDBM_mesh_normals_update(em);
146 BKE_editmesh_tessface_calc(em);
148 /* derivedMesh might be needed for solving parenting,
149 * so re-create it here */
150 makeDerivedMesh(scene, obedit, em, CD_MASK_BAREMESH | CD_MASK_ORIGINDEX, false);
152 BM_ITER_MESH (eve, &iter, em->bm, BM_VERTS_OF_MESH) {
153 if (BM_elem_flag_test(eve, BM_ELEM_SELECT)) {
154 if (v1 == 0) v1 = nr;
155 else if (v2 == 0) v2 = nr;
156 else if (v3 == 0) v3 = nr;
157 else if (v4 == 0) v4 = nr;
163 else if (ELEM(obedit->type, OB_SURF, OB_CURVE)) {
164 ListBase *editnurb = object_editcurve_get(obedit);
168 nu = editnurb->first;
170 if (nu->type == CU_BEZIER) {
174 if (BEZT_ISSEL_ANY_HIDDENHANDLES(cu, bezt)) {
175 if (v1 == 0) v1 = nr;
176 else if (v2 == 0) v2 = nr;
177 else if (v3 == 0) v3 = nr;
178 else if (v4 == 0) v4 = nr;
187 a = nu->pntsu * nu->pntsv;
189 if (bp->f1 & SELECT) {
190 if (v1 == 0) v1 = nr;
191 else if (v2 == 0) v2 = nr;
192 else if (v3 == 0) v3 = nr;
193 else if (v4 == 0) v4 = nr;
203 else if (obedit->type == OB_LATTICE) {
204 Lattice *lt = obedit->data;
206 a = lt->editlatt->latt->pntsu * lt->editlatt->latt->pntsv * lt->editlatt->latt->pntsw;
207 bp = lt->editlatt->latt->def;
209 if (bp->f1 & SELECT) {
210 if (v1 == 0) v1 = nr;
211 else if (v2 == 0) v2 = nr;
212 else if (v3 == 0) v3 = nr;
213 else if (v4 == 0) v4 = nr;
221 if (v4 || !((v1 && v2 == 0 && v3 == 0) || (v1 && v2 && v3))) {
222 BKE_report(op->reports, RPT_ERROR, "Select either 1 or 3 vertices to parent to");
223 return OPERATOR_CANCELLED;
226 CTX_DATA_BEGIN (C, Object *, ob, selected_editable_objects)
229 DAG_id_tag_update(&ob->id, OB_RECALC_OB | OB_RECALC_DATA | OB_RECALC_TIME);
230 par = obedit->parent;
232 if (BKE_object_parent_loop_check(par, ob)) {
233 BKE_report(op->reports, RPT_ERROR, "Loop in parents");
238 ob->parent = BASACT->object;
240 ob->partype = PARVERT3;
245 /* inverse parent matrix */
246 BKE_object_workob_calc_parent(scene, ob, &workob);
247 invert_m4_m4(ob->parentinv, workob.obmat);
250 ob->partype = PARVERT1;
253 /* inverse parent matrix */
254 BKE_object_workob_calc_parent(scene, ob, &workob);
255 invert_m4_m4(ob->parentinv, workob.obmat);
262 DAG_relations_tag_update(bmain);
264 WM_event_add_notifier(C, NC_OBJECT, NULL);
266 return OPERATOR_FINISHED;
269 void OBJECT_OT_vertex_parent_set(wmOperatorType *ot)
272 ot->name = "Make Vertex Parent";
273 ot->description = "Parent selected objects to the selected vertices";
274 ot->idname = "OBJECT_OT_vertex_parent_set";
277 ot->invoke = WM_operator_confirm;
278 ot->poll = vertex_parent_set_poll;
279 ot->exec = vertex_parent_set_exec;
282 ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
285 /********************** Make Proxy Operator *************************/
287 /* set the object to proxify */
288 static int make_proxy_invoke(bContext *C, wmOperator *op, const wmEvent *event)
290 Scene *scene = CTX_data_scene(C);
291 Object *ob = ED_object_active_context(C);
294 if (!scene || ID_IS_LINKED_DATABLOCK(scene) || !ob)
295 return OPERATOR_CANCELLED;
297 /* Get object to work on - use a menu if we need to... */
298 if (ob->dup_group && ID_IS_LINKED_DATABLOCK(ob->dup_group)) {
299 /* gives menu with list of objects in group */
300 /* proxy_group_objects_menu(C, op, ob, ob->dup_group); */
301 WM_enum_search_invoke(C, op, event);
302 return OPERATOR_CANCELLED;
304 else if (ID_IS_LINKED_DATABLOCK(ob)) {
305 uiPopupMenu *pup = UI_popup_menu_begin(C, IFACE_("OK?"), ICON_QUESTION);
306 uiLayout *layout = UI_popup_menu_layout(pup);
308 /* create operator menu item with relevant properties filled in */
309 uiItemFullO_ptr(layout, op->type, op->type->name, ICON_NONE, NULL,
310 WM_OP_EXEC_REGION_WIN, UI_ITEM_O_RETURN_PROPS);
312 /* present the menu and be done... */
313 UI_popup_menu_end(C, pup);
315 /* this invoke just calls another instance of this operator... */
316 return OPERATOR_INTERFACE;
319 /* error.. cannot continue */
320 BKE_report(op->reports, RPT_ERROR, "Can only make proxy for a referenced object or group");
321 return OPERATOR_CANCELLED;
326 static int make_proxy_exec(bContext *C, wmOperator *op)
328 Main *bmain = CTX_data_main(C);
329 Object *ob, *gob = ED_object_active_context(C);
331 Scene *scene = CTX_data_scene(C);
333 if (gob->dup_group != NULL) {
334 go = BLI_findlink(&gob->dup_group->gobject, RNA_enum_get(op->ptr, "object"));
344 Base *newbase, *oldbase = BASACT;
345 char name[MAX_ID_NAME + 4];
347 BLI_snprintf(name, sizeof(name), "%s_proxy", ((ID *)(gob ? gob : ob))->name + 2);
349 /* Add new object for the proxy */
350 newob = BKE_object_add(bmain, scene, OB_EMPTY, name);
353 newbase = BASACT; /* BKE_object_add sets active... */
354 newbase->lay = oldbase->lay;
355 newob->lay = newbase->lay;
357 /* remove base, leave user count of object, it gets linked in BKE_object_make_proxy */
359 BKE_scene_base_unlink(scene, oldbase);
363 BKE_object_make_proxy(newob, ob, gob);
365 /* depsgraph flushes are needed for the new data */
366 DAG_relations_tag_update(bmain);
367 DAG_id_tag_update(&newob->id, OB_RECALC_OB | OB_RECALC_DATA | OB_RECALC_TIME);
368 WM_event_add_notifier(C, NC_OBJECT | ND_DRAW, newob);
371 BKE_report(op->reports, RPT_ERROR, "No object to make proxy for");
372 return OPERATOR_CANCELLED;
375 return OPERATOR_FINISHED;
378 /* Generic itemf's for operators that take library args */
379 static EnumPropertyItem *proxy_group_object_itemf(bContext *C, PointerRNA *UNUSED(ptr),
380 PropertyRNA *UNUSED(prop), bool *r_free)
382 EnumPropertyItem item_tmp = {0}, *item = NULL;
385 Object *ob = ED_object_active_context(C);
388 if (!ob || !ob->dup_group)
389 return DummyRNA_DEFAULT_items;
391 /* find the object to affect */
392 for (go = ob->dup_group->gobject.first; go; go = go->next) {
393 item_tmp.identifier = item_tmp.name = go->ob->id.name + 2;
394 item_tmp.value = i++;
395 RNA_enum_item_add(&item, &totitem, &item_tmp);
398 RNA_enum_item_end(&item, &totitem);
404 void OBJECT_OT_proxy_make(wmOperatorType *ot)
409 ot->name = "Make Proxy";
410 ot->idname = "OBJECT_OT_proxy_make";
411 ot->description = "Add empty object to become local replacement data of a library-linked object";
414 ot->invoke = make_proxy_invoke;
415 ot->exec = make_proxy_exec;
416 ot->poll = ED_operator_object_active;
419 ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
422 /* XXX, relies on hard coded ID at the moment */
423 prop = RNA_def_enum(ot->srna, "object", DummyRNA_DEFAULT_items, 0, "Proxy Object",
424 "Name of lib-linked/grouped object to make a proxy for");
425 RNA_def_enum_funcs(prop, proxy_group_object_itemf);
426 RNA_def_property_flag(prop, PROP_ENUM_NO_TRANSLATE);
430 /********************** Clear Parent Operator ******************* */
432 typedef enum eObClearParentTypes {
433 CLEAR_PARENT_ALL = 0,
434 CLEAR_PARENT_KEEP_TRANSFORM,
436 } eObClearParentTypes;
438 EnumPropertyItem prop_clear_parent_types[] = {
439 {CLEAR_PARENT_ALL, "CLEAR", 0, "Clear Parent",
440 "Completely clear the parenting relationship, including involved modifiers if any"},
441 {CLEAR_PARENT_KEEP_TRANSFORM, "CLEAR_KEEP_TRANSFORM", 0, "Clear and Keep Transformation",
442 "As 'Clear Parent', but keep the current visual transformations of the object"},
443 {CLEAR_PARENT_INVERSE, "CLEAR_INVERSE", 0, "Clear Parent Inverse",
444 "Reset the transform corrections applied to the parenting relationship, does not remove parenting itself"},
445 {0, NULL, 0, NULL, NULL}
448 /* Helper for ED_object_parent_clear() - Remove deform-modifiers associated with parent */
449 static void object_remove_parent_deform_modifiers(Object *ob, const Object *par)
451 if (ELEM(par->type, OB_ARMATURE, OB_LATTICE, OB_CURVE)) {
452 ModifierData *md, *mdn;
454 /* assume that we only need to remove the first instance of matching deform modifier here */
455 for (md = ob->modifiers.first; md; md = mdn) {
460 /* need to match types (modifier + parent) and references */
461 if ((md->type == eModifierType_Armature) && (par->type == OB_ARMATURE)) {
462 ArmatureModifierData *amd = (ArmatureModifierData *)md;
463 if (amd->object == par) {
467 else if ((md->type == eModifierType_Lattice) && (par->type == OB_LATTICE)) {
468 LatticeModifierData *lmd = (LatticeModifierData *)md;
469 if (lmd->object == par) {
473 else if ((md->type == eModifierType_Curve) && (par->type == OB_CURVE)) {
474 CurveModifierData *cmd = (CurveModifierData *)md;
475 if (cmd->object == par) {
480 /* free modifier if match */
482 BLI_remlink(&ob->modifiers, md);
489 void ED_object_parent_clear(Object *ob, const int type)
491 if (ob->parent == NULL)
495 case CLEAR_PARENT_ALL:
497 /* for deformers, remove corresponding modifiers to prevent a large number of modifiers building up */
498 object_remove_parent_deform_modifiers(ob, ob->parent);
500 /* clear parenting relationship completely */
504 case CLEAR_PARENT_KEEP_TRANSFORM:
506 /* remove parent, and apply the parented transform result as object's local transforms */
508 BKE_object_apply_mat4(ob, ob->obmat, true, false);
511 case CLEAR_PARENT_INVERSE:
513 /* object stays parented, but the parent inverse (i.e. offset from parent to retain binding state)
514 * is cleared. In other words: nothing to do here! */
519 /* Always clear parentinv matrix for sake of consistency, see T41950. */
520 unit_m4(ob->parentinv);
522 DAG_id_tag_update(&ob->id, OB_RECALC_OB | OB_RECALC_DATA | OB_RECALC_TIME);
525 /* note, poll should check for editable scene */
526 static int parent_clear_exec(bContext *C, wmOperator *op)
528 Main *bmain = CTX_data_main(C);
529 const int type = RNA_enum_get(op->ptr, "type");
531 CTX_DATA_BEGIN (C, Object *, ob, selected_editable_objects)
533 ED_object_parent_clear(ob, type);
537 DAG_relations_tag_update(bmain);
538 WM_event_add_notifier(C, NC_OBJECT | ND_TRANSFORM, NULL);
539 WM_event_add_notifier(C, NC_OBJECT | ND_PARENT, NULL);
540 return OPERATOR_FINISHED;
543 void OBJECT_OT_parent_clear(wmOperatorType *ot)
546 ot->name = "Clear Parent";
547 ot->description = "Clear the object's parenting";
548 ot->idname = "OBJECT_OT_parent_clear";
551 ot->invoke = WM_menu_invoke;
552 ot->exec = parent_clear_exec;
554 ot->poll = ED_operator_object_active_editable;
557 ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
559 ot->prop = RNA_def_enum(ot->srna, "type", prop_clear_parent_types, CLEAR_PARENT_ALL, "Type", "");
562 /* ******************** Make Parent Operator *********************** */
564 void ED_object_parent(Object *ob, Object *par, const int type, const char *substr)
566 /* Always clear parentinv matrix for sake of consistency, see T41950. */
567 unit_m4(ob->parentinv);
569 if (!par || BKE_object_parent_loop_check(par, ob)) {
571 ob->partype = PAROBJECT;
572 ob->parsubstr[0] = 0;
576 /* Other partypes are deprecated, do not use here! */
577 BLI_assert(ELEM(type & PARTYPE, PAROBJECT, PARSKEL, PARVERT1, PARVERT3, PARBONE));
579 /* this could use some more checks */
582 ob->partype &= ~PARTYPE;
584 BLI_strncpy(ob->parsubstr, substr, sizeof(ob->parsubstr));
587 /* Operator Property */
588 EnumPropertyItem prop_make_parent_types[] = {
589 {PAR_OBJECT, "OBJECT", 0, "Object", ""},
590 {PAR_ARMATURE, "ARMATURE", 0, "Armature Deform", ""},
591 {PAR_ARMATURE_NAME, "ARMATURE_NAME", 0, " With Empty Groups", ""},
592 {PAR_ARMATURE_AUTO, "ARMATURE_AUTO", 0, " With Automatic Weights", ""},
593 {PAR_ARMATURE_ENVELOPE, "ARMATURE_ENVELOPE", 0, " With Envelope Weights", ""},
594 {PAR_BONE, "BONE", 0, "Bone", ""},
595 {PAR_BONE_RELATIVE, "BONE_RELATIVE", 0, "Bone Relative", ""},
596 {PAR_CURVE, "CURVE", 0, "Curve Deform", ""},
597 {PAR_FOLLOW, "FOLLOW", 0, "Follow Path", ""},
598 {PAR_PATH_CONST, "PATH_CONST", 0, "Path Constraint", ""},
599 {PAR_LATTICE, "LATTICE", 0, "Lattice Deform", ""},
600 {PAR_VERTEX, "VERTEX", 0, "Vertex", ""},
601 {PAR_VERTEX_TRI, "VERTEX_TRI", 0, "Vertex (Triangle)", ""},
602 {0, NULL, 0, NULL, NULL}
605 bool ED_object_parent_set(ReportList *reports, Main *bmain, Scene *scene, Object *ob, Object *par,
606 int partype, const bool xmirror, const bool keep_transform, const int vert_par[3])
608 bPoseChannel *pchan = NULL;
609 const bool pararm = ELEM(partype, PAR_ARMATURE, PAR_ARMATURE_NAME, PAR_ARMATURE_ENVELOPE, PAR_ARMATURE_AUTO);
611 DAG_id_tag_update(&par->id, OB_RECALC_OB);
614 if (partype == PAR_FOLLOW || partype == PAR_PATH_CONST) {
615 if (par->type != OB_CURVE)
618 Curve *cu = par->data;
620 if ((cu->flag & CU_PATH) == 0) {
621 cu->flag |= CU_PATH | CU_FOLLOW;
622 BKE_displist_make_curveTypes(scene, par, 0); /* force creation of path data */
625 cu->flag |= CU_FOLLOW;
628 /* if follow, add F-Curve for ctime (i.e. "eval_time") so that path-follow works */
629 if (partype == PAR_FOLLOW) {
630 /* get or create F-Curve */
631 bAction *act = verify_adt_action(&cu->id, 1);
632 FCurve *fcu = verify_fcurve(act, NULL, NULL, "eval_time", 0, 1);
634 /* setup dummy 'generator' modifier here to get 1-1 correspondence still working */
635 if (!fcu->bezt && !fcu->fpt && !fcu->modifiers.first)
636 add_fmodifier(&fcu->modifiers, FMODIFIER_TYPE_GENERATOR);
639 /* fall back on regular parenting now (for follow only) */
640 if (partype == PAR_FOLLOW)
641 partype = PAR_OBJECT;
644 else if (ELEM(partype, PAR_BONE, PAR_BONE_RELATIVE)) {
645 pchan = BKE_pose_channel_active(par);
648 BKE_report(reports, RPT_ERROR, "No active bone");
654 if (BKE_object_parent_loop_check(par, ob)) {
655 BKE_report(reports, RPT_ERROR, "Loop in parents");
661 /* apply transformation of previous parenting */
662 if (keep_transform) {
663 /* was removed because of bug [#23577],
664 * but this can be handy in some cases too [#32616], so make optional */
665 BKE_object_apply_mat4(ob, ob->obmat, false, false);
668 /* set the parent (except for follow-path constraint option) */
669 if (partype != PAR_PATH_CONST) {
671 /* Always clear parentinv matrix for sake of consistency, see T41950. */
672 unit_m4(ob->parentinv);
677 BLI_strncpy(ob->parsubstr, pchan->name, sizeof(ob->parsubstr));
679 ob->parsubstr[0] = 0;
681 if (partype == PAR_PATH_CONST) {
682 /* don't do anything here, since this is not technically "parenting" */
684 else if (ELEM(partype, PAR_CURVE, PAR_LATTICE) || (pararm)) {
685 /* partype is now set to PAROBJECT so that invisible 'virtual' modifiers don't need to be created
686 * NOTE: the old (2.4x) method was to set ob->partype = PARSKEL, creating the virtual modifiers
688 ob->partype = PAROBJECT; /* note, dna define, not operator property */
689 /* ob->partype = PARSKEL; */ /* note, dna define, not operator property */
691 /* BUT, to keep the deforms, we need a modifier, and then we need to set the object that it uses
692 * - We need to ensure that the modifier we're adding doesn't already exist, so we check this by
693 * assuming that the parent is selected too...
695 /* XXX currently this should only happen for meshes, curves, surfaces,
696 * and lattices - this stuff isn't available for metas yet */
697 if (ELEM(ob->type, OB_MESH, OB_CURVE, OB_SURF, OB_FONT, OB_LATTICE)) {
701 case PAR_CURVE: /* curve deform */
702 if (modifiers_isDeformedByCurve(ob) != par) {
703 md = ED_object_modifier_add(reports, bmain, scene, ob, NULL, eModifierType_Curve);
705 ((CurveModifierData *)md)->object = par;
707 if (par->curve_cache && par->curve_cache->path == NULL) {
708 DAG_id_tag_update(&par->id, OB_RECALC_DATA);
712 case PAR_LATTICE: /* lattice deform */
713 if (modifiers_isDeformedByLattice(ob) != par) {
714 md = ED_object_modifier_add(reports, bmain, scene, ob, NULL, eModifierType_Lattice);
716 ((LatticeModifierData *)md)->object = par;
720 default: /* armature deform */
721 if (modifiers_isDeformedByArmature(ob) != par) {
722 md = ED_object_modifier_add(reports, bmain, scene, ob, NULL, eModifierType_Armature);
724 ((ArmatureModifierData *)md)->object = par;
731 else if (partype == PAR_BONE) {
732 ob->partype = PARBONE; /* note, dna define, not operator property */
734 pchan->bone->flag &= ~BONE_RELATIVE_PARENTING;
736 else if (partype == PAR_BONE_RELATIVE) {
737 ob->partype = PARBONE; /* note, dna define, not operator property */
739 pchan->bone->flag |= BONE_RELATIVE_PARENTING;
741 else if (partype == PAR_VERTEX) {
742 ob->partype = PARVERT1;
743 ob->par1 = vert_par[0];
745 else if (partype == PAR_VERTEX_TRI) {
746 ob->partype = PARVERT3;
747 copy_v3_v3_int(&ob->par1, vert_par);
750 ob->partype = PAROBJECT; /* note, dna define, not operator property */
754 if (partype == PAR_PATH_CONST) {
756 bFollowPathConstraint *data;
757 float cmat[4][4], vec[3];
759 con = BKE_constraint_add_for_object(ob, "AutoPath", CONSTRAINT_TYPE_FOLLOWPATH);
764 BKE_constraint_target_matrix_get(scene, con, 0, CONSTRAINT_OBTYPE_OBJECT, NULL, cmat, scene->r.cfra);
765 sub_v3_v3v3(vec, ob->obmat[3], cmat[3]);
767 copy_v3_v3(ob->loc, vec);
769 else if (pararm && (ob->type == OB_MESH) && (par->type == OB_ARMATURE)) {
770 if (partype == PAR_ARMATURE_NAME)
771 create_vgroups_from_armature(reports, scene, ob, par, ARM_GROUPS_NAME, false);
772 else if (partype == PAR_ARMATURE_ENVELOPE)
773 create_vgroups_from_armature(reports, scene, ob, par, ARM_GROUPS_ENVELOPE, xmirror);
774 else if (partype == PAR_ARMATURE_AUTO) {
776 create_vgroups_from_armature(reports, scene, ob, par, ARM_GROUPS_AUTO, xmirror);
779 /* get corrected inverse */
780 ob->partype = PAROBJECT;
781 BKE_object_workob_calc_parent(scene, ob, &workob);
783 invert_m4_m4(ob->parentinv, workob.obmat);
786 /* calculate inverse parent matrix */
787 BKE_object_workob_calc_parent(scene, ob, &workob);
788 invert_m4_m4(ob->parentinv, workob.obmat);
791 DAG_id_tag_update(&ob->id, OB_RECALC_OB | OB_RECALC_DATA);
800 static void parent_set_vert_find(KDTree *tree, Object *child, int vert_par[3], bool is_tri)
802 const float *co_find = child->obmat[3];
804 KDTreeNearest nearest[3];
807 tot = BLI_kdtree_find_nearest_n(tree, co_find, nearest, 3);
808 BLI_assert(tot == 3);
811 vert_par[0] = nearest[0].index;
812 vert_par[1] = nearest[1].index;
813 vert_par[2] = nearest[2].index;
815 BLI_assert(min_iii(UNPACK3(vert_par)) >= 0);
818 vert_par[0] = BLI_kdtree_find_nearest(tree, co_find, NULL);
819 BLI_assert(vert_par[0] >= 0);
825 static int parent_set_exec(bContext *C, wmOperator *op)
827 Main *bmain = CTX_data_main(C);
828 Scene *scene = CTX_data_scene(C);
829 Object *par = ED_object_active_context(C);
830 int partype = RNA_enum_get(op->ptr, "type");
831 const bool xmirror = RNA_boolean_get(op->ptr, "xmirror");
832 const bool keep_transform = RNA_boolean_get(op->ptr, "keep_transform");
835 /* vertex parent (kdtree) */
836 const bool is_vert_par = ELEM(partype, PAR_VERTEX, PAR_VERTEX_TRI);
837 const bool is_tri = partype == PAR_VERTEX_TRI;
839 struct KDTree *tree = NULL;
840 int vert_par[3] = {0, 0, 0};
841 const int *vert_par_p = is_vert_par ? vert_par : NULL;
845 tree = BKE_object_as_kdtree(par, &tree_tot);
846 BLI_assert(tree != NULL);
848 if (tree_tot < (is_tri ? 3 : 1)) {
849 BKE_report(op->reports, RPT_ERROR, "Not enough vertices for vertex-parent");
855 /* Non vertex-parent */
856 CTX_DATA_BEGIN (C, Object *, ob, selected_editable_objects)
859 parent_set_vert_find(tree, ob, vert_par, is_tri);
862 if (!ED_object_parent_set(op->reports, bmain, scene, ob, par, partype, xmirror, keep_transform, vert_par_p)) {
871 BLI_kdtree_free(tree);
875 return OPERATOR_CANCELLED;
877 DAG_relations_tag_update(bmain);
878 WM_event_add_notifier(C, NC_OBJECT | ND_TRANSFORM, NULL);
879 WM_event_add_notifier(C, NC_OBJECT | ND_PARENT, NULL);
881 return OPERATOR_FINISHED;
885 static int parent_set_invoke(bContext *C, wmOperator *UNUSED(op), const wmEvent *UNUSED(event))
887 Object *ob = ED_object_active_context(C);
888 uiPopupMenu *pup = UI_popup_menu_begin(C, IFACE_("Set Parent To"), ICON_NONE);
889 uiLayout *layout = UI_popup_menu_layout(pup);
891 wmOperatorType *ot = WM_operatortype_find("OBJECT_OT_parent_set", true);
895 uiItemEnumO_ptr(layout, ot, NULL, 0, "type", PAR_OBJECT);
897 opptr = uiItemFullO_ptr(layout, ot, IFACE_("Object"), ICON_NONE, NULL, WM_OP_EXEC_DEFAULT, UI_ITEM_O_RETURN_PROPS);
898 RNA_enum_set(&opptr, "type", PAR_OBJECT);
899 RNA_boolean_set(&opptr, "keep_transform", false);
901 opptr = uiItemFullO_ptr(layout, ot, IFACE_("Object (Keep Transform)"), ICON_NONE, NULL, WM_OP_EXEC_DEFAULT,
902 UI_ITEM_O_RETURN_PROPS);
903 RNA_enum_set(&opptr, "type", PAR_OBJECT);
904 RNA_boolean_set(&opptr, "keep_transform", true);
906 /* ob becomes parent, make the associated menus */
907 if (ob->type == OB_ARMATURE) {
908 uiItemEnumO_ptr(layout, ot, NULL, 0, "type", PAR_ARMATURE);
909 uiItemEnumO_ptr(layout, ot, NULL, 0, "type", PAR_ARMATURE_NAME);
910 uiItemEnumO_ptr(layout, ot, NULL, 0, "type", PAR_ARMATURE_ENVELOPE);
911 uiItemEnumO_ptr(layout, ot, NULL, 0, "type", PAR_ARMATURE_AUTO);
912 uiItemEnumO_ptr(layout, ot, NULL, 0, "type", PAR_BONE);
913 uiItemEnumO_ptr(layout, ot, NULL, 0, "type", PAR_BONE_RELATIVE);
915 else if (ob->type == OB_CURVE) {
916 uiItemEnumO_ptr(layout, ot, NULL, 0, "type", PAR_CURVE);
917 uiItemEnumO_ptr(layout, ot, NULL, 0, "type", PAR_FOLLOW);
918 uiItemEnumO_ptr(layout, ot, NULL, 0, "type", PAR_PATH_CONST);
920 else if (ob->type == OB_LATTICE) {
921 uiItemEnumO_ptr(layout, ot, NULL, 0, "type", PAR_LATTICE);
924 /* vertex parenting */
925 if (OB_TYPE_SUPPORT_PARVERT(ob->type)) {
926 uiItemEnumO_ptr(layout, ot, NULL, 0, "type", PAR_VERTEX);
927 uiItemEnumO_ptr(layout, ot, NULL, 0, "type", PAR_VERTEX_TRI);
930 UI_popup_menu_end(C, pup);
932 return OPERATOR_INTERFACE;
935 static bool parent_set_draw_check_prop(PointerRNA *ptr, PropertyRNA *prop)
937 const char *prop_id = RNA_property_identifier(prop);
938 const int type = RNA_enum_get(ptr, "type");
940 /* Only show XMirror for PAR_ARMATURE_ENVELOPE and PAR_ARMATURE_AUTO! */
941 if (STREQ(prop_id, "xmirror")) {
942 if (ELEM(type, PAR_ARMATURE_ENVELOPE, PAR_ARMATURE_AUTO))
951 static void parent_set_ui(bContext *C, wmOperator *op)
953 uiLayout *layout = op->layout;
954 wmWindowManager *wm = CTX_wm_manager(C);
957 RNA_pointer_create(&wm->id, op->type->srna, op->properties, &ptr);
959 /* Main auto-draw call. */
960 uiDefAutoButsRNA(layout, &ptr, parent_set_draw_check_prop, '\0');
963 void OBJECT_OT_parent_set(wmOperatorType *ot)
966 ot->name = "Make Parent";
967 ot->description = "Set the object's parenting";
968 ot->idname = "OBJECT_OT_parent_set";
971 ot->invoke = parent_set_invoke;
972 ot->exec = parent_set_exec;
973 ot->poll = ED_operator_object_active;
974 ot->ui = parent_set_ui;
977 ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
979 ot->prop = RNA_def_enum(ot->srna, "type", prop_make_parent_types, 0, "Type", "");
980 RNA_def_boolean(ot->srna, "xmirror", false, "X Mirror",
981 "Apply weights symmetrically along X axis, for Envelope/Automatic vertex groups creation");
982 RNA_def_boolean(ot->srna, "keep_transform", false, "Keep Transform",
983 "Apply transformation before parenting");
986 /* ************ Make Parent Without Inverse Operator ******************* */
988 static int parent_noinv_set_exec(bContext *C, wmOperator *op)
990 Main *bmain = CTX_data_main(C);
991 Object *par = ED_object_active_context(C);
993 DAG_id_tag_update(&par->id, OB_RECALC_OB);
995 /* context iterator */
996 CTX_DATA_BEGIN (C, Object *, ob, selected_editable_objects)
999 if (BKE_object_parent_loop_check(par, ob)) {
1000 BKE_report(op->reports, RPT_ERROR, "Loop in parents");
1003 /* clear inverse matrix and also the object location */
1004 unit_m4(ob->parentinv);
1005 memset(ob->loc, 0, 3 * sizeof(float));
1007 /* set recalc flags */
1008 DAG_id_tag_update(&ob->id, OB_RECALC_OB | OB_RECALC_DATA);
1010 /* set parenting type for object - object only... */
1012 ob->partype = PAROBJECT; /* note, dna define, not operator property */
1018 DAG_relations_tag_update(bmain);
1019 WM_event_add_notifier(C, NC_OBJECT | ND_TRANSFORM, NULL);
1021 return OPERATOR_FINISHED;
1024 void OBJECT_OT_parent_no_inverse_set(wmOperatorType *ot)
1027 ot->name = "Make Parent without Inverse";
1028 ot->description = "Set the object's parenting without setting the inverse parent correction";
1029 ot->idname = "OBJECT_OT_parent_no_inverse_set";
1032 ot->invoke = WM_operator_confirm;
1033 ot->exec = parent_noinv_set_exec;
1034 ot->poll = ED_operator_object_active_editable;
1037 ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
1040 /************************ Clear Slow Parent Operator *********************/
1042 static int object_slow_parent_clear_exec(bContext *C, wmOperator *UNUSED(op))
1044 Scene *scene = CTX_data_scene(C);
1046 CTX_DATA_BEGIN (C, Object *, ob, selected_editable_objects)
1049 if (ob->partype & PARSLOW) {
1050 ob->partype -= PARSLOW;
1051 BKE_object_where_is_calc(scene, ob);
1052 ob->partype |= PARSLOW;
1053 DAG_id_tag_update(&ob->id, OB_RECALC_OB);
1059 WM_event_add_notifier(C, NC_SCENE, scene);
1061 return OPERATOR_FINISHED;
1064 void OBJECT_OT_slow_parent_clear(wmOperatorType *ot)
1067 ot->name = "Clear Slow Parent";
1068 ot->description = "Clear the object's slow parent";
1069 ot->idname = "OBJECT_OT_slow_parent_clear";
1072 ot->invoke = WM_operator_confirm;
1073 ot->exec = object_slow_parent_clear_exec;
1074 ot->poll = ED_operator_view3d_active;
1077 ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
1080 /********************** Make Slow Parent Operator *********************/
1082 static int object_slow_parent_set_exec(bContext *C, wmOperator *UNUSED(op))
1084 Scene *scene = CTX_data_scene(C);
1086 CTX_DATA_BEGIN (C, Object *, ob, selected_editable_objects)
1089 ob->partype |= PARSLOW;
1091 DAG_id_tag_update(&ob->id, OB_RECALC_OB);
1095 WM_event_add_notifier(C, NC_SCENE, scene);
1097 return OPERATOR_FINISHED;
1100 void OBJECT_OT_slow_parent_set(wmOperatorType *ot)
1103 ot->name = "Set Slow Parent";
1104 ot->description = "Set the object's slow parent";
1105 ot->idname = "OBJECT_OT_slow_parent_set";
1108 ot->invoke = WM_operator_confirm;
1109 ot->exec = object_slow_parent_set_exec;
1110 ot->poll = ED_operator_view3d_active;
1113 ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
1116 /* ******************** Clear Track Operator ******************* */
1120 CLEAR_TRACK_KEEP_TRANSFORM = 2,
1123 static EnumPropertyItem prop_clear_track_types[] = {
1124 {CLEAR_TRACK, "CLEAR", 0, "Clear Track", ""},
1125 {CLEAR_TRACK_KEEP_TRANSFORM, "CLEAR_KEEP_TRANSFORM", 0, "Clear and Keep Transformation (Clear Track)", ""},
1126 {0, NULL, 0, NULL, NULL}
1129 /* note, poll should check for editable scene */
1130 static int object_track_clear_exec(bContext *C, wmOperator *op)
1132 Main *bmain = CTX_data_main(C);
1133 const int type = RNA_enum_get(op->ptr, "type");
1135 if (CTX_data_edit_object(C)) {
1136 BKE_report(op->reports, RPT_ERROR, "Operation cannot be performed in edit mode");
1137 return OPERATOR_CANCELLED;
1139 CTX_DATA_BEGIN (C, Object *, ob, selected_editable_objects)
1141 bConstraint *con, *pcon;
1143 /* remove track-object for old track */
1145 DAG_id_tag_update(&ob->id, OB_RECALC_OB | OB_RECALC_DATA | OB_RECALC_TIME);
1147 /* also remove all tracking constraints */
1148 for (con = ob->constraints.last; con; con = pcon) {
1150 if (ELEM(con->type, CONSTRAINT_TYPE_TRACKTO, CONSTRAINT_TYPE_LOCKTRACK, CONSTRAINT_TYPE_DAMPTRACK))
1151 BKE_constraint_remove(&ob->constraints, con);
1154 if (type == CLEAR_TRACK_KEEP_TRANSFORM)
1155 BKE_object_apply_mat4(ob, ob->obmat, true, true);
1159 DAG_relations_tag_update(bmain);
1160 WM_event_add_notifier(C, NC_OBJECT | ND_TRANSFORM, NULL);
1162 return OPERATOR_FINISHED;
1165 void OBJECT_OT_track_clear(wmOperatorType *ot)
1168 ot->name = "Clear Track";
1169 ot->description = "Clear tracking constraint or flag from object";
1170 ot->idname = "OBJECT_OT_track_clear";
1173 ot->invoke = WM_menu_invoke;
1174 ot->exec = object_track_clear_exec;
1176 ot->poll = ED_operator_objectmode;
1179 ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
1181 ot->prop = RNA_def_enum(ot->srna, "type", prop_clear_track_types, 0, "Type", "");
1184 /************************** Make Track Operator *****************************/
1187 CREATE_TRACK_DAMPTRACK = 1,
1188 CREATE_TRACK_TRACKTO = 2,
1189 CREATE_TRACK_LOCKTRACK = 3,
1192 static EnumPropertyItem prop_make_track_types[] = {
1193 {CREATE_TRACK_DAMPTRACK, "DAMPTRACK", 0, "Damped Track Constraint", ""},
1194 {CREATE_TRACK_TRACKTO, "TRACKTO", 0, "Track To Constraint", ""},
1195 {CREATE_TRACK_LOCKTRACK, "LOCKTRACK", 0, "Lock Track Constraint", ""},
1196 {0, NULL, 0, NULL, NULL}
1199 static int track_set_exec(bContext *C, wmOperator *op)
1201 Main *bmain = CTX_data_main(C);
1202 Object *obact = ED_object_active_context(C);
1204 const int type = RNA_enum_get(op->ptr, "type");
1207 case CREATE_TRACK_DAMPTRACK:
1210 bDampTrackConstraint *data;
1212 CTX_DATA_BEGIN (C, Object *, ob, selected_editable_objects)
1215 con = BKE_constraint_add_for_object(ob, "AutoTrack", CONSTRAINT_TYPE_DAMPTRACK);
1219 DAG_id_tag_update(&ob->id, OB_RECALC_OB | OB_RECALC_DATA | OB_RECALC_TIME);
1221 /* Lamp, Camera and Speaker track differently by default */
1222 if (ELEM(ob->type, OB_LAMP, OB_CAMERA, OB_SPEAKER)) {
1223 data->trackflag = TRACK_nZ;
1230 case CREATE_TRACK_TRACKTO:
1233 bTrackToConstraint *data;
1235 CTX_DATA_BEGIN (C, Object *, ob, selected_editable_objects)
1238 con = BKE_constraint_add_for_object(ob, "AutoTrack", CONSTRAINT_TYPE_TRACKTO);
1242 DAG_id_tag_update(&ob->id, OB_RECALC_OB | OB_RECALC_DATA | OB_RECALC_TIME);
1244 /* Lamp, Camera and Speaker track differently by default */
1245 if (ELEM(ob->type, OB_LAMP, OB_CAMERA, OB_SPEAKER)) {
1246 data->reserved1 = TRACK_nZ;
1247 data->reserved2 = UP_Y;
1254 case CREATE_TRACK_LOCKTRACK:
1257 bLockTrackConstraint *data;
1259 CTX_DATA_BEGIN (C, Object *, ob, selected_editable_objects)
1262 con = BKE_constraint_add_for_object(ob, "AutoTrack", CONSTRAINT_TYPE_LOCKTRACK);
1266 DAG_id_tag_update(&ob->id, OB_RECALC_OB | OB_RECALC_DATA | OB_RECALC_TIME);
1268 /* Lamp, Camera and Speaker track differently by default */
1269 if (ELEM(ob->type, OB_LAMP, OB_CAMERA, OB_SPEAKER)) {
1270 data->trackflag = TRACK_nZ;
1271 data->lockflag = LOCK_Y;
1280 DAG_relations_tag_update(bmain);
1281 WM_event_add_notifier(C, NC_OBJECT | ND_TRANSFORM, NULL);
1283 return OPERATOR_FINISHED;
1286 void OBJECT_OT_track_set(wmOperatorType *ot)
1289 ot->name = "Make Track";
1290 ot->description = "Make the object track another object, using various methods/constraints";
1291 ot->idname = "OBJECT_OT_track_set";
1294 ot->invoke = WM_menu_invoke;
1295 ot->exec = track_set_exec;
1297 ot->poll = ED_operator_objectmode;
1300 ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
1303 ot->prop = RNA_def_enum(ot->srna, "type", prop_make_track_types, 0, "Type", "");
1306 /************************** Move to Layer Operator *****************************/
1308 static unsigned int move_to_layer_init(bContext *C, wmOperator *op)
1311 unsigned int lay = 0;
1313 if (!RNA_struct_property_is_set(op->ptr, "layers")) {
1314 /* note: layers are set in bases, library objects work for this */
1315 CTX_DATA_BEGIN (C, Base *, base, selected_bases)
1321 for (a = 0; a < 20; a++)
1322 values[a] = (lay & (1 << a)) != 0;
1324 RNA_boolean_set_array(op->ptr, "layers", values);
1327 RNA_boolean_get_array(op->ptr, "layers", values);
1329 for (a = 0; a < 20; a++)
1337 static int move_to_layer_invoke(bContext *C, wmOperator *op, const wmEvent *event)
1339 View3D *v3d = CTX_wm_view3d(C);
1340 if (v3d && v3d->localvd) {
1341 return WM_operator_confirm_message(C, op, "Move out of Local View");
1344 move_to_layer_init(C, op);
1345 return WM_operator_props_popup(C, op, event);
1349 static int move_to_layer_exec(bContext *C, wmOperator *op)
1351 Main *bmain = CTX_data_main(C);
1352 Scene *scene = CTX_data_scene(C);
1353 View3D *v3d = CTX_wm_view3d(C);
1354 unsigned int lay, local;
1355 /* bool is_lamp = false; */ /* UNUSED */
1357 lay = move_to_layer_init(C, op);
1360 if (lay == 0) return OPERATOR_CANCELLED;
1362 if (v3d && v3d->localvd) {
1363 /* now we can move out of localview. */
1364 /* note: layers are set in bases, library objects work for this */
1365 CTX_DATA_BEGIN (C, Base *, base, selected_bases)
1367 lay = base->lay & ~v3d->lay;
1369 base->object->lay = lay;
1370 base->object->flag &= ~SELECT;
1371 base->flag &= ~SELECT;
1372 /* if (base->object->type == OB_LAMP) is_lamp = true; */
1377 /* normal non localview operation */
1378 /* note: layers are set in bases, library objects work for this */
1379 CTX_DATA_BEGIN (C, Base *, base, selected_bases)
1381 /* upper byte is used for local view */
1382 local = base->lay & 0xFF000000;
1383 base->lay = lay + local;
1384 base->object->lay = base->lay;
1385 /* if (base->object->type == OB_LAMP) is_lamp = true; */
1390 /* warning, active object may be hidden now */
1392 WM_event_add_notifier(C, NC_OBJECT | ND_DRAW, scene);
1393 WM_event_add_notifier(C, NC_SCENE | ND_LAYER_CONTENT, scene);
1395 DAG_relations_tag_update(bmain);
1397 return OPERATOR_FINISHED;
1400 void OBJECT_OT_move_to_layer(wmOperatorType *ot)
1403 ot->name = "Move to Layer";
1404 ot->description = "Move the object to different layers";
1405 ot->idname = "OBJECT_OT_move_to_layer";
1408 ot->invoke = move_to_layer_invoke;
1409 ot->exec = move_to_layer_exec;
1410 ot->poll = ED_operator_objectmode;
1413 ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
1416 RNA_def_boolean_layer_member(ot->srna, "layers", 20, NULL, "Layer", "");
1419 /************************** Link to Scene Operator *****************************/
1422 static void link_to_scene(Main *UNUSED(bmain), unsigned short UNUSED(nr))
1424 Scene *sce = (Scene *) BLI_findlink(&bmain->scene, G.curscreen->scenenr - 1);
1427 if (sce == NULL) return;
1428 if (sce->id.lib) return;
1430 for (base = FIRSTBASE; base; base = base->next) {
1431 if (TESTBASE(v3d, base)) {
1432 nbase = MEM_mallocN(sizeof(Base), "newbase");
1434 BLI_addhead(&(sce->base), nbase);
1435 id_us_plus((ID *)base->object);
1441 Base *ED_object_scene_link(Scene *scene, Object *ob)
1445 if (BKE_scene_base_find(scene, ob)) {
1449 base = BKE_scene_base_add(scene, ob);
1450 id_us_plus(&ob->id);
1455 static int make_links_scene_exec(bContext *C, wmOperator *op)
1457 Scene *scene_to = BLI_findlink(&CTX_data_main(C)->scene, RNA_enum_get(op->ptr, "scene"));
1459 if (scene_to == NULL) {
1460 BKE_report(op->reports, RPT_ERROR, "Could not find scene");
1461 return OPERATOR_CANCELLED;
1464 if (scene_to == CTX_data_scene(C)) {
1465 BKE_report(op->reports, RPT_ERROR, "Cannot link objects into the same scene");
1466 return OPERATOR_CANCELLED;
1469 if (ID_IS_LINKED_DATABLOCK(scene_to)) {
1470 BKE_report(op->reports, RPT_ERROR, "Cannot link objects into a linked scene");
1471 return OPERATOR_CANCELLED;
1474 CTX_DATA_BEGIN (C, Base *, base, selected_bases)
1476 ED_object_scene_link(scene_to, base->object);
1480 /* redraw the 3D view because the object center points are colored differently */
1481 WM_event_add_notifier(C, NC_OBJECT | ND_DRAW, NULL);
1483 /* one day multiple scenes will be visible, then we should have some update function for them */
1484 return OPERATOR_FINISHED;
1488 MAKE_LINKS_OBDATA = 1,
1489 MAKE_LINKS_MATERIALS = 2,
1490 MAKE_LINKS_ANIMDATA = 3,
1491 MAKE_LINKS_GROUP = 4,
1492 MAKE_LINKS_DUPLIGROUP = 5,
1493 MAKE_LINKS_MODIFIERS = 6,
1494 MAKE_LINKS_FONTS = 7,
1497 /* Return true if make link data is allowed, false otherwise */
1498 static bool allow_make_links_data(const int type, Object *ob_src, Object *ob_dst)
1501 case MAKE_LINKS_OBDATA:
1502 if (ob_src->type == ob_dst->type && ob_src->type != OB_EMPTY) {
1506 case MAKE_LINKS_MATERIALS:
1507 if (OB_TYPE_SUPPORT_MATERIAL(ob_src->type) && OB_TYPE_SUPPORT_MATERIAL(ob_dst->type)) {
1511 case MAKE_LINKS_ANIMDATA:
1512 case MAKE_LINKS_GROUP:
1513 case MAKE_LINKS_DUPLIGROUP:
1515 case MAKE_LINKS_MODIFIERS:
1516 if (!ELEM(OB_EMPTY, ob_src->type, ob_dst->type)) {
1520 case MAKE_LINKS_FONTS:
1521 if ((ob_src->data != ob_dst->data) && (ob_src->type == OB_FONT) && (ob_dst->type == OB_FONT)) {
1529 static int make_links_data_exec(bContext *C, wmOperator *op)
1531 Main *bmain = CTX_data_main(C);
1532 Scene *scene = CTX_data_scene(C);
1533 const int type = RNA_enum_get(op->ptr, "type");
1539 LinkNode *ob_groups = NULL;
1540 bool is_cycle = false;
1541 bool is_lib = false;
1543 ob_src = ED_object_active_context(C);
1545 /* avoid searching all groups in source object each time */
1546 if (type == MAKE_LINKS_GROUP) {
1547 ob_groups = BKE_object_groups(ob_src);
1550 CTX_DATA_BEGIN (C, Base *, base_dst, selected_editable_bases)
1552 Object *ob_dst = base_dst->object;
1554 if (ob_src != ob_dst) {
1555 if (allow_make_links_data(type, ob_src, ob_dst)) {
1556 obdata_id = ob_dst->data;
1559 case MAKE_LINKS_OBDATA: /* obdata */
1560 id_us_min(obdata_id);
1562 obdata_id = ob_src->data;
1563 id_us_plus(obdata_id);
1564 ob_dst->data = obdata_id;
1566 /* if amount of material indices changed: */
1567 test_object_materials(ob_dst, ob_dst->data);
1569 DAG_id_tag_update(&ob_dst->id, OB_RECALC_DATA);
1571 case MAKE_LINKS_MATERIALS:
1572 /* new approach, using functions from kernel */
1573 for (a = 0; a < ob_src->totcol; a++) {
1574 Material *ma = give_current_material(ob_src, a + 1);
1575 assign_material(ob_dst, ma, a + 1, BKE_MAT_ASSIGN_USERPREF); /* also works with ma==NULL */
1577 DAG_id_tag_update(&ob_dst->id, OB_RECALC_DATA);
1579 case MAKE_LINKS_ANIMDATA:
1580 BKE_animdata_copy_id((ID *)ob_dst, (ID *)ob_src, false);
1581 if (ob_dst->data && ob_src->data) {
1582 if (ID_IS_LINKED_DATABLOCK(obdata_id)) {
1586 BKE_animdata_copy_id((ID *)ob_dst->data, (ID *)ob_src->data, false);
1588 DAG_id_tag_update(&ob_dst->id, OB_RECALC_OB | OB_RECALC_DATA | OB_RECALC_TIME);
1590 case MAKE_LINKS_GROUP:
1592 LinkNode *group_node;
1594 /* first clear groups */
1595 BKE_object_groups_clear(scene, base_dst, ob_dst);
1597 /* now add in the groups from the link nodes */
1598 for (group_node = ob_groups; group_node; group_node = group_node->next) {
1599 if (ob_dst->dup_group != group_node->link) {
1600 BKE_group_object_add(group_node->link, ob_dst, scene, base_dst);
1608 case MAKE_LINKS_DUPLIGROUP:
1609 ob_dst->dup_group = ob_src->dup_group;
1610 if (ob_dst->dup_group) {
1611 id_us_plus(&ob_dst->dup_group->id);
1612 ob_dst->transflag |= OB_DUPLIGROUP;
1615 case MAKE_LINKS_MODIFIERS:
1616 BKE_object_link_modifiers(ob_dst, ob_src);
1617 DAG_id_tag_update(&ob_dst->id, OB_RECALC_OB | OB_RECALC_DATA | OB_RECALC_TIME);
1619 case MAKE_LINKS_FONTS:
1621 Curve *cu_src = ob_src->data;
1622 Curve *cu_dst = ob_dst->data;
1624 if (ID_IS_LINKED_DATABLOCK(obdata_id)) {
1630 id_us_min(&cu_dst->vfont->id);
1631 cu_dst->vfont = cu_src->vfont;
1632 id_us_plus((ID *)cu_dst->vfont);
1634 id_us_min(&cu_dst->vfontb->id);
1635 cu_dst->vfontb = cu_src->vfontb;
1636 id_us_plus((ID *)cu_dst->vfontb);
1638 id_us_min(&cu_dst->vfonti->id);
1639 cu_dst->vfonti = cu_src->vfonti;
1640 id_us_plus((ID *)cu_dst->vfonti);
1641 if (cu_dst->vfontbi)
1642 id_us_min(&cu_dst->vfontbi->id);
1643 cu_dst->vfontbi = cu_src->vfontbi;
1644 id_us_plus((ID *)cu_dst->vfontbi);
1646 DAG_id_tag_update(&ob_dst->id, OB_RECALC_OB | OB_RECALC_DATA | OB_RECALC_TIME);
1655 if (type == MAKE_LINKS_GROUP) {
1657 BLI_linklist_free(ob_groups, NULL);
1661 BKE_report(op->reports, RPT_WARNING, "Skipped some groups because of cycle detected");
1666 BKE_report(op->reports, RPT_WARNING, "Skipped editing library object data");
1669 DAG_relations_tag_update(bmain);
1670 WM_event_add_notifier(C, NC_SPACE | ND_SPACE_VIEW3D, CTX_wm_view3d(C));
1671 WM_event_add_notifier(C, NC_ANIMATION | ND_NLA_ACTCHANGE, CTX_wm_view3d(C));
1672 WM_event_add_notifier(C, NC_OBJECT, NULL);
1674 return OPERATOR_FINISHED;
1678 void OBJECT_OT_make_links_scene(wmOperatorType *ot)
1683 ot->name = "Link Objects to Scene";
1684 ot->description = "Link selection to another scene";
1685 ot->idname = "OBJECT_OT_make_links_scene";
1688 ot->invoke = WM_enum_search_invoke;
1689 ot->exec = make_links_scene_exec;
1690 /* better not run the poll check */
1693 ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
1696 prop = RNA_def_enum(ot->srna, "scene", DummyRNA_NULL_items, 0, "Scene", "");
1697 RNA_def_enum_funcs(prop, RNA_scene_local_itemf);
1698 RNA_def_property_flag(prop, PROP_ENUM_NO_TRANSLATE);
1702 void OBJECT_OT_make_links_data(wmOperatorType *ot)
1704 static EnumPropertyItem make_links_items[] = {
1705 {MAKE_LINKS_OBDATA, "OBDATA", 0, "Object Data", ""},
1706 {MAKE_LINKS_MATERIALS, "MATERIAL", 0, "Materials", ""},
1707 {MAKE_LINKS_ANIMDATA, "ANIMATION", 0, "Animation Data", ""},
1708 {MAKE_LINKS_GROUP, "GROUPS", 0, "Group", ""},
1709 {MAKE_LINKS_DUPLIGROUP, "DUPLIGROUP", 0, "DupliGroup", ""},
1710 {MAKE_LINKS_MODIFIERS, "MODIFIERS", 0, "Modifiers", ""},
1711 {MAKE_LINKS_FONTS, "FONTS", 0, "Fonts", ""},
1712 {0, NULL, 0, NULL, NULL}};
1715 ot->name = "Link Data";
1716 ot->description = "Apply active object links to other selected objects";
1717 ot->idname = "OBJECT_OT_make_links_data";
1720 ot->exec = make_links_data_exec;
1721 ot->poll = ED_operator_object_active;
1724 ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
1727 ot->prop = RNA_def_enum(ot->srna, "type", make_links_items, 0, "Type", "");
1731 /**************************** Make Single User ********************************/
1733 static void single_object_users(Main *bmain, Scene *scene, View3D *v3d, const int flag, const bool copy_groups)
1737 Group *group, *groupn;
1740 clear_sca_new_poins(); /* sensor/contr/act */
1742 /* newid may still have some trash from Outliner tree building,
1743 * so clear that first to avoid errors [#26002] */
1744 for (ob = bmain->object.first; ob; ob = ob->id.next)
1745 ob->id.newid = NULL;
1747 /* duplicate (must set newid) */
1748 for (base = FIRSTBASE; base; base = base->next) {
1751 if ((base->flag & flag) == flag) {
1752 if (!ID_IS_LINKED_DATABLOCK(ob) && ob->id.us > 1) {
1753 /* base gets copy of object */
1754 obn = BKE_object_copy(bmain, ob);
1758 if (ob->flag & OB_FROMGROUP) {
1759 obn->flag |= OB_FROMGROUP;
1763 /* copy already clears */
1765 /* remap gpencil parenting */
1768 bGPdata *gpd = scene->gpd;
1769 for (bGPDlayer *gpl = gpd->layers.first; gpl; gpl = gpl->next) {
1770 if (gpl->parent == ob) {
1776 base->flag = obn->flag;
1783 /* duplicate groups that consist entirely of duplicated objects */
1784 for (group = bmain->group.first; group; group = group->id.next) {
1785 group->id.newid = NULL;
1787 if (copy_groups && group->gobject.first) {
1788 bool all_duplicated = true;
1790 for (go = group->gobject.first; go; go = go->next) {
1791 if (!(go->ob && (go->ob->id.newid))) {
1792 all_duplicated = false;
1797 if (all_duplicated) {
1798 groupn = BKE_group_copy(bmain, group);
1800 for (go = groupn->gobject.first; go; go = go->next)
1801 go->ob = (Object *)go->ob->id.newid;
1806 /* group pointers in scene */
1807 BKE_scene_groups_relink(scene);
1809 ID_NEW(scene->camera);
1810 if (v3d) ID_NEW(v3d->camera);
1812 /* object and group pointers */
1813 for (base = FIRSTBASE; base; base = base->next) {
1814 BKE_libblock_relink(&base->object->id);
1817 set_sca_new_poins();
1820 /* not an especially efficient function, only added so the single user
1821 * button can be functional.*/
1822 void ED_object_single_user(Main *bmain, Scene *scene, Object *ob)
1825 const bool copy_groups = false;
1827 for (base = FIRSTBASE; base; base = base->next) {
1828 if (base->object == ob) base->flag |= OB_DONE;
1829 else base->flag &= ~OB_DONE;
1832 single_object_users(bmain, scene, NULL, OB_DONE, copy_groups);
1835 static void new_id_matar(Main *bmain, Material **matar, const int totcol)
1840 for (a = 0; a < totcol; a++) {
1841 id = (ID *)matar[a];
1842 if (id && !ID_IS_LINKED_DATABLOCK(id)) {
1844 matar[a] = (Material *)id->newid;
1845 id_us_plus(id->newid);
1848 else if (id->us > 1) {
1849 matar[a] = BKE_material_copy(bmain, matar[a]);
1851 id->newid = (ID *)matar[a];
1857 static void single_obdata_users(Main *bmain, Scene *scene, const int flag)
1869 for (base = FIRSTBASE; base; base = base->next) {
1871 if (!ID_IS_LINKED_DATABLOCK(ob) && (base->flag & flag) == flag) {
1874 if (id && id->us > 1 && !ID_IS_LINKED_DATABLOCK(id)) {
1875 DAG_id_tag_update(&ob->id, OB_RECALC_DATA);
1879 ob->data = la = BKE_lamp_copy(bmain, ob->data);
1880 for (a = 0; a < MAX_MTEX; a++) {
1882 ID_NEW(la->mtex[a]->object);
1887 ob->data = BKE_camera_copy(bmain, ob->data);
1890 ob->data = me = BKE_mesh_copy(bmain, ob->data);
1892 BKE_animdata_copy_id_action((ID *)me->key);
1895 ob->data = BKE_mball_copy(bmain, ob->data);
1900 ob->data = cu = BKE_curve_copy(bmain, ob->data);
1902 ID_NEW(cu->taperobj);
1904 BKE_animdata_copy_id_action((ID *)cu->key);
1907 ob->data = lat = BKE_lattice_copy(bmain, ob->data);
1909 BKE_animdata_copy_id_action((ID *)lat->key);
1912 DAG_id_tag_update(&ob->id, OB_RECALC_DATA);
1913 ob->data = BKE_armature_copy(bmain, ob->data);
1914 BKE_pose_rebuild(ob, ob->data);
1917 ob->data = BKE_speaker_copy(bmain, ob->data);
1920 if (G.debug & G_DEBUG)
1921 printf("ERROR %s: can't copy %s\n", __func__, id->name);
1925 /* Copy animation data after object data became local,
1926 * otherwise old and new object data will share the same
1927 * AnimData structure, which is not what we want.
1930 BKE_animdata_copy_id_action((ID *)ob->data);
1933 id->newid = ob->data;
1938 me = bmain->mesh.first;
1940 ID_NEW(me->texcomesh);
1945 static void single_object_action_users(Scene *scene, const int flag)
1950 for (base = FIRSTBASE; base; base = base->next) {
1952 if (!ID_IS_LINKED_DATABLOCK(ob) && (flag == 0 || (base->flag & SELECT)) ) {
1953 DAG_id_tag_update(&ob->id, OB_RECALC_DATA);
1954 BKE_animdata_copy_id_action(&ob->id);
1959 static void single_mat_users(Main *bmain, Scene *scene, const int flag, const bool do_textures)
1967 for (base = FIRSTBASE; base; base = base->next) {
1969 if (!ID_IS_LINKED_DATABLOCK(ob) && (flag == 0 || (base->flag & SELECT)) ) {
1970 for (a = 1; a <= ob->totcol; a++) {
1971 ma = give_current_material(ob, a);
1973 /* do not test for LIB_TAG_NEW: this functions guaranteed delivers single_users! */
1975 if (ma->id.us > 1) {
1976 man = BKE_material_copy(bmain, ma);
1977 BKE_animdata_copy_id_action(&man->id);
1980 assign_material(ob, man, a, BKE_MAT_ASSIGN_USERPREF);
1983 for (b = 0; b < MAX_MTEX; b++) {
1984 if (ma->mtex[b] && (tex = ma->mtex[b]->tex)) {
1985 if (tex->id.us > 1) {
1986 id_us_min(&tex->id);
1987 tex = BKE_texture_copy(bmain, tex);
1988 BKE_animdata_copy_id_action(&tex->id);
1989 man->mtex[b]->tex = tex;
2001 static void do_single_tex_user(Main *bmain, Tex **from)
2006 if (tex == NULL) return;
2008 if (tex->id.newid) {
2009 *from = (Tex *)tex->id.newid;
2010 id_us_plus(tex->id.newid);
2011 id_us_min(&tex->id);
2013 else if (tex->id.us > 1) {
2014 texn = BKE_texture_copy(bmain, tex);
2015 BKE_animdata_copy_id_action(&texn->id);
2016 tex->id.newid = (ID *)texn;
2017 id_us_min(&tex->id);
2022 static void single_tex_users_expand(Main *bmain)
2024 /* only when 'parent' blocks are LIB_TAG_NEW */
2030 for (ma = bmain->mat.first; ma; ma = ma->id.next) {
2031 if (ma->id.tag & LIB_TAG_NEW) {
2032 for (b = 0; b < MAX_MTEX; b++) {
2033 if (ma->mtex[b] && ma->mtex[b]->tex) {
2034 do_single_tex_user(bmain, &(ma->mtex[b]->tex));
2040 for (la = bmain->lamp.first; la; la = la->id.next) {
2041 if (la->id.tag & LIB_TAG_NEW) {
2042 for (b = 0; b < MAX_MTEX; b++) {
2043 if (la->mtex[b] && la->mtex[b]->tex) {
2044 do_single_tex_user(bmain, &(la->mtex[b]->tex));
2050 for (wo = bmain->world.first; wo; wo = wo->id.next) {
2051 if (wo->id.tag & LIB_TAG_NEW) {
2052 for (b = 0; b < MAX_MTEX; b++) {
2053 if (wo->mtex[b] && wo->mtex[b]->tex) {
2054 do_single_tex_user(bmain, &(wo->mtex[b]->tex));
2061 static void single_mat_users_expand(Main *bmain)
2063 /* only when 'parent' blocks are LIB_TAG_NEW */
2071 for (ob = bmain->object.first; ob; ob = ob->id.next)
2072 if (ob->id.tag & LIB_TAG_NEW)
2073 new_id_matar(bmain, ob->mat, ob->totcol);
2075 for (me = bmain->mesh.first; me; me = me->id.next)
2076 if (me->id.tag & LIB_TAG_NEW)
2077 new_id_matar(bmain, me->mat, me->totcol);
2079 for (cu = bmain->curve.first; cu; cu = cu->id.next)
2080 if (cu->id.tag & LIB_TAG_NEW)
2081 new_id_matar(bmain, cu->mat, cu->totcol);
2083 for (mb = bmain->mball.first; mb; mb = mb->id.next)
2084 if (mb->id.tag & LIB_TAG_NEW)
2085 new_id_matar(bmain, mb->mat, mb->totcol);
2087 /* material imats */
2088 for (ma = bmain->mat.first; ma; ma = ma->id.next)
2089 if (ma->id.tag & LIB_TAG_NEW)
2090 for (a = 0; a < MAX_MTEX; a++)
2092 ID_NEW(ma->mtex[a]->object);
2095 /* used for copying scenes */
2096 void ED_object_single_users(Main *bmain, Scene *scene, const bool full, const bool copy_groups)
2098 single_object_users(bmain, scene, NULL, 0, copy_groups);
2101 single_obdata_users(bmain, scene, 0);
2102 single_object_action_users(scene, 0);
2103 single_mat_users_expand(bmain);
2104 single_tex_users_expand(bmain);
2107 BKE_main_id_clear_newpoins(bmain);
2108 DAG_relations_tag_update(bmain);
2111 /******************************* Make Local ***********************************/
2113 /* helper for below, ma was checked to be not NULL */
2114 static void make_local_makelocalmaterial(Material *ma)
2119 id_make_local(G.main, &ma->id, false, false);
2121 for (b = 0; b < MAX_MTEX; b++)
2122 if (ma->mtex[b] && ma->mtex[b]->tex)
2123 id_make_local(G.main, &ma->mtex[b]->tex->id, false, false);
2125 adt = BKE_animdata_from_id(&ma->id);
2126 if (adt) BKE_animdata_make_local(adt);
2132 MAKE_LOCAL_SELECT_OB = 1,
2133 MAKE_LOCAL_SELECT_OBDATA = 2,
2134 MAKE_LOCAL_SELECT_OBDATA_MATERIAL = 3,
2138 static int tag_localizable_looper(
2139 void *UNUSED(user_data), ID *UNUSED(self_id), ID **id_pointer, const int UNUSED(cd_flag))
2142 (*id_pointer)->tag &= ~LIB_TAG_DOIT;
2145 return IDWALK_RET_NOP;
2148 static void tag_localizable_objects(bContext *C, const int mode)
2150 Main *bmain = CTX_data_main(C);
2152 BKE_main_id_tag_all(bmain, LIB_TAG_DOIT, false);
2154 /* Set LIB_TAG_DOIT flag for all selected objects, so next we can check whether
2155 * object is gonna to become local or not.
2157 CTX_DATA_BEGIN (C, Object *, object, selected_objects)
2159 object->id.tag |= LIB_TAG_DOIT;
2161 /* If data is also gonna to become local, mark data we're interested in
2162 * as gonna-to-be-local.
2164 if (mode == MAKE_LOCAL_SELECT_OBDATA && object->data) {
2165 ID *data_id = (ID *) object->data;
2166 data_id->tag |= LIB_TAG_DOIT;
2171 /* Also forbid making objects local if other library objects are using
2172 * them for modifiers or constraints.
2174 for (Object *object = bmain->object.first; object; object = object->id.next) {
2175 if ((object->id.tag & LIB_TAG_DOIT) == 0) {
2176 BKE_library_foreach_ID_link(&object->id, tag_localizable_looper, NULL, IDWALK_READONLY);
2179 ID *data_id = (ID *) object->data;
2180 if ((data_id->tag & LIB_TAG_DOIT) == 0) {
2181 BKE_library_foreach_ID_link(data_id, tag_localizable_looper, NULL, IDWALK_READONLY);
2186 /* TODO(sergey): Drivers targets? */
2190 * Instance indirectly referenced zero user objects,
2191 * otherwise they're lost on reload, see T40595.
2193 static bool make_local_all__instance_indirect_unused(Main *bmain, Scene *scene)
2196 bool changed = false;
2198 for (ob = bmain->object.first; ob; ob = ob->id.next) {
2199 if (ID_IS_LINKED_DATABLOCK(ob) && (ob->id.us == 0)) {
2202 id_us_plus(&ob->id);
2204 base = BKE_scene_base_add(scene, ob);
2205 base->flag |= SELECT;
2206 base->object->flag = base->flag;
2207 DAG_id_tag_update(&ob->id, OB_RECALC_OB | OB_RECALC_DATA | OB_RECALC_TIME);
2216 static int make_local_exec(bContext *C, wmOperator *op)
2218 Main *bmain = CTX_data_main(C);
2219 Scene *scene = CTX_data_scene(C);
2221 Material *ma, ***matarar;
2224 const int mode = RNA_enum_get(op->ptr, "type");
2227 if (mode == MAKE_LOCAL_ALL) {
2228 /* de-select so the user can differentiate newly instanced from existing objects */
2229 BKE_scene_base_deselect_all(scene);
2231 if (make_local_all__instance_indirect_unused(bmain, scene)) {
2232 BKE_report(op->reports, RPT_INFO,
2233 "Orphan library objects added to the current scene to avoid loss");
2236 BKE_library_make_local(bmain, NULL, false, false); /* NULL is all libs */
2237 WM_event_add_notifier(C, NC_WINDOW, NULL);
2238 return OPERATOR_FINISHED;
2241 tag_localizable_objects(C, mode);
2242 BKE_main_id_clear_newpoins(bmain);
2244 CTX_DATA_BEGIN (C, Object *, ob, selected_objects)
2246 if ((ob->id.tag & LIB_TAG_DOIT) == 0) {
2251 id_make_local(bmain, &ob->id, false, false);
2255 /* maybe object pointers */
2256 CTX_DATA_BEGIN (C, Object *, ob, selected_objects)
2258 if (ob->id.lib == NULL) {
2264 CTX_DATA_BEGIN (C, Object *, ob, selected_objects)
2266 if ((ob->id.tag & LIB_TAG_DOIT) == 0) {
2272 if (id && (ELEM(mode, MAKE_LOCAL_SELECT_OBDATA, MAKE_LOCAL_SELECT_OBDATA_MATERIAL))) {
2273 id_make_local(bmain, id, false, false);
2274 adt = BKE_animdata_from_id(id);
2275 if (adt) BKE_animdata_make_local(adt);
2277 /* tag indirect data direct */
2278 matarar = give_matarar(ob);
2280 for (a = 0; a < ob->totcol; a++) {
2283 id_lib_extern(&ma->id);
2288 adt = BKE_animdata_from_id(&ob->id);
2289 if (adt) BKE_animdata_make_local(adt);
2293 if (mode == MAKE_LOCAL_SELECT_OBDATA_MATERIAL) {
2294 CTX_DATA_BEGIN (C, Object *, ob, selected_objects)
2296 if ((ob->id.tag & LIB_TAG_DOIT) == 0) {
2300 if (ob->type == OB_LAMP) {
2303 for (b = 0; b < MAX_MTEX; b++)
2304 if (la->mtex[b] && la->mtex[b]->tex)
2305 id_make_local(bmain, &la->mtex[b]->tex->id, false, false);
2308 for (a = 0; a < ob->totcol; a++) {
2311 make_local_makelocalmaterial(ma);
2314 matarar = (Material ***)give_matarar(ob);
2316 for (a = 0; a < ob->totcol; a++) {
2319 make_local_makelocalmaterial(ma);
2327 WM_event_add_notifier(C, NC_WINDOW, NULL);
2329 return OPERATOR_FINISHED;
2332 void OBJECT_OT_make_local(wmOperatorType *ot)
2334 static EnumPropertyItem type_items[] = {
2335 {MAKE_LOCAL_SELECT_OB, "SELECT_OBJECT", 0, "Selected Objects", ""},
2336 {MAKE_LOCAL_SELECT_OBDATA, "SELECT_OBDATA", 0, "Selected Objects and Data", ""},
2337 {MAKE_LOCAL_SELECT_OBDATA_MATERIAL, "SELECT_OBDATA_MATERIAL", 0, "Selected Objects, Data and Materials", ""},
2338 {MAKE_LOCAL_ALL, "ALL", 0, "All", ""},
2339 {0, NULL, 0, NULL, NULL}
2343 ot->name = "Make Local";
2344 ot->description = "Make library linked data-blocks local to this file";
2345 ot->idname = "OBJECT_OT_make_local";
2348 ot->invoke = WM_menu_invoke;
2349 ot->exec = make_local_exec;
2350 ot->poll = ED_operator_objectmode;
2353 ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
2356 ot->prop = RNA_def_enum(ot->srna, "type", type_items, 0, "Type", "");
2360 MAKE_SINGLE_USER_ALL = 1,
2361 MAKE_SINGLE_USER_SELECTED = 2,
2364 static int make_single_user_exec(bContext *C, wmOperator *op)
2366 Main *bmain = CTX_data_main(C);
2367 Scene *scene = CTX_data_scene(C);
2368 View3D *v3d = CTX_wm_view3d(C); /* ok if this is NULL */
2369 const int flag = (RNA_enum_get(op->ptr, "type") == MAKE_SINGLE_USER_SELECTED) ? SELECT : 0;
2370 const bool copy_groups = false;
2371 bool update_deps = false;
2373 BKE_main_id_clear_newpoins(bmain);
2375 if (RNA_boolean_get(op->ptr, "object")) {
2376 single_object_users(bmain, scene, v3d, flag, copy_groups);
2378 /* needed since object relationships may have changed */
2382 if (RNA_boolean_get(op->ptr, "obdata")) {
2383 single_obdata_users(bmain, scene, flag);
2386 if (RNA_boolean_get(op->ptr, "material")) {
2387 single_mat_users(bmain, scene, flag, RNA_boolean_get(op->ptr, "texture"));
2390 #if 0 /* can't do this separate from materials */
2391 if (RNA_boolean_get(op->ptr, "texture"))
2392 single_mat_users(scene, flag, true);
2394 if (RNA_boolean_get(op->ptr, "animation")) {
2395 single_object_action_users(scene, flag);
2398 /* TODO(sergey): This should not be needed, however some tool still could rely
2399 * on the fact, that id->newid is kept NULL by default.
2400 * Need to make sure all the guys are learing newid before they're
2401 * using it, not after.
2403 BKE_main_id_clear_newpoins(bmain);
2405 WM_event_add_notifier(C, NC_WINDOW, NULL);
2408 DAG_relations_tag_update(bmain);
2411 return OPERATOR_FINISHED;
2414 void OBJECT_OT_make_single_user(wmOperatorType *ot)
2416 static EnumPropertyItem type_items[] = {
2417 {MAKE_SINGLE_USER_SELECTED, "SELECTED_OBJECTS", 0, "Selected Objects", ""},
2418 {MAKE_SINGLE_USER_ALL, "ALL", 0, "All", ""},
2419 {0, NULL, 0, NULL, NULL}};
2422 ot->name = "Make Single User";
2423 ot->description = "Make linked data local to each object";
2424 ot->idname = "OBJECT_OT_make_single_user";
2427 ot->invoke = WM_menu_invoke;
2428 ot->exec = make_single_user_exec;
2429 ot->poll = ED_operator_objectmode;
2432 ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
2435 ot->prop = RNA_def_enum(ot->srna, "type", type_items, MAKE_SINGLE_USER_SELECTED, "Type", "");
2437 RNA_def_boolean(ot->srna, "object", 0, "Object", "Make single user objects");
2438 RNA_def_boolean(ot->srna, "obdata", 0, "Object Data", "Make single user object data");
2439 RNA_def_boolean(ot->srna, "material", 0, "Materials", "Make materials local to each data-block");
2440 RNA_def_boolean(ot->srna, "texture", 0, "Textures",
2441 "Make textures local to each material (needs 'Materials' to be set too)");
2442 RNA_def_boolean(ot->srna, "animation", 0, "Object Animation", "Make animation data local to each object");
2445 static int drop_named_material_invoke(bContext *C, wmOperator *op, const wmEvent *event)
2447 Base *base = ED_view3d_give_base_under_cursor(C, event->mval);
2449 char name[MAX_ID_NAME - 2];
2451 RNA_string_get(op->ptr, "name", name);
2452 ma = (Material *)BKE_libblock_find_name(ID_MA, name);
2453 if (base == NULL || ma == NULL)
2454 return OPERATOR_CANCELLED;
2456 assign_material(base->object, ma, 1, BKE_MAT_ASSIGN_USERPREF);
2458 DAG_id_tag_update(&base->object->id, OB_RECALC_OB);
2460 WM_event_add_notifier(C, NC_OBJECT | ND_OB_SHADING, base->object);
2461 WM_event_add_notifier(C, NC_SPACE | ND_SPACE_VIEW3D, CTX_wm_view3d(C));
2462 WM_event_add_notifier(C, NC_MATERIAL | ND_SHADING_LINKS, ma);
2464 return OPERATOR_FINISHED;
2467 /* used for dropbox */
2468 /* assigns to object under cursor, only first material slot */
2469 void OBJECT_OT_drop_named_material(wmOperatorType *ot)
2472 ot->name = "Drop Named Material on Object";
2473 ot->description = "";
2474 ot->idname = "OBJECT_OT_drop_named_material";
2477 ot->invoke = drop_named_material_invoke;
2478 ot->poll = ED_operator_objectmode;
2481 ot->flag = OPTYPE_UNDO | OPTYPE_INTERNAL;
2484 RNA_def_string(ot->srna, "name", "Material", MAX_ID_NAME - 2, "Name", "Material name to assign");
2487 static int object_unlink_data_exec(bContext *C, wmOperator *op)
2490 PropertyPointerRNA pprop;
2492 UI_context_active_but_prop_get_templateID(C, &pprop.ptr, &pprop.prop);
2494 if (pprop.prop == NULL) {
2495 BKE_report(op->reports, RPT_ERROR, "Incorrect context for running object data unlink");
2496 return OPERATOR_CANCELLED;
2499 id = pprop.ptr.id.data;
2501 if (GS(id->name) == ID_OB) {
2502 Object *ob = (Object *)id;
2504 ID *id_data = ob->data;
2506 if (GS(id_data->name) == ID_IM) {
2511 BKE_report(op->reports, RPT_ERROR, "Can't unlink this object data");
2512 return OPERATOR_CANCELLED;
2517 RNA_property_update(C, &pprop.ptr, pprop.prop);
2519 return OPERATOR_FINISHED;
2523 * \note Only for empty-image objects, this operator is needed
2525 void OBJECT_OT_unlink_data(wmOperatorType *ot)
2528 ot->name = "Unlink";
2529 ot->idname = "OBJECT_OT_unlink_data";
2530 ot->description = "";
2533 ot->exec = object_unlink_data_exec;
2536 ot->flag = OPTYPE_INTERNAL;