Skip to content

Commit

Permalink
Merge pull request #4 from nodedge/feature/scene-coding
Browse files Browse the repository at this point in the history
Feature/scene coding
  • Loading branch information
nodedge committed Nov 3, 2020
2 parents 97b85ce + 3b43dab commit d368130
Show file tree
Hide file tree
Showing 60 changed files with 874 additions and 253 deletions.
3 changes: 3 additions & 0 deletions docs/build_docs.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
import os
import shutil

shutil.rmtree("source/apidoc")
os.mkdir("source/apidoc")
os.system("sphinx-apidoc -e -T -f -o source/apidoc/ -t source/_templates/ ../nodedge")
os.system("make clean")
os.system("make html")
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
nodedge.blocks.input\_block
nodedge.application\_styler
===========================

.. automodule:: nodedge.blocks.input_block
.. automodule:: nodedge.application_styler
:members:
:undoc-members:
:show-inheritance:
7 changes: 0 additions & 7 deletions docs/source/apidoc/nodedge.blocks.add_block.rst

This file was deleted.

7 changes: 0 additions & 7 deletions docs/source/apidoc/nodedge.blocks.divide_block.rst

This file was deleted.

7 changes: 0 additions & 7 deletions docs/source/apidoc/nodedge.blocks.multiply_block.rst

This file was deleted.

7 changes: 0 additions & 7 deletions docs/source/apidoc/nodedge.blocks.output_block.rst

This file was deleted.

6 changes: 0 additions & 6 deletions docs/source/apidoc/nodedge.blocks.rst
Original file line number Diff line number Diff line change
Expand Up @@ -6,18 +6,12 @@ Submodules

.. toctree::

nodedge.blocks.add_block
nodedge.blocks.block
nodedge.blocks.block_config
nodedge.blocks.divide_block
nodedge.blocks.graphics_block
nodedge.blocks.graphics_block_content
nodedge.blocks.graphics_input_block_content
nodedge.blocks.graphics_output_block_content
nodedge.blocks.input_block
nodedge.blocks.multiply_block
nodedge.blocks.output_block
nodedge.blocks.subtract_block

Module contents
---------------
Expand Down
7 changes: 0 additions & 7 deletions docs/source/apidoc/nodedge.blocks.subtract_block.rst

This file was deleted.

7 changes: 0 additions & 7 deletions docs/source/apidoc/nodedge.code_generator.rst

This file was deleted.

7 changes: 7 additions & 0 deletions docs/source/apidoc/nodedge.logger.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
nodedge.logger
==============

.. automodule:: nodedge.logger
:members:
:undoc-members:
:show-inheritance:
4 changes: 3 additions & 1 deletion docs/source/apidoc/nodedge.rst
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ Submodules

.. toctree::

nodedge.code_generator
nodedge.application_styler
nodedge.connector
nodedge.edge
nodedge.edge_dragging
Expand All @@ -29,13 +29,15 @@ Submodules
nodedge.graphics_socket
nodedge.graphics_view
nodedge.history_list_widget
nodedge.logger
nodedge.mdi_area
nodedge.mdi_widget
nodedge.mdi_window
nodedge.node
nodedge.node_list_widget
nodedge.scene
nodedge.scene_clipboard
nodedge.scene_coder
nodedge.scene_history
nodedge.scene_items_table_widget
nodedge.serializable
Expand Down
7 changes: 7 additions & 0 deletions docs/source/apidoc/nodedge.scene_coder.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
nodedge.scene\_coder
====================

.. automodule:: nodedge.scene_coder
:members:
:undoc-members:
:show-inheritance:
73 changes: 71 additions & 2 deletions examples/calculator/calculator.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
}
],
"content": {
"value": "1"
"value": "10"
},
"operationCode": 1
},
Expand All @@ -39,7 +39,7 @@
}
],
"content": {
"value": "1"
"value": "3"
},
"operationCode": 1
},
Expand Down Expand Up @@ -246,6 +246,57 @@
"outputSockets": [],
"content": {},
"operationCode": 2
},
{
"id": 64719888,
"title": "Modulo",
"posX": -60.0,
"posY": 185.0,
"inputSockets": [
{
"id": 74608816,
"index": 0,
"allowMultiEdges": false,
"location": 1,
"socketType": 2
},
{
"id": 74609296,
"index": 1,
"allowMultiEdges": false,
"location": 1,
"socketType": 2
}
],
"outputSockets": [
{
"id": 74611312,
"index": 0,
"allowMultiEdges": true,
"location": 4,
"socketType": 1
}
],
"content": {},
"operationCode": 7
},
{
"id": 192604048,
"title": "Output",
"posX": 246.0,
"posY": 187.0,
"inputSockets": [
{
"id": 192605008,
"index": 0,
"allowMultiEdges": false,
"location": 1,
"socketType": 1
}
],
"outputSockets": [],
"content": {},
"operationCode": 2
}
],
"edges": [
Expand Down Expand Up @@ -320,6 +371,24 @@
"edgeType": 3,
"source": 1583749319312,
"target": 1583749804528
},
{
"id": 83443312,
"edgeType": 3,
"source": 74608816,
"target": 1583749280096
},
{
"id": 64719984,
"edgeType": 3,
"source": 74609296,
"target": 1583749280960
},
{
"id": 192603792,
"edgeType": 3,
"source": 74611312,
"target": 192605008
}
]
}
44 changes: 44 additions & 0 deletions examples/calculator/main_code_generation.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
import os
import sys

from PySide2.QtGui import QColor, QPalette
from PySide2.QtWidgets import QApplication

from nodedge.mdi_window import MdiWindow
from nodedge.scene_coder import SceneCoder
from nodedge.utils import dumpException

sys.path.insert(0, os.path.join(os.path.dirname(__file__), "..", "..")) # noqa: E402
os.environ["QT_API"] = "pyside"

if __name__ == "__main__":
app: QApplication = QApplication(sys.argv)

QApplication.setStyle("Fusion")
p = QApplication.palette()
p.setColor(QPalette.Window, QColor(53, 53, 53))
p.setColor(QPalette.Button, QColor(53, 53, 53))
p.setColor(QPalette.Highlight, QColor(142, 45, 197))
p.setColor(QPalette.ButtonText, QColor(255, 255, 255))
p.setColor(QPalette.WindowText, QColor(255, 255, 255))
app.setPalette(p)

window = MdiWindow()

window.show()

window.openFile(f"{os.path.dirname(__file__)}/calculator.json")

currentScene = window.currentEditorWidget.scene
coder = SceneCoder(currentScene)
orderedNodeList, currentSceneCode = coder.generateCode()
print(currentSceneCode)

generatedFileString = coder.createFileFromGeneratedCode(
orderedNodeList, currentSceneCode
)

try:
sys.exit(app.exec_())
except Exception as e:
dumpException(e)
2 changes: 1 addition & 1 deletion nodedge/__init__.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
# -*- coding: utf-8 -*-

DEBUG_ITEMS_PRESSED: bool = True
DEBUG_ITEMS_PRESSED: bool = False
21 changes: 13 additions & 8 deletions nodedge/blocks/__init__.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,18 @@
# -*- coding: utf-8 -*-
import glob
from os.path import basename, dirname, isfile, join

from .add_block import *
from .divide_block import *
from .input_block import *
from .multiply_block import *
from .output_block import *
from .subtract_block import *
from .autogen.operator.add_block import *
from .autogen.operator.eq_block import *
from .autogen.operator.ge_block import *
from .autogen.operator.gt_block import *
from .autogen.operator.le_block import *
from .autogen.operator.lt_block import *
from .autogen.operator.mod_block import *
from .autogen.operator.mul_block import *
from .autogen.operator.pow_block import *
from .autogen.operator.sub_block import *
from .autogen.operator.truediv_block import *
from .custom.input_block import *
from .custom.output_block import *

# modules = glob.glob(join(dirname(__file__), "*.py"))
# __all__ = [basename(f)[:-3] for f in modules if isfile(f) and f.endswith('_block.py')]
Empty file.
Empty file.
Original file line number Diff line number Diff line change
Expand Up @@ -3,37 +3,36 @@
from operator import add

from nodedge.blocks.block import Block, EvaluationError
from nodedge.blocks.block_config import BLOCKS_ICONS_PATH, OP_NODE_ADD, registerNode
from nodedge.blocks.block_config import BLOCKS_ICONS_PATH, registerNode

try:
from nodedge.blocks.block_config import OP_NODE_ADD
except:
op_block_string = -1


@registerNode(OP_NODE_ADD)
class AddBlock(Block):
icon = f"{BLOCKS_ICONS_PATH}/add.png"
operationCode = OP_NODE_ADD
operationTitle = "Add"
operationTitle = "Addition"
contentLabel = "+"
contentLabelObjectName = "BlockBackground"
evalString = "add"
library = "operator"

def evalImplementation(self):
inputs = []
for i in range(len(self.inputSockets)):
inputs.append(self.inputNodeAt(i))

try:
operation = f"{AddBlock.evalString}("
for curr_input in inputs:
operation += f"{curr_input.eval()},"
operation = operation[:-1] + ")"
evaluatedInputs = [str(currentInput.eval()) for currentInput in inputs]
operation = f"{AddBlock.evalString}({', '.join(evaluatedInputs)})"
result = eval(operation)
except TypeError as e:
raise EvaluationError(e)

self.value = result

return self.value


# TODO: use join method instead ','.join(list_of_strings)
# TODO: Find a way to extract exceptions from evalImplementation
# TODO: Create a script to generate tests for blocks: list of Inputs and list of expected Outputs
38 changes: 38 additions & 0 deletions nodedge/blocks/autogen/operator/eq_block.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
# -*- coding: utf-8 -*-

from operator import eq

from nodedge.blocks.block import Block, EvaluationError
from nodedge.blocks.block_config import BLOCKS_ICONS_PATH, registerNode

try:
from nodedge.blocks.block_config import OP_NODE_EQUAL
except:
op_block_string = -1


@registerNode(OP_NODE_EQUAL)
class EqBlock(Block):
icon = f"{BLOCKS_ICONS_PATH}/eq.png"
operationCode = OP_NODE_EQUAL
operationTitle = "Equal"
contentLabel = "=="
contentLabelObjectName = "BlockBackground"
evalString = "eq"
library = "operator"

def evalImplementation(self):
inputs = []
for i in range(len(self.inputSockets)):
inputs.append(self.inputNodeAt(i))

try:
evaluatedInputs = [str(currentInput.eval()) for currentInput in inputs]
operation = f"{EqBlock.evalString}({', '.join(evaluatedInputs)})"
result = eval(operation)
except TypeError as e:
raise EvaluationError(e)

self.value = result

return self.value

0 comments on commit d368130

Please sign in to comment.