* RNA: utility function to retrieve strings. It will use a fixed
[blender.git] / source / blender / makesrna / intern / rna_dependency.c
1
2 #include <stdio.h>
3 #include <stdlib.h>
4 #include <string.h>
5
6 #include "DNA_scene_types.h"
7
8 #include "RNA_access.h"
9 #include "RNA_types.h"
10
11 typedef struct RNAGenDeps {
12         void *udata;
13         PropDependencyCallback cb;
14 } RNAGenDeps;
15
16 static void rna_generate_deps(RNAGenDeps *gen, PointerRNA *ptr, PointerRNA *idptr)
17 {
18         PropertyRNA *prop;
19         PointerRNA pptr;
20         CollectionPropertyIterator iter;
21
22         /* traverse recursively into ID struct properties, other
23          * pointers we potentially add as dependencies */
24
25         for(prop=ptr->type->properties.first; prop; prop=prop->next) {
26                 if(prop->type == PROP_POINTER) {
27                         RNA_property_pointer_get(prop, ptr, &pptr);
28
29                         if(pptr.data && pptr.type) {
30                                 if(idptr && (pptr.type->flag & STRUCT_ID)) {
31                                         if(prop->flag & PROP_EVALUATE_DEPENDENCY)
32                                                 gen->cb(gen->udata, ptr, &pptr);
33                                         else if(prop->flag & PROP_INVERSE_EVALUATE_DEPENDENCY)
34                                                 gen->cb(gen->udata, ptr, &pptr);
35                                 }
36                                 else
37                                         rna_generate_deps(gen, &pptr, (idptr)? idptr: &pptr);
38                         }
39                 }
40                 else if(prop->type == PROP_COLLECTION) {
41                         RNA_property_collection_begin(prop, &iter, ptr);
42
43                         while(iter.valid) {
44                                 RNA_property_collection_get(prop, &iter, &pptr);
45
46                                 if(pptr.data && pptr.type) {
47                                         if(idptr && (pptr.type->flag & STRUCT_ID)) {
48                                                 if(prop->flag & PROP_EVALUATE_DEPENDENCY)
49                                                         gen->cb(gen->udata, ptr, &pptr);
50                                                 else if(prop->flag & PROP_INVERSE_EVALUATE_DEPENDENCY)
51                                                         gen->cb(gen->udata, ptr, &pptr);
52                                         }
53                                         else
54                                                 rna_generate_deps(gen, &pptr, (idptr)? idptr: &pptr);
55                                 }
56                                 
57                                 RNA_property_collection_next(prop, &iter);
58                         }
59
60                         RNA_property_collection_end(prop, &iter);
61                 }
62         }
63 }
64
65 void RNA_generate_dependencies(PointerRNA *ptr, void *udata, PropDependencyCallback cb)
66 {
67         RNAGenDeps gen;
68
69         gen.udata= udata;
70         gen.cb= cb;
71
72         rna_generate_deps(&gen, ptr, NULL);
73 }
74
75 void RNA_test_dependencies_cb(void *udata, PointerRNA *from, PointerRNA *to)
76 {
77         PropertyRNA *prop;
78         char name[256], nameto[256];
79         
80         prop= from->type? from->type->nameproperty: NULL;
81         if(prop) RNA_property_string_get(prop, from, name);
82         else strcpy(name, "unknown");
83
84         prop= from->type? from->type->nameproperty: NULL;
85         if(prop) RNA_property_string_get(prop, to, nameto);
86         else strcpy(nameto, "unknown");
87         
88         printf("%s (%s) -> %s (%s)\n", name, from->type->cname, nameto, to->type->cname);
89 }
90