Object: add BKE_object_obdata_to_type utility function

Move functionality to get the object type from an ID
into it's own function.
This commit is contained in:
Campbell Barton
2020-09-03 16:23:49 +10:00
parent c017e1cb67
commit 3cbfe96681
5 changed files with 50 additions and 51 deletions
+1 -1
View File
@@ -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);
+1
View File
@@ -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);
+1 -1
View File
@@ -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;
+38
View File
@@ -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)
{
+9 -49
View File
@@ -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;
if (data) {
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);