From 64a584b38a73d4745e02620051592c5b05439647 Mon Sep 17 00:00:00 2001 From: Alexander Gavrilov Date: Wed, 24 Jun 2020 22:10:43 +0300 Subject: Python API: add methods to allow copying of constraints. Blender has an operator to do Copy & Paste of constraints between objects and bones, but no simple method to do that directly via the Python API is provided. This adds a copy() method to object and pose bone constraint collections. Differential Revision: https://developer.blender.org/D8112 --- source/blender/blenkernel/intern/constraint.c | 45 +++++++++++++++++++++------ 1 file changed, 36 insertions(+), 9 deletions(-) (limited to 'source/blender/blenkernel/intern/constraint.c') diff --git a/source/blender/blenkernel/intern/constraint.c b/source/blender/blenkernel/intern/constraint.c index c5b70cf6924..74e3cc567ec 100644 --- a/source/blender/blenkernel/intern/constraint.c +++ b/source/blender/blenkernel/intern/constraint.c @@ -5448,18 +5448,11 @@ static bConstraint *add_new_constraint_internal(const char *name, short type) return con; } -/* if pchan is not NULL then assume we're adding a pose constraint */ -static bConstraint *add_new_constraint(Object *ob, - bPoseChannel *pchan, - const char *name, - short type) +/* Add a newly created constraint to the constraint list. */ +static void add_new_constraint_to_list(Object *ob, bPoseChannel *pchan, bConstraint *con) { - bConstraint *con; ListBase *list; - /* add the constraint */ - con = add_new_constraint_internal(name, type); - /* find the constraint stack - bone or object? */ list = (pchan) ? (&pchan->constraints) : (&ob->constraints); @@ -5481,6 +5474,20 @@ static bConstraint *add_new_constraint(Object *ob, /* make this constraint the active one */ BKE_constraints_active_set(list, con); } +} + +/* if pchan is not NULL then assume we're adding a pose constraint */ +static bConstraint *add_new_constraint(Object *ob, + bPoseChannel *pchan, + const char *name, + short type) +{ + bConstraint *con; + + /* add the constraint */ + con = add_new_constraint_internal(name, type); + + add_new_constraint_to_list(ob, pchan, con); /* set type+owner specific immutable settings */ /* TODO: does action constraint need anything here - i.e. spaceonce? */ @@ -5614,6 +5621,26 @@ bConstraint *BKE_constraint_duplicate_ex(bConstraint *src, const int flag, const return dst; } +/* Add a copy of the given constraint for the given bone */ +bConstraint *BKE_constraint_copy_for_pose(Object *ob, bPoseChannel *pchan, bConstraint *src) +{ + if (pchan == NULL) { + return NULL; + } + + bConstraint *new_con = BKE_constraint_duplicate_ex(src, 0, !ID_IS_LINKED(ob)); + add_new_constraint_to_list(ob, pchan, new_con); + return new_con; +} + +/* Add a copy of the given constraint for the given object */ +bConstraint *BKE_constraint_copy_for_object(Object *ob, bConstraint *src) +{ + bConstraint *new_con = BKE_constraint_duplicate_ex(src, 0, !ID_IS_LINKED(ob)); + add_new_constraint_to_list(ob, NULL, new_con); + return new_con; +} + /* duplicate all of the constraints in a constraint stack */ void BKE_constraints_copy_ex(ListBase *dst, const ListBase *src, const int flag, bool do_extern) { -- cgit v1.2.3