updates to patch from Dan Eicher, allow adding a NodeGroup through bpy.data.node_grou...
authorCampbell Barton <ideasman42@gmail.com>
Fri, 3 Dec 2010 03:44:39 +0000 (03:44 +0000)
committerCampbell Barton <ideasman42@gmail.com>
Fri, 3 Dec 2010 03:44:39 +0000 (03:44 +0000)
made some minor changes.

source/blender/blenkernel/BKE_node.h
source/blender/blenkernel/intern/node.c
source/blender/editors/space_node/node_edit.c
source/blender/makesrna/intern/rna_main_api.c
source/blender/makesrna/intern/rna_nodetree.c

index 7b4925b..0e96587 100644 (file)
@@ -129,7 +129,7 @@ typedef struct bNodeType {
 
 void                   ntreeVerifyTypes(struct bNodeTree *ntree);
 
-struct bNodeTree *ntreeAddTree(int type);
+struct bNodeTree *ntreeAddTree(const char *name, int type, const short is_group);
 void                   ntreeInitTypes(struct bNodeTree *ntree);
 
 void                   ntreeMakeOwnType(struct bNodeTree *ntree);
index 4c88cbf..7f6fcdb 100644 (file)
@@ -496,9 +496,7 @@ bNode *nodeMakeGroupFromSelected(bNodeTree *ntree)
                return NULL;
        
        /* OK! new nodetree */
-       ngroup= alloc_libblock(&G.main->nodetree, ID_NT, "NodeGroup");
-       ngroup->type= ntree->type;
-       ngroup->alltypes= ntree->alltypes;
+       ngroup= ntreeAddTree("NodeGroup", ntree->type, TRUE);
        
        /* move nodes over */
        for(node= ntree->nodes.first; node; node= nextn) {
@@ -884,6 +882,11 @@ bNode *nodeAddNodeType(bNodeTree *ntree, int type, bNodeTree *ngroup, ID *id)
        bNode *node= NULL;
        bNodeType *ntype= NULL;
 
+       if (ngroup && BLI_findindex(&G.main->nodetree, ngroup)==-1) {
+               printf("nodeAddNodeType() error: '%s' not in main->nodetree\n", ngroup->id.name);
+               return NULL;
+       }
+
        if(type>=NODE_DYNAMIC_MENU) {
                int a=0, idx= type-NODE_DYNAMIC_MENU;
                ntype= ntree->alltypes.first;
@@ -1034,21 +1037,22 @@ void nodeRemSocketLinks(bNodeTree *ntree, bNodeSocket *sock)
 }
 
 
-bNodeTree *ntreeAddTree(int type)
+bNodeTree *ntreeAddTree(const char *name, int type, const short is_group)
 {
-       bNodeTree *ntree= MEM_callocN(sizeof(bNodeTree), "new node tree");
+       bNodeTree *ntree;
+
+       if (is_group)
+               ntree= alloc_libblock(&G.main->nodetree, ID_NT, name);
+       else {
+               ntree= MEM_callocN(sizeof(bNodeTree), "new node tree");
+               *( (short *)ntree->id.name )= type;
+               BLI_strncpy(ntree->id.name+2, name, sizeof(ntree->id.name));
+       }
+
        ntree->type= type;
        ntree->alltypes.first = NULL;
        ntree->alltypes.last = NULL;
 
-       /* this helps RNA identify ID pointers as nodetree */
-       if(ntree->type==NTREE_SHADER)
-               BLI_strncpy(ntree->id.name, "NTShader Nodetree", sizeof(ntree->id.name));
-       else if(ntree->type==NTREE_COMPOSIT)
-               BLI_strncpy(ntree->id.name, "NTCompositing Nodetree", sizeof(ntree->id.name));
-       else if(ntree->type==NTREE_TEXTURE)
-               BLI_strncpy(ntree->id.name, "NTTexture Nodetree", sizeof(ntree->id.name));
-       
        ntreeInitTypes(ntree);
        return ntree;
 }
index 46ebfad..5dd4b98 100644 (file)
@@ -244,7 +244,7 @@ void ED_node_shader_default(Material *ma)
                return;
        }
        
-       ma->nodetree= ntreeAddTree(NTREE_SHADER);
+       ma->nodetree= ntreeAddTree("Shader Nodetree", NTREE_SHADER, FALSE);
        
        out= nodeAddNodeType(ma->nodetree, SH_NODE_OUTPUT, NULL, NULL);
        out->locx= 300.0f; out->locy= 300.0f;
@@ -275,7 +275,7 @@ void ED_node_composit_default(Scene *sce)
                return;
        }
        
-       sce->nodetree= ntreeAddTree(NTREE_COMPOSIT);
+       sce->nodetree= ntreeAddTree("Compositing Nodetree", NTREE_COMPOSIT, FALSE);
        
        out= nodeAddNodeType(sce->nodetree, CMP_NODE_COMPOSITE, NULL, NULL);
        out->locx= 300.0f; out->locy= 400.0f;
@@ -312,7 +312,7 @@ void ED_node_texture_default(Tex *tx)
                return;
        }
        
-       tx->nodetree= ntreeAddTree(NTREE_TEXTURE);
+       tx->nodetree= ntreeAddTree("Texture Nodetree", NTREE_TEXTURE, FALSE);
        
        out= nodeAddNodeType(tx->nodetree, TEX_NODE_OUTPUT, NULL, NULL);
        out->locx= 300.0f; out->locy= 300.0f;
index 71fe63c..4af91dd 100644 (file)
@@ -201,10 +201,12 @@ void rna_Main_materials_remove(Main *bmain, ReportList *reports, struct Material
        /* XXX python now has invalid pointer? */
 }
 
-// XXX, commended for now, need to see how this can be used with node groups.
-struct bNodeTree *rna_Main_nodetree_new(Main *bmain, int type)
+struct bNodeTree *rna_Main_nodetree_new(Main *bmain, const char *name, int type)
 {
-       bNodeTree *tree = ntreeAddTree(type);
+       bNodeTree *tree = ntreeAddTree(name, type, TRUE);
+
+       ntreeMakeOwnType(tree);
+
        tree->id.us--;
        return tree;
 }
@@ -598,23 +600,24 @@ void RNA_def_main_materials(BlenderRNA *brna, PropertyRNA *cprop)
 void RNA_def_main_node_groups(BlenderRNA *brna, PropertyRNA *cprop)
 {
        StructRNA *srna;
-//     FunctionRNA *func;
-//     PropertyRNA *parm;
+       FunctionRNA *func;
+       PropertyRNA *parm;
 
-/*     static EnumPropertyItem node_nodetree_items[] = {
+       static EnumPropertyItem node_nodetree_items[] = {
        {0, "SHADER",       0,    "Shader",       ""},
        {1, "COMPOSITE",    0,    "Composite",    ""},
        {2, "TEXTURE",      0,    "Texture",      ""},
-       {0, NULL, 0, NULL, NULL}}; */
+       {0, NULL, 0, NULL, NULL}};
 
        RNA_def_property_srna(cprop, "MainNodeTrees");
        srna= RNA_def_struct(brna, "MainNodeTrees", NULL);
        RNA_def_struct_ui_text(srna, "Main Node Trees", "Collection of node trees");
 
-#if 0 // need to see some examples of using these functions before enabling.
        func= RNA_def_function(srna, "new", "rna_Main_nodetree_new");
        RNA_def_function_ui_description(func, "Add a new node tree to the main database");
-       parm= RNA_def_enum(func, "type", node_nodetree_items, 0, "Type", "The type of curve object to add");
+       parm= RNA_def_string(func, "name", "NodeGroup", 0, "", "New name for the datablock.");
+       RNA_def_property_flag(parm, PROP_REQUIRED);
+       parm= RNA_def_enum(func, "type", node_nodetree_items, 0, "Type", "The type of node_group to add");
        RNA_def_property_flag(parm, PROP_REQUIRED);
        /* return type */
        parm= RNA_def_pointer(func, "tree", "NodeTree", "", "New node tree datablock.");
@@ -625,7 +628,6 @@ void RNA_def_main_node_groups(BlenderRNA *brna, PropertyRNA *cprop)
        RNA_def_function_ui_description(func, "Remove a node tree from the current blendfile.");
        parm= RNA_def_pointer(func, "tree", "NodeTree", "", "Node tree to remove.");
        RNA_def_property_flag(parm, PROP_REQUIRED|PROP_NEVER_NULL);
-#endif
 }
 void RNA_def_main_meshes(BlenderRNA *brna, PropertyRNA *cprop)
 {
index 6297a78..183b64f 100644 (file)
@@ -429,7 +429,13 @@ static EnumPropertyItem *rna_Node_channel_itemf(bContext *C, PointerRNA *ptr, in
 
 static bNode *rna_NodeTree_node_new(bNodeTree *ntree, bContext *C, ReportList *reports, int type, bNodeTree *group)
 {
-       bNode *node= nodeAddNodeType(ntree, type, group, NULL);
+       bNode *node;
+
+       if (type == NODE_GROUP && group == NULL) {
+               BKE_reportf(reports, RPT_ERROR, "node type \'GROUP\' missing group argument");
+               return NULL;
+       }
+       node = nodeAddNodeType(ntree, type, group, NULL);
 
        if (node == NULL) {
                 BKE_reportf(reports, RPT_ERROR, "Unable to create node");
@@ -438,6 +444,9 @@ static bNode *rna_NodeTree_node_new(bNodeTree *ntree, bContext *C, ReportList *r
                nodeVerifyGroup(ntree); /* update group node socket links*/
                NodeTagChanged(ntree, node);
                WM_event_add_notifier(C, NC_NODE|NA_EDITED, ntree);
+
+               if (group)
+                       id_us_plus(&group->id);
        }
 
        return node;