2
$\begingroup$

How can I distinguish the type of node in Python when importing node groups from other files?

I want, after I read a folder with blend files and search in the files for objects of type node_groups, to set a different action depending on the type of the group.

The crucial thing is: The type of the nodes must be clearly recognizable in this case BEFORE the node has even been imported!

Finally there are GeometryNodeTree, ShaderNodeTree, CompositorNodeTree and TextureNodeTree, but I somehow can't find a way to query this type.

This is the code I use to read the files:

for fn in os.listdir(dirpath):
    if fn.endswith(".blend"):
        filepath = os.path.join(dirpath, fn)
        with bpy.data.libraries.load(filepath) as (data_from, data_to):
            for group_name in data_from.node_groups:
                print("Very well, and what type are we talking about here?")

What do I need this for?

I would like to improve the addon Node Presets and here it needs a distinction of the nodes, so that not for each node type a separate path must be specified: How can I use custom node groups as node templates (Blender 3.1)?

$\endgroup$
2
  • $\begingroup$ How did you solve this? Did you try something similar to this? $\endgroup$ Aug 26, 2023 at 6:01
  • 1
    $\begingroup$ @HarryMcKenzie You're right, that would indeed be a solution. However, I'm still hesitant to use this technique because it just takes way too long to launch another Blender instance in the background (without notifying the user). Of course, this could be reduced by always checking a hash of the files and only setting an action if the file has changed, as discussed in the chat. I solved the problem for now by simply separating the paths for each node type. Not the best solution, but it works. $\endgroup$
    – quellenform
    Aug 26, 2023 at 7:09

1 Answer 1

2
+50
$\begingroup$

if data_from.node_groups is the string name of the node group, you need to look it up in bpy.data.node_groups and then walk its nodes member accessing the name and type field, something like this:

for node in bpy.data.node_groups[group_name].nodes:
    print(node.name, node.type)

Here's the output from a sample node group:

Image Texture TEX_IMAGE
Mapping MAPPING
Texture Coordinate TEX_COORD
Group Input GROUP_INPUT
Group Output GROUP_OUTPUT

and here's the node group that generated that output:

Sample Node group

$\endgroup$
6
  • $\begingroup$ Thanks @MartyFouts, that works so far, but unfortunately not as long as the node has not been imported. I try to read the nodes directly from external files and decide what to do with them based on their type before importing them. $\endgroup$
    – quellenform
    Apr 8, 2022 at 19:29
  • 1
    $\begingroup$ @quellenform As far as I know you can't do that. There's really no support for reading parts of a blend file; and there are discussions on devtalk where developers make it clear that they don't want such code for various good reasons. Pretty much the entire BPY API is based on data being in data blocks on the current running instance. $\endgroup$ Apr 8, 2022 at 19:32
  • $\begingroup$ OK, thanks for this information! What exactly are the reasons? Personally, I think that this decision (from a programmer's point of view) is quite nonsense. Surely one should declare the type of an object if it has completely different properties, right? $\endgroup$
    – quellenform
    Apr 8, 2022 at 19:35
  • $\begingroup$ It's been a year since I looked into it and I can't find the exact thread now. I thought it was this thread but it doesn't seem to have the remarks I vaguely remember. $\endgroup$ Apr 8, 2022 at 19:42
  • $\begingroup$ OK, I believe you now, there really seems to be no viable solution to this problem. Nevertheless, you get my 50RP, as a small thank you! ;-) $\endgroup$
    – quellenform
    Apr 29, 2022 at 17:41

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .