Blender  V2.93
rna_asset.c
Go to the documentation of this file.
1 /*
2  * This program is free software; you can redistribute it and/or
3  * modify it under the terms of the GNU General Public License
4  * as published by the Free Software Foundation; either version 2
5  * of the License, or (at your option) any later version.
6  *
7  * This program is distributed in the hope that it will be useful,
8  * but WITHOUT ANY WARRANTY; without even the implied warranty of
9  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10  * GNU General Public License for more details.
11  *
12  * You should have received a copy of the GNU General Public License
13  * along with this program; if not, write to the Free Software Foundation,
14  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
15  */
16 
21 #include <stdlib.h>
22 
23 #include "RNA_define.h"
24 #include "RNA_enum_types.h"
25 
26 #include "DNA_asset_types.h"
27 #include "DNA_defs.h"
28 
29 #include "rna_internal.h"
30 
31 #ifdef RNA_RUNTIME
32 
33 # include "BKE_asset.h"
34 # include "BKE_idprop.h"
35 
36 # include "BLI_listbase.h"
37 
38 # include "RNA_access.h"
39 
40 static AssetTag *rna_AssetMetaData_tag_new(AssetMetaData *asset_data,
41  ReportList *reports,
42  const char *name,
43  bool skip_if_exists)
44 {
45  AssetTag *tag = NULL;
46 
47  if (skip_if_exists) {
49 
50  if (!result.is_new) {
52  reports, RPT_WARNING, "Tag '%s' already present for given asset", result.tag->name);
53  /* Report, but still return valid item. */
54  }
55  tag = result.tag;
56  }
57  else {
58  tag = BKE_asset_metadata_tag_add(asset_data, name);
59  }
60 
61  return tag;
62 }
63 
64 static void rna_AssetMetaData_tag_remove(AssetMetaData *asset_data,
65  ReportList *reports,
66  PointerRNA *tag_ptr)
67 {
68  AssetTag *tag = tag_ptr->data;
69  if (BLI_findindex(&asset_data->tags, tag) == -1) {
70  BKE_reportf(reports, RPT_ERROR, "Tag '%s' not found in given asset", tag->name);
71  return;
72  }
73 
75  RNA_POINTER_INVALIDATE(tag_ptr);
76 }
77 
78 static IDProperty *rna_AssetMetaData_idprops(PointerRNA *ptr, bool create)
79 {
80  AssetMetaData *asset_data = ptr->data;
81 
82  if (create && !asset_data->properties) {
83  IDPropertyTemplate val = {0};
84  asset_data->properties = IDP_New(IDP_GROUP, &val, "RNA_AssetMetaData group");
85  }
86 
87  return asset_data->properties;
88 }
89 
90 static void rna_AssetMetaData_description_get(PointerRNA *ptr, char *value)
91 {
92  AssetMetaData *asset_data = ptr->data;
93 
94  if (asset_data->description) {
95  strcpy(value, asset_data->description);
96  }
97  else {
98  value[0] = '\0';
99  }
100 }
101 
102 static int rna_AssetMetaData_description_length(PointerRNA *ptr)
103 {
104  AssetMetaData *asset_data = ptr->data;
105  return asset_data->description ? strlen(asset_data->description) : 0;
106 }
107 
108 static void rna_AssetMetaData_description_set(PointerRNA *ptr, const char *value)
109 {
110  AssetMetaData *asset_data = ptr->data;
111 
112  if (asset_data->description) {
113  MEM_freeN(asset_data->description);
114  }
115 
116  if (value[0]) {
117  asset_data->description = BLI_strdup(value);
118  }
119  else {
120  asset_data->description = NULL;
121  }
122 }
123 
124 static void rna_AssetMetaData_active_tag_range(
125  PointerRNA *ptr, int *min, int *max, int *softmin, int *softmax)
126 {
127  const AssetMetaData *asset_data = ptr->data;
128  *min = *softmin = 0;
129  *max = *softmax = MAX2(asset_data->tot_tags - 1, 0);
130 }
131 
132 #else
133 
134 static void rna_def_asset_tag(BlenderRNA *brna)
135 {
136  StructRNA *srna;
137  PropertyRNA *prop;
138 
139  srna = RNA_def_struct(brna, "AssetTag", NULL);
140  RNA_def_struct_ui_text(srna, "Asset Tag", "User defined tag (name token)");
141 
142  prop = RNA_def_property(srna, "name", PROP_STRING, PROP_NONE);
144  RNA_def_property_ui_text(prop, "Name", "The identifier that makes up this tag");
145  RNA_def_struct_name_property(srna, prop);
146 }
147 
149 {
150  StructRNA *srna;
151 
152  FunctionRNA *func;
153  PropertyRNA *parm;
154 
155  RNA_def_property_srna(cprop, "AssetTags");
156  srna = RNA_def_struct(brna, "AssetTags", NULL);
157  RNA_def_struct_sdna(srna, "AssetMetaData");
158  RNA_def_struct_ui_text(srna, "Asset Tags", "Collection of custom asset tags");
159 
160  /* Tag collection */
161  func = RNA_def_function(srna, "new", "rna_AssetMetaData_tag_new");
162  RNA_def_function_ui_description(func, "Add a new tag to this asset");
164  parm = RNA_def_string(func, "name", NULL, MAX_NAME, "Name", "");
166  parm = RNA_def_boolean(func,
167  "skip_if_exists",
168  false,
169  "Skip if Exists",
170  "Do not add a new tag if one of the same type already exists");
171  /* return type */
172  parm = RNA_def_pointer(func, "tag", "AssetTag", "", "New tag");
173  RNA_def_function_return(func, parm);
174 
175  func = RNA_def_function(srna, "remove", "rna_AssetMetaData_tag_remove");
176  RNA_def_function_ui_description(func, "Remove an existing tag from this asset");
178  /* tag to remove */
179  parm = RNA_def_pointer(func, "tag", "AssetTag", "", "Removed tag");
182 }
183 
184 static void rna_def_asset_data(BlenderRNA *brna)
185 {
186  StructRNA *srna;
187  PropertyRNA *prop;
188 
189  srna = RNA_def_struct(brna, "AssetMetaData", NULL);
190  RNA_def_struct_ui_text(srna, "Asset Data", "Additional data stored for an asset data-block");
191  // RNA_def_struct_ui_icon(srna, ICON_ASSET); /* TODO: Icon doesn't exist!. */
192  /* The struct has custom properties, but no pointer properties to other IDs! */
193  RNA_def_struct_idprops_func(srna, "rna_AssetMetaData_idprops");
195 
196  prop = RNA_def_property(srna, "description", PROP_STRING, PROP_NONE);
198  "rna_AssetMetaData_description_get",
199  "rna_AssetMetaData_description_length",
200  "rna_AssetMetaData_description_set");
202  prop, "Description", "A description of the asset to be displayed for the user");
203 
204  prop = RNA_def_property(srna, "tags", PROP_COLLECTION, PROP_NONE);
205  RNA_def_property_struct_type(prop, "AssetTag");
208  "Tags",
209  "Custom tags (name tokens) for the asset, used for filtering and "
210  "general asset management");
211  rna_def_asset_tags_api(brna, prop);
212 
213  prop = RNA_def_property(srna, "active_tag", PROP_INT, PROP_NONE);
214  RNA_def_property_int_funcs(prop, NULL, NULL, "rna_AssetMetaData_active_tag_range");
215  RNA_def_property_ui_text(prop, "Active Tag", "Index of the tag set for editing");
216 }
217 
219 {
221 
222  rna_def_asset_tag(brna);
223  rna_def_asset_data(brna);
224 
226 }
227 
228 #endif
void BKE_asset_metadata_tag_remove(struct AssetMetaData *asset_data, struct AssetTag *tag)
Definition: asset.cc:104
struct AssetTagEnsureResult BKE_asset_metadata_tag_ensure(struct AssetMetaData *asset_data, const char *name)
Definition: asset.cc:81
struct AssetTag * BKE_asset_metadata_tag_add(struct AssetMetaData *asset_data, const char *name)
Definition: asset.cc:71
struct IDProperty * IDP_New(const char type, const IDPropertyTemplate *val, const char *name) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL()
Definition: idprop.c:907
void BKE_reportf(ReportList *reports, ReportType type, const char *format,...) ATTR_PRINTF_FORMAT(3
int BLI_findindex(const struct ListBase *listbase, const void *vlink) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL(1)
char * BLI_strdup(const char *str) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL() ATTR_MALLOC
Definition: string.c:70
#define MAX2(a, b)
@ IDP_GROUP
Definition: DNA_ID.h:101
#define MAX_NAME
Definition: DNA_defs.h:62
#define RNA_POINTER_INVALIDATE(ptr)
Definition: RNA_access.h:1425
@ PARM_RNAPTR
Definition: RNA_types.h:339
@ PARM_REQUIRED
Definition: RNA_types.h:337
@ FUNC_USE_REPORTS
Definition: RNA_types.h:578
@ STRUCT_NO_DATABLOCK_IDPROPERTIES
Definition: RNA_types.h:632
@ PROP_INT
Definition: RNA_types.h:74
@ PROP_STRING
Definition: RNA_types.h:76
@ PROP_COLLECTION
Definition: RNA_types.h:79
@ PROP_THICK_WRAP
Definition: RNA_types.h:270
@ PROP_EDITABLE
Definition: RNA_types.h:175
@ PROP_NEVER_NULL
Definition: RNA_types.h:225
@ PROP_NONE
Definition: RNA_types.h:113
void(* MEM_freeN)(void *vmemh)
Definition: mallocn.c:41
static void rna_def_asset_data(BlenderRNA *brna)
Definition: rna_asset.c:184
static void rna_def_asset_tag(BlenderRNA *brna)
Definition: rna_asset.c:134
static void rna_def_asset_tags_api(BlenderRNA *brna, PropertyRNA *cprop)
Definition: rna_asset.c:148
void RNA_def_asset(BlenderRNA *brna)
Definition: rna_asset.c:218
void RNA_define_animate_sdna(bool animate)
Definition: rna_define.c:766
PropertyRNA * RNA_def_boolean(StructOrFunctionRNA *cont_, const char *identifier, bool default_value, const char *ui_name, const char *ui_description)
Definition: rna_define.c:3481
PropertyRNA * RNA_def_pointer(StructOrFunctionRNA *cont_, const char *identifier, const char *type, const char *ui_name, const char *ui_description)
Definition: rna_define.c:4159
void RNA_def_struct_flag(StructRNA *srna, int flag)
Definition: rna_define.c:1152
void RNA_def_parameter_clear_flags(PropertyRNA *prop, PropertyFlag flag_property, ParameterFlag flag_parameter)
Definition: rna_define.c:1555
void RNA_def_property_string_funcs(PropertyRNA *prop, const char *get, const char *length, const char *set)
Definition: rna_define.c:3312
void RNA_def_function_return(FunctionRNA *func, PropertyRNA *ret)
Definition: rna_define.c:4302
void RNA_def_property_ui_text(PropertyRNA *prop, const char *name, const char *description)
Definition: rna_define.c:1676
FunctionRNA * RNA_def_function(StructRNA *srna, const char *identifier, const char *call)
Definition: rna_define.c:4262
void RNA_def_property_srna(PropertyRNA *prop, const char *type)
Definition: rna_define.c:3462
void RNA_def_struct_ui_text(StructRNA *srna, const char *name, const char *description)
Definition: rna_define.c:1259
void RNA_def_struct_sdna(StructRNA *srna, const char *structname)
Definition: rna_define.c:1067
void RNA_def_property_string_maxlength(PropertyRNA *prop, int maxlength)
Definition: rna_define.c:1940
void RNA_def_property_struct_type(PropertyRNA *prop, const char *type)
Definition: rna_define.c:1792
void RNA_def_function_ui_description(FunctionRNA *func, const char *description)
Definition: rna_define.c:4337
PropertyRNA * RNA_def_property(StructOrFunctionRNA *cont_, const char *identifier, int type, int subtype)
Definition: rna_define.c:1279
void RNA_def_struct_name_property(struct StructRNA *srna, struct PropertyRNA *prop)
Definition: rna_define.c:1122
void RNA_def_function_flag(FunctionRNA *func, int flag)
Definition: rna_define.c:4332
StructRNA * RNA_def_struct(BlenderRNA *brna, const char *identifier, const char *from)
Definition: rna_define.c:1047
void RNA_def_property_int_funcs(PropertyRNA *prop, const char *get, const char *set, const char *range)
Definition: rna_define.c:3055
PropertyRNA * RNA_def_string(StructOrFunctionRNA *cont_, const char *identifier, const char *default_value, int maxlen, const char *ui_name, const char *ui_description)
Definition: rna_define.c:3675
void RNA_def_struct_idprops_func(StructRNA *srna, const char *idproperties)
Definition: rna_define.c:1179
void RNA_def_property_flag(PropertyRNA *prop, PropertyFlag flag)
Definition: rna_define.c:1512
void RNA_def_parameter_flags(PropertyRNA *prop, PropertyFlag flag_property, ParameterFlag flag_parameter)
Definition: rna_define.c:1547
#define min(a, b)
Definition: sort.c:51
The meta-data of an asset. By creating and giving this for a data-block (ID.asset_data),...
struct IDProperty * properties
struct AssetTag * tag
Definition: BKE_asset.h:38
User defined tag. Currently only used by assets, could be used more often at some point....
char name[64]
void * data
Definition: RNA_types.h:52
float max
PointerRNA * ptr
Definition: wm_files.c:3157