3
$\begingroup$

I created a "voxel" data visualization system via DICOM files in Blender. However, as the DICOM changes from patient to patient, it is necessary to allow the user to change the transparency of the slices, for this it is necessary to link the parameters to a node group.

The problem is that I am unable to link an existing group in the scene during the lasso -for-, when creating the slices from DICOM.

I tried this command:

bpy.ops.node.add_node (type = "ShaderNodeGroup", use_transform = True, settings = [{"name": "node_tree", "value": "bpy.data.node_groups ['GroupVoxelShader']"}])

But it gives error of poll() failed.

So I tried to create the node group inside the material first and then make a link, so it creates a block, but with the following name: Missing Data-Block.

enter image description here

I tried to find a way to link with the group bpy.data.node_groups['GroupVoxelShader'] as shown in the image below:

enter image description here

But I can only do this manually, it was not possible to link automatically already in lasso, with Python.

Does anyone know how to do this? Thanks for reading!

$\endgroup$

1 Answer 1

3
$\begingroup$
import bpy

#you need to somehow specify the material(maybe loop through a few?..):
node_tree = bpy.data.materials['Material'].node_tree
# And a node group that you have in the file:
node_group_name = "NodeGroup"


nodes = node_tree.nodes
links = node_tree.links

# Let's at least check if some image node and output node exists 
# you might need a way to determine which ones to use if there are multiple 
image_node = output_node = None
node_group_exists = False
for node in nodes:    
    if node.type == 'TEX_IMAGE':
        image_node = node
    elif node.type == 'OUTPUT_MATERIAL':
        output_node = node
    elif node.type == 'GROUP': # in case the script was used already
        if node.node_tree.name == node_group_name:
            node_group_exists = True
            print('exists')
            group_node = node

if output_node and image_node:
    if node_group_name in bpy.data.node_groups and not node_group_exists:
        group_node=nodes.new("ShaderNodeGroup")
        # Creating the group is not enough, we need to specify data(node tree) for it
        group_node.node_tree = bpy.data.node_groups[node_group_name] 
        group_node.location = (0,0) #This is default anyway,  but in case you wish to move it

    links.new(image_node.outputs[0], group_node.inputs[0])
    links.new(group_node.outputs[0], output_node.inputs[0])
$\endgroup$
1
  • $\begingroup$ It works! Thank you very much! $\endgroup$
    – cogitas3d
    Apr 14, 2020 at 13:38

You must log in to answer this question.

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