Skip to content

Instantly share code, notes, and snippets.

@p2or
Last active May 21, 2023 19:14
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save p2or/b92d0859c68ac78673a2d0bedb2af09f to your computer and use it in GitHub Desktop.
Save p2or/b92d0859c68ac78673a2d0bedb2af09f to your computer and use it in GitHub Desktop.
Add nodes and frame them #Blender
#https://blender.stackexchange.com/q/99989/3710
import bpy
class FrameSelection(bpy.types.Operator):
"""Add a frame to all selected Nodes"""
bl_idname = "node.frame_selection"
bl_label = "Add Frame"
bl_options = {'REGISTER', 'UNDO'}
@classmethod
def poll(cls, context):
space = context.space_data
return space.type == 'NODE_EDITOR'
def execute(self, context):
space = context.space_data
node_tree = space.node_tree
node_active = context.active_node
node_selected = context.selected_nodes
#context.area.tag_redraw()
#context.space_data.node_tree.update_tag()
'''
if len(node_selected) == 0:
self.report({'ERROR'}, "Nothing in selection")
return {'CANCELLED'}
'''
a = node_tree.nodes.new('ShaderNodeBsdfDiffuse')
a.location = 0,0
b = node_tree.nodes.new('ShaderNodeBsdfDiffuse')
b.location = 200,0
frame = node_tree.nodes.new('NodeFrame')
for node in [a,b]:
node.parent = frame
bpy.ops.node.select_all()
return {'FINISHED'}
def register():
bpy.utils.register_class(FrameSelection)
def unregister():
bpy.utils.unregister_class(FrameSelection)
if __name__ == "__main__":
register()
@zeffii
Copy link

zeffii commented Feb 3, 2018

a fairer comparison would be this:

to be executed in an empty cycles (nodes enabled) material tree called "Material"

import bpy

class FrameSelection(bpy.types.Operator):
    """Add a frame to all selected Nodes"""
    bl_idname = "node.frame_selection"
    bl_label = "Add Frame"
    bl_options = {'REGISTER', 'UNDO'}
    
    @classmethod
    def poll(cls, context):
        space = context.space_data
        return space.type == 'NODE_EDITOR'

    def execute(self, context):
        #space = context.space_data
        #node_tree = space.node_tree
        node_active = context.active_node
        node_selected = context.selected_nodes

        nt = bpy.data.materials['Material'].node_tree
        nodes = nt.nodes

        a = nodes.new('ShaderNodeBsdfDiffuse')
        a.location = 0, 0

        b = nodes.new('ShaderNodeBsdfDiffuse')
        b.location = 200, 0

        f = nodes.new('NodeFrame')
        f.location = -200, 0

        a.parent = f
        b.parent = f        
              
        return {'FINISHED'}

def register():
    bpy.utils.register_class(FrameSelection)


def unregister():
    bpy.utils.unregister_class(FrameSelection)


if __name__ == "__main__":
    register()

@zeffii
Copy link

zeffii commented Feb 3, 2018

or even here any empty Cycles nodebased node tree

import bpy

class AddFramedNodes(bpy.types.Operator):
    """Add a few framed nodes for test"""
    bl_idname = "node.test_add_framed_nodes"
    bl_label = "Add Framed Nodes"
    bl_options = {'REGISTER', 'UNDO'}
    
    @classmethod
    def poll(cls, context):
        space = context.space_data
        return space.type == 'NODE_EDITOR'

    def execute(self, context):
        space = context.space_data
        node_tree = space.node_tree
        # node_active = context.active_node
        # node_selected = context.selected_nodes
        
        if node_tree and space.edit_tree:
            nt = node_tree

        nodes = nt.nodes

        a = nodes.new('ShaderNodeBsdfDiffuse')
        a.location = 0, 0

        b = nodes.new('ShaderNodeBsdfDiffuse')
        b.location = 200, 0

        f = nodes.new('NodeFrame')
        f.location = -200, 0

        a.parent = f
        b.parent = f        
              
        return {'FINISHED'}

def register():
    bpy.utils.register_class(AddFramedNodes)


def unregister():
    bpy.utils.unregister_class(AddFramedNodes)


if __name__ == "__main__":
    register()

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment