RNA: rename bpy.data.grease_pencil to grease_pencils
[blender.git] / source / blender / blenkernel / intern / idcode.c
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  * The Original Code is Copyright (C) 2001-2002 by NaN Holding BV.
17  * All rights reserved.
18  * return info about ID types
19  */
20
21 /** \file
22  * \ingroup bke
23  */
24
25 #include <stdlib.h>
26 #include <string.h>
27
28 #include "DNA_ID.h"
29
30 #include "BLI_utildefines.h"
31
32 #include "BLT_translation.h"
33
34 #include "BKE_main.h"
35 #include "BKE_idcode.h"
36
37 typedef struct {
38         unsigned short code;
39         const char *name, *plural;
40
41         const char *i18n_context;
42
43         int flags;
44 #define IDTYPE_FLAGS_ISLINKABLE (1 << 0)
45 } IDType;
46
47 /* plural need to match rna_main.c's MainCollectionDef */
48 /* WARNING! Keep it in sync with i18n contexts in BLT_translation.h */
49 static IDType idtypes[] = {
50         /** ID's directly below must all be in #Main, and be kept in sync with #MAX_LIBARRAY (membership, not order) */
51         {ID_AC,   "Action",             "actions",         BLT_I18NCONTEXT_ID_ACTION,             IDTYPE_FLAGS_ISLINKABLE},
52         {ID_AR,   "Armature",           "armatures",       BLT_I18NCONTEXT_ID_ARMATURE,           IDTYPE_FLAGS_ISLINKABLE},
53         {ID_BR,   "Brush",              "brushes",         BLT_I18NCONTEXT_ID_BRUSH,              IDTYPE_FLAGS_ISLINKABLE},
54         {ID_CA,   "Camera",             "cameras",         BLT_I18NCONTEXT_ID_CAMERA,             IDTYPE_FLAGS_ISLINKABLE},
55         {ID_CF,   "CacheFile",          "cache_files",     BLT_I18NCONTEXT_ID_CACHEFILE,          IDTYPE_FLAGS_ISLINKABLE},
56         {ID_GR,   "Collection",         "collections",     BLT_I18NCONTEXT_ID_COLLECTION,         IDTYPE_FLAGS_ISLINKABLE},
57         {ID_CU,   "Curve",              "curves",          BLT_I18NCONTEXT_ID_CURVE,              IDTYPE_FLAGS_ISLINKABLE},
58         {ID_GD,   "GPencil",            "grease_pencils",  BLT_I18NCONTEXT_ID_GPENCIL,            IDTYPE_FLAGS_ISLINKABLE}, /* rename gpencil */
59
60         {ID_IM,   "Image",              "images",          BLT_I18NCONTEXT_ID_IMAGE,              IDTYPE_FLAGS_ISLINKABLE},
61         {ID_IP,   "Ipo",                "ipos",            "",                                    IDTYPE_FLAGS_ISLINKABLE}, /* deprecated */
62         {ID_KE,   "Key",                "shape_keys",      BLT_I18NCONTEXT_ID_SHAPEKEY,           0                      },
63         {ID_LA,   "Light",              "lights",          BLT_I18NCONTEXT_ID_LIGHT,              IDTYPE_FLAGS_ISLINKABLE},
64         {ID_LI,   "Library",            "libraries",       BLT_I18NCONTEXT_ID_LIBRARY,            0                      },
65         {ID_LS,   "FreestyleLineStyle", "linestyles",      BLT_I18NCONTEXT_ID_FREESTYLELINESTYLE, IDTYPE_FLAGS_ISLINKABLE},
66         {ID_LT,   "Lattice",            "lattices",        BLT_I18NCONTEXT_ID_LATTICE,            IDTYPE_FLAGS_ISLINKABLE},
67         {ID_MA,   "Material",           "materials",       BLT_I18NCONTEXT_ID_MATERIAL,           IDTYPE_FLAGS_ISLINKABLE},
68         {ID_MB,   "Metaball",           "metaballs",       BLT_I18NCONTEXT_ID_METABALL,           IDTYPE_FLAGS_ISLINKABLE},
69         {ID_MC,   "MovieClip",          "movieclips",      BLT_I18NCONTEXT_ID_MOVIECLIP,          IDTYPE_FLAGS_ISLINKABLE},
70         {ID_ME,   "Mesh",               "meshes",          BLT_I18NCONTEXT_ID_MESH,               IDTYPE_FLAGS_ISLINKABLE},
71         {ID_MSK,  "Mask",               "masks",           BLT_I18NCONTEXT_ID_MASK,               IDTYPE_FLAGS_ISLINKABLE},
72         {ID_NT,   "NodeTree",           "node_groups",     BLT_I18NCONTEXT_ID_NODETREE,           IDTYPE_FLAGS_ISLINKABLE},
73         {ID_OB,   "Object",             "objects",         BLT_I18NCONTEXT_ID_OBJECT,             IDTYPE_FLAGS_ISLINKABLE},
74         {ID_PA,   "ParticleSettings",   "particles",       BLT_I18NCONTEXT_ID_PARTICLESETTINGS,   IDTYPE_FLAGS_ISLINKABLE},
75         {ID_PAL,  "Palettes",           "palettes",        BLT_I18NCONTEXT_ID_PALETTE,            IDTYPE_FLAGS_ISLINKABLE},
76         {ID_PC,   "PaintCurve",         "paint_curves",    BLT_I18NCONTEXT_ID_PAINTCURVE,         IDTYPE_FLAGS_ISLINKABLE},
77         {ID_LP,   "LightProbe",         "light_probes",    BLT_I18NCONTEXT_ID_LIGHTPROBE,         IDTYPE_FLAGS_ISLINKABLE},
78         {ID_SCE,  "Scene",              "scenes",          BLT_I18NCONTEXT_ID_SCENE,              IDTYPE_FLAGS_ISLINKABLE},
79         {ID_SCR,  "Screen",             "screens",         BLT_I18NCONTEXT_ID_SCREEN,             IDTYPE_FLAGS_ISLINKABLE},
80         {ID_SEQ,  "Sequence",           "sequences",       BLT_I18NCONTEXT_ID_SEQUENCE,           0                      }, /* not actually ID data */
81         {ID_SPK,  "Speaker",            "speakers",        BLT_I18NCONTEXT_ID_SPEAKER,            IDTYPE_FLAGS_ISLINKABLE},
82         {ID_SO,   "Sound",              "sounds",          BLT_I18NCONTEXT_ID_SOUND,              IDTYPE_FLAGS_ISLINKABLE},
83         {ID_TE,   "Texture",            "textures",        BLT_I18NCONTEXT_ID_TEXTURE,            IDTYPE_FLAGS_ISLINKABLE},
84         {ID_TXT,  "Text",               "texts",           BLT_I18NCONTEXT_ID_TEXT,               IDTYPE_FLAGS_ISLINKABLE},
85         {ID_VF,   "VFont",              "fonts",           BLT_I18NCONTEXT_ID_VFONT,              IDTYPE_FLAGS_ISLINKABLE},
86         {ID_WO,   "World",              "worlds",          BLT_I18NCONTEXT_ID_WORLD,              IDTYPE_FLAGS_ISLINKABLE},
87         {ID_WM,   "WindowManager",      "window_managers", BLT_I18NCONTEXT_ID_WINDOWMANAGER,      0                       },
88         {ID_WS,   "WorkSpace",          "workspaces",      BLT_I18NCONTEXT_ID_WORKSPACE,          IDTYPE_FLAGS_ISLINKABLE},
89
90         /** Keep last, not an ID exactly, only include for completeness */
91         {ID_LINK_PLACEHOLDER, "Link Placeholder", "link_placeholders", BLT_I18NCONTEXT_ID_ID, 0}, /* plural is fake */
92 };
93
94 /* -1 for ID_LINK_PLACEHOLDER */
95 BLI_STATIC_ASSERT((ARRAY_SIZE(idtypes) - 1 == MAX_LIBARRAY), "Missing IDType");
96
97 static IDType *idtype_from_name(const char *str)
98 {
99         int i = ARRAY_SIZE(idtypes);
100
101         while (i--) {
102                 if (STREQ(str, idtypes[i].name)) {
103                         return &idtypes[i];
104                 }
105         }
106
107         return NULL;
108 }
109 static IDType *idtype_from_code(short idcode)
110 {
111         int i = ARRAY_SIZE(idtypes);
112
113         while (i--)
114                 if (idcode == idtypes[i].code)
115                         return &idtypes[i];
116
117         return NULL;
118 }
119
120 /**
121  * Return if the ID code is a valid ID code.
122  *
123  * \param idcode: The code to check.
124  * \return Boolean, 0 when invalid.
125  */
126 bool BKE_idcode_is_valid(short idcode)
127 {
128         return idtype_from_code(idcode) ? true : false;
129 }
130
131 /**
132  * Return non-zero when an ID type is linkable.
133  *
134  * \param idcode: The code to check.
135  * \return Boolean, 0 when non linkable.
136  */
137 bool BKE_idcode_is_linkable(short idcode)
138 {
139         IDType *idt = idtype_from_code(idcode);
140         BLI_assert(idt);
141         return idt ? ((idt->flags & IDTYPE_FLAGS_ISLINKABLE) != 0) : false;
142 }
143
144 /**
145  * Convert an idcode into a name.
146  *
147  * \param idcode: The code to convert.
148  * \return A static string representing the name of
149  * the code.
150  */
151 const char *BKE_idcode_to_name(short idcode)
152 {
153         IDType *idt = idtype_from_code(idcode);
154         BLI_assert(idt);
155         return idt ? idt->name : NULL;
156 }
157
158 /**
159  * Convert a name into an idcode (ie. ID_SCE)
160  *
161  * \param name: The name to convert.
162  * \return The code for the name, or 0 if invalid.
163  */
164 short BKE_idcode_from_name(const char *name)
165 {
166         IDType *idt = idtype_from_name(name);
167         BLI_assert(idt);
168         return idt ? idt->code : 0;
169 }
170
171 /**
172  * Convert an idcode into an idfilter (e.g. ID_OB -> FILTER_ID_OB).
173  */
174 int BKE_idcode_to_idfilter(const short idcode)
175 {
176 #define CASE_IDFILTER(_id) case ID_##_id: return FILTER_ID_##_id
177
178         switch (idcode) {
179                 CASE_IDFILTER(AC);
180                 CASE_IDFILTER(AR);
181                 CASE_IDFILTER(BR);
182                 CASE_IDFILTER(CA);
183                 CASE_IDFILTER(CF);
184                 CASE_IDFILTER(CU);
185                 CASE_IDFILTER(GD);
186                 CASE_IDFILTER(GR);
187                 CASE_IDFILTER(IM);
188                 CASE_IDFILTER(LA);
189                 CASE_IDFILTER(LS);
190                 CASE_IDFILTER(LT);
191                 CASE_IDFILTER(MA);
192                 CASE_IDFILTER(MB);
193                 CASE_IDFILTER(MC);
194                 CASE_IDFILTER(ME);
195                 CASE_IDFILTER(MSK);
196                 CASE_IDFILTER(NT);
197                 CASE_IDFILTER(OB);
198                 CASE_IDFILTER(PA);
199                 CASE_IDFILTER(PAL);
200                 CASE_IDFILTER(PC);
201                 CASE_IDFILTER(LP);
202                 CASE_IDFILTER(SCE);
203                 CASE_IDFILTER(SPK);
204                 CASE_IDFILTER(SO);
205                 CASE_IDFILTER(TE);
206                 CASE_IDFILTER(TXT);
207                 CASE_IDFILTER(VF);
208                 CASE_IDFILTER(WO);
209                 CASE_IDFILTER(WS);
210                 default:
211                         return 0;
212         }
213
214 #undef CASE_IDFILTER
215 }
216
217 /**
218  * Convert an idfilter into an idcode (e.g. FILTER_ID_OB -> ID_OB).
219  */
220 short BKE_idcode_from_idfilter(const int idfilter)
221 {
222 #define CASE_IDFILTER(_id) case FILTER_ID_##_id: return ID_##_id
223
224         switch (idfilter) {
225                 CASE_IDFILTER(AC);
226                 CASE_IDFILTER(AR);
227                 CASE_IDFILTER(BR);
228                 CASE_IDFILTER(CA);
229                 CASE_IDFILTER(CF);
230                 CASE_IDFILTER(CU);
231                 CASE_IDFILTER(GD);
232                 CASE_IDFILTER(GR);
233                 CASE_IDFILTER(IM);
234                 CASE_IDFILTER(LA);
235                 CASE_IDFILTER(LS);
236                 CASE_IDFILTER(LT);
237                 CASE_IDFILTER(MA);
238                 CASE_IDFILTER(MB);
239                 CASE_IDFILTER(MC);
240                 CASE_IDFILTER(ME);
241                 CASE_IDFILTER(MSK);
242                 CASE_IDFILTER(NT);
243                 CASE_IDFILTER(OB);
244                 CASE_IDFILTER(PA);
245                 CASE_IDFILTER(PAL);
246                 CASE_IDFILTER(PC);
247                 CASE_IDFILTER(LP);
248                 CASE_IDFILTER(SCE);
249                 CASE_IDFILTER(SPK);
250                 CASE_IDFILTER(SO);
251                 CASE_IDFILTER(TE);
252                 CASE_IDFILTER(TXT);
253                 CASE_IDFILTER(VF);
254                 CASE_IDFILTER(WO);
255                 default:
256                         return 0;
257         }
258
259 #undef CASE_IDFILTER
260 }
261
262 /**
263  * Convert an idcode into an index (e.g. ID_OB -> INDEX_ID_OB).
264  */
265 int BKE_idcode_to_index(const short idcode)
266 {
267 #define CASE_IDINDEX(_id) case ID_##_id: return INDEX_ID_##_id
268
269         switch ((ID_Type)idcode) {
270                 CASE_IDINDEX(AC);
271                 CASE_IDINDEX(AR);
272                 CASE_IDINDEX(BR);
273                 CASE_IDINDEX(CA);
274                 CASE_IDINDEX(CF);
275                 CASE_IDINDEX(CU);
276                 CASE_IDINDEX(GD);
277                 CASE_IDINDEX(GR);
278                 CASE_IDINDEX(IM);
279                 CASE_IDINDEX(KE);
280                 CASE_IDINDEX(IP);
281                 CASE_IDINDEX(LA);
282                 CASE_IDINDEX(LI);
283                 CASE_IDINDEX(LS);
284                 CASE_IDINDEX(LT);
285                 CASE_IDINDEX(MA);
286                 CASE_IDINDEX(MB);
287                 CASE_IDINDEX(MC);
288                 CASE_IDINDEX(ME);
289                 CASE_IDINDEX(MSK);
290                 CASE_IDINDEX(NT);
291                 CASE_IDINDEX(OB);
292                 CASE_IDINDEX(PA);
293                 CASE_IDINDEX(PAL);
294                 CASE_IDINDEX(PC);
295                 CASE_IDINDEX(LP);
296                 CASE_IDINDEX(SCE);
297                 CASE_IDINDEX(SCR);
298                 CASE_IDINDEX(SPK);
299                 CASE_IDINDEX(SO);
300                 CASE_IDINDEX(TE);
301                 CASE_IDINDEX(TXT);
302                 CASE_IDINDEX(VF);
303                 CASE_IDINDEX(WM);
304                 CASE_IDINDEX(WO);
305                 CASE_IDINDEX(WS);
306         }
307
308         BLI_assert(0);
309         return -1;
310
311 #undef CASE_IDINDEX
312 }
313
314 /**
315  * Get an idcode from an index (e.g. INDEX_ID_OB -> ID_OB).
316  */
317 short BKE_idcode_from_index(const int index)
318 {
319 #define CASE_IDCODE(_id) case INDEX_ID_##_id: return ID_##_id
320
321         switch (index) {
322                 CASE_IDCODE(AC);
323                 CASE_IDCODE(AR);
324                 CASE_IDCODE(BR);
325                 CASE_IDCODE(CA);
326                 CASE_IDCODE(CF);
327                 CASE_IDCODE(CU);
328                 CASE_IDCODE(GD);
329                 CASE_IDCODE(GR);
330                 CASE_IDCODE(IM);
331                 CASE_IDCODE(KE);
332                 CASE_IDCODE(IP);
333                 CASE_IDCODE(LA);
334                 CASE_IDCODE(LI);
335                 CASE_IDCODE(LS);
336                 CASE_IDCODE(LT);
337                 CASE_IDCODE(MA);
338                 CASE_IDCODE(MB);
339                 CASE_IDCODE(MC);
340                 CASE_IDCODE(ME);
341                 CASE_IDCODE(MSK);
342                 CASE_IDCODE(NT);
343                 CASE_IDCODE(OB);
344                 CASE_IDCODE(PA);
345                 CASE_IDCODE(PAL);
346                 CASE_IDCODE(PC);
347                 CASE_IDCODE(LP);
348                 CASE_IDCODE(SCE);
349                 CASE_IDCODE(SCR);
350                 CASE_IDCODE(SPK);
351                 CASE_IDCODE(SO);
352                 CASE_IDCODE(TE);
353                 CASE_IDCODE(TXT);
354                 CASE_IDCODE(VF);
355                 CASE_IDCODE(WM);
356                 CASE_IDCODE(WO);
357                 CASE_IDCODE(WS);
358         }
359
360         BLI_assert(0);
361         return -1;
362
363 #undef CASE_IDCODE
364 }
365
366 /**
367  * Convert an idcode into a name (plural).
368  *
369  * \param idcode: The code to convert.
370  * \return A static string representing the name of
371  * the code.
372  */
373 const char *BKE_idcode_to_name_plural(short idcode)
374 {
375         IDType *idt = idtype_from_code(idcode);
376         BLI_assert(idt);
377         return idt ? idt->plural : NULL;
378 }
379
380 /**
381  * Convert an idcode into its translations' context.
382  *
383  * \param idcode: The code to convert.
384  * \return A static string representing the i18n context of the code.
385  */
386 const char *BKE_idcode_to_translation_context(short idcode)
387 {
388         IDType *idt = idtype_from_code(idcode);
389         BLI_assert(idt);
390         return idt ? idt->i18n_context : BLT_I18NCONTEXT_DEFAULT;
391 }
392
393 /**
394  * Return an ID code and steps the index forward 1.
395  *
396  * \param index: start as 0.
397  * \return the code, 0 when all codes have been returned.
398  */
399 short BKE_idcode_iter_step(int *index)
400 {
401         return (*index < ARRAY_SIZE(idtypes)) ? idtypes[(*index)++].code : 0;
402 }