diff options
author | Jacques Lucke <jacques@blender.org> | 2020-11-06 19:45:29 +0300 |
---|---|---|
committer | Jacques Lucke <jacques@blender.org> | 2020-11-06 19:45:29 +0300 |
commit | 58e9b51f9551bc68011b85554bad4dd6487576ce (patch) | |
tree | 5a94f87b4b43565bd7f3421292a0ed8795b96417 /source/blender/blenkernel/intern/constraint.c | |
parent | 37ef37711d997899041d84e8c7a17e1fbb4efab9 (diff) |
Refactor: move Constraint .blend I/O to blenkernel
Ref T76372.
Diffstat (limited to 'source/blender/blenkernel/intern/constraint.c')
-rw-r--r-- | source/blender/blenkernel/intern/constraint.c | 176 |
1 files changed, 176 insertions, 0 deletions
diff --git a/source/blender/blenkernel/intern/constraint.c b/source/blender/blenkernel/intern/constraint.c index d21529467a1..a1b53b721d3 100644 --- a/source/blender/blenkernel/intern/constraint.c +++ b/source/blender/blenkernel/intern/constraint.c @@ -21,6 +21,9 @@ * \ingroup bke */ +/* Allow using deprecated functionality for .blend file I/O. */ +#define DNA_DEPRECATED_ALLOW + #include <float.h> #include <math.h> #include <stddef.h> @@ -80,6 +83,8 @@ #include "DEG_depsgraph.h" #include "DEG_depsgraph_query.h" +#include "BLO_read_write.h" + #include "CLG_log.h" #ifdef WITH_PYTHON @@ -6086,3 +6091,174 @@ void BKE_constraints_solve(struct Depsgraph *depsgraph, } } } + +void BKE_constraint_blend_write(BlendWriter *writer, ListBase *conlist) +{ + LISTBASE_FOREACH (bConstraint *, con, conlist) { + const bConstraintTypeInfo *cti = BKE_constraint_typeinfo_get(con); + + /* Write the specific data */ + if (cti && con->data) { + /* firstly, just write the plain con->data struct */ + BLO_write_struct_by_name(writer, cti->structName, con->data); + + /* do any constraint specific stuff */ + switch (con->type) { + case CONSTRAINT_TYPE_PYTHON: { + bPythonConstraint *data = con->data; + + /* write targets */ + LISTBASE_FOREACH (bConstraintTarget *, ct, &data->targets) { + BLO_write_struct(writer, bConstraintTarget, ct); + } + + /* Write ID Properties -- and copy this comment EXACTLY for easy finding + * of library blocks that implement this.*/ + IDP_BlendWrite(writer, data->prop); + + break; + } + case CONSTRAINT_TYPE_ARMATURE: { + bArmatureConstraint *data = con->data; + + /* write targets */ + LISTBASE_FOREACH (bConstraintTarget *, ct, &data->targets) { + BLO_write_struct(writer, bConstraintTarget, ct); + } + + break; + } + case CONSTRAINT_TYPE_SPLINEIK: { + bSplineIKConstraint *data = con->data; + + /* write points array */ + BLO_write_float_array(writer, data->numpoints, data->points); + + break; + } + } + } + + /* Write the constraint */ + BLO_write_struct(writer, bConstraint, con); + } +} + +void BKE_constraint_blend_read_data(BlendDataReader *reader, ListBase *lb) +{ + BLO_read_list(reader, lb); + LISTBASE_FOREACH (bConstraint *, con, lb) { + BLO_read_data_address(reader, &con->data); + + switch (con->type) { + case CONSTRAINT_TYPE_PYTHON: { + bPythonConstraint *data = con->data; + + BLO_read_list(reader, &data->targets); + + BLO_read_data_address(reader, &data->prop); + IDP_BlendDataRead(reader, &data->prop); + break; + } + case CONSTRAINT_TYPE_ARMATURE: { + bArmatureConstraint *data = con->data; + + BLO_read_list(reader, &data->targets); + + break; + } + case CONSTRAINT_TYPE_SPLINEIK: { + bSplineIKConstraint *data = con->data; + + BLO_read_data_address(reader, &data->points); + break; + } + case CONSTRAINT_TYPE_KINEMATIC: { + bKinematicConstraint *data = con->data; + + con->lin_error = 0.0f; + con->rot_error = 0.0f; + + /* version patch for runtime flag, was not cleared in some case */ + data->flag &= ~CONSTRAINT_IK_AUTO; + break; + } + case CONSTRAINT_TYPE_CHILDOF: { + /* XXX version patch, in older code this flag wasn't always set, and is inherent to type */ + if (con->ownspace == CONSTRAINT_SPACE_POSE) { + con->flag |= CONSTRAINT_SPACEONCE; + } + break; + } + case CONSTRAINT_TYPE_TRANSFORM_CACHE: { + bTransformCacheConstraint *data = con->data; + data->reader = NULL; + data->reader_object_path[0] = '\0'; + } + } + } +} + +/* temp struct used to transport needed info to lib_link_constraint_cb() */ +typedef struct tConstraintLinkData { + BlendLibReader *reader; + ID *id; +} tConstraintLinkData; +/* callback function used to relink constraint ID-links */ +static void lib_link_constraint_cb(bConstraint *UNUSED(con), + ID **idpoin, + bool UNUSED(is_reference), + void *userdata) +{ + tConstraintLinkData *cld = (tConstraintLinkData *)userdata; + BLO_read_id_address(cld->reader, cld->id->lib, idpoin); +} + +void BKE_constraint_blend_read_lib(BlendLibReader *reader, ID *id, ListBase *conlist) +{ + tConstraintLinkData cld; + + /* legacy fixes */ + LISTBASE_FOREACH (bConstraint *, con, conlist) { + /* patch for error introduced by changing constraints (dunno how) */ + /* if con->data type changes, dna cannot resolve the pointer! (ton) */ + if (con->data == NULL) { + con->type = CONSTRAINT_TYPE_NULL; + } + /* own ipo, all constraints have it */ + BLO_read_id_address(reader, id->lib, &con->ipo); /* XXX deprecated - old animation system */ + + /* If linking from a library, clear 'local' library override flag. */ + if (id->lib != NULL) { + con->flag &= ~CONSTRAINT_OVERRIDE_LIBRARY_LOCAL; + } + } + + /* relink all ID-blocks used by the constraints */ + cld.reader = reader; + cld.id = id; + + BKE_constraints_id_loop(conlist, lib_link_constraint_cb, &cld); +} + +/* callback function used to expand constraint ID-links */ +static void expand_constraint_cb(bConstraint *UNUSED(con), + ID **idpoin, + bool UNUSED(is_reference), + void *userdata) +{ + BlendExpander *expander = userdata; + BLO_expand(expander, *idpoin); +} + +void BKE_constraint_blend_read_expand(BlendExpander *expander, ListBase *lb) +{ + BKE_constraints_id_loop(lb, expand_constraint_cb, expander); + + /* deprecated manual expansion stuff */ + LISTBASE_FOREACH (bConstraint *, curcon, lb) { + if (curcon->ipo) { + BLO_expand(expander, curcon->ipo); /* XXX deprecated - old animation system */ + } + } +} |