0
$\begingroup$

A Custom Node Creates a Collection and invokes an Operator that registers a 'TIMER' and constantly updates some properties of the 'MotherNode'.

import bpy

class SFX_ClockNode(bpy.types.Node):
    '''SFX_ClockNode'''
    bl_idname = 'SFX_ClockNode'
    bl_label = 'Clock'
    bl_icon = 'CURVE_NCIRCLE'
    bl_width_min = 340
    bl_width_max = 340

@classmethod
def poll(cls, ntree):
    return ntree.bl_idname == 'SFX_NodeTree'

def update_value(self,context):
    self.update()
    pass

operator_started_bit1 : bpy.props.BoolProperty(name = "Operator Started",
                                description = "Operator Started",
                                default = False)
date :bpy.props.StringProperty(name='Date',
                            description='Date',
                            default = 'Sun Jun 20 23:21:05 1993')

TickTime_prop: bpy.props.FloatProperty(name = "Tick Time",
                                description ="Sanity Check message round trip Time",
                                default=0.1,
                                precision=1,
                                update = update_value)

def init(self, context):
    self.draw_model(context)
    pass

def copy(self, node):
    print("copied node", node)

def free(self):
    self.operator_started_bit1 = False
    bpy.data.collections.remove(bpy.data.collections.get('ww SFX_Nodes'))
    print('Node destroyed',self)

def draw_buttons(self, context, layout):
    box = layout.box()
    col = box.column(align = True)
    row = col.split(factor = 0.3)
    if not(self.operator_started_bit1):
        row.operator('sfx.clockstartop',text ='Start')
    else:
       row.operator('sfx.commstarteddiag',text ='Started')
    row1 = row.split(factor = 0.75) 
    row1.prop(self,'date',text = '')
    row2 = row1.split(factor = 1)
    row2.prop( self, 'TickTime_prop', text = '')


def draw_buttons_ext(self, context, layout):
    pass

def draw_model(self,context):
    collection = bpy.data.collections.new('ww SFX_Nodes')
    bpy.context.scene.collection.children.link(collection)

def update(self):
    pass

and the associated Operator

import bpy
import time

class SFX_OT_ClockStartOp(bpy.types.Operator):
    """ This operator Starts the Clock"""
    bl_idname = "sfx.clockstartop"
    bl_label = "Clock Start"

def modal(self, context, event):
    if event.type == 'TIMER':
        self.MotherNode.TickTime_prop = (time.time_ns() - self.old_time)/1000000.0
        if not(self.MotherNode.operator_started_bit1):             # destroy
            ret =self.End_Comm(context)
            return ret
        else: 
            self.MotherNode.date = time.asctime()
            self.old_time = time.time_ns()
            return {'PASS_THROUGH'}
    return {'PASS_THROUGH'}

def execute(self, context):
    print('Clock Start -- execute')
    self._timer = context.window_manager.event_timer_add(0.001, window=context.window)
    context.window_manager.modal_handler_add(self)
    self.old_time = time.time_ns()
    return {'RUNNING_MODAL'}

def invoke(self, context, event):
    print('Clock Start -- invoke')
    self.MotherNode = context.active_node
    self.MotherNode.operator_started_bit1=True

    return self.execute(context)

def End_Comm(self,context):        
    print('Clock Start -- end timer')
    self.MotherNode.operator_started_bit1 = False
    context.window_manager.event_timer_remove(self._timer)
    return {'CANCELLED'} 

I would like the Node to start the Operator when the Node is created as having to push the Start Button is a shabby workaround. Inserting

bpy.ops.sfx.clockstartop('INVOKE_DEFAULT')

into the init method of the Node wont work as at the moment of node creation no active_node is in the context. Any Ideas to accomplish this ?

THX Martin

$\endgroup$

1 Answer 1

0
$\begingroup$

To achieve this I wrote an operator that inserts the node in the NodeTree sets it active and starts the associated Operator

import bpy

class SFX_OT_IN_Adder(bpy.types.Operator):
    """ Insert a Clock Node to the Node Tree and start it"""
    bl_idname = "sfx.in_clock"
    bl_label = "Insert Adder Node"

def invoke(self, context, event):
    node_tree = bpy.context.space_data.edit_tree.name
    Node = bpy.data.node_groups[node_tree].nodes.new('SFX_ClockNode')
    bpy.data.node_groups[node_tree].nodes.active = Node
    bpy.ops.sfx.clockstartop('INVOKE_DEFAULT')
    return {'FINISHED'}

def draw(self,context):
    pass

and put it in the node_menu

layout.operator('sfx.in_clock', text = 'Clock', icon = "CURVE_NCIRCLE" )
$\endgroup$

You must log in to answer this question.

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