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_speaker_types.h"
49 #include "DNA_world_types.h"
50 #include "DNA_object_types.h"
51 #include "DNA_vfont_types.h"
54 #include "BLI_listbase.h"
55 #include "BLI_linklist.h"
56 #include "BLI_string.h"
57 #include "BLI_utildefines.h"
59 #include "BLF_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"
78 #include "BKE_material.h"
79 #include "BKE_mball.h"
81 #include "BKE_modifier.h"
82 #include "BKE_object.h"
83 #include "BKE_report.h"
85 #include "BKE_scene.h"
86 #include "BKE_speaker.h"
87 #include "BKE_texture.h"
88 #include "BKE_tessmesh.h"
93 #include "UI_interface.h"
94 #include "UI_resources.h"
96 #include "RNA_access.h"
97 #include "RNA_define.h"
98 #include "RNA_enum_types.h"
100 #include "ED_armature.h"
101 #include "ED_curve.h"
102 #include "ED_keyframing.h"
103 #include "ED_object.h"
105 #include "ED_screen.h"
106 #include "ED_view3d.h"
108 #include "object_intern.h"
110 /*********************** Make Vertex Parent Operator ************************/
112 static int vertex_parent_set_poll(bContext *C)
114 return ED_operator_editmesh(C) || ED_operator_editsurfcurve(C) || ED_operator_editlattice(C);
117 static int vertex_parent_set_exec(bContext *C, wmOperator *op)
119 Main *bmain = CTX_data_main(C);
120 Scene *scene = CTX_data_scene(C);
121 Object *obedit = CTX_data_edit_object(C);
129 int a, v1 = 0, v2 = 0, v3 = 0, v4 = 0, nr = 1;
131 /* we need 1 to 3 selected vertices */
133 if (obedit->type == OB_MESH) {
134 Mesh *me = obedit->data;
137 EDBM_mesh_load(obedit);
138 EDBM_mesh_make(scene->toolsettings, scene, obedit);
140 em = me->edit_btmesh;
142 EDBM_mesh_normals_update(em);
143 BMEdit_RecalcTessellation(em);
145 /* derivedMesh might be needed for solving parenting,
146 * so re-create it here */
147 makeDerivedMesh(scene, obedit, em, CD_MASK_BAREMESH, 0);
149 BM_ITER_MESH (eve, &iter, em->bm, BM_VERTS_OF_MESH) {
150 if (BM_elem_flag_test(eve, BM_ELEM_SELECT)) {
151 if (v1 == 0) v1 = nr;
152 else if (v2 == 0) v2 = nr;
153 else if (v3 == 0) v3 = nr;
154 else if (v4 == 0) v4 = nr;
160 else if (ELEM(obedit->type, OB_SURF, OB_CURVE)) {
161 ListBase *editnurb = object_editcurve_get(obedit);
165 nu = editnurb->first;
167 if (nu->type == CU_BEZIER) {
171 if (BEZSELECTED_HIDDENHANDLES(cu, bezt)) {
172 if (v1 == 0) v1 = nr;
173 else if (v2 == 0) v2 = nr;
174 else if (v3 == 0) v3 = nr;
175 else if (v4 == 0) v4 = nr;
184 a = nu->pntsu * nu->pntsv;
186 if (bp->f1 & SELECT) {
187 if (v1 == 0) v1 = nr;
188 else if (v2 == 0) v2 = nr;
189 else if (v3 == 0) v3 = nr;
190 else if (v4 == 0) v4 = nr;
200 else if (obedit->type == OB_LATTICE) {
201 Lattice *lt = obedit->data;
203 a = lt->editlatt->latt->pntsu * lt->editlatt->latt->pntsv * lt->editlatt->latt->pntsw;
204 bp = lt->editlatt->latt->def;
206 if (bp->f1 & SELECT) {
207 if (v1 == 0) v1 = nr;
208 else if (v2 == 0) v2 = nr;
209 else if (v3 == 0) v3 = nr;
210 else if (v4 == 0) v4 = nr;
218 if (v4 || !((v1 && v2 == 0 && v3 == 0) || (v1 && v2 && v3))) {
219 BKE_report(op->reports, RPT_ERROR, "Select either 1 or 3 vertices to parent to");
220 return OPERATOR_CANCELLED;
223 CTX_DATA_BEGIN (C, Object *, ob, selected_editable_objects)
226 DAG_id_tag_update(&ob->id, OB_RECALC_OB | OB_RECALC_DATA | OB_RECALC_TIME);
227 par = obedit->parent;
230 if (par == ob) break;
234 BKE_report(op->reports, RPT_ERROR, "Loop in parents");
239 ob->parent = BASACT->object;
241 ob->partype = PARVERT3;
246 /* inverse parent matrix */
247 BKE_object_workob_calc_parent(scene, ob, &workob);
248 invert_m4_m4(ob->parentinv, workob.obmat);
251 ob->partype = PARVERT1;
254 /* inverse parent matrix */
255 BKE_object_workob_calc_parent(scene, ob, &workob);
256 invert_m4_m4(ob->parentinv, workob.obmat);
263 DAG_relations_tag_update(bmain);
265 WM_event_add_notifier(C, NC_OBJECT, NULL);
267 return OPERATOR_FINISHED;
270 void OBJECT_OT_vertex_parent_set(wmOperatorType *ot)
273 ot->name = "Make Vertex Parent";
274 ot->description = "Parent selected objects to the selected vertices";
275 ot->idname = "OBJECT_OT_vertex_parent_set";
278 ot->invoke = WM_operator_confirm;
279 ot->poll = vertex_parent_set_poll;
280 ot->exec = vertex_parent_set_exec;
283 ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
286 /********************** Make Proxy Operator *************************/
288 /* set the object to proxify */
289 static int make_proxy_invoke(bContext *C, wmOperator *op, const wmEvent *event)
291 Scene *scene = CTX_data_scene(C);
292 Object *ob = ED_object_active_context(C);
295 if (!scene || scene->id.lib || !ob)
296 return OPERATOR_CANCELLED;
298 /* Get object to work on - use a menu if we need to... */
299 if (ob->dup_group && ob->dup_group->id.lib) {
300 /* gives menu with list of objects in group */
301 //proxy_group_objects_menu(C, op, ob, ob->dup_group);
302 WM_enum_search_invoke(C, op, event);
303 return OPERATOR_CANCELLED;
306 else if (ob->id.lib) {
307 uiPopupMenu *pup = uiPupMenuBegin(C, IFACE_("OK?"), ICON_QUESTION);
308 uiLayout *layout = uiPupMenuLayout(pup);
310 /* create operator menu item with relevant properties filled in */
311 uiItemFullO_ptr(layout, op->type, op->type->name, ICON_NONE, NULL, WM_OP_EXEC_REGION_WIN, UI_ITEM_O_RETURN_PROPS);
313 /* present the menu and be done... */
314 uiPupMenuEnd(C, pup);
317 /* error.. cannot continue */
318 BKE_report(op->reports, RPT_ERROR, "Can only make proxy for a referenced object or group");
321 /* this invoke just calls another instance of this operator... */
322 return OPERATOR_CANCELLED;
325 static int make_proxy_exec(bContext *C, wmOperator *op)
327 Main *bmain = CTX_data_main(C);
328 Object *ob, *gob = ED_object_active_context(C);
330 Scene *scene = CTX_data_scene(C);
332 if (gob->dup_group != NULL) {
333 go = BLI_findlink(&gob->dup_group->gobject, RNA_enum_get(op->ptr, "object"));
343 Base *newbase, *oldbase = BASACT;
344 char name[MAX_ID_NAME + 4];
346 /* Add new object for the proxy */
347 newob = BKE_object_add(scene, OB_EMPTY);
349 BLI_snprintf(name, sizeof(name), "%s_proxy", ((ID *)(gob ? gob : ob))->name + 2);
351 rename_id(&newob->id, name);
354 newbase = BASACT; /* BKE_object_add sets active... */
355 newbase->lay = oldbase->lay;
356 newob->lay = newbase->lay;
358 /* remove base, leave user count of object, it gets linked in BKE_object_make_proxy */
360 BKE_scene_base_unlink(scene, oldbase);
364 BKE_object_make_proxy(newob, ob, gob);
366 /* depsgraph flushes are needed for the new data */
367 DAG_relations_tag_update(bmain);
368 DAG_id_tag_update(&newob->id, OB_RECALC_OB | OB_RECALC_DATA | OB_RECALC_TIME);
369 WM_event_add_notifier(C, NC_OBJECT | ND_DRAW, newob);
372 BKE_report(op->reports, RPT_ERROR, "No object to make proxy for");
373 return OPERATOR_CANCELLED;
376 return OPERATOR_FINISHED;
379 /* Generic itemf's for operators that take library args */
380 static EnumPropertyItem *proxy_group_object_itemf(bContext *C, PointerRNA *UNUSED(ptr), PropertyRNA *UNUSED(prop), int *free)
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 prop = RNA_def_enum(ot->srna, "object", DummyRNA_DEFAULT_items, 0, "Proxy Object", "Name of lib-linked/grouped object to make a proxy for"); /* XXX, relies on hard coded ID at the moment */
423 RNA_def_enum_funcs(prop, proxy_group_object_itemf);
427 /********************** Clear Parent Operator ******************* */
429 typedef enum eObClearParentTypes {
430 CLEAR_PARENT_ALL = 0,
431 CLEAR_PARENT_KEEP_TRANSFORM,
433 } eObClearParentTypes;
435 EnumPropertyItem prop_clear_parent_types[] = {
436 {CLEAR_PARENT_ALL, "CLEAR", 0, "Clear Parent", ""},
437 {CLEAR_PARENT_KEEP_TRANSFORM, "CLEAR_KEEP_TRANSFORM", 0, "Clear and Keep Transformation", ""},
438 {CLEAR_PARENT_INVERSE, "CLEAR_INVERSE", 0, "Clear Parent Inverse", ""},
439 {0, NULL, 0, NULL, NULL}
442 /* Helper for ED_object_parent_clear() - Remove deform-modifiers associated with parent */
443 static void object_remove_parent_deform_modifiers(Object *ob, const Object *par)
445 if (ELEM3(par->type, OB_ARMATURE, OB_LATTICE, OB_CURVE)) {
446 ModifierData *md, *mdn;
448 /* assume that we only need to remove the first instance of matching deform modifier here */
449 for (md = ob->modifiers.first; md; md = mdn) {
454 /* need to match types (modifier + parent) and references */
455 if ((md->type == eModifierType_Armature) && (par->type == OB_ARMATURE)) {
456 ArmatureModifierData *amd = (ArmatureModifierData *)md;
457 if (amd->object == par) {
461 else if ((md->type == eModifierType_Lattice) && (par->type == OB_LATTICE)) {
462 LatticeModifierData *lmd = (LatticeModifierData *)md;
463 if (lmd->object == par) {
467 else if ((md->type == eModifierType_Curve) && (par->type == OB_CURVE)) {
468 CurveModifierData *cmd = (CurveModifierData *)md;
469 if (cmd->object == par) {
474 /* free modifier if match */
476 BLI_remlink(&ob->modifiers, md);
483 void ED_object_parent_clear(Object *ob, int type)
485 if (ob->parent == NULL)
489 case CLEAR_PARENT_ALL:
491 /* for deformers, remove corresponding modifiers to prevent a large number of modifiers building up */
492 object_remove_parent_deform_modifiers(ob, ob->parent);
494 /* clear parenting relationship completely */
499 case CLEAR_PARENT_KEEP_TRANSFORM:
501 /* remove parent, and apply the parented transform result as object's local transforms */
503 BKE_object_apply_mat4(ob, ob->obmat, TRUE, FALSE);
507 case CLEAR_PARENT_INVERSE:
509 /* object stays parented, but the parent inverse (i.e. offset from parent to retain binding state) is cleared */
510 unit_m4(ob->parentinv);
515 DAG_id_tag_update(&ob->id, OB_RECALC_OB | OB_RECALC_DATA | OB_RECALC_TIME);
518 /* note, poll should check for editable scene */
519 static int parent_clear_exec(bContext *C, wmOperator *op)
521 Main *bmain = CTX_data_main(C);
522 int type = RNA_enum_get(op->ptr, "type");
524 CTX_DATA_BEGIN (C, Object *, ob, selected_editable_objects)
526 ED_object_parent_clear(ob, type);
530 DAG_relations_tag_update(bmain);
531 WM_event_add_notifier(C, NC_OBJECT | ND_TRANSFORM, NULL);
532 WM_event_add_notifier(C, NC_OBJECT | ND_PARENT, NULL);
533 return OPERATOR_FINISHED;
536 void OBJECT_OT_parent_clear(wmOperatorType *ot)
539 ot->name = "Clear Parent";
540 ot->description = "Clear the object's parenting";
541 ot->idname = "OBJECT_OT_parent_clear";
544 ot->invoke = WM_menu_invoke;
545 ot->exec = parent_clear_exec;
547 ot->poll = ED_operator_object_active_editable;
550 ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
552 ot->prop = RNA_def_enum(ot->srna, "type", prop_clear_parent_types, 0, "Type", "");
555 /* ******************** Make Parent Operator *********************** */
557 void ED_object_parent(Object *ob, Object *par, int type, const char *substr)
559 if (!par || BKE_object_parent_loop_check(par, ob)) {
561 ob->partype = PAROBJECT;
562 ob->parsubstr[0] = 0;
566 /* this could use some more checks */
569 ob->partype &= ~PARTYPE;
571 BLI_strncpy(ob->parsubstr, substr, sizeof(ob->parsubstr));
574 /* Operator Property */
575 EnumPropertyItem prop_make_parent_types[] = {
576 {PAR_OBJECT, "OBJECT", 0, "Object", ""},
577 {PAR_ARMATURE, "ARMATURE", 0, "Armature Deform", ""},
578 {PAR_ARMATURE_NAME, "ARMATURE_NAME", 0, " With Empty Groups", ""},
579 {PAR_ARMATURE_AUTO, "ARMATURE_AUTO", 0, " With Automatic Weights", ""},
580 {PAR_ARMATURE_ENVELOPE, "ARMATURE_ENVELOPE", 0, " With Envelope Weights", ""},
581 {PAR_BONE, "BONE", 0, "Bone", ""},
582 {PAR_BONE_RELATIVE, "BONE_RELATIVE", 0, "Bone Relative", ""},
583 {PAR_CURVE, "CURVE", 0, "Curve Deform", ""},
584 {PAR_FOLLOW, "FOLLOW", 0, "Follow Path", ""},
585 {PAR_PATH_CONST, "PATH_CONST", 0, "Path Constraint", ""},
586 {PAR_LATTICE, "LATTICE", 0, "Lattice Deform", ""},
587 {PAR_VERTEX, "VERTEX", 0, "Vertex", ""},
588 {PAR_TRIA, "TRIA", 0, "Triangle", ""},
589 {0, NULL, 0, NULL, NULL}
592 int ED_object_parent_set(ReportList *reports, Main *bmain, Scene *scene, Object *ob, Object *par,
593 int partype, int xmirror, int keep_transform)
595 bPoseChannel *pchan = NULL;
596 int pararm = ELEM4(partype, PAR_ARMATURE, PAR_ARMATURE_NAME, PAR_ARMATURE_ENVELOPE, PAR_ARMATURE_AUTO);
598 DAG_id_tag_update(&par->id, OB_RECALC_OB);
601 if (partype == PAR_FOLLOW || partype == PAR_PATH_CONST) {
602 if (par->type != OB_CURVE)
605 Curve *cu = par->data;
607 if ((cu->flag & CU_PATH) == 0) {
608 cu->flag |= CU_PATH | CU_FOLLOW;
609 BKE_displist_make_curveTypes(scene, par, 0); /* force creation of path data */
612 cu->flag |= CU_FOLLOW;
615 /* if follow, add F-Curve for ctime (i.e. "eval_time") so that path-follow works */
616 if (partype == PAR_FOLLOW) {
617 /* get or create F-Curve */
618 bAction *act = verify_adt_action(&cu->id, 1);
619 FCurve *fcu = verify_fcurve(act, NULL, NULL, "eval_time", 0, 1);
621 /* setup dummy 'generator' modifier here to get 1-1 correspondence still working */
622 if (!fcu->bezt && !fcu->fpt && !fcu->modifiers.first)
623 add_fmodifier(&fcu->modifiers, FMODIFIER_TYPE_GENERATOR);
626 /* fall back on regular parenting now (for follow only) */
627 if (partype == PAR_FOLLOW)
628 partype = PAR_OBJECT;
631 else if (ELEM(partype, PAR_BONE, PAR_BONE_RELATIVE)) {
632 pchan = BKE_pose_channel_active(par);
635 BKE_report(reports, RPT_ERROR, "No active bone");
641 if (BKE_object_parent_loop_check(par, ob)) {
642 BKE_report(reports, RPT_ERROR, "Loop in parents");
648 /* apply transformation of previous parenting */
649 if (keep_transform) {
650 /* was removed because of bug [#23577],
651 * but this can be handy in some cases too [#32616], so make optional */
652 BKE_object_apply_mat4(ob, ob->obmat, FALSE, FALSE);
655 /* set the parent (except for follow-path constraint option) */
656 if (partype != PAR_PATH_CONST) {
662 BLI_strncpy(ob->parsubstr, pchan->name, sizeof(ob->parsubstr));
664 ob->parsubstr[0] = 0;
666 if (partype == PAR_PATH_CONST) {
667 /* don't do anything here, since this is not technically "parenting" */
669 else if (ELEM(partype, PAR_CURVE, PAR_LATTICE) || (pararm)) {
670 /* partype is now set to PAROBJECT so that invisible 'virtual' modifiers don't need to be created
671 * NOTE: the old (2.4x) method was to set ob->partype = PARSKEL, creating the virtual modifiers
673 ob->partype = PAROBJECT; /* note, dna define, not operator property */
674 //ob->partype = PARSKEL; /* note, dna define, not operator property */
676 /* BUT, to keep the deforms, we need a modifier, and then we need to set the object that it uses
677 * - We need to ensure that the modifier we're adding doesn't already exist, so we check this by
678 * assuming that the parent is selected too...
680 // XXX currently this should only happen for meshes, curves, surfaces, and lattices - this stuff isn't available for metas yet
681 if (ELEM5(ob->type, OB_MESH, OB_CURVE, OB_SURF, OB_FONT, OB_LATTICE)) {
685 case PAR_CURVE: /* curve deform */
686 if ( modifiers_isDeformedByCurve(ob) != par) {
687 md = ED_object_modifier_add(reports, bmain, scene, ob, NULL, eModifierType_Curve);
689 ((CurveModifierData *)md)->object = par;
693 case PAR_LATTICE: /* lattice deform */
694 if (modifiers_isDeformedByLattice(ob) != par) {
695 md = ED_object_modifier_add(reports, bmain, scene, ob, NULL, eModifierType_Lattice);
697 ((LatticeModifierData *)md)->object = par;
701 default: /* armature deform */
702 if (modifiers_isDeformedByArmature(ob) != par) {
703 md = ED_object_modifier_add(reports, bmain, scene, ob, NULL, eModifierType_Armature);
705 ((ArmatureModifierData *)md)->object = par;
712 else if (partype == PAR_BONE) {
713 ob->partype = PARBONE; /* note, dna define, not operator property */
715 pchan->bone->flag &= ~BONE_RELATIVE_PARENTING;
717 else if (partype == PAR_BONE_RELATIVE) {
718 ob->partype = PARBONE; /* note, dna define, not operator property */
720 pchan->bone->flag |= BONE_RELATIVE_PARENTING;
723 ob->partype = PAROBJECT; /* note, dna define, not operator property */
726 if (partype == PAR_PATH_CONST) {
728 bFollowPathConstraint *data;
729 float cmat[4][4], vec[3];
731 con = BKE_add_ob_constraint(ob, "AutoPath", CONSTRAINT_TYPE_FOLLOWPATH);
736 BKE_get_constraint_target_matrix(scene, con, 0, CONSTRAINT_OBTYPE_OBJECT, NULL, cmat, scene->r.cfra);
737 sub_v3_v3v3(vec, ob->obmat[3], cmat[3]);
743 else if (pararm && ob->type == OB_MESH && par->type == OB_ARMATURE) {
744 if (partype == PAR_ARMATURE_NAME)
745 create_vgroups_from_armature(reports, scene, ob, par, ARM_GROUPS_NAME, FALSE);
746 else if (partype == PAR_ARMATURE_ENVELOPE)
747 create_vgroups_from_armature(reports, scene, ob, par, ARM_GROUPS_ENVELOPE, xmirror);
748 else if (partype == PAR_ARMATURE_AUTO) {
750 create_vgroups_from_armature(reports, scene, ob, par, ARM_GROUPS_AUTO, xmirror);
753 /* get corrected inverse */
754 ob->partype = PAROBJECT;
755 BKE_object_workob_calc_parent(scene, ob, &workob);
757 invert_m4_m4(ob->parentinv, workob.obmat);
760 /* calculate inverse parent matrix */
761 BKE_object_workob_calc_parent(scene, ob, &workob);
762 invert_m4_m4(ob->parentinv, workob.obmat);
765 DAG_id_tag_update(&ob->id, OB_RECALC_OB | OB_RECALC_DATA);
772 static int parent_set_exec(bContext *C, wmOperator *op)
774 Main *bmain = CTX_data_main(C);
775 Scene *scene = CTX_data_scene(C);
776 Object *par = ED_object_active_context(C);
777 int partype = RNA_enum_get(op->ptr, "type");
778 int xmirror = RNA_boolean_get(op->ptr, "xmirror");
779 int keep_transform = RNA_boolean_get(op->ptr, "keep_transform");
782 CTX_DATA_BEGIN (C, Object *, ob, selected_editable_objects)
784 if (!ED_object_parent_set(op->reports, bmain, scene, ob, par, partype, xmirror, keep_transform)) {
792 return OPERATOR_CANCELLED;
794 DAG_relations_tag_update(bmain);
795 WM_event_add_notifier(C, NC_OBJECT | ND_TRANSFORM, NULL);
796 WM_event_add_notifier(C, NC_OBJECT | ND_PARENT, NULL);
798 return OPERATOR_FINISHED;
802 static int parent_set_invoke(bContext *C, wmOperator *UNUSED(op), const wmEvent *UNUSED(event))
804 Object *ob = ED_object_active_context(C);
805 uiPopupMenu *pup = uiPupMenuBegin(C, IFACE_("Set Parent To"), ICON_NONE);
806 uiLayout *layout = uiPupMenuLayout(pup);
808 wmOperatorType *ot = WM_operatortype_find("OBJECT_OT_parent_set", TRUE);
812 uiItemEnumO_ptr(layout, ot, NULL, 0, "type", PAR_OBJECT);
814 opptr = uiItemFullO_ptr(layout, ot, IFACE_("Object"), ICON_NONE, NULL, WM_OP_EXEC_DEFAULT, UI_ITEM_O_RETURN_PROPS);
815 RNA_enum_set(&opptr, "type", PAR_OBJECT);
816 RNA_boolean_set(&opptr, "keep_transform", FALSE);
818 opptr = uiItemFullO_ptr(layout, ot, IFACE_("Object (Keep Transform)"), ICON_NONE, NULL, WM_OP_EXEC_DEFAULT,
819 UI_ITEM_O_RETURN_PROPS);
820 RNA_enum_set(&opptr, "type", PAR_OBJECT);
821 RNA_boolean_set(&opptr, "keep_transform", TRUE);
823 /* ob becomes parent, make the associated menus */
824 if (ob->type == OB_ARMATURE) {
825 uiItemEnumO_ptr(layout, ot, NULL, 0, "type", PAR_ARMATURE);
826 uiItemEnumO_ptr(layout, ot, NULL, 0, "type", PAR_ARMATURE_NAME);
827 uiItemEnumO_ptr(layout, ot, NULL, 0, "type", PAR_ARMATURE_ENVELOPE);
828 uiItemEnumO_ptr(layout, ot, NULL, 0, "type", PAR_ARMATURE_AUTO);
829 uiItemEnumO_ptr(layout, ot, NULL, 0, "type", PAR_BONE);
830 uiItemEnumO_ptr(layout, ot, NULL, 0, "type", PAR_BONE_RELATIVE);
832 else if (ob->type == OB_CURVE) {
833 uiItemEnumO_ptr(layout, ot, NULL, 0, "type", PAR_CURVE);
834 uiItemEnumO_ptr(layout, ot, NULL, 0, "type", PAR_FOLLOW);
835 uiItemEnumO_ptr(layout, ot, NULL, 0, "type", PAR_PATH_CONST);
837 else if (ob->type == OB_LATTICE) {
838 uiItemEnumO_ptr(layout, ot, NULL, 0, "type", PAR_LATTICE);
841 uiPupMenuEnd(C, pup);
843 return OPERATOR_CANCELLED;
846 static int parent_set_draw_check_prop(PointerRNA *ptr, PropertyRNA *prop)
848 const char *prop_id = RNA_property_identifier(prop);
849 int type = RNA_enum_get(ptr, "type");
851 /* Only show XMirror for PAR_ARMATURE_ENVELOPE and PAR_ARMATURE_AUTO! */
852 if (strcmp(prop_id, "xmirror") == 0) {
853 if (ELEM(type, PAR_ARMATURE_ENVELOPE, PAR_ARMATURE_AUTO))
862 static void parent_set_ui(bContext *C, wmOperator *op)
864 uiLayout *layout = op->layout;
865 wmWindowManager *wm = CTX_wm_manager(C);
868 RNA_pointer_create(&wm->id, op->type->srna, op->properties, &ptr);
870 /* Main auto-draw call. */
871 uiDefAutoButsRNA(layout, &ptr, parent_set_draw_check_prop, '\0');
874 void OBJECT_OT_parent_set(wmOperatorType *ot)
877 ot->name = "Make Parent";
878 ot->description = "Set the object's parenting";
879 ot->idname = "OBJECT_OT_parent_set";
882 ot->invoke = parent_set_invoke;
883 ot->exec = parent_set_exec;
884 ot->poll = ED_operator_object_active;
885 ot->ui = parent_set_ui;
888 ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
890 RNA_def_enum(ot->srna, "type", prop_make_parent_types, 0, "Type", "");
891 RNA_def_boolean(ot->srna, "xmirror", FALSE, "X Mirror",
892 "Apply weights symmetrically along X axis, for Envelope/Automatic vertex groups creation");
893 RNA_def_boolean(ot->srna, "keep_transform", FALSE, "Keep Transform",
894 "Apply transformation before parenting");
898 /* ************ Make Parent Without Inverse Operator ******************* */
900 static int parent_noinv_set_exec(bContext *C, wmOperator *op)
902 Main *bmain = CTX_data_main(C);
903 Object *par = ED_object_active_context(C);
905 DAG_id_tag_update(&par->id, OB_RECALC_OB);
907 /* context iterator */
908 CTX_DATA_BEGIN (C, Object *, ob, selected_editable_objects)
911 if (BKE_object_parent_loop_check(par, ob)) {
912 BKE_report(op->reports, RPT_ERROR, "Loop in parents");
915 /* clear inverse matrix and also the object location */
916 unit_m4(ob->parentinv);
917 memset(ob->loc, 0, 3 * sizeof(float));
919 /* set recalc flags */
920 DAG_id_tag_update(&ob->id, OB_RECALC_OB | OB_RECALC_DATA);
922 /* set parenting type for object - object only... */
924 ob->partype = PAROBJECT; /* note, dna define, not operator property */
930 DAG_relations_tag_update(bmain);
931 WM_event_add_notifier(C, NC_OBJECT | ND_TRANSFORM, NULL);
933 return OPERATOR_FINISHED;
936 void OBJECT_OT_parent_no_inverse_set(wmOperatorType *ot)
939 ot->name = "Make Parent without Inverse";
940 ot->description = "Set the object's parenting without setting the inverse parent correction";
941 ot->idname = "OBJECT_OT_parent_no_inverse_set";
944 ot->invoke = WM_operator_confirm;
945 ot->exec = parent_noinv_set_exec;
946 ot->poll = ED_operator_object_active_editable;
949 ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
952 /************************ Clear Slow Parent Operator *********************/
954 static int object_slow_parent_clear_exec(bContext *C, wmOperator *UNUSED(op))
956 Scene *scene = CTX_data_scene(C);
958 CTX_DATA_BEGIN (C, Object *, ob, selected_editable_objects)
961 if (ob->partype & PARSLOW) {
962 ob->partype -= PARSLOW;
963 BKE_object_where_is_calc(scene, ob);
964 ob->partype |= PARSLOW;
965 DAG_id_tag_update(&ob->id, OB_RECALC_OB);
971 WM_event_add_notifier(C, NC_SCENE, scene);
973 return OPERATOR_FINISHED;
976 void OBJECT_OT_slow_parent_clear(wmOperatorType *ot)
980 ot->name = "Clear Slow Parent";
981 ot->description = "Clear the object's slow parent";
982 ot->idname = "OBJECT_OT_slow_parent_clear";
985 ot->invoke = WM_operator_confirm;
986 ot->exec = object_slow_parent_clear_exec;
987 ot->poll = ED_operator_view3d_active;
990 ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
993 /********************** Make Slow Parent Operator *********************/
995 static int object_slow_parent_set_exec(bContext *C, wmOperator *UNUSED(op))
997 Scene *scene = CTX_data_scene(C);
999 CTX_DATA_BEGIN (C, Object *, ob, selected_editable_objects)
1002 ob->partype |= PARSLOW;
1004 DAG_id_tag_update(&ob->id, OB_RECALC_OB);
1009 WM_event_add_notifier(C, NC_SCENE, scene);
1011 return OPERATOR_FINISHED;
1014 void OBJECT_OT_slow_parent_set(wmOperatorType *ot)
1018 ot->name = "Set Slow Parent";
1019 ot->description = "Set the object's slow parent";
1020 ot->idname = "OBJECT_OT_slow_parent_set";
1023 ot->invoke = WM_operator_confirm;
1024 ot->exec = object_slow_parent_set_exec;
1025 ot->poll = ED_operator_view3d_active;
1028 ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
1031 /* ******************** Clear Track Operator ******************* */
1033 static EnumPropertyItem prop_clear_track_types[] = {
1034 {0, "CLEAR", 0, "Clear Track", ""},
1035 {1, "CLEAR_KEEP_TRANSFORM", 0, "Clear and Keep Transformation (Clear Track)", ""},
1036 {0, NULL, 0, NULL, NULL}
1039 /* note, poll should check for editable scene */
1040 static int object_track_clear_exec(bContext *C, wmOperator *op)
1042 Main *bmain = CTX_data_main(C);
1043 int type = RNA_enum_get(op->ptr, "type");
1045 if (CTX_data_edit_object(C)) {
1046 BKE_report(op->reports, RPT_ERROR, "Operation cannot be performed in edit mode");
1047 return OPERATOR_CANCELLED;
1049 CTX_DATA_BEGIN (C, Object *, ob, selected_editable_objects)
1051 bConstraint *con, *pcon;
1053 /* remove track-object for old track */
1055 DAG_id_tag_update(&ob->id, OB_RECALC_OB | OB_RECALC_DATA | OB_RECALC_TIME);
1057 /* also remove all tracking constraints */
1058 for (con = ob->constraints.last; con; con = pcon) {
1060 if (ELEM3(con->type, CONSTRAINT_TYPE_TRACKTO, CONSTRAINT_TYPE_LOCKTRACK, CONSTRAINT_TYPE_DAMPTRACK))
1061 BKE_remove_constraint(&ob->constraints, con);
1065 BKE_object_apply_mat4(ob, ob->obmat, TRUE, TRUE);
1069 DAG_relations_tag_update(bmain);
1070 WM_event_add_notifier(C, NC_OBJECT | ND_TRANSFORM, NULL);
1072 return OPERATOR_FINISHED;
1075 void OBJECT_OT_track_clear(wmOperatorType *ot)
1078 ot->name = "Clear Track";
1079 ot->description = "Clear tracking constraint or flag from object";
1080 ot->idname = "OBJECT_OT_track_clear";
1083 ot->invoke = WM_menu_invoke;
1084 ot->exec = object_track_clear_exec;
1086 ot->poll = ED_operator_objectmode;
1089 ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
1091 ot->prop = RNA_def_enum(ot->srna, "type", prop_clear_track_types, 0, "Type", "");
1094 /************************** Make Track Operator *****************************/
1096 static EnumPropertyItem prop_make_track_types[] = {
1097 {1, "DAMPTRACK", 0, "Damped Track Constraint", ""},
1098 {2, "TRACKTO", 0, "Track To Constraint", ""},
1099 {3, "LOCKTRACK", 0, "Lock Track Constraint", ""},
1100 {0, NULL, 0, NULL, NULL}
1103 static int track_set_exec(bContext *C, wmOperator *op)
1105 Main *bmain = CTX_data_main(C);
1106 Object *obact = ED_object_active_context(C);
1108 int type = RNA_enum_get(op->ptr, "type");
1112 bDampTrackConstraint *data;
1114 CTX_DATA_BEGIN (C, Object *, ob, selected_editable_objects)
1117 con = BKE_add_ob_constraint(ob, "AutoTrack", CONSTRAINT_TYPE_DAMPTRACK);
1121 DAG_id_tag_update(&ob->id, OB_RECALC_OB | OB_RECALC_DATA | OB_RECALC_TIME);
1123 /* Lamp, Camera and Speaker track differently by default */
1124 if (ob->type == OB_LAMP || ob->type == OB_CAMERA || ob->type == OB_SPEAKER)
1125 data->trackflag = TRACK_nZ;
1130 else if (type == 2) {
1132 bTrackToConstraint *data;
1134 CTX_DATA_BEGIN (C, Object *, ob, selected_editable_objects)
1137 con = BKE_add_ob_constraint(ob, "AutoTrack", CONSTRAINT_TYPE_TRACKTO);
1141 DAG_id_tag_update(&ob->id, OB_RECALC_OB | OB_RECALC_DATA | OB_RECALC_TIME);
1143 /* Lamp, Camera and Speaker track differently by default */
1144 if (ob->type == OB_LAMP || ob->type == OB_CAMERA || ob->type == OB_SPEAKER) {
1145 data->reserved1 = TRACK_nZ;
1146 data->reserved2 = UP_Y;
1152 else if (type == 3) {
1154 bLockTrackConstraint *data;
1156 CTX_DATA_BEGIN (C, Object *, ob, selected_editable_objects)
1159 con = BKE_add_ob_constraint(ob, "AutoTrack", CONSTRAINT_TYPE_LOCKTRACK);
1163 DAG_id_tag_update(&ob->id, OB_RECALC_OB | OB_RECALC_DATA | OB_RECALC_TIME);
1165 /* Lamp, Camera and Speaker track differently by default */
1166 if (ob->type == OB_LAMP || ob->type == OB_CAMERA || ob->type == OB_SPEAKER) {
1167 data->trackflag = TRACK_nZ;
1168 data->lockflag = LOCK_Y;
1175 DAG_relations_tag_update(bmain);
1176 WM_event_add_notifier(C, NC_OBJECT | ND_TRANSFORM, NULL);
1178 return OPERATOR_FINISHED;
1181 void OBJECT_OT_track_set(wmOperatorType *ot)
1184 ot->name = "Make Track";
1185 ot->description = "Make the object track another object, either by constraint or old way or locked track";
1186 ot->idname = "OBJECT_OT_track_set";
1189 ot->invoke = WM_menu_invoke;
1190 ot->exec = track_set_exec;
1192 ot->poll = ED_operator_objectmode;
1195 ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
1198 ot->prop = RNA_def_enum(ot->srna, "type", prop_make_track_types, 0, "Type", "");
1201 /************************** Move to Layer Operator *****************************/
1203 static unsigned int move_to_layer_init(bContext *C, wmOperator *op)
1206 unsigned int lay = 0;
1208 if (!RNA_struct_property_is_set(op->ptr, "layers")) {
1209 /* note: layers are set in bases, library objects work for this */
1210 CTX_DATA_BEGIN (C, Base *, base, selected_bases)
1216 for (a = 0; a < 20; a++)
1217 values[a] = (lay & (1 << a));
1219 RNA_boolean_set_array(op->ptr, "layers", values);
1222 RNA_boolean_get_array(op->ptr, "layers", values);
1224 for (a = 0; a < 20; a++)
1232 static int move_to_layer_invoke(bContext *C, wmOperator *op, const wmEvent *event)
1234 View3D *v3d = CTX_wm_view3d(C);
1235 if (v3d && v3d->localvd) {
1236 return WM_operator_confirm_message(C, op, "Move from localview");
1239 move_to_layer_init(C, op);
1240 return WM_operator_props_popup(C, op, event);
1244 static int move_to_layer_exec(bContext *C, wmOperator *op)
1246 Main *bmain = CTX_data_main(C);
1247 Scene *scene = CTX_data_scene(C);
1248 View3D *v3d = CTX_wm_view3d(C);
1249 unsigned int lay, local;
1250 /* int is_lamp = FALSE; */ /* UNUSED */
1252 lay = move_to_layer_init(C, op);
1255 if (lay == 0) return OPERATOR_CANCELLED;
1257 if (v3d && v3d->localvd) {
1258 /* now we can move out of localview. */
1259 /* note: layers are set in bases, library objects work for this */
1260 CTX_DATA_BEGIN (C, Base *, base, selected_bases)
1262 lay = base->lay & ~v3d->lay;
1264 base->object->lay = lay;
1265 base->object->flag &= ~SELECT;
1266 base->flag &= ~SELECT;
1267 /* if (base->object->type == OB_LAMP) is_lamp = TRUE; */
1272 /* normal non localview operation */
1273 /* note: layers are set in bases, library objects work for this */
1274 CTX_DATA_BEGIN (C, Base *, base, selected_bases)
1276 /* upper byte is used for local view */
1277 local = base->lay & 0xFF000000;
1278 base->lay = lay + local;
1279 base->object->lay = lay;
1280 /* if (base->object->type == OB_LAMP) is_lamp = TRUE; */
1285 /* warning, active object may be hidden now */
1287 WM_event_add_notifier(C, NC_OBJECT | ND_DRAW, scene);
1288 WM_event_add_notifier(C, NC_SCENE | ND_LAYER_CONTENT, scene);
1290 DAG_relations_tag_update(bmain);
1292 return OPERATOR_FINISHED;
1295 void OBJECT_OT_move_to_layer(wmOperatorType *ot)
1298 ot->name = "Move to Layer";
1299 ot->description = "Move the object to different layers";
1300 ot->idname = "OBJECT_OT_move_to_layer";
1303 ot->invoke = move_to_layer_invoke;
1304 ot->exec = move_to_layer_exec;
1305 ot->poll = ED_operator_objectmode;
1308 ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
1311 RNA_def_boolean_layer_member(ot->srna, "layers", 20, NULL, "Layer", "");
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 == 0) return;
1323 if (sce->id.lib) return;
1325 for (base = FIRSTBASE; base; base = base->next) {
1326 if (TESTBASE(v3d, base)) {
1328 nbase = MEM_mallocN(sizeof(Base), "newbase");
1330 BLI_addhead(&(sce->base), nbase);
1331 id_us_plus((ID *)base->object);
1337 Base *ED_object_scene_link(Scene *scene, Object *ob)
1341 if (BKE_scene_base_find(scene, ob)) {
1345 base = BKE_scene_base_add(scene, ob);
1346 id_us_plus(&ob->id);
1351 static int make_links_scene_exec(bContext *C, wmOperator *op)
1353 Scene *scene_to = BLI_findlink(&CTX_data_main(C)->scene, RNA_enum_get(op->ptr, "scene"));
1355 if (scene_to == NULL) {
1356 BKE_report(op->reports, RPT_ERROR, "Could not find scene");
1357 return OPERATOR_CANCELLED;
1360 if (scene_to == CTX_data_scene(C)) {
1361 BKE_report(op->reports, RPT_ERROR, "Cannot link objects into the same scene");
1362 return OPERATOR_CANCELLED;
1365 if (scene_to->id.lib) {
1366 BKE_report(op->reports, RPT_ERROR, "Cannot link objects into a linked scene");
1367 return OPERATOR_CANCELLED;
1370 CTX_DATA_BEGIN (C, Base *, base, selected_bases)
1372 ED_object_scene_link(scene_to, base->object);
1376 /* redraw the 3D view because the object center points are colored differently */
1377 WM_event_add_notifier(C, NC_OBJECT | ND_DRAW, NULL);
1379 /* one day multiple scenes will be visible, then we should have some update function for them */
1380 return OPERATOR_FINISHED;
1384 MAKE_LINKS_OBDATA = 1,
1385 MAKE_LINKS_MATERIALS,
1386 MAKE_LINKS_ANIMDATA,
1388 MAKE_LINKS_DUPLIGROUP,
1389 MAKE_LINKS_MODIFIERS,
1393 /* Return 1 if make link data is allow, zero otherwise */
1394 static int allow_make_links_data(const int type, Object *ob_src, Object *ob_dst)
1397 case MAKE_LINKS_OBDATA:
1398 if (ob_src->type == ob_dst->type && ob_src->type != OB_EMPTY)
1401 case MAKE_LINKS_MATERIALS:
1402 if (OB_TYPE_SUPPORT_MATERIAL(ob_src->type) &&
1403 OB_TYPE_SUPPORT_MATERIAL(ob_dst->type))
1408 case MAKE_LINKS_ANIMDATA:
1409 case MAKE_LINKS_GROUP:
1410 case MAKE_LINKS_DUPLIGROUP:
1412 case MAKE_LINKS_MODIFIERS:
1413 if (ob_src->type != OB_EMPTY && ob_dst->type != OB_EMPTY)
1416 case MAKE_LINKS_FONTS:
1417 if ((ob_src->data != ob_dst->data) &&
1418 (ob_src->type == OB_FONT) &&
1419 (ob_dst->type == OB_FONT))
1428 static int make_links_data_exec(bContext *C, wmOperator *op)
1430 Main *bmain = CTX_data_main(C);
1431 Scene *scene = CTX_data_scene(C);
1432 const int type = RNA_enum_get(op->ptr, "type");
1438 LinkNode *ob_groups = NULL;
1439 int is_cycle = FALSE;
1441 ob_src = ED_object_active_context(C);
1443 /* avoid searching all groups in source object each time */
1444 if (type == MAKE_LINKS_GROUP) {
1445 ob_groups = BKE_object_groups(ob_src);
1448 CTX_DATA_BEGIN (C, Base *, base_dst, selected_editable_bases)
1450 Object *ob_dst = base_dst->object;
1452 if (ob_src != ob_dst) {
1453 if (allow_make_links_data(type, ob_src, ob_dst)) {
1455 case MAKE_LINKS_OBDATA: /* obdata */
1463 /* if amount of material indices changed: */
1464 test_object_materials(ob_dst->data);
1466 DAG_id_tag_update(&ob_dst->id, OB_RECALC_DATA);
1468 case MAKE_LINKS_MATERIALS:
1469 /* new approach, using functions from kernel */
1470 for (a = 0; a < ob_src->totcol; a++) {
1471 Material *ma = give_current_material(ob_src, a + 1);
1472 assign_material(ob_dst, ma, a + 1, BKE_MAT_ASSIGN_USERPREF); /* also works with ma==NULL */
1475 case MAKE_LINKS_ANIMDATA:
1476 BKE_copy_animdata_id((ID *)ob_dst, (ID *)ob_src, FALSE);
1477 BKE_copy_animdata_id((ID *)ob_dst->data, (ID *)ob_src->data, FALSE);
1479 case MAKE_LINKS_GROUP:
1481 LinkNode *group_node;
1483 /* first clear groups */
1484 BKE_object_groups_clear(scene, base_dst, ob_dst);
1486 /* now add in the groups from the link nodes */
1487 for (group_node = ob_groups; group_node; group_node = group_node->next) {
1488 if (ob_dst->dup_group != group_node->link) {
1489 add_to_group(group_node->link, ob_dst, scene, base_dst);
1496 case MAKE_LINKS_DUPLIGROUP:
1497 ob_dst->dup_group = ob_src->dup_group;
1498 if (ob_dst->dup_group) {
1499 id_lib_extern(&ob_dst->dup_group->id);
1500 ob_dst->transflag |= OB_DUPLIGROUP;
1503 case MAKE_LINKS_MODIFIERS:
1504 BKE_object_link_modifiers(ob_dst, ob_src);
1505 DAG_id_tag_update(&ob_dst->id, OB_RECALC_OB | OB_RECALC_DATA | OB_RECALC_TIME);
1507 case MAKE_LINKS_FONTS:
1509 Curve *cu_src = ob_src->data;
1510 Curve *cu_dst = ob_dst->data;
1512 if (cu_dst->vfont) cu_dst->vfont->id.us--;
1513 cu_dst->vfont = cu_src->vfont;
1514 id_us_plus((ID *)cu_dst->vfont);
1515 if (cu_dst->vfontb) cu_dst->vfontb->id.us--;
1516 cu_dst->vfontb = cu_src->vfontb;
1517 id_us_plus((ID *)cu_dst->vfontb);
1518 if (cu_dst->vfonti) cu_dst->vfonti->id.us--;
1519 cu_dst->vfonti = cu_src->vfonti;
1520 id_us_plus((ID *)cu_dst->vfonti);
1521 if (cu_dst->vfontbi) cu_dst->vfontbi->id.us--;
1522 cu_dst->vfontbi = cu_src->vfontbi;
1523 id_us_plus((ID *)cu_dst->vfontbi);
1525 DAG_id_tag_update(&ob_dst->id, OB_RECALC_OB | OB_RECALC_DATA | OB_RECALC_TIME);
1534 if (type == MAKE_LINKS_GROUP) {
1536 BLI_linklist_free(ob_groups, NULL);
1540 BKE_report(op->reports, RPT_WARNING, "Skipped some groups because of cycle detected");
1544 DAG_relations_tag_update(bmain);
1545 WM_event_add_notifier(C, NC_SPACE | ND_SPACE_VIEW3D, CTX_wm_view3d(C));
1546 WM_event_add_notifier(C, NC_OBJECT, NULL);
1548 return OPERATOR_FINISHED;
1552 void OBJECT_OT_make_links_scene(wmOperatorType *ot)
1557 ot->name = "Link Objects to Scene";
1558 ot->description = "Link selection to another scene";
1559 ot->idname = "OBJECT_OT_make_links_scene";
1562 ot->invoke = WM_enum_search_invoke;
1563 ot->exec = make_links_scene_exec;
1564 /* better not run the poll check */
1567 ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
1570 prop = RNA_def_enum(ot->srna, "scene", DummyRNA_NULL_items, 0, "Scene", "");
1571 RNA_def_enum_funcs(prop, RNA_scene_local_itemf);
1575 void OBJECT_OT_make_links_data(wmOperatorType *ot)
1577 static EnumPropertyItem make_links_items[] = {
1578 {MAKE_LINKS_OBDATA, "OBDATA", 0, "Object Data", ""},
1579 {MAKE_LINKS_MATERIALS, "MATERIAL", 0, "Materials", ""},
1580 {MAKE_LINKS_ANIMDATA, "ANIMATION", 0, "Animation Data", ""},
1581 {MAKE_LINKS_GROUP, "GROUPS", 0, "Group", ""},
1582 {MAKE_LINKS_DUPLIGROUP, "DUPLIGROUP", 0, "DupliGroup", ""},
1583 {MAKE_LINKS_MODIFIERS, "MODIFIERS", 0, "Modifiers", ""},
1584 {MAKE_LINKS_FONTS, "FONTS", 0, "Fonts", ""},
1585 {0, NULL, 0, NULL, NULL}};
1588 ot->name = "Link Data";
1589 ot->description = "Make links from the active object to other selected objects";
1590 ot->idname = "OBJECT_OT_make_links_data";
1593 ot->exec = make_links_data_exec;
1594 ot->poll = ED_operator_object_active;
1597 ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
1600 ot->prop = RNA_def_enum(ot->srna, "type", make_links_items, 0, "Type", "");
1604 /**************************** Make Single User ********************************/
1606 static void single_object_users(Scene *scene, View3D *v3d, int flag)
1611 clear_sca_new_poins(); /* sensor/contr/act */
1613 /* duplicate (must set newid) */
1614 for (base = FIRSTBASE; base; base = base->next) {
1617 /* newid may still have some trash from Outliner tree building,
1618 * so clear that first to avoid errors [#26002]
1620 ob->id.newid = NULL;
1622 if ((base->flag & flag) == flag) {
1623 if (ob->id.lib == NULL && ob->id.us > 1) {
1624 /* base gets copy of object */
1625 obn = BKE_object_copy(ob);
1632 ID_NEW(scene->camera);
1633 if (v3d) ID_NEW(v3d->camera);
1635 /* object pointers */
1636 for (base = FIRSTBASE; base; base = base->next) {
1637 BKE_object_relink(base->object);
1640 set_sca_new_poins();
1643 /* not an especially efficient function, only added so the single user
1644 * button can be functional.*/
1645 void ED_object_single_user(Scene *scene, Object *ob)
1649 for (base = FIRSTBASE; base; base = base->next) {
1650 if (base->object == ob) base->flag |= OB_DONE;
1651 else base->flag &= ~OB_DONE;
1654 single_object_users(scene, NULL, OB_DONE);
1657 static void new_id_matar(Material **matar, int totcol)
1662 for (a = 0; a < totcol; a++) {
1663 id = (ID *)matar[a];
1664 if (id && id->lib == NULL) {
1666 matar[a] = (Material *)id->newid;
1667 id_us_plus(id->newid);
1670 else if (id->us > 1) {
1671 matar[a] = BKE_material_copy(matar[a]);
1673 id->newid = (ID *)matar[a];
1679 static void single_obdata_users(Main *bmain, Scene *scene, int flag)
1690 for (base = FIRSTBASE; base; base = base->next) {
1692 if (ob->id.lib == NULL && (base->flag & flag) == flag) {
1695 if (id && id->us > 1 && id->lib == NULL) {
1696 DAG_id_tag_update(&ob->id, OB_RECALC_DATA);
1698 BKE_copy_animdata_id_action(id);
1702 ob->data = la = BKE_lamp_copy(ob->data);
1703 for (a = 0; a < MAX_MTEX; a++) {
1705 ID_NEW(la->mtex[a]->object);
1710 ob->data = BKE_camera_copy(ob->data);
1713 ob->data = BKE_mesh_copy(ob->data);
1715 //if (me && me->key)
1716 // ipo_idnew(me->key->ipo); /* drivers */
1719 ob->data = BKE_mball_copy(ob->data);
1724 ob->data = cu = BKE_curve_copy(ob->data);
1726 ID_NEW(cu->taperobj);
1729 ob->data = BKE_lattice_copy(ob->data);
1732 DAG_id_tag_update(&ob->id, OB_RECALC_DATA);
1733 ob->data = BKE_armature_copy(ob->data);
1734 BKE_pose_rebuild(ob, ob->data);
1737 ob->data = BKE_speaker_copy(ob->data);
1740 if (G.debug & G_DEBUG)
1741 printf("ERROR %s: can't copy %s\n", __func__, id->name);
1746 id->newid = ob->data;
1753 me = bmain->mesh.first;
1755 ID_NEW(me->texcomesh);
1760 static void single_object_action_users(Scene *scene, int flag)
1765 for (base = FIRSTBASE; base; base = base->next) {
1767 if (ob->id.lib == NULL && (flag == 0 || (base->flag & SELECT)) ) {
1768 DAG_id_tag_update(&ob->id, OB_RECALC_DATA);
1769 BKE_copy_animdata_id_action(&ob->id);
1774 static void single_mat_users(Scene *scene, int flag, int do_textures)
1782 for (base = FIRSTBASE; base; base = base->next) {
1784 if (ob->id.lib == NULL && (flag == 0 || (base->flag & SELECT)) ) {
1786 for (a = 1; a <= ob->totcol; a++) {
1787 ma = give_current_material(ob, a);
1789 /* do not test for LIB_NEW: this functions guaranteed delivers single_users! */
1791 if (ma->id.us > 1) {
1792 man = BKE_material_copy(ma);
1793 BKE_copy_animdata_id_action(&man->id);
1796 assign_material(ob, man, a, BKE_MAT_ASSIGN_USERPREF);
1799 for (b = 0; b < MAX_MTEX; b++) {
1800 if (ma->mtex[b] && (tex = ma->mtex[b]->tex)) {
1801 if (tex->id.us > 1) {
1803 tex = BKE_texture_copy(tex);
1804 BKE_copy_animdata_id_action(&tex->id);
1805 man->mtex[b]->tex = tex;
1817 static void do_single_tex_user(Tex **from)
1822 if (tex == NULL) return;
1824 if (tex->id.newid) {
1825 *from = (Tex *)tex->id.newid;
1826 id_us_plus(tex->id.newid);
1829 else if (tex->id.us > 1) {
1830 texn = BKE_texture_copy(tex);
1831 BKE_copy_animdata_id_action(&texn->id);
1832 tex->id.newid = (ID *)texn;
1838 static void single_tex_users_expand(Main *bmain)
1840 /* only when 'parent' blocks are LIB_NEW */
1846 for (ma = bmain->mat.first; ma; ma = ma->id.next) {
1847 if (ma->id.flag & LIB_NEW) {
1848 for (b = 0; b < MAX_MTEX; b++) {
1849 if (ma->mtex[b] && ma->mtex[b]->tex) {
1850 do_single_tex_user(&(ma->mtex[b]->tex));
1856 for (la = bmain->lamp.first; la; la = la->id.next) {
1857 if (la->id.flag & LIB_NEW) {
1858 for (b = 0; b < MAX_MTEX; b++) {
1859 if (la->mtex[b] && la->mtex[b]->tex) {
1860 do_single_tex_user(&(la->mtex[b]->tex));
1866 for (wo = bmain->world.first; wo; wo = wo->id.next) {
1867 if (wo->id.flag & LIB_NEW) {
1868 for (b = 0; b < MAX_MTEX; b++) {
1869 if (wo->mtex[b] && wo->mtex[b]->tex) {
1870 do_single_tex_user(&(wo->mtex[b]->tex));
1877 static void single_mat_users_expand(Main *bmain)
1879 /* only when 'parent' blocks are LIB_NEW */
1887 for (ob = bmain->object.first; ob; ob = ob->id.next)
1888 if (ob->id.flag & LIB_NEW)
1889 new_id_matar(ob->mat, ob->totcol);
1891 for (me = bmain->mesh.first; me; me = me->id.next)
1892 if (me->id.flag & LIB_NEW)
1893 new_id_matar(me->mat, me->totcol);
1895 for (cu = bmain->curve.first; cu; cu = cu->id.next)
1896 if (cu->id.flag & LIB_NEW)
1897 new_id_matar(cu->mat, cu->totcol);
1899 for (mb = bmain->mball.first; mb; mb = mb->id.next)
1900 if (mb->id.flag & LIB_NEW)
1901 new_id_matar(mb->mat, mb->totcol);
1903 /* material imats */
1904 for (ma = bmain->mat.first; ma; ma = ma->id.next)
1905 if (ma->id.flag & LIB_NEW)
1906 for (a = 0; a < MAX_MTEX; a++)
1908 ID_NEW(ma->mtex[a]->object);
1911 /* used for copying scenes */
1912 void ED_object_single_users(Main *bmain, Scene *scene, int full)
1914 single_object_users(scene, NULL, 0);
1917 single_obdata_users(bmain, scene, 0);
1918 single_object_action_users(scene, 0);
1919 single_mat_users_expand(bmain);
1920 single_tex_users_expand(bmain);
1923 clear_id_newpoins();
1926 /******************************* Make Local ***********************************/
1928 /* helper for below, ma was checked to be not NULL */
1929 static void make_local_makelocalmaterial(Material *ma)
1934 id_make_local(&ma->id, false);
1936 for (b = 0; b < MAX_MTEX; b++)
1937 if (ma->mtex[b] && ma->mtex[b]->tex)
1938 id_make_local(&ma->mtex[b]->tex->id, false);
1940 adt = BKE_animdata_from_id(&ma->id);
1941 if (adt) BKE_animdata_make_local(adt);
1947 MAKE_LOCAL_SELECT_OB,
1948 MAKE_LOCAL_SELECT_OBDATA,
1949 MAKE_LOCAL_SELECT_OBDATA_MATERIAL,
1953 static int make_local_exec(bContext *C, wmOperator *op)
1955 Main *bmain = CTX_data_main(C);
1957 ParticleSystem *psys;
1958 Material *ma, ***matarar;
1961 int a, b, mode = RNA_enum_get(op->ptr, "type");
1963 if (mode == MAKE_LOCAL_ALL) {
1964 BKE_library_make_local(bmain, NULL, false); /* NULL is all libs */
1965 WM_event_add_notifier(C, NC_WINDOW, NULL);
1966 return OPERATOR_FINISHED;
1969 clear_id_newpoins();
1971 CTX_DATA_BEGIN (C, Object *, ob, selected_objects)
1974 id_make_local(&ob->id, false);
1978 /* maybe object pointers */
1979 CTX_DATA_BEGIN (C, Object *, ob, selected_objects)
1981 if (ob->id.lib == NULL) {
1987 CTX_DATA_BEGIN (C, Object *, ob, selected_objects)
1991 if (id && (ELEM(mode, MAKE_LOCAL_SELECT_OBDATA, MAKE_LOCAL_SELECT_OBDATA_MATERIAL))) {
1992 id_make_local(id, false);
1993 adt = BKE_animdata_from_id(id);
1994 if (adt) BKE_animdata_make_local(adt);
1996 /* tag indirect data direct */
1997 matarar = (Material ***)give_matarar(ob);
1999 for (a = 0; a < ob->totcol; a++) {
2002 id_lib_extern(&ma->id);
2007 for (psys = ob->particlesystem.first; psys; psys = psys->next)
2008 id_make_local(&psys->part->id, false);
2010 adt = BKE_animdata_from_id(&ob->id);
2011 if (adt) BKE_animdata_make_local(adt);
2015 if (mode == MAKE_LOCAL_SELECT_OBDATA_MATERIAL) {
2016 CTX_DATA_BEGIN (C, Object *, ob, selected_objects)
2018 if (ob->type == OB_LAMP) {
2021 for (b = 0; b < MAX_MTEX; b++)
2022 if (la->mtex[b] && la->mtex[b]->tex)
2023 id_make_local(&la->mtex[b]->tex->id, false);
2026 for (a = 0; a < ob->totcol; a++) {
2029 make_local_makelocalmaterial(ma);
2032 matarar = (Material ***)give_matarar(ob);
2034 for (a = 0; a < ob->totcol; a++) {
2037 make_local_makelocalmaterial(ma);
2045 WM_event_add_notifier(C, NC_WINDOW, NULL);
2047 return OPERATOR_FINISHED;
2050 void OBJECT_OT_make_local(wmOperatorType *ot)
2052 static EnumPropertyItem type_items[] = {
2053 {MAKE_LOCAL_SELECT_OB, "SELECT_OBJECT", 0, "Selected Objects", ""},
2054 {MAKE_LOCAL_SELECT_OBDATA, "SELECT_OBDATA", 0, "Selected Objects and Data", ""},
2055 {MAKE_LOCAL_SELECT_OBDATA_MATERIAL, "SELECT_OBDATA_MATERIAL", 0, "Selected Objects, Data and Materials", ""},
2056 {MAKE_LOCAL_ALL, "ALL", 0, "All", ""},
2057 {0, NULL, 0, NULL, NULL}
2061 ot->name = "Make Local";
2062 ot->description = "Make library linked datablocks local to this file";
2063 ot->idname = "OBJECT_OT_make_local";
2066 ot->invoke = WM_menu_invoke;
2067 ot->exec = make_local_exec;
2068 ot->poll = ED_operator_objectmode;
2071 ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
2074 ot->prop = RNA_def_enum(ot->srna, "type", type_items, 0, "Type", "");
2077 static int make_single_user_exec(bContext *C, wmOperator *op)
2079 Main *bmain = CTX_data_main(C);
2080 Scene *scene = CTX_data_scene(C);
2081 View3D *v3d = CTX_wm_view3d(C); /* ok if this is NULL */
2082 int flag = RNA_enum_get(op->ptr, "type"); /* 0==ALL, SELECTED==selected objecs */
2084 if (RNA_boolean_get(op->ptr, "object"))
2085 single_object_users(scene, v3d, flag);
2087 if (RNA_boolean_get(op->ptr, "obdata"))
2088 single_obdata_users(bmain, scene, flag);
2090 if (RNA_boolean_get(op->ptr, "material"))
2091 single_mat_users(scene, flag, RNA_boolean_get(op->ptr, "texture"));
2093 #if 0 /* can't do this separate from materials */
2094 if (RNA_boolean_get(op->ptr, "texture"))
2095 single_mat_users(scene, flag, TRUE);
2097 if (RNA_boolean_get(op->ptr, "animation"))
2098 single_object_action_users(scene, flag);
2100 clear_id_newpoins();
2102 WM_event_add_notifier(C, NC_WINDOW, NULL);
2103 return OPERATOR_FINISHED;
2106 void OBJECT_OT_make_single_user(wmOperatorType *ot)
2108 static EnumPropertyItem type_items[] = {
2109 {SELECT, "SELECTED_OBJECTS", 0, "Selected Objects", ""},
2110 {0, "ALL", 0, "All", ""},
2111 {0, NULL, 0, NULL, NULL}};
2114 ot->name = "Make Single User";
2115 ot->description = "Make linked data local to each object";
2116 ot->idname = "OBJECT_OT_make_single_user";
2119 ot->invoke = WM_menu_invoke;
2120 ot->exec = make_single_user_exec;
2121 ot->poll = ED_operator_objectmode;
2124 ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
2127 ot->prop = RNA_def_enum(ot->srna, "type", type_items, SELECT, "Type", "");
2129 RNA_def_boolean(ot->srna, "object", 0, "Object", "Make single user objects");
2130 RNA_def_boolean(ot->srna, "obdata", 0, "Object Data", "Make single user object data");
2131 RNA_def_boolean(ot->srna, "material", 0, "Materials", "Make materials local to each datablock");
2132 RNA_def_boolean(ot->srna, "texture", 0, "Textures", "Make textures local to each material");
2133 RNA_def_boolean(ot->srna, "animation", 0, "Object Animation", "Make animation data local to each object");
2136 static int drop_named_material_invoke(bContext *C, wmOperator *op, const wmEvent *event)
2138 Base *base = ED_view3d_give_base_under_cursor(C, event->mval);
2140 char name[MAX_ID_NAME - 2];
2142 RNA_string_get(op->ptr, "name", name);
2143 ma = (Material *)BKE_libblock_find_name(ID_MA, name);
2144 if (base == NULL || ma == NULL)
2145 return OPERATOR_CANCELLED;
2147 assign_material(base->object, ma, 1, BKE_MAT_ASSIGN_USERPREF);
2149 WM_event_add_notifier(C, NC_SPACE | ND_SPACE_VIEW3D, CTX_wm_view3d(C));
2150 WM_event_add_notifier(C, NC_MATERIAL | ND_SHADING_LINKS, ma);
2152 return OPERATOR_FINISHED;
2155 /* used for dropbox */
2156 /* assigns to object under cursor, only first material slot */
2157 void OBJECT_OT_drop_named_material(wmOperatorType *ot)
2161 ot->name = "Drop Named Material on Object";
2162 ot->description = "";
2163 ot->idname = "OBJECT_OT_drop_named_material";
2166 ot->invoke = drop_named_material_invoke;
2167 ot->poll = ED_operator_objectmode;
2170 ot->flag = OPTYPE_UNDO | OPTYPE_INTERNAL;
2173 RNA_def_string(ot->srna, "name", "Material", MAX_ID_NAME - 2, "Name", "Material name to assign");