Refactor: move Constraint .blend I/O to blenkernel

Ref T76372.
This commit is contained in:
Jacques Lucke 2020-11-06 17:45:29 +01:00
parent 37ef37711d
commit 58e9b51f95
Notes: blender-bot 2023-02-14 09:34:18 +01:00
Referenced by issue #76372, Blenloader Decentralization
4 changed files with 195 additions and 179 deletions

View File

@ -31,6 +31,10 @@ struct Scene;
struct bConstraint;
struct bConstraintTarget;
struct bPoseChannel;
struct BlendWriter;
struct BlendDataReader;
struct BlendLibReader;
struct BlendExpander;
/* ---------------------------------------------------------------------------- */
#ifdef __cplusplus
@ -222,6 +226,13 @@ void BKE_constraints_solve(struct Depsgraph *depsgraph,
struct bConstraintOb *cob,
float ctime);
void BKE_constraint_blend_write(struct BlendWriter *writer, struct ListBase *conlist);
void BKE_constraint_blend_read_data(struct BlendDataReader *reader, struct ListBase *lb);
void BKE_constraint_blend_read_lib(struct BlendLibReader *reader,
struct ID *id,
struct ListBase *conlist);
void BKE_constraint_blend_read_expand(struct BlendExpander *expander, struct ListBase *lb);
#ifdef __cplusplus
}
#endif

View File

@ -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 */
}
}
}

View File

@ -2572,103 +2572,6 @@ static void lib_link_constraint_channels(BlendLibReader *reader, ID *id, ListBas
/** \name Read ID: Armature
* \{ */
/* 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);
}
static void lib_link_constraints(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);
}
static void direct_link_constraints(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';
}
}
}
}
static void lib_link_pose(BlendLibReader *reader, Object *ob, bPose *pose)
{
bArmature *arm = ob->data;
@ -2702,7 +2605,7 @@ static void lib_link_pose(BlendLibReader *reader, Object *ob, bPose *pose)
}
LISTBASE_FOREACH (bPoseChannel *, pchan, &pose->chanbase) {
lib_link_constraints(reader, (ID *)ob, &pchan->constraints);
BKE_constraint_blend_read_lib(reader, (ID *)ob, &pchan->constraints);
pchan->bone = BKE_armature_find_bone_name(arm, pchan->name);
@ -2848,7 +2751,7 @@ static void lib_link_object(BlendLibReader *reader, Object *ob)
/* WARNING! Also check expand_object(), should reflect the stuff below. */
lib_link_pose(reader, ob, ob->pose);
lib_link_constraints(reader, &ob->id, &ob->constraints);
BKE_constraint_blend_read_lib(reader, &ob->id, &ob->constraints);
/* XXX deprecated - old animation system <<< */
lib_link_constraint_channels(reader, &ob->id, &ob->constraintChannels);
@ -2937,7 +2840,7 @@ static void direct_link_pose(BlendDataReader *reader, bPose *pose)
BLO_read_data_address(reader, &pchan->bbone_prev);
BLO_read_data_address(reader, &pchan->bbone_next);
direct_link_constraints(reader, &pchan->constraints);
BKE_constraint_blend_read_data(reader, &pchan->constraints);
BLO_read_data_address(reader, &pchan->prop);
IDP_BlendDataRead(reader, &pchan->prop);
@ -3111,7 +3014,7 @@ static void direct_link_object(BlendDataReader *reader, Object *ob)
BLO_read_list(reader, &ob->particlesystem);
BKE_particle_system_blend_read_data(reader, &ob->particlesystem);
direct_link_constraints(reader, &ob->constraints);
BKE_constraint_blend_read_data(reader, &ob->constraints);
BLO_read_list(reader, &ob->hooks);
while (ob->hooks.first) {
@ -5274,28 +5177,6 @@ static void expand_id(BlendExpander *expander, ID *id)
expand_id_embedded_id(expander, id);
}
/* 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);
}
static void expand_constraints(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 */
}
}
}
static void expand_pose(BlendExpander *expander, bPose *pose)
{
if (!pose) {
@ -5303,7 +5184,7 @@ static void expand_pose(BlendExpander *expander, bPose *pose)
}
LISTBASE_FOREACH (bPoseChannel *, chan, &pose->chanbase) {
expand_constraints(expander, &chan->constraints);
BKE_constraint_blend_read_expand(expander, &chan->constraints);
IDP_BlendReadExpand(expander, chan->prop);
BLO_expand(expander, chan->custom);
}
@ -5339,7 +5220,7 @@ static void expand_object(BlendExpander *expander, Object *ob)
expand_pose(expander, ob->pose);
BLO_expand(expander, ob->poselib);
expand_constraints(expander, &ob->constraints);
BKE_constraint_blend_read_expand(expander, &ob->constraints);
BLO_expand(expander, ob->gpd);

View File

@ -811,58 +811,6 @@ static void write_userdef(BlendWriter *writer, const UserDef *userdef)
}
}
static void write_constraints(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);
}
}
static void write_pose(BlendWriter *writer, bPose *pose, bArmature *arm)
{
/* Write each channel */
@ -880,7 +828,7 @@ static void write_pose(BlendWriter *writer, bPose *pose, bArmature *arm)
IDP_BlendWrite(writer, chan->prop);
}
write_constraints(writer, &chan->constraints);
BKE_constraint_blend_write(writer, &chan->constraints);
animviz_motionpath_blend_write(writer, chan->mpath);
@ -966,7 +914,7 @@ static void write_object(BlendWriter *writer, Object *ob, const void *id_address
write_pose(writer, ob->pose, arm);
write_defgroups(writer, &ob->defbase);
write_fmaps(writer, &ob->fmaps);
write_constraints(writer, &ob->constraints);
BKE_constraint_blend_write(writer, &ob->constraints);
animviz_motionpath_blend_write(writer, ob->mpath);
BLO_write_struct(writer, PartDeflect, ob->pd);