Welcome to mirror list, hosted at ThFree Co, Russian Federation.

rna_access.c « intern « makesrna « blender « source - git.blender.org/blender.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: f666d2ce08b9bce2da3cd17a34643e55b33ffa5a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
/**
 * $Id$
 *
 * ***** BEGIN GPL LICENSE BLOCK *****
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software Foundation,
 * Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 *
 * Contributor(s): Blender Foundation (2008).
 *
 * ***** END GPL LICENSE BLOCK *****
 */

#include <stdlib.h>
#include <string.h>

#include "MEM_guardedalloc.h"

#include "RNA_access.h"
#include "RNA_types.h"

#include "rna_internal.h"

#include "BLI_blenlib.h"
#include "BLI_dynstr.h"

/* Pointer */

void RNA_pointer_main_get(struct Main *main, struct PointerRNA *r_ptr)
{
	r_ptr->data= main;
	r_ptr->type= &RNA_Main;
	r_ptr->id.data= NULL;
	r_ptr->id.type= NULL;
}

static void rna_pointer_inherit_id(PointerRNA *parent, PointerRNA *ptr)
{
	if(ptr->type && ptr->type->flag & STRUCT_ID) {
		ptr->id.data= ptr->data;
		ptr->id.type= ptr->type;
	}
	else {
		ptr->id.data= parent->id.data;
		ptr->id.type= parent->id.type;
	}
}

/* Property */

int RNA_property_editable(PropertyRNA *prop, PointerRNA *ptr)
{
	return (prop->flag & PROP_EDITABLE);
}

int RNA_property_evaluated(PropertyRNA *prop, PointerRNA *ptr)
{
	return (prop->flag & PROP_EVALUATED);
}

void RNA_property_notify(PropertyRNA *prop, struct bContext *C, PointerRNA *ptr)
{
	if(prop->notify)
		prop->notify(C, ptr);
}

int RNA_property_boolean_get(PropertyRNA *prop, PointerRNA *ptr)
{
	BooleanPropertyRNA *bprop= (BooleanPropertyRNA*)prop;

	return bprop->get(ptr);
}

void RNA_property_boolean_set(PropertyRNA *prop, PointerRNA *ptr, int value)
{
	BooleanPropertyRNA *bprop= (BooleanPropertyRNA*)prop;

	bprop->set(ptr, value);
}

int RNA_property_boolean_get_array(PropertyRNA *prop, PointerRNA *ptr, int index)
{
	BooleanPropertyRNA *bprop= (BooleanPropertyRNA*)prop;

	return bprop->getarray(ptr, index);
}

void RNA_property_boolean_set_array(PropertyRNA *prop, PointerRNA *ptr, int index, int value)
{
	BooleanPropertyRNA *bprop= (BooleanPropertyRNA*)prop;

	bprop->setarray(ptr, index, value);
}

int RNA_property_int_get(PropertyRNA *prop, PointerRNA *ptr)
{
	IntPropertyRNA *iprop= (IntPropertyRNA*)prop;

	return iprop->get(ptr);
}

void RNA_property_int_set(PropertyRNA *prop, PointerRNA *ptr, int value)
{
	IntPropertyRNA *iprop= (IntPropertyRNA*)prop;

	iprop->set(ptr, value);
}

int RNA_property_int_get_array(PropertyRNA *prop, PointerRNA *ptr, int index)
{
	IntPropertyRNA *iprop= (IntPropertyRNA*)prop;

	return iprop->getarray(ptr, index);
}

void RNA_property_int_set_array(PropertyRNA *prop, PointerRNA *ptr, int index, int value)
{
	IntPropertyRNA *iprop= (IntPropertyRNA*)prop;

	iprop->setarray(ptr, index, value);
}

float RNA_property_float_get(PropertyRNA *prop, PointerRNA *ptr)
{
	FloatPropertyRNA *fprop= (FloatPropertyRNA*)prop;

	return fprop->get(ptr);
}

void RNA_property_float_set(PropertyRNA *prop, PointerRNA *ptr, float value)
{
	FloatPropertyRNA *fprop= (FloatPropertyRNA*)prop;

	fprop->set(ptr, value);
}

float RNA_property_float_get_array(PropertyRNA *prop, PointerRNA *ptr, int index)
{
	FloatPropertyRNA *fprop= (FloatPropertyRNA*)prop;

	return fprop->getarray(ptr, index);
}

void RNA_property_float_set_array(PropertyRNA *prop, PointerRNA *ptr, int index, float value)
{
	FloatPropertyRNA *fprop= (FloatPropertyRNA*)prop;

	fprop->setarray(ptr, index, value);
}

void RNA_property_string_get(PropertyRNA *prop, PointerRNA *ptr, char *value)
{
	StringPropertyRNA *sprop= (StringPropertyRNA*)prop;

	sprop->get(ptr, value);
}

int RNA_property_string_length(PropertyRNA *prop, PointerRNA *ptr)
{
	StringPropertyRNA *sprop= (StringPropertyRNA*)prop;

	return sprop->length(ptr);
}

void RNA_property_string_set(PropertyRNA *prop, PointerRNA *ptr, const char *value)
{
	StringPropertyRNA *sprop= (StringPropertyRNA*)prop;

	sprop->set(ptr, value);
}

int RNA_property_enum_get(PropertyRNA *prop, PointerRNA *ptr)
{
	EnumPropertyRNA *eprop= (EnumPropertyRNA*)prop;

	return eprop->get(ptr);
}

void RNA_property_enum_set(PropertyRNA *prop, PointerRNA *ptr, int value)
{
	EnumPropertyRNA *eprop= (EnumPropertyRNA*)prop;

	eprop->set(ptr, value);
}

void RNA_property_pointer_get(PropertyRNA *prop, PointerRNA *ptr, PointerRNA *r_ptr)
{
	PointerPropertyRNA *pprop= (PointerPropertyRNA*)prop;

	r_ptr->data= pprop->get(ptr);

	if(r_ptr->data) {
		r_ptr->type= RNA_property_pointer_type(prop, ptr);
		rna_pointer_inherit_id(ptr, r_ptr);
	}
	else
		memset(r_ptr, 0, sizeof(*r_ptr));
}

void RNA_property_pointer_set(PropertyRNA *prop, PointerRNA *ptr, PointerRNA *ptr_value)
{
	PointerPropertyRNA *pprop= (PointerPropertyRNA*)prop;

	pprop->set(ptr, ptr_value->data);
}

StructRNA *RNA_property_pointer_type(PropertyRNA *prop, PointerRNA *ptr)
{
	PointerPropertyRNA *pprop= (PointerPropertyRNA*)prop;

	if(pprop->type)
		return pprop->type(ptr);
	
	return pprop->structtype;
}

void RNA_property_collection_begin(PropertyRNA *prop, CollectionPropertyIterator *iter, PointerRNA *ptr)
{
	CollectionPropertyRNA *cprop= (CollectionPropertyRNA*)prop;

	iter->pointer= *ptr;
	cprop->begin(iter, ptr);
}

void RNA_property_collection_next(PropertyRNA *prop, CollectionPropertyIterator *iter)
{
	CollectionPropertyRNA *cprop= (CollectionPropertyRNA*)prop;

	cprop->next(iter);
}

void RNA_property_collection_end(PropertyRNA *prop, CollectionPropertyIterator *iter)
{
	CollectionPropertyRNA *cprop= (CollectionPropertyRNA*)prop;

	if(cprop->end)
		cprop->end(iter);
}

void RNA_property_collection_get(PropertyRNA *prop, CollectionPropertyIterator *iter, PointerRNA *r_ptr)
{
	CollectionPropertyRNA *cprop= (CollectionPropertyRNA*)prop;

	r_ptr->data= cprop->get(iter);

	if(r_ptr->data) {
		r_ptr->type= RNA_property_collection_type(prop, iter);
		rna_pointer_inherit_id(&iter->pointer, r_ptr);
	}
	else
		memset(r_ptr, 0, sizeof(*r_ptr));
}

StructRNA *RNA_property_collection_type(PropertyRNA *prop, CollectionPropertyIterator *iter)
{
	CollectionPropertyRNA *cprop= (CollectionPropertyRNA*)prop;

	if(cprop->type)
		return cprop->type(iter);
	
	return cprop->structtype;
}

int RNA_property_collection_length(PropertyRNA *prop, PointerRNA *ptr)
{
	CollectionPropertyRNA *cprop= (CollectionPropertyRNA*)prop;

	if(cprop->length) {
		return cprop->length(ptr);
	}
	else {
		CollectionPropertyIterator iter;
		int length= 0;

		for(cprop->begin(&iter, ptr); iter.valid; cprop->next(&iter))
			length++;

		if(cprop->end)
			cprop->end(&iter);

		return length;
	}
}

int RNA_property_collection_lookup_int(PropertyRNA *prop, PointerRNA *ptr, int key, PointerRNA *r_ptr)
{
	CollectionPropertyRNA *cprop= (CollectionPropertyRNA*)prop;

	if(cprop->lookupint) {
		/* we have a callback defined, use it */
		r_ptr->data= cprop->lookupint(ptr, key, &r_ptr->type);

		if(r_ptr->data) {
			if(!r_ptr->type)
				r_ptr->type= cprop->structtype;
			rna_pointer_inherit_id(ptr, r_ptr);

			return 1;
		}
		else {
			memset(r_ptr, 0, sizeof(*r_ptr));
			return 0;
		}
	}
	else {
		/* no callback defined, just iterate and find the nth item */
		CollectionPropertyIterator iter;
		int i;

		RNA_property_collection_begin(prop, &iter, ptr);
		for(i=0; iter.valid; RNA_property_collection_next(prop, &iter), i++) {
			if(i == key) {
				RNA_property_collection_get(prop, &iter, r_ptr);
				break;
			}
		}
		RNA_property_collection_end(prop, &iter);

		if(!iter.valid)
			memset(r_ptr, 0, sizeof(*r_ptr));

		return iter.valid;
	}
}

int RNA_property_collection_lookup_string(PropertyRNA *prop, PointerRNA *ptr, const char *key, PointerRNA *r_ptr)
{
	CollectionPropertyRNA *cprop= (CollectionPropertyRNA*)prop;

	if(cprop->lookupstring) {
		/* we have a callback defined, use it */
		r_ptr->data= cprop->lookupstring(ptr, key, &r_ptr->type);

		if(r_ptr->data) {
			if(!r_ptr->type)
				r_ptr->type= cprop->structtype;
			rna_pointer_inherit_id(ptr, r_ptr);

			return 1;
		}
		else {
			memset(r_ptr, 0, sizeof(*r_ptr));
			return 0;
		}
	}
	else {
		/* no callback defined, compare with name properties if they exist */
		CollectionPropertyIterator iter;
		PropertyRNA *prop;
		PointerRNA iterptr;
		char name[256], *nameptr;
		int length, alloc, found= 0;

		RNA_property_collection_begin(prop, &iter, ptr);
		for(; iter.valid; RNA_property_collection_next(prop, &iter)) {
			RNA_property_collection_get(prop, &iter, &iterptr);

			if(iterptr.data && iterptr.type->nameproperty) {
				prop= iterptr.type->nameproperty;

				length= RNA_property_string_length(prop, &iterptr);

				if(sizeof(name)-1 < length) {
					nameptr= name;
					alloc= 0;
				}
				else {
					nameptr= MEM_mallocN(sizeof(char)*length+1, "RNA_lookup_string");
					alloc= 1;
				}

				RNA_property_string_get(prop, &iterptr, nameptr);

				if(strcmp(nameptr, key) == 0) {
					*r_ptr= iterptr;
					found= 1;
				}

				if(alloc)
					MEM_freeN(nameptr);

				if(found)
					break;
			}
		}
		RNA_property_collection_end(prop, &iter);

		if(!iter.valid)
			memset(r_ptr, 0, sizeof(*r_ptr));

		return iter.valid;
	}
}

/* Standard iterator functions */

void rna_iterator_listbase_begin(CollectionPropertyIterator *iter, ListBase *lb)
{
	iter->internal= lb->first;
	iter->valid= (iter->internal != NULL);
}

void rna_iterator_listbase_next(CollectionPropertyIterator *iter)
{
	iter->internal= ((Link*)iter->internal)->next;
	iter->valid= (iter->internal != NULL);
}

void *rna_iterator_listbase_get(CollectionPropertyIterator *iter)
{
	return iter->internal;
}

void rna_iterator_array_begin(CollectionPropertyIterator *iter, void *ptr, int itemsize, int length)
{
	ArrayIterator *internal;

	internal= MEM_callocN(sizeof(ArrayIterator), "ArrayIterator");
	internal->ptr= ptr;
	internal->endptr= ((char*)ptr)+length*itemsize;
	internal->itemsize= itemsize;

	iter->internal= internal;
	iter->valid= (internal->ptr != internal->endptr);
}

void rna_iterator_array_next(CollectionPropertyIterator *iter)
{
	ArrayIterator *internal= iter->internal;

	internal->ptr += internal->itemsize;
	iter->valid= (internal->ptr != internal->endptr);
}

void *rna_iterator_array_get(CollectionPropertyIterator *iter)
{
	ArrayIterator *internal= iter->internal;

	return internal->ptr;
}

void rna_iterator_array_end(CollectionPropertyIterator *iter)
{
	MEM_freeN(iter->internal);
}

/* RNA Path - Experiment */

static char *rna_path_token(const char **path, char *fixedbuf, int fixedlen, int bracket)
{
	const char *p;
	char *buf;
	int i, j, len, escape;

	len= 0;

	if(bracket) {
		/* get data between [], check escaping ] with \] */
		if(**path == '[') (*path)++;
		else return NULL;

		p= *path;

		escape= 0;
		while(*p && (*p != ']' || escape)) {
			escape= (*p == '\\');
			len++;
			p++;
		}

		if(*p != ']') return NULL;
	}
	else {
		/* get data until . or [ */
		p= *path;

		while(*p && *p != '.' && *p != '[') {
			len++;
			p++;
		}
	}
	
	/* empty, return */
	if(len == 0)
		return NULL;
	
	/* try to use fixed buffer if possible */
	if(len+1 < fixedlen)
		buf= fixedbuf;
	else
		buf= MEM_callocN(sizeof(char)*(len+1), "rna_path_token");

	/* copy string, taking into account escaped ] */
	for(p=*path, i=0, j=0; i<len; i++, p++) {
		if(*p == '\\' && *(p+1) == ']');
		else buf[j++]= *p;
	}

	buf[j]= 0;

	/* set path to start of next token */
	if(*p == ']') p++;
	if(*p == '.') p++;
	*path= p;

	return buf;
}

int RNA_path_resolve(PointerRNA *ptr, const char *path, PointerRNA *r_ptr, PropertyRNA **r_prop)
{
	PropertyRNA *prop;
	PointerRNA curptr, nextptr;
	char fixedbuf[256], *token;
	int len, intkey;

	prop= NULL;
	curptr= *ptr;

	while(*path) {
		/* look up property name in current struct */
		token= rna_path_token(&path, fixedbuf, sizeof(fixedbuf), 0);

		if(!token)
			return 0;

		for(prop=curptr.type->properties.first; prop; prop=prop->next)
			if(strcmp(token, prop->cname) == 0)
				break;

		if(token != fixedbuf)
			MEM_freeN(token);

		if(!prop)
			return 0;

		/* now look up the value of this property if it is a pointer or
		 * collection, otherwise return the property rna so that the
		 * caller can read the value of the property itself */
		if(prop->type == PROP_POINTER) {
			RNA_property_pointer_get(prop, &curptr, &nextptr);

			if(nextptr.data)
				curptr= nextptr;
			else
				return 0;
		}
		else if(prop->type == PROP_COLLECTION && *path) {
			/* resolve the lookup with [] brackets */
			token= rna_path_token(&path, fixedbuf, sizeof(fixedbuf), 1);

			if(!token)
				return 0;

			len= strlen(token);

			/* check for "" to see if it is a string */
			if(len >= 2 && *token == '"' && token[len-2] == '"') {
				/* strip away "" */
				token[len-2]= 0;
				RNA_property_collection_lookup_string(prop, &curptr, token+1, &nextptr);
			}
			else {
				/* otherwise do int lookup */
				intkey= atoi(token);
				RNA_property_collection_lookup_int(prop, &curptr, intkey, &nextptr);
			}

			if(token != fixedbuf)
				MEM_freeN(token);

			if(nextptr.data)
				curptr= nextptr;
			else
				return 0;
		}
	}

	*r_ptr= curptr;
	*r_prop= prop;

	return 1;
}

char *RNA_path_append(const char *path, PropertyRNA *prop, int intkey, const char *strkey)
{
	DynStr *dynstr;
	const char *s;
	char appendstr[128], *result;
	
	dynstr= BLI_dynstr_new();

	/* add .cname */
	if(path) {
		BLI_dynstr_append(dynstr, (char*)path);
		if(*path)
			BLI_dynstr_append(dynstr, ".");
	}

	BLI_dynstr_append(dynstr, (char*)prop->cname);

	if(prop->type == PROP_COLLECTION) {
		/* add ["strkey"] or [intkey] */
		BLI_dynstr_append(dynstr, "[");

		if(strkey) {
			BLI_dynstr_append(dynstr, "\"");
			for(s=strkey; *s; s++) {
				if(*s == '[') {
					appendstr[0]= '\\';
					appendstr[1]= *s;
					appendstr[2]= 0;
				}
				else {
					appendstr[0]= *s;
					appendstr[1]= 0;
				}
				BLI_dynstr_append(dynstr, appendstr);
			}
			BLI_dynstr_append(dynstr, "\"");
		}
		else {
			sprintf(appendstr, "%d", intkey);
			BLI_dynstr_append(dynstr, appendstr);
		}

		BLI_dynstr_append(dynstr, "]");
	}

	result= BLI_dynstr_get_cstring(dynstr);
	BLI_dynstr_free(dynstr);

	return result;
}

char *RNA_path_back(const char *path)
{
	char fixedbuf[256];
	const char *previous, *current;
	char *result, *token;
	int i;

	if(!path)
		return NULL;

	previous= NULL;
	current= path;

	/* parse token by token until the end, then we back up to the previous
	 * position and strip of the next token to get the path one step back */
	while(*current) {
		token= rna_path_token(&current, fixedbuf, sizeof(fixedbuf), 0);

		if(!token)
			return NULL;
		if(token != fixedbuf)
			MEM_freeN(token);

		/* in case of collection we also need to strip off [] */
		token= rna_path_token(&current, fixedbuf, sizeof(fixedbuf), 1);
		if(token && token != fixedbuf)
			MEM_freeN(token);
		
		if(!*current)
			break;

		previous= current;
	}

	if(!previous)
		return NULL;

	/* copy and strip off last token */
	i= previous - path;
	result= BLI_strdup(path);

	if(i > 0 && result[i-1] == '.') i--;
	result[i]= 0;

	return result;
}