Skip to content

Commit

Permalink
Changed generated code
Browse files Browse the repository at this point in the history
  • Loading branch information
Leo committed Jan 21, 2022
1 parent 69f74ed commit 9e5e232
Show file tree
Hide file tree
Showing 3 changed files with 120 additions and 74 deletions.
2 changes: 1 addition & 1 deletion __init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
"A Node System to create game logic."
),
"author": "pgi, Leopold A-C (Iza Zed)",
"version": (2, 0, 2),
"version": (2, 0, 3),
"blender": (2, 91, 0),
"location": "View Menu",
"category": "Game Engine"
Expand Down
2 changes: 1 addition & 1 deletion basicnodes/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -9570,7 +9570,7 @@ def init(self, context):
self.inputs.new(NLConditionSocket.bl_idname, "Condition")
self.inputs.new(NLGameObjectSocket.bl_idname, "Target Object")
self.inputs.new(NLSocketLogicTree.bl_idname, "Tree Name")
self.inputs.new(NLBooleanSocket.bl_idname, "Enabled")
self.inputs.new(NLBooleanSocket.bl_idname, "Initialize")
self.inputs[-1].use_toggle = True
self.outputs.new(NLConditionSocket.bl_idname, 'Done')

Expand Down
190 changes: 118 additions & 72 deletions ops/tree_code_generator.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,17 +31,76 @@ def close(self):


MODULE_TEMPLATE = """\
import foo
# MACHINE GENERATED
import bge, bpy, sys
import mathutils
import math
from collections import OrderedDict
class {}Wrapper():
def __init__(self, args):
{}
self.condition = args['Execution Condition']
self.consumed = False
owner = self.object
network = self.network = ULLogicTree()
CON0000 = ULOnInit()
ACT0001 = ULPrintValue()
ACT0002 = ULPlayAction()
ACT0001.condition = ACT0002.STARTED
ACT0001.value = ACT0002.FRAME
ACT0002.condition = CON0000
ACT0002.game_object = "NLO:U_O"
ACT0002.action_name = "CubeAction"
ACT0002.start_frame = 0.0
ACT0002.end_frame = 13.170000076293945
ACT0002.layer = 0
ACT0002.priority = 0
ACT0002.play_mode = bge.logic.KX_ACTION_MODE_PLAY + 3
ACT0002.stop = False
ACT0002.layer_weight = 1.0
ACT0002.speed = 1.0
ACT0002.blendin = 0.0
ACT0002.blend_mode = bge.logic.KX_ACTION_BLEND_BLEND
network.add_cell(CON0000)
network.add_cell(ACT0002)
network.add_cell(ACT0001)
network._owner = owner
network.setup()
network.stopped = not owner.get('NL__NodeTree')
if args['Only Run At Startup']:
self.consumed = True
network.evaluate()
def evaluate(self):
if self.consumed:
return
owner = self.object
if self.condition:
cond = owner[self.condition]
if not cond: return
network = self.network
if network.stopped: return
shutdown = network.evaluate()
if shutdown is True:
self.consumed = True
class NodeTree(bge.types.KX_PythonComponent):
args = OrderedDict([
("Only Run At Startup", False),
("Execution Condition", "")
])
def start(self, args):
self.logictree = NodeTreeWrapper(args)
def update(self):
self.logictree.evaluate()
class Foo:
was auch immer
def start():
%(start)s
def update():
...
"""

# MODULE_TEMPLATE % {'start': lines}
Expand Down Expand Up @@ -83,6 +142,8 @@ def write_code_for_tree(self, tree):
self.write_init_content(tree, writer)
indent = self.write_pulse_line(tree, writer)
self.write_pulse_content(tree, writer, indent)
if tree.mode:
self.write_component_part(tree, writer, 0)

def write_unloader(self, writer):
writer.write_line("def unload_pyd(a, b):")
Expand Down Expand Up @@ -132,26 +193,17 @@ def write_to_text(self, tree):
# for module in user_modules:
# line_writer.write_line('{} = bgelogic.load_user_logic("{}")', module, module)
line_writer.write_line('')
# self.write_unloader(line_writer)
line_writer.write_line(f'class {tree_name}(bge.types.KX_PythonComponent):')
line_writer.write_line('')
line_writer.set_indent_level(1)
line_writer.write_line('consumed = False')
line_writer.write_line('condition = ""')
line_writer.write_line('args = OrderedDict([')
line_writer.set_indent_level(2)
line_writer.write_line('("Only Run At Startup", False),')
line_writer.write_line('("Execution Condition", "")')
line_writer.set_indent_level(1)
line_writer.write_line('])')
line_writer.write_line(f'class {tree_name}Wrapper():')
line_writer.write_line('')
line_writer.write_line('def start(self, args):')
line_writer.set_indent_level(1)
line_writer.write_line('def __init__(self, game_object, exec_cond="", startup=False):')
line_writer.set_indent_level(2)
line_writer.write_line("from uplogic import nodes, utils")
line_writer.write_line("from uplogic.nodes.logictree import ULLogicTree")
self.write_imports(tree, line_writer)
line_writer.write_line("self.condition = args['Execution Condition']")
line_writer.write_line("owner = self.object")
line_writer.write_line("self.condition = exec_cond")
line_writer.write_line("owner = self.owner = game_object")
return line_writer

def write_to_file(self, tree):
Expand Down Expand Up @@ -182,29 +234,24 @@ def write_init_content(self, tree, line_writer):
for varname in self._sort_cellvarnames(cell_var_names, uid_map):
if not uid_map.is_removed(varname):
line_writer.write_line("network.add_cell({})", varname)
# line_writer.write_line('owner["IGNLTree_{}"] = network', tree.name)
tree_name = utils.make_valid_name(tree.name)
line_writer.write_line('owner["IGNLTree_{}"] = network', tree_name)
line_writer.write_line("network._owner = owner")
line_writer.write_line("network.setup()")
line_writer.write_line("network.stopped = not owner.get('{}')", utils.get_key_network_initial_status_for_tree(tree))
if isinstance(line_writer, BLTextWrapper):
line_writer.write_line("if args['Only Run At Startup']:")
line_writer.set_indent_level(line_writer._indent_level + 1)
line_writer.write_line("self.consumed = True")
line_writer.write_line("network.evaluate()")
line_writer.set_indent_level(line_writer._indent_level - 1)
line_writer.write_line("return network")
line_writer.write_line("self.consumed = startup")

def write_pulse_line(self, tree, line_writer):
line_writer.set_indent_level(line_writer._indent_level - 1)
line_writer.write_line("")
if isinstance(line_writer, BLTextWrapper):
line_writer.write_line('def update(self):')
line_writer.write_line('def evaluate(self):')
line_writer.set_indent_level(2)
line_writer.write_line("if self.consumed:")
line_writer.set_indent_level(3)
line_writer.write_line("return")
line_writer.set_indent_level(2)
line_writer.write_line("owner = self.object")
line_writer.write_line("owner = self.owner")
line_writer.write_line("if self.condition:")
line_writer.set_indent_level(3)
line_writer.write_line("cond = owner[self.condition]")
Expand Down Expand Up @@ -232,49 +279,48 @@ def write_pulse_content(self, tree, line_writer, indent):
line_writer.write_line("controller.sensors[0].repeat = False")
else:
line_writer.write_line("self.consumed = True")
line_writer.close()

def update_package(self):
this_module_dir = os.path.dirname(__file__)
bge_netlogic_dir = os.path.dirname(this_module_dir)
uplogic_dir = os.path.join(bge_netlogic_dir, 'uplogic')
import site
uplogic_path = os.path.join(site.getsitepackages()[-1], 'uplogic')

try:
if os.path.isdir(uplogic_path):
shutil.rmtree(uplogic_path)
os.mkdir(uplogic_path)
self.recreate_dir(uplogic_dir, uplogic_path)
except PermissionError:
initfile = self.create_text_file("__init__.py")
initfile.close()
self.rewrite_file('nodes.py', uplogic_dir, bpy.abspath('//bgelogic/'))

def recreate_dir(self, from_dir, to_dir):
for f in os.listdir(from_dir):
entry = os.path.join(from_dir, f)
if os.path.isdir(entry):
new_dir = os.path.join(to_dir, f)
os.mkdir(new_dir)
self.recreate_dir(entry, new_dir)
else:
self.rewrite_file(f, from_dir, to_dir)

def rewrite_file(self, f, from_path, to_path):
writer = self.create_text_file(f, os.path.join(to_path))
input_f = os.path.join(from_path, f)
bgelogic_source_code = None
with open(input_f, "r") as f:
bgelogic_source_code = f.read()
assert (bgelogic_source_code is not None)
writer.write_line(bgelogic_source_code)
writer.close()
def write_component_part(self, tree, line_writer, indent=0):
line_writer.set_indent_level(0)
line_writer.write_line('')
line_writer.write_line('')
tree_name = utils.make_valid_name(tree.name)
line_writer.write_line(f'class {tree_name}(bge.types.KX_PythonComponent):')
line_writer.set_indent_level(1)
line_writer.write_line('args = OrderedDict([')
line_writer.set_indent_level(2)
line_writer.write_line('("Only Run At Startup", False),')
line_writer.write_line('("Execution Condition", "")')
line_writer.set_indent_level(1)
line_writer.write_line('])')
line_writer.write_line('def start(self, args):')
line_writer.set_indent_level(2)
line_writer.write_line(f'self.logictree = {tree_name}Wrapper(')
line_writer.set_indent_level(3)
line_writer.write_line('self.object,')
line_writer.write_line('exec_cond=args["Execution Condition"],')
line_writer.write_line('startup=args["Only Run At Startup"]')
line_writer.set_indent_level(2)
line_writer.write_line(')')
line_writer.write_line('self.logictree.evaluate()')
line_writer.set_indent_level(1)
line_writer.write_line('def update(self):')
line_writer.set_indent_level(2)
line_writer.write_line('if not self.logictree.consumed:')
line_writer.set_indent_level(3)
line_writer.write_line('self.logictree.evaluate()')
line_writer.set_indent_level(0)
line_writer.write_line('')
line_writer.write_line('')
# self.write_unloader(line_writer)
line_writer.write_line('def get_tree(obj):')
line_writer.set_indent_level(1)
line_writer.write_line(f'return {tree_name}Wrapper(obj)')

def _write_tree(self, tree, line_writer):
uid_map = UIDMap()
cell_uid = 0
node_cellvar_list = []
# node_cellvar_list = []
for node in tree.nodes:
prefix = None
if not (
Expand Down

0 comments on commit 9e5e232

Please sign in to comment.