From a12c2a85610e28248effd019ae46518da22dac30 Mon Sep 17 00:00:00 2001 From: Joshua Leung Date: Mon, 16 Nov 2009 12:33:42 +0000 Subject: Constraints: Code cleanup * Removing duplicate api functions * Shuffled around newly added api functions to make the ordering more consistent * Fixes for a few bugs in the api functions as I checked over them * Replaced most of the #defines for flags and modes with enums --- source/blender/blenkernel/BKE_constraint.h | 19 +-- source/blender/blenkernel/intern/constraint.c | 234 +++++++++++++------------- source/blender/blenkernel/intern/fmodifier.c | 12 +- 3 files changed, 131 insertions(+), 134 deletions(-) (limited to 'source/blender/blenkernel') diff --git a/source/blender/blenkernel/BKE_constraint.h b/source/blender/blenkernel/BKE_constraint.h index 7bb17d9a0e0..a8ccc84a7c3 100644 --- a/source/blender/blenkernel/BKE_constraint.h +++ b/source/blender/blenkernel/BKE_constraint.h @@ -102,23 +102,12 @@ typedef struct bConstraintTypeInfo { bConstraintTypeInfo *constraint_get_typeinfo(struct bConstraint *con); bConstraintTypeInfo *get_constraint_typeinfo(int type); -struct bConstraint *add_ob_constraint(struct Object *ob, const char *name, short type); -struct bConstraint *add_pose_constraint(struct Object *ob, struct bPoseChannel *pchan, const char *name, short type); - -struct bConstraint *find_active_constraint(ListBase *constraints); -void set_active_constraint(ListBase *constraints, struct bConstraint *con); - - -int remove_constraint(ListBase *constraints, struct bConstraint *con); -int remove_constraint_index(ListBase *constraints, int index); - /* ---------------------------------------------------------------------------- */ /* Useful macros for testing various common flag combinations */ /* Constraint Target Macros */ #define VALID_CONS_TARGET(ct) ((ct) && (ct->tar)) - /* ---------------------------------------------------------------------------- */ /* Constraint function prototypes */ @@ -129,7 +118,15 @@ void copy_constraints(struct ListBase *dst, struct ListBase *src); void relink_constraints(struct ListBase *list); void free_constraint_data(struct bConstraint *con); +/* Constraint API function prototypes */ struct bConstraint *constraints_get_active(struct ListBase *list); +void constraints_set_active(ListBase *list, struct bConstraint *con); + +struct bConstraint *add_ob_constraint(struct Object *ob, const char *name, short type); +struct bConstraint *add_pose_constraint(struct Object *ob, struct bPoseChannel *pchan, const char *name, short type); + +int remove_constraint(ListBase *list, struct bConstraint *con); +int remove_constraint_index(ListBase *list, int index); /* Constraints + Proxies function prototypes */ void extract_proxylocal_constraints(struct ListBase *dst, struct ListBase *src); diff --git a/source/blender/blenkernel/intern/constraint.c b/source/blender/blenkernel/intern/constraint.c index 5015420000d..46fdec90528 100644 --- a/source/blender/blenkernel/intern/constraint.c +++ b/source/blender/blenkernel/intern/constraint.c @@ -3605,171 +3605,155 @@ bConstraintTypeInfo *constraint_get_typeinfo (bConstraint *con) return NULL; } -/* Creates a new constraint, initialises its data, and returns it */ -static bConstraint *add_new_constraint_internal(const char *name, short type) +/* ************************* General Constraints API ************************** */ +/* The functions here are called by various parts of Blender. Very few (should be none if possible) + * constraint-specific code should occur here. + */ + +/* ---------- Data Management ------- */ + +/* Free data of a specific constraint if it has any info */ +void free_constraint_data (bConstraint *con) +{ + if (con->data) { + bConstraintTypeInfo *cti= constraint_get_typeinfo(con); + + /* perform any special freeing constraint may have */ + if (cti && cti->free_data) + cti->free_data(con); + + /* free constraint data now */ + MEM_freeN(con->data); + } +} + +/* Free all constraints from a constraint-stack */ +void free_constraints (ListBase *list) { bConstraint *con; - bConstraintTypeInfo *cti; + + /* Free constraint data and also any extra data */ + for (con= list->first; con; con= con->next) + free_constraint_data(con); + + /* Free the whole list */ + BLI_freelistN(list); +} - con = MEM_callocN(sizeof(bConstraint), "Constraint"); + +/* Remove the specified constraint from the given constraint stack */ +int remove_constraint (ListBase *list, bConstraint *con) +{ + if (con) { + free_constraint_data(con); + BLI_freelinkN(list, con); + return 1; + } + else + return 0; +} + +/* Remove the nth constraint from the given constraint stack */ +int remove_constraint_index (ListBase *list, int index) +{ + bConstraint *con= BLI_findlink(list, index); + + if (con) + return remove_constraint(list, con); + else + return 0; +} + +/* ......... */ + +/* Creates a new constraint, initialises its data, and returns it */ +static bConstraint *add_new_constraint_internal (const char *name, short type) +{ + bConstraint *con= MEM_callocN(sizeof(bConstraint), "Constraint"); + bConstraintTypeInfo *cti= get_constraint_typeinfo(type); + const char *newName; /* Set up a generic constraint datablock */ con->type = type; con->flag |= CONSTRAINT_EXPAND; con->enforce = 1.0f; - /* Load the data for it */ - cti = constraint_get_typeinfo(con); + /* Determine a basic name, and info */ if (cti) { + /* initialise constraint data */ con->data = MEM_callocN(cti->size, cti->structName); - + /* only constraints that change any settings need this */ if (cti->new_data) cti->new_data(con->data); - - /* set the name based on the type of constraint */ - name= name ? name : cti->name; + + /* if no name is provided, use the type of the constraint as the name */ + newName= (name && name[0]) ? name : cti->name; } - else - name= name ? name : "Const"; - - strcpy(con->name, name); - + else { + /* if no name is provided, use the generic "Const" name */ + // NOTE: any constraint type that gets here really shouldn't get added... + newName= (name && name[0]) ? name : "Const"; + } + + /* copy the name */ + BLI_strncpy(con->name, newName, sizeof(con->name)); + + /* return the new constraint */ 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) +static bConstraint *add_new_constraint (Object *ob, bPoseChannel *pchan, const char *name, short type) { bConstraint *con; ListBase *list; - + + /* add the constraint */ con= add_new_constraint_internal(name, type); - - if(pchan) list= &pchan->constraints; - else list= &ob->constraints; - + + /* find the constraint stack - bone or object? */ + list = (pchan) ? (&pchan->constraints) : (&ob->constraints); + if (list) { - bConstraint *coniter; - /* add new constraint to end of list of constraints before ensuring that it has a unique name * (otherwise unique-naming code will fail, since it assumes element exists in list) */ BLI_addtail(list, con); unique_constraint_name(con, list); - + /* if the target list is a list on some PoseChannel belonging to a proxy-protected * Armature layer, we must tag newly added constraints with a flag which allows them * to persist after proxy syncing has been done */ if (proxylocked_constraints_owner(ob, pchan)) con->flag |= CONSTRAINT_PROXY_LOCAL; - - /* make this constraint the active one - * - since constraint was added at end of stack, we can just go - * through deactivating all previous ones - */ - con->flag |= CONSTRAINT_ACTIVE; - for (coniter= con->prev; coniter; coniter= coniter->prev) - coniter->flag &= ~CONSTRAINT_ACTIVE; + + /* make this constraint the active one */ + constraints_set_active(list, con); } - + return con; } -bConstraint *add_pose_constraint(Object *ob, bPoseChannel *pchan, const char *name, short type) +/* ......... */ + +/* Add new constraint for the given bone */ +bConstraint *add_pose_constraint (Object *ob, bPoseChannel *pchan, const char *name, short type) { - if(pchan==NULL) + if (pchan == NULL) return NULL; - + return add_new_constraint(ob, pchan, name, type); } +/* Add new constraint for the given object */ bConstraint *add_ob_constraint(Object *ob, const char *name, short type) { return add_new_constraint(ob, NULL, name, type); } -struct bConstraint *find_active_constraint(ListBase *constraints) -{ - bConstraint *con; - if (constraints==NULL) - return NULL; - - for(con= constraints->first; con; con= con->next) { - if(con->flag & CONSTRAINT_ACTIVE) - return con; - } - - return NULL; -} - -void set_active_constraint(ListBase *constraints, struct bConstraint *con) -{ - bConstraint *con_i; - for(con_i= constraints->first; con_i; con_i= con_i->next) { - if(con_i==con) con->flag |= CONSTRAINT_ACTIVE; - else con->flag &= ~CONSTRAINT_ACTIVE; - } -} - -int remove_constraint(ListBase *constraints, struct bConstraint *con) -{ - if(con) { - free_constraint_data(con); - BLI_freelinkN(constraints, con); - return 1; - } - else { - return 0; - } -} - -int remove_constraint_index(ListBase *constraints, int index) -{ - bConstraint *con= BLI_findlink(constraints, index); - if(con) { - return remove_constraint(constraints, con); - } - else { - return 0; - } -} - -/* ************************* General Constraints API ************************** */ -/* The functions here are called by various parts of Blender. Very few (should be none if possible) - * constraint-specific code should occur here. - */ - -/* ---------- Data Management ------- */ - -/* Free data of a specific constraint if it has any info */ -void free_constraint_data (bConstraint *con) -{ - if (con->data) { - bConstraintTypeInfo *cti= constraint_get_typeinfo(con); - - /* perform any special freeing constraint may have */ - if (cti && cti->free_data) - cti->free_data(con); - - /* free constraint data now */ - MEM_freeN(con->data); - } -} - -/* Free all constraints from a constraint-stack */ -void free_constraints (ListBase *list) -{ - bConstraint *con; - - /* Free constraint data and also any extra data */ - for (con= list->first; con; con= con->next) - free_constraint_data(con); - - /* Free the whole list */ - BLI_freelistN(list); -} +/* ......... */ /* Reassign links that constraints have to other data (called during file loading?) */ void relink_constraints (ListBase *conlist) @@ -3801,6 +3785,8 @@ void relink_constraints (ListBase *conlist) } } +/* ......... */ + /* duplicate all of the constraints in a constraint stack */ void copy_constraints (ListBase *dst, ListBase *src) { @@ -3815,6 +3801,7 @@ void copy_constraints (ListBase *dst, ListBase *src) /* make a new copy of the constraint's data */ con->data = MEM_dupallocN(con->data); + // NOTE: depreceated... old animation system id_us_plus((ID *)con->ipo); /* only do specific constraints if required */ @@ -3823,6 +3810,8 @@ void copy_constraints (ListBase *dst, ListBase *src) } } +/* ......... */ + /* finds the 'active' constraint in a constraint stack */ bConstraint *constraints_get_active (ListBase *list) { @@ -3840,6 +3829,19 @@ bConstraint *constraints_get_active (ListBase *list) return NULL; } +/* Set the given constraint as the active one (clearing all the others) */ +void constraints_set_active (ListBase *list, bConstraint *con) +{ + bConstraint *c; + + for (c= list->first; c; c= c->next) { + if (c == con) + c->flag |= CONSTRAINT_ACTIVE; + else + c->flag &= ~CONSTRAINT_ACTIVE; + } +} + /* -------- Constraints and Proxies ------- */ /* Rescue all constraints tagged as being CONSTRAINT_PROXY_LOCAL (i.e. added to bone that's proxy-synced in this file) */ diff --git a/source/blender/blenkernel/intern/fmodifier.c b/source/blender/blenkernel/intern/fmodifier.c index e9e12e29606..5daa2ed1924 100644 --- a/source/blender/blenkernel/intern/fmodifier.c +++ b/source/blender/blenkernel/intern/fmodifier.c @@ -1013,22 +1013,20 @@ int remove_fmodifier (ListBase *modifiers, FModifier *fcm) if (modifiers) { BLI_freelinkN(modifiers, fcm); return 1; - } else { + } + else { // XXX this case can probably be removed some day, as it shouldn't happen... printf("remove_fmodifier() - no modifier stack given \n"); MEM_freeN(fcm); return 0; } } + +/* Remove and free the nth F-Modifier from the given stack */ int remove_fmodifier_index (ListBase *modifiers, int index) { FModifier *fcm= BLI_findlink(modifiers, index); - if(fcm) { - return remove_fmodifier(modifiers, fcm); - } - else { - return 0; - } + return remove_fmodifier(modifiers, fcm); } /* Remove all of a given F-Curve's modifiers */ -- cgit v1.2.3