Skip to content

Commit 677dbf4

Browse files
committed
Implement changes from engine-php
1 parent 3d8e095 commit 677dbf4

File tree

13 files changed

+136
-86
lines changed

13 files changed

+136
-86
lines changed

Blackprint/Constructor/Port.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -155,11 +155,11 @@ def _callAll(this):
155155

156156
def createLinker(this):
157157
# Callable port
158-
if(this.source == 'output' and (this.type == FunctionType or this.type == Types.Route)):
158+
if(this.source == 'output' and (this.type == Types.Trigger or this.type == Types.Route)):
159159
# Disable sync
160160
this._sync = False
161161

162-
if(this.type != FunctionType):
162+
if(this.type != Types.Trigger):
163163
this.isRoute = True
164164
this.iface.node.routes.disableOut = True
165165

@@ -305,7 +305,7 @@ def assignType(this, type_):
305305
if(type_['feature'] == PortFeature.Union):
306306
type_ = Types.Any
307307
elif(type_['feature'] == PortFeature.Trigger):
308-
type_ = FunctionType
308+
type_ = type_['type']
309309
elif(type_['feature'] == PortFeature.ArrayOf):
310310
type_ = list
311311
elif(type_['feature'] == PortFeature.Default):
@@ -403,8 +403,8 @@ def connectCable(this, cable: Cable):
403403

404404
# Remove cable if type restriction
405405
if(not isInstance or (
406-
cableOwner.type == FunctionType and this.type != FunctionType
407-
or cableOwner.type != FunctionType and this.type == FunctionType
406+
cableOwner.type == Types.Trigger and this.type != Types.Trigger
407+
or cableOwner.type != Types.Trigger and this.type == Types.Trigger
408408
)):
409409
this._cableConnectError('cable.wrong_type_pair', {
410410
"cable": cable,
@@ -456,7 +456,7 @@ def connectCable(this, cable: Cable):
456456
out = cable.target
457457

458458
# Remove old cable if the port not support array
459-
if(inp.feature != PortFeature.ArrayOf and inp.type != FunctionType):
459+
if(inp.feature != PortFeature.ArrayOf and inp.type != Types.Trigger):
460460
cables = inp.cables # Cables in input port
461461
cableLen = len(cables)
462462

Blackprint/Constructor/PortLink.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
import re
22

3-
from types import FunctionType
43
from typing import Dict
54

65
from ..Internal import EvPortSelf
@@ -127,7 +126,7 @@ def __getitem__(this, key):
127126
# else: output ports
128127

129128
# This may get called if the port is lazily assigned with Slot port feature
130-
if(port.type == FunctionType):
129+
if(port.type == Types.Trigger):
131130
if(port._call_ == None):
132131
port._call_ = lambda: port._callAll()
133132

@@ -199,7 +198,7 @@ def _add(this, portName, val):
199198
if(val['feature'] == Port.Union):
200199
val = Types.Any
201200
elif(val['feature'] == Port.Trigger):
202-
val = FunctionType
201+
val = Types.Trigger
203202
elif(val['feature'] == Port.ArrayOf):
204203
val = list
205204
elif(val['feature'] == Port.Default):

Blackprint/Engine.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -308,6 +308,11 @@ def settings(this, which, val):
308308
which = which.replace('.', '_')
309309
this.settings[which] = val
310310

311+
def linkVariables(this, vars):
312+
for temp in vars:
313+
Utils.setDeepProperty(this.variables, temp.id.split('/'), temp)
314+
this._emit('variable.new', temp)
315+
311316
def _getTargetPortType(this, instance, whichPort, targetNodes):
312317
target = targetNodes[0] # ToDo: check all target in case if it's supporting Union type
313318
targetIface = instance.ifaceList[target['i']]
@@ -419,6 +424,8 @@ def createVariable(this, id, options):
419424
parentObj = Utils.getDeepProperty(this.variables, ids, 1)
420425

421426
if(parentObj != None and lastId in parentObj):
427+
if(parentObj[lastId].isShared): return
428+
422429
this.variables[id].destroy()
423430
del this.variables[id]
424431

Blackprint/Internal.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
# from .Utils import Utils
33
from .Constructor import InstanceEvent
44
from .Types import Types
5+
from .Nodes.BPVariable_init import BPVariable, VarScope
56

67
class Internal:
78
nodes = {}
@@ -66,6 +67,16 @@ def registerEvent(namespace, options):
6667

6768
Internal.events[namespace] = InstanceEvent(options)
6869

70+
def createVariable(namespace, options=[]):
71+
if(re.search(r'/\s/', namespace) != None):
72+
raise Exception(f"Namespace can't have space character: '{namespace}'")
73+
74+
temp = BPVariable(namespace, options)
75+
temp._scope = VarScope.public
76+
temp.isShared = True
77+
78+
return temp
79+
6980
# Below is for internal only
7081
class EvError:
7182
def __init__(this, type, data):

Blackprint/Nodes/BPEvent.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
from ..Types import Types
2-
from types import FunctionType
32
from typing import Dict
43

54
from ..Port.PortFeature import Port

Blackprint/Nodes/BPFunction.py

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
import re
22

3-
from types import FunctionType
43
from typing import Dict
54

65
from ..Port.PortFeature import Port
@@ -10,6 +9,7 @@
109
from ..Constructor.CustomEvent import CustomEvent
1110
from ..Constructor.Port import Port as PortClass
1211
from .BPVariable import VarScope, BPVariable
12+
from ..Types import Types
1313
from .Enums import Enums
1414
from ..Internal import EvVariableNew, registerNode, registerInterface
1515
import re
@@ -276,7 +276,7 @@ def update(this, cable):
276276

277277
# Sync all port value
278278
for key, value in IOutput.items():
279-
if(value.type == FunctionType): continue
279+
if(value.type == Types.Trigger): continue
280280
Output[key] = thisInput[key]
281281

282282
return
@@ -348,7 +348,7 @@ def update(this, cable):
348348

349349
# Sync all port value
350350
for key, value in IOutput.items():
351-
if(value.type == FunctionType): continue
351+
if(value.type == Types.Trigger): continue
352352
Output[key] = thisInput[key]
353353

354354
return
@@ -379,6 +379,8 @@ def _BpFnInit(this):
379379
# ToDo: will this be slower if we lazy import the module like below?
380380
from ..Engine import Engine
381381
this.bpInstance = Engine()
382+
if(this.data != None and ('pause' in this.data)):
383+
this.bpInstance.executionOrder.pause = True
382384

383385
bpFunction = node._funcInstance
384386

@@ -421,6 +423,7 @@ def _save(ev, eventName, force=False):
421423
this._save = _save
422424
this.bpInstance.on('cable.connect cable.disconnect node.created node.delete node.id.changed port.default.changed _port.split _port.unsplit _port.resync.allow _port.resync.disallow', this._save)
423425

426+
def imported(this, data): this.data = data
424427
def renamePort(this, which, fromName, toName):
425428
this.node._funcInstance.renamePort(which, fromName, toName)
426429
this._save(False, False, True)
@@ -461,7 +464,7 @@ def addPort(this, port: PortClass, customName):
461464
nodeB = this.node
462465
refName = PortName(name)
463466

464-
portType = getFnPortType(port, 'input', this._funcMain, refName)
467+
portType = getFnPortType(port, 'input', this, refName)
465468
nodeA._funcInstance.input[name] = portType
466469

467470
else: # Output (input) . Main (output)
@@ -476,12 +479,12 @@ def addPort(this, port: PortClass, customName):
476479
nodeB = this._funcMain.node
477480
refName = PortName(name)
478481

479-
portType = getFnPortType(port, 'output', this._funcMain, refName)
482+
portType = getFnPortType(port, 'output', this, refName)
480483
nodeB._funcInstance.output[name] = portType
481484

482485
outputPort = nodeB.createPort('output', name, portType)
483486

484-
if(portType == FunctionType):
487+
if(portType == Types.Trigger):
485488
inputPort = nodeA.createPort('input', name, Port.Trigger(lambda port: outputPort._callAll()))
486489
else: inputPort = nodeA.createPort('input', name, portType)
487490

Blackprint/Nodes/BPVariable.py

Lines changed: 26 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,16 @@
1-
from types import FunctionType
21
from ..Node import Node
32
from ..Interface import Interface
43
from ..Nodes.Enums import Enums
5-
from ..Constructor.CustomEvent import CustomEvent
64
from ..Constructor.Port import Port as PortClass
75
from ..Utils import Utils
86
from ..Internal import registerNode, registerInterface
97
from ..Types import Types
108
from ..Port.PortFeature import Port
11-
import re
9+
from .BPVariable_init import BPVariable, VarScope
1210

1311
# Don't delete even unused, this is needed for importing the internal node
1412
from .Environments import BPEnvGet
1513

16-
# For internal library use only
17-
class VarScope:
18-
public = 0
19-
private = 1
20-
shared = 2
21-
2214
@registerNode('BP/Var/Set')
2315
class VarSet(Node):
2416
input = {}
@@ -62,46 +54,6 @@ def __init__(this, instance):
6254

6355
def destroy(this): this.iface.destroyIface()
6456

65-
# used for instance.createVariable
66-
class BPVariable(CustomEvent):
67-
type = None
68-
# this.totalSet = 0
69-
# this.totalGet = 0
70-
71-
def __init__(this, id, options={}):
72-
CustomEvent.__init__(this)
73-
74-
id = re.sub(r'/^\/|\/$/m', '', id)
75-
id = re.sub(r'/[`~!@#$%^&*()\-_+={}\[\]:"|;\'\\\\,.<>?]+/', '_', id)
76-
this.id = id
77-
this.title = options['title'] if 'title' in options else id
78-
79-
# this.rootInstance = instance
80-
this.id = this.title = id
81-
this.type = Types.Slot
82-
this._value = None
83-
this.used = []
84-
85-
# The type need to be defined dynamically on first cable connect
86-
87-
@property
88-
def value(this):
89-
return this._value
90-
91-
@value.setter
92-
def value(this, val):
93-
if(this._value == val): return
94-
95-
this._value = val
96-
this.emit('value')
97-
98-
def destroy(this):
99-
map = this.used
100-
for iface in map:
101-
iface.node.instance.deleteNode(iface)
102-
103-
map.clear()
104-
10557
class BPVarGetSet(Interface):
10658
_onChanged = None
10759
_dynamicPort = True # Port is initialized dynamically
@@ -158,10 +110,14 @@ def useType(this, port: PortClass):
158110

159111
if(port == None): raise Exception("Can't set type with None")
160112
temp.type = port._config if port._config != None else port.type
113+
if(isinstance(temp, dict) and temp.type['feature'] == Port.Trigger):
114+
temp.type = Types.Trigger
161115

162116
if(port.type == Types.Slot):
163117
this.waitTypeChange(temp, port)
164-
else: temp.emit('type.assigned')
118+
else:
119+
this._recheckRoute()
120+
temp.emit('type.assigned')
165121

166122
# Also create port for other node that using this variable
167123
used = temp.used
@@ -172,19 +128,34 @@ def waitTypeChange(this, bpVar, port=None):
172128
def callback():
173129
if(port != None):
174130
bpVar.type = port._config if port._config != None else port.type
131+
if(isinstance(bpVar, dict) and bpVar.type['feature'] == Port.Trigger):
132+
bpVar.type = Types.Trigger
133+
175134
bpVar.emit('type.assigned')
176135
else:
177136
if this.input['Val'] != None:
178137
target = this.input['Val']
179138
else: target = this.output['Val']
180139
target.assignType(bpVar.type)
181140

141+
this._recheckRoute()
142+
182143
this._waitTypeChange = callback
183144
this._destroyWaitType = lambda: bpVar.off('type.assigned', this._waitTypeChange)
184145

185146
iPort = port if port != None else bpVar
186147
iPort.once('type.assigned', this._waitTypeChange)
187148

149+
def _recheckRoute(this):
150+
if(
151+
(hasattr(this, 'input') and ('Val' in this.input) and this.input['Val'].type == Types.Trigger)
152+
or
153+
(hasattr(this, 'output') and ('Val' in this.output) and this.output['Val'].type == Types.Trigger)
154+
):
155+
routes = this.node.routes
156+
routes.disableOut = True
157+
routes.noUpdate = True
158+
188159
def destroyIface(this):
189160
temp = this._destroyWaitType
190161
if(temp != None):
@@ -220,6 +191,7 @@ def changeVar(this, name, scopeId):
220191
if(varRef.type == Types.Slot): return
221192

222193
this._reinitPort()
194+
this._recheckRoute()
223195

224196
def _reinitPort(this):
225197
temp = this._bpVarRef
@@ -234,7 +206,7 @@ def _reinitPort(this):
234206
ref = node.output
235207
node.createPort('output', 'Val', temp.type)
236208

237-
if(temp.type == FunctionType):
209+
if(temp.type == Types.Trigger):
238210
this._eventListen = 'call'
239211
def callback(ev): ref['Val']()
240212
this._onChanged = callback
@@ -243,7 +215,7 @@ def callback(ev): ref['Val']()
243215
def callback(ev): ref['Val'] = temp._value
244216
this._onChanged = callback
245217

246-
if(temp.type != FunctionType):
218+
if(temp.type != Types.Trigger):
247219
node.output['Val'] = temp._value
248220

249221
temp.on(this._eventListen, this._onChanged)
@@ -265,6 +237,7 @@ def changeVar(this, name, scopeId):
265237
if(varRef.type == Types.Slot): return
266238

267239
this._reinitPort()
240+
this._recheckRoute()
268241

269242
def _reinitPort(this):
270243
input = this.input
@@ -277,7 +250,7 @@ def _reinitPort(this):
277250
if('Val' in input):
278251
node.deletePort('input', 'Val')
279252

280-
if(temp.type == FunctionType):
253+
if(temp.type == Types.Trigger):
281254
node.createPort('input', 'Val', Port.Trigger(lambda port: temp.emit('call')))
282255

283256
else: node.createPort('input', 'Val', temp.type)

0 commit comments

Comments
 (0)