0
$\begingroup$

I am currently learning how to create addons in Blender and now I got a problem. I try to create an addon which creates x times a math node. But when I try to create an IntProperty, it doesn't show up in the panel

import bpy
from bpy.props import IntProperty

class AddManyNodesOP(bpy.types.Operator):
    bl_idname = ".add_many_nodes"
    bl_label = "Adds many Nodes"
    bl_description = "Adds many Nodes"

    number_Nodes : bpy.props.IntProperty(
        name ="number_Nodes",
        default =5,
        min = 1
        )



    def execute(self,context):
        tree = bpy.context.scene.node_tree
        for i in range(self.number_Nodes):
           math_node = tree.nodes.new(type="CompositorNodeMath")
           math_node.location = (100*i,0)

        return {'FINISHED'}


class TooManyNodesPanel(bpy.types.Panel):
    bl_label = "Too many Nodes"
    bl_idname = "TOO_MANY_NODES"
    bl_space_type = 'NODE_EDITOR'
    bl_region_type = 'UI'

    def draw(self, context):
        layout = self.layout
        row = layout.row()
        row.prop(context.scene.node_tree, 'number_Nodes')
        row = layout.row()
        row.operator(".add_many_nodes",text= "Add")        



classes = [AddManyNodesOP,TooManyNodesPanel]
def register():
    for cls in classes:
        bpy.utils.register_class(cls)


def unregister():
    for cls in classes:
        bpy.utils.unregister_class(cls)


if __name__ == "__main__":
    register()
$\endgroup$

2 Answers 2

2
$\begingroup$

Setter / Getter on property

enter image description here

Something like this can be done with an update, or setter and getter on a blender property. The operator has been ditched altogether.

Because the compositor is being used, the Scene type is a natural choice. For shader nodes choose Material.

The getter returns how many math nodes are in the node tree.

The setter adds more if greater.

Haven't wired in removing the nodes. Would consider removing nodes to match input number as long as they have no links.

import bpy
from bpy.props import IntProperty

class NODES_PT_math_nodes(bpy.types.Panel):
    bl_label = "Too many Nodes"
    bl_idname = "NODES_PT_math_nodes"
    bl_space_type = 'NODE_EDITOR'
    bl_region_type = 'UI'

    def draw(self, context):
        layout = self.layout
        scene = context.scene
        col = layout.column()
        col.prop(scene, 'math_nodes')


def get_math_nodes(self):
    node_tree = self.node_tree
    return len([n for n in node_tree.nodes if n.type == "MATH"])

def set_math_nodes(self, value):
    node_tree = self.node_tree
    nodes = node_tree.nodes
    m = get_math_nodes(self)
    delta = value - m
    if delta > 0:
        for i in range(delta):
            n = nodes.new(type="CompositorNodeMath")
            n.location.x += (m) * (n.width + 20)

def register():    
    bpy.utils.register_class(NODES_PT_math_nodes)
    bpy.types.Scene.math_nodes = IntProperty(
            min=0,
            soft_max=10,
            get=get_math_nodes,
            set=set_math_nodes)

register()
$\endgroup$
0
$\begingroup$

Found a solution:

import bpy
from bpy.props import IntProperty

class AddManyNodesOP(bpy.types.Operator):
    bl_idname = ".add_many_nodes"
    bl_label = "Adds many Nodes"
    bl_description = "Adds many Nodes"







    def execute(self,context):
        tree = bpy.context.scene.node_tree
        for i in range(context.scene.number_Nodes_Prop):
           math_node = tree.nodes.new(type="CompositorNodeMath")
           math_node.location = (100*i,0)

        return {'FINISHED'}


class TooManyNodesPanel(bpy.types.Panel):
    bl_label = "Too many Nodes"
    bl_idname = "TOO_MANY_NODES"
    bl_space_type = 'NODE_EDITOR'
    bl_region_type = 'UI'

    def draw(self, context):
        layout = self.layout
        row = layout.row()
        row.prop(context.scene, 'number_Nodes_Prop')
        row = layout.row()
        row.operator(".add_many_nodes",text= "Add")        



classes = [AddManyNodesOP,TooManyNodesPanel]
def register():
    for cls in classes:
        bpy.utils.register_class(cls)
    bpy.types.Scene.number_Nodes_Prop = bpy.props.IntProperty(name ="number_Nodes", default =5,min = 1)


def unregister():
    for cls in classes:
        bpy.utils.unregister_class(cls)
    del bpy.types.Scene.number_Nodes


if __name__ == "__main__":
    register()

I don't know why or how this works,I would be gratefu, if someone can explain this to me.

bpy.types.Scene.number_Nodes_Prop = bpy.props.IntProperty(name....

...

$\endgroup$
1
  • $\begingroup$ same here, adding custom props to the main scene object is really a crap idea $\endgroup$
    – Phil
    Apr 26, 2022 at 9:42

You must log in to answer this question.

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