From 3cbfe96681f69fcbccd1519d731168700dbf878a Mon Sep 17 00:00:00 2001 From: Campbell Barton Date: Thu, 3 Sep 2020 16:23:49 +1000 Subject: Object: add BKE_object_obdata_to_type utility function Move functionality to get the object type from an ID into it's own function. --- source/blender/blenkernel/BKE_curve.h | 2 +- source/blender/blenkernel/BKE_object.h | 1 + source/blender/blenkernel/intern/curve.c | 2 +- source/blender/blenkernel/intern/object.c | 38 ++++++++++++++++++ source/blender/makesrna/intern/rna_main_api.c | 58 +++++---------------------- 5 files changed, 50 insertions(+), 51 deletions(-) diff --git a/source/blender/blenkernel/BKE_curve.h b/source/blender/blenkernel/BKE_curve.h index 0bd4e3a7582..5930578c505 100644 --- a/source/blender/blenkernel/BKE_curve.h +++ b/source/blender/blenkernel/BKE_curve.h @@ -83,7 +83,7 @@ void BKE_curve_editfont_free(struct Curve *cu); void BKE_curve_init(struct Curve *cu, const short curve_type); struct Curve *BKE_curve_add(struct Main *bmain, const char *name, int type); struct Curve *BKE_curve_copy(struct Main *bmain, const struct Curve *cu); -short BKE_curve_type_get(struct Curve *cu); +short BKE_curve_type_get(const struct Curve *cu); void BKE_curve_type_test(struct Object *ob); void BKE_curve_curve_dimension_update(struct Curve *cu); diff --git a/source/blender/blenkernel/BKE_object.h b/source/blender/blenkernel/BKE_object.h index 020225d2794..9f006ea21ef 100644 --- a/source/blender/blenkernel/BKE_object.h +++ b/source/blender/blenkernel/BKE_object.h @@ -131,6 +131,7 @@ struct Object *BKE_object_add_for_data(struct Main *bmain, bool do_id_user) ATTR_RETURNS_NONNULL; void *BKE_object_obdata_add_from_type(struct Main *bmain, int type, const char *name) ATTR_NONNULL(1); +int BKE_object_obdata_to_type(const struct ID *id) ATTR_NONNULL(1); struct Object *BKE_object_copy(struct Main *bmain, const struct Object *ob); bool BKE_object_is_libdata(const struct Object *ob); diff --git a/source/blender/blenkernel/intern/curve.c b/source/blender/blenkernel/intern/curve.c index 4d6b8feea26..4ac6399c4e4 100644 --- a/source/blender/blenkernel/intern/curve.c +++ b/source/blender/blenkernel/intern/curve.c @@ -268,7 +268,7 @@ ListBase *BKE_curve_editNurbs_get(Curve *cu) return NULL; } -short BKE_curve_type_get(Curve *cu) +short BKE_curve_type_get(const Curve *cu) { Nurb *nu; int type = cu->type; diff --git a/source/blender/blenkernel/intern/object.c b/source/blender/blenkernel/intern/object.c index b7694afb55b..871861c3167 100644 --- a/source/blender/blenkernel/intern/object.c +++ b/source/blender/blenkernel/intern/object.c @@ -1274,6 +1274,44 @@ void *BKE_object_obdata_add_from_type(Main *bmain, int type, const char *name) } } +/** + * Return -1 on failure. + */ +int BKE_object_obdata_to_type(const ID *id) +{ + /* Keep in sync with #OB_DATA_SUPPORT_ID macro. */ + switch (GS(id->name)) { + case ID_ME: + return OB_MESH; + case ID_CU: + return BKE_curve_type_get((const Curve *)id); + case ID_MB: + return OB_MBALL; + case ID_LA: + return OB_LAMP; + case ID_SPK: + return OB_SPEAKER; + case ID_CA: + return OB_CAMERA; + case ID_LT: + return OB_LATTICE; + case ID_GD: + return OB_GPENCIL; + case ID_AR: + return OB_ARMATURE; + case ID_LP: + return OB_LIGHTPROBE; + case ID_HA: + return OB_HAIR; + case ID_PT: + return OB_POINTCLOUD; + case ID_VO: + return OB_VOLUME; + default: + return -1; + } +} + /* more general add: creates minimum required data, but without vertices etc. */ Object *BKE_object_add_only_object(Main *bmain, int type, const char *name) { diff --git a/source/blender/makesrna/intern/rna_main_api.c b/source/blender/makesrna/intern/rna_main_api.c index 7c941ddb524..8a34bf0245b 100644 --- a/source/blender/makesrna/intern/rna_main_api.c +++ b/source/blender/makesrna/intern/rna_main_api.c @@ -228,57 +228,17 @@ static Object *rna_Main_objects_new(Main *bmain, ReportList *reports, const char Object *ob; int type = OB_EMPTY; + if (data) { - /* keep in sync with OB_DATA_SUPPORT_ID() macro */ - switch (GS(data->name)) { - case ID_ME: - type = OB_MESH; - break; - case ID_CU: - type = BKE_curve_type_get((Curve *)data); - break; - case ID_MB: - type = OB_MBALL; - break; - case ID_LA: - type = OB_LAMP; - break; - case ID_SPK: - type = OB_SPEAKER; - break; - case ID_CA: - type = OB_CAMERA; - break; - case ID_LT: - type = OB_LATTICE; - break; - case ID_GD: - type = OB_GPENCIL; - break; - case ID_AR: - type = OB_ARMATURE; - break; - case ID_LP: - type = OB_LIGHTPROBE; - break; - case ID_HA: - type = OB_HAIR; - break; - case ID_PT: - type = OB_POINTCLOUD; - break; - case ID_VO: - type = OB_VOLUME; - break; - default: { - const char *idname; - if (RNA_enum_id_from_value(rna_enum_id_type_items, GS(data->name), &idname) == 0) { - idname = "UNKNOWN"; - } - - BKE_reportf(reports, RPT_ERROR, "ID type '%s' is not valid for an object", idname); - return NULL; + type = BKE_object_obdata_to_type(data); + if (type == -1) { + const char *idname; + if (RNA_enum_id_from_value(rna_enum_id_type_items, GS(data->name), &idname) == 0) { + idname = "UNKNOWN"; } + + BKE_reportf(reports, RPT_ERROR, "ID type '%s' is not valid for an object", idname); + return NULL; } id_us_plus(data); -- cgit v1.2.3