=ID Properties=

The code for preserving ID properties was apparently not
working.  Fixed that by adding a new function,
IDP_ReplaceInGroup, that automatically handles overriding a
property in a group while preserving the property order.

Its odd though that the previous fix I had wasn't
working :/
This commit is contained in:
Joseph Eagar 2007-05-22 04:41:21 +00:00
parent 6b103057e8
commit 9ac39d2e99
3 changed files with 32 additions and 41 deletions

@ -78,6 +78,11 @@ void IDP_LinkID(struct IDProperty *prop, ID *id);
void IDP_UnlinkID(struct IDProperty *prop);
/*-------- Group Functions -------*/
/*checks if a property with the same name as prop exists, and if so replaces it.
Use this to preserve order!*/
void IDP_ReplaceInGroup(struct IDProperty *group, struct IDProperty *prop);
/*
This function has a sanity check to make sure ID properties with the same name don't
get added to the group.

@ -53,8 +53,8 @@ static char idp_size_table[] = {
1, /*strings*/
sizeof(int),
sizeof(float),
sizeof(float)*3, /*Vector type*/
sizeof(float)*16, /*Matrix type, we allocate max 4x4 even if in 3x3 mode*/
sizeof(float)*3, /*Vector type, deprecated*/
sizeof(float)*16, /*Matrix type, deprecated*/
0, /*arrays don't have a fixed size*/
sizeof(ListBase), /*Group type*/
sizeof(void*)
@ -169,6 +169,27 @@ void IDP_UnlinkID(IDProperty *prop)
}
/*-------- Group Functions -------*/
/*checks if a property with the same name as prop exists, and if so replaces it.*/
void IDP_ReplaceInGroup(IDProperty *group, IDProperty *prop)
{
IDProperty *loop;
for (loop=group->data.group.first; loop; loop=loop->next) {
if (BSTR_EQ(loop->name, prop->name)) {
if (loop->next) BLI_insertlinkbefore(&group->data.group, loop->next, prop);
else BLI_addtail(&group->data.group, prop);
BLI_remlink(&group->data.group, loop);
IDP_FreeProperty(loop);
MEM_freeN(loop);
group->len++;
return;
}
}
group->len++;
BLI_addtail(&group->data.group, prop);
}
/*returns 0 if an id property with the same name exists and it failed,
or 1 if it succeeded in adding to the group.*/
int IDP_AddToGroup(IDProperty *group, IDProperty *prop)
@ -323,26 +344,6 @@ IDProperty *IDP_New(int type, IDPropertyTemplate val, char *name)
/* heh I think all needed values are set properly by calloc anyway :) */
break;
}
case IDP_MATRIX:
prop = MEM_callocN(sizeof(IDProperty), "IDProperty array");
if (val.matrix_or_vector.matvec_size == IDP_MATRIX4X4)
prop->data.pointer = MEM_callocN(sizeof(float)*4*4, "matrix 4x4 idproperty");
else
prop->data.pointer = MEM_callocN(sizeof(float)*3*3, "matrix 3x3 idproperty");
case IDP_VECTOR:
prop = MEM_callocN(sizeof(IDProperty), "IDProperty array");
switch (val.matrix_or_vector.matvec_size) {
case IDP_VECTOR4D:
prop->data.pointer = MEM_callocN(sizeof(float)*4, "vector 4d idproperty");
break;
case IDP_VECTOR3D:
prop->data.pointer = MEM_callocN(sizeof(float)*3, "vector 3d idproperty");
break;
case IDP_VECTOR2D:
prop->data.pointer = MEM_callocN(sizeof(float)*2, "vector 2d idproperty");
break;
}
default:
{
prop = MEM_callocN(sizeof(IDProperty), "IDProperty array");
@ -370,10 +371,6 @@ void IDP_FreeProperty(IDProperty *prop)
case IDP_GROUP:
IDP_FreeGroup(prop);
break;
case IDP_VECTOR:
case IDP_MATRIX:
MEM_freeN(prop->data.pointer);
break;
}
}

@ -86,9 +86,6 @@ PyObject *BPy_IDGroup_WrapData( ID *id, IDProperty *prop )
array->prop = prop;
return (PyObject*) array;
}
case IDP_MATRIX:
case IDP_VECTOR:
break;
}
Py_RETURN_NONE;
}
@ -141,12 +138,12 @@ int BPy_IDGroup_SetData(BPy_IDProperty *self, IDProperty *prop, PyObject *value)
return 0;
}
PyObject *BPy_IDGroup_GetName(BPy_IDProperty *self)
PyObject *BPy_IDGroup_GetName(BPy_IDProperty *self, void *bleh)
{
return Py_BuildValue("s", self->prop->name);
}
int BPy_IDGroup_SetName(BPy_IDProperty *self, PyObject *value)
int BPy_IDGroup_SetName(BPy_IDProperty *self, PyObject *value, void *bleh)
{
char *st;
if (!PyString_Check(value))
@ -206,7 +203,7 @@ PyObject *BPy_IDGroup_Map_GetItem(BPy_IDProperty *self, PyObject *item)
/*returns NULL on success, error string on failure*/
char *BPy_IDProperty_Map_ValidateAndCreate(char *name, IDProperty *group, PyObject *ob)
{
IDProperty *prop = NULL, *prop2=NULL, *prev=NULL;
IDProperty *prop = NULL;
IDPropertyTemplate val = {0};
if (PyFloat_Check(ob)) {
@ -285,15 +282,7 @@ char *BPy_IDProperty_Map_ValidateAndCreate(char *name, IDProperty *group, PyObje
Py_XDECREF(vals);
} else return "invalid property value";
prop2 = IDP_GetPropertyFromGroup(group, prop->name);
if (prop2) {
prev=prop2->prev; /*we want to insert new property in same place as old*/
IDP_RemFromGroup(group, prop2);
IDP_FreeProperty(prop2);
MEM_freeN(prop2);
}
IDP_InsertToGroup(group, prev, prop);
IDP_ReplaceInGroup(group, prop);
return NULL;
}