PyAPI: Store PyInstances for ID's

This means once an ID is created,
it will keep using the same PyObject instance.

This has some advantages:
- Avoids unnecessary re-creation of instances on UI poll / redraw.
- Accessing free'd ID's gives an exception instead of crashing.
  (long standing annoyance!, though this only applies to ID's
   and not yet other data that uses the ID's - vertices for eg).
- Allows using instance comparison (a little faster).

Note that the instances won't be kept between undo.
This commit is contained in:
Campbell Barton 2017-07-26 23:49:20 +10:00
parent 40a45e393e
commit 3ed5c9a610
7 changed files with 22 additions and 7 deletions

View File

@ -2221,6 +2221,7 @@ static void direct_link_id(FileData *fd, ID *id)
/* this case means the data was written incorrectly, it should not happen */
IDP_DirectLinkGroup_OrFree(&id->properties, (fd->flags & FD_FLAGS_SWITCH_ENDIAN), fd);
}
id->py_instance = NULL;
}
/* ************ READ CurveMapping *************** */

View File

@ -143,6 +143,7 @@ typedef struct ID {
int us;
int icon_id;
IDProperty *properties;
void *py_instance;
} ID;
/**

View File

@ -299,15 +299,15 @@ typedef struct Object {
struct FluidsimSettings *fluidsimSettings; /* if fluidsim enabled, store additional settings */
/* Runtime valuated curve-specific data, not stored in the file */
struct CurveCache *curve_cache;
struct DerivedMesh *derivedDeform, *derivedFinal;
uint64_t lastDataMask; /* the custom data layer mask that was last used to calculate derivedDeform and derivedFinal */
uint64_t customdata_mask; /* (extra) custom data layer mask to use for creating derivedmesh, set by depsgraph */
unsigned int state; /* bit masks of game controllers that are active */
unsigned int init_state; /* bit masks of initial state as recorded by the users */
/* Runtime valuated curve-specific data, not stored in the file */
struct CurveCache *curve_cache;
ListBase gpulamp; /* runtime, for glsl lamp display only */
ListBase pc_ids;
ListBase *duplilist; /* for temporary dupli list storage, only for use by RNA API */

View File

@ -1720,12 +1720,12 @@ typedef struct Scene {
/* Physics simulation settings */
struct PhysicsSettings physics_settings;
/* Movie Tracking */
struct MovieClip *clip; /* active movie clip */
uint64_t customdata_mask; /* XXX. runtime flag for drawing, actually belongs in the window, only used by BKE_object_handle_update() */
uint64_t customdata_mask_modal; /* XXX. same as above but for temp operator use (gl renders) */
/* Movie Tracking */
struct MovieClip *clip; /* active movie clip */
/* Color Management */
ColorManagedViewSettings view_settings;
ColorManagedDisplaySettings display_settings;

View File

@ -61,8 +61,8 @@ typedef struct Text {
char *undo_buf;
int undo_pos, undo_len;
void *compiled;
double mtime;
void *compiled;
} Text;
#define TXT_TABSIZE 4

View File

@ -394,6 +394,14 @@ static void rna_ID_animation_data_free(ID *id, Main *bmain)
DEG_relations_tag_update(bmain);
}
#ifdef WITH_PYTHON
void **rna_ID_instance(PointerRNA *ptr)
{
ID *id = (ID *)ptr->data;
return &id->py_instance;
}
#endif
static void rna_IDPArray_begin(CollectionPropertyIterator *iter, PointerRNA *ptr)
{
IDProperty *prop = (IDProperty *)ptr->data;
@ -1071,6 +1079,10 @@ static void rna_def_ID(BlenderRNA *brna)
"Tag the ID to update its display data, "
"e.g. when calling :class:`bpy.types.Scene.update`");
RNA_def_enum_flag(func, "refresh", update_flag_items, 0, "", "Type of updates to perform");
#ifdef WITH_PYTHON
RNA_def_struct_register_funcs(srna, NULL, NULL, "rna_ID_instance");
#endif
}
static void rna_def_library(BlenderRNA *brna)

View File

@ -221,6 +221,7 @@ void rna_ID_name_set(struct PointerRNA *ptr, const char *value);
struct StructRNA *rna_ID_refine(struct PointerRNA *ptr);
struct IDProperty *rna_ID_idprops(struct PointerRNA *ptr, bool create);
void rna_ID_fake_user_set(struct PointerRNA *ptr, int value);
void **rna_ID_instance(PointerRNA *ptr);
struct IDProperty *rna_PropertyGroup_idprops(struct PointerRNA *ptr, bool create);
void rna_PropertyGroup_unregister(struct Main *bmain, struct StructRNA *type);
struct StructRNA *rna_PropertyGroup_register(struct Main *bmain, struct ReportList *reports, void *data,