├── .gitignore ├── .pylintrc ├── CONTRIBUTING.md ├── LICENSE ├── README.md ├── assets ├── assets_source │ ├── ADD_NODE.png │ ├── BRUSH_CHECKERBOARD.png │ └── IMAGE.png ├── encode_bitmaps.py └── screenshots │ └── gsnodegraph-demo.jpg ├── gsnodegraph ├── __init__.py ├── assets │ ├── __init__.py │ └── bitmaps.py ├── constants.py ├── graph │ ├── __init__.py │ ├── base.py │ ├── btn.py │ └── utils │ │ ├── __init__.py │ │ └── z_matrix.py └── node │ ├── __init__.py │ ├── node.py │ ├── socket.py │ ├── utils.py │ └── wire.py ├── main.py ├── nodegraph.py ├── nodes ├── __init__.py └── nodes.py ├── pyproject.toml ├── setup.cfg └── setup.py /.gitignore: -------------------------------------------------------------------------------- 1 | # Ignore PyPI stuff 2 | build 3 | dist 4 | gsnodegraph.egg-info 5 | pypi.txt 6 | 7 | # Ignore python misc. 8 | __pycache__ 9 | 10 | # Ignore test files 11 | testing.py 12 | -------------------------------------------------------------------------------- /.pylintrc: -------------------------------------------------------------------------------- 1 | generated-members = wx.* 2 | extension-pkg-whitelist = wx 3 | -------------------------------------------------------------------------------- /CONTRIBUTING.md: -------------------------------------------------------------------------------- 1 | # Contributing to gsnodegraph 2 | 3 | Thanks for taking the time to contribute. We're excited to have you! 4 | 5 | gsnodegraph is a node graph widget built as a wxPython extension library originally created for use in [Gimel Studio](https://github.com/GimelStudio/GimelStudio). 6 | 7 | The main purpose of the gsnode graph package is to provide a powerful node graph widget for use in Gimel Studio. Since Gimel Studio directy relies on this package, some issues in the main Gimel Studio repository will make reference to something that needs to be fixed/implemented here. 8 | 9 | Please see the Gimel Studio [CONTRIBUTING.md](https://github.com/GimelStudio/GimelStudio/blob/master/CONTRIBUTING.md) for overall guidelines and details on contributing. 10 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | Apache License 2 | Version 2.0, January 2004 3 | http://www.apache.org/licenses/ 4 | 5 | TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION 6 | 7 | 1. Definitions. 8 | 9 | "License" shall mean the terms and conditions for use, reproduction, 10 | and distribution as defined by Sections 1 through 9 of this document. 11 | 12 | "Licensor" shall mean the copyright owner or entity authorized by 13 | the copyright owner that is granting the License. 14 | 15 | "Legal Entity" shall mean the union of the acting entity and all 16 | other entities that control, are controlled by, or are under common 17 | control with that entity. For the purposes of this definition, 18 | "control" means (i) the power, direct or indirect, to cause the 19 | direction or management of such entity, whether by contract or 20 | otherwise, or (ii) ownership of fifty percent (50%) or more of the 21 | outstanding shares, or (iii) beneficial ownership of such entity. 22 | 23 | "You" (or "Your") shall mean an individual or Legal Entity 24 | exercising permissions granted by this License. 25 | 26 | "Source" form shall mean the preferred form for making modifications, 27 | including but not limited to software source code, documentation 28 | source, and configuration files. 29 | 30 | "Object" form shall mean any form resulting from mechanical 31 | transformation or translation of a Source form, including but 32 | not limited to compiled object code, generated documentation, 33 | and conversions to other media types. 34 | 35 | "Work" shall mean the work of authorship, whether in Source or 36 | Object form, made available under the License, as indicated by a 37 | copyright notice that is included in or attached to the work 38 | (an example is provided in the Appendix below). 39 | 40 | "Derivative Works" shall mean any work, whether in Source or Object 41 | form, that is based on (or derived from) the Work and for which the 42 | editorial revisions, annotations, elaborations, or other modifications 43 | represent, as a whole, an original work of authorship. For the purposes 44 | of this License, Derivative Works shall not include works that remain 45 | separable from, or merely link (or bind by name) to the interfaces of, 46 | the Work and Derivative Works thereof. 47 | 48 | "Contribution" shall mean any work of authorship, including 49 | the original version of the Work and any modifications or additions 50 | to that Work or Derivative Works thereof, that is intentionally 51 | submitted to Licensor for inclusion in the Work by the copyright owner 52 | or by an individual or Legal Entity authorized to submit on behalf of 53 | the copyright owner. For the purposes of this definition, "submitted" 54 | means any form of electronic, verbal, or written communication sent 55 | to the Licensor or its representatives, including but not limited to 56 | communication on electronic mailing lists, source code control systems, 57 | and issue tracking systems that are managed by, or on behalf of, the 58 | Licensor for the purpose of discussing and improving the Work, but 59 | excluding communication that is conspicuously marked or otherwise 60 | designated in writing by the copyright owner as "Not a Contribution." 61 | 62 | "Contributor" shall mean Licensor and any individual or Legal Entity 63 | on behalf of whom a Contribution has been received by Licensor and 64 | subsequently incorporated within the Work. 65 | 66 | 2. Grant of Copyright License. Subject to the terms and conditions of 67 | this License, each Contributor hereby grants to You a perpetual, 68 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 69 | copyright license to reproduce, prepare Derivative Works of, 70 | publicly display, publicly perform, sublicense, and distribute the 71 | Work and such Derivative Works in Source or Object form. 72 | 73 | 3. Grant of Patent License. Subject to the terms and conditions of 74 | this License, each Contributor hereby grants to You a perpetual, 75 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 76 | (except as stated in this section) patent license to make, have made, 77 | use, offer to sell, sell, import, and otherwise transfer the Work, 78 | where such license applies only to those patent claims licensable 79 | by such Contributor that are necessarily infringed by their 80 | Contribution(s) alone or by combination of their Contribution(s) 81 | with the Work to which such Contribution(s) was submitted. If You 82 | institute patent litigation against any entity (including a 83 | cross-claim or counterclaim in a lawsuit) alleging that the Work 84 | or a Contribution incorporated within the Work constitutes direct 85 | or contributory patent infringement, then any patent licenses 86 | granted to You under this License for that Work shall terminate 87 | as of the date such litigation is filed. 88 | 89 | 4. Redistribution. You may reproduce and distribute copies of the 90 | Work or Derivative Works thereof in any medium, with or without 91 | modifications, and in Source or Object form, provided that You 92 | meet the following conditions: 93 | 94 | (a) You must give any other recipients of the Work or 95 | Derivative Works a copy of this License; and 96 | 97 | (b) You must cause any modified files to carry prominent notices 98 | stating that You changed the files; and 99 | 100 | (c) You must retain, in the Source form of any Derivative Works 101 | that You distribute, all copyright, patent, trademark, and 102 | attribution notices from the Source form of the Work, 103 | excluding those notices that do not pertain to any part of 104 | the Derivative Works; and 105 | 106 | (d) If the Work includes a "NOTICE" text file as part of its 107 | distribution, then any Derivative Works that You distribute must 108 | include a readable copy of the attribution notices contained 109 | within such NOTICE file, excluding those notices that do not 110 | pertain to any part of the Derivative Works, in at least one 111 | of the following places: within a NOTICE text file distributed 112 | as part of the Derivative Works; within the Source form or 113 | documentation, if provided along with the Derivative Works; or, 114 | within a display generated by the Derivative Works, if and 115 | wherever such third-party notices normally appear. The contents 116 | of the NOTICE file are for informational purposes only and 117 | do not modify the License. You may add Your own attribution 118 | notices within Derivative Works that You distribute, alongside 119 | or as an addendum to the NOTICE text from the Work, provided 120 | that such additional attribution notices cannot be construed 121 | as modifying the License. 122 | 123 | You may add Your own copyright statement to Your modifications and 124 | may provide additional or different license terms and conditions 125 | for use, reproduction, or distribution of Your modifications, or 126 | for any such Derivative Works as a whole, provided Your use, 127 | reproduction, and distribution of the Work otherwise complies with 128 | the conditions stated in this License. 129 | 130 | 5. Submission of Contributions. Unless You explicitly state otherwise, 131 | any Contribution intentionally submitted for inclusion in the Work 132 | by You to the Licensor shall be under the terms and conditions of 133 | this License, without any additional terms or conditions. 134 | Notwithstanding the above, nothing herein shall supersede or modify 135 | the terms of any separate license agreement you may have executed 136 | with Licensor regarding such Contributions. 137 | 138 | 6. Trademarks. This License does not grant permission to use the trade 139 | names, trademarks, service marks, or product names of the Licensor, 140 | except as required for reasonable and customary use in describing the 141 | origin of the Work and reproducing the content of the NOTICE file. 142 | 143 | 7. Disclaimer of Warranty. Unless required by applicable law or 144 | agreed to in writing, Licensor provides the Work (and each 145 | Contributor provides its Contributions) on an "AS IS" BASIS, 146 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 147 | implied, including, without limitation, any warranties or conditions 148 | of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A 149 | PARTICULAR PURPOSE. You are solely responsible for determining the 150 | appropriateness of using or redistributing the Work and assume any 151 | risks associated with Your exercise of permissions under this License. 152 | 153 | 8. Limitation of Liability. In no event and under no legal theory, 154 | whether in tort (including negligence), contract, or otherwise, 155 | unless required by applicable law (such as deliberate and grossly 156 | negligent acts) or agreed to in writing, shall any Contributor be 157 | liable to You for damages, including any direct, indirect, special, 158 | incidental, or consequential damages of any character arising as a 159 | result of this License or out of the use or inability to use the 160 | Work (including but not limited to damages for loss of goodwill, 161 | work stoppage, computer failure or malfunction, or any and all 162 | other commercial damages or losses), even if such Contributor 163 | has been advised of the possibility of such damages. 164 | 165 | 9. Accepting Warranty or Additional Liability. While redistributing 166 | the Work or Derivative Works thereof, You may choose to offer, 167 | and charge a fee for, acceptance of support, warranty, indemnity, 168 | or other liability obligations and/or rights consistent with this 169 | License. However, in accepting such obligations, You may act only 170 | on Your own behalf and on Your sole responsibility, not on behalf 171 | of any other Contributor, and only if You agree to indemnify, 172 | defend, and hold each Contributor harmless for any liability 173 | incurred by, or claims asserted against, such Contributor by reason 174 | of your accepting any such warranty or additional liability. 175 | 176 | END OF TERMS AND CONDITIONS 177 | 178 | APPENDIX: How to apply the Apache License to your work. 179 | 180 | To apply the Apache License to your work, attach the following 181 | boilerplate notice, with the fields enclosed by brackets "[]" 182 | replaced with your own identifying information. (Don't include 183 | the brackets!) The text should be enclosed in the appropriate 184 | comment syntax for the file format. We also recommend that a 185 | file or class name and description of purpose be included on the 186 | same "printed page" as the copyright notice for easier 187 | identification within third-party archives. 188 | 189 | Copyright [yyyy] [name of copyright owner] 190 | 191 | Licensed under the Apache License, Version 2.0 (the "License"); 192 | you may not use this file except in compliance with the License. 193 | You may obtain a copy of the License at 194 | 195 | http://www.apache.org/licenses/LICENSE-2.0 196 | 197 | Unless required by applicable law or agreed to in writing, software 198 | distributed under the License is distributed on an "AS IS" BASIS, 199 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 200 | See the License for the specific language governing permissions and 201 | limitations under the License. -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/GimelStudio/gsnodegraph/a8e84ef0c9a76ba28469a35dc458867ae7c35a2e/README.md -------------------------------------------------------------------------------- /assets/assets_source/ADD_NODE.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/GimelStudio/gsnodegraph/a8e84ef0c9a76ba28469a35dc458867ae7c35a2e/assets/assets_source/ADD_NODE.png -------------------------------------------------------------------------------- /assets/assets_source/BRUSH_CHECKERBOARD.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/GimelStudio/gsnodegraph/a8e84ef0c9a76ba28469a35dc458867ae7c35a2e/assets/assets_source/BRUSH_CHECKERBOARD.png -------------------------------------------------------------------------------- /assets/assets_source/IMAGE.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/GimelStudio/gsnodegraph/a8e84ef0c9a76ba28469a35dc458867ae7c35a2e/assets/assets_source/IMAGE.png -------------------------------------------------------------------------------- /assets/encode_bitmaps.py: -------------------------------------------------------------------------------- 1 | # ---------------------------------------------------------------------------- 2 | # gsnodegraph Copyright 2019-2022 by Noah Rahm and contributors 3 | # 4 | # Licensed under the Apache License, Version 2.0 (the "License"); 5 | # you may not use this file except in compliance with the License. 6 | # You may obtain a copy of the License at 7 | # 8 | # http://www.apache.org/licenses/LICENSE-2.0 9 | # 10 | # Unless required by applicable law or agreed to in writing, software 11 | # distributed under the License is distributed on an "AS IS" BASIS, 12 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | # See the License for the specific language governing permissions and 14 | # limitations under the License. 15 | # ---------------------------------------------------------------------------- 16 | 17 | """ 18 | Utility script to generate PyEmbeddedImage bitmap icons and write into 19 | 'bitmaps.py' file from images in the assets_source directory. 20 | 21 | 1. Just run this file in-place and an 'bitmaps.py' file should be 22 | generated. 23 | 2. Place the bitmaps.py file in the 'gsnodegraph/bitmaps' directory. 24 | """ 25 | 26 | import os 27 | from wx.tools import img2py 28 | 29 | 30 | def PrepareIconCommands(dest_file='bitmaps.py'): 31 | filelist = [] 32 | for file in os.listdir("assets_source/"): 33 | if file.endswith('.png'): 34 | filelist.append(file) 35 | 36 | commandlines = [] 37 | for icon in filelist: 38 | ico_path = "assets_source/{}".format(icon) 39 | ico_name = icon.split('.')[0] 40 | cmd = "-a -n ICON_{} {} {}".format(ico_name, ico_path, dest_file) 41 | commandlines.append(cmd) 42 | return commandlines 43 | 44 | 45 | if __name__ == "__main__": 46 | command_lines = PrepareIconCommands() 47 | for line in command_lines: 48 | args = line.split() 49 | img2py.main(args) 50 | 51 | # Add import statement to top 52 | with open("bitmaps.py", "r+") as file: 53 | file.write("from wx.lib.embeddedimage import PyEmbeddedImage\n#") 54 | print("Done!") 55 | -------------------------------------------------------------------------------- /assets/screenshots/gsnodegraph-demo.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/GimelStudio/gsnodegraph/a8e84ef0c9a76ba28469a35dc458867ae7c35a2e/assets/screenshots/gsnodegraph-demo.jpg -------------------------------------------------------------------------------- /gsnodegraph/__init__.py: -------------------------------------------------------------------------------- 1 | from .graph import (NodeGraphBase, 2 | ID_CONTEXTMENU_DUPLICATENODE, 3 | ID_CONTEXTMENU_DELETENODES, 4 | EVT_GSNODEGRAPH_NODESELECT, 5 | EVT_GSNODEGRAPH_NODEDISCONNECT, 6 | EVT_GSNODEGRAPH_NODECONNECT, 7 | EVT_GSNODEGRAPH_MOUSEZOOM, 8 | EVT_GSNODEGRAPH_ADDNODEBTN) 9 | from .node import NodeBase, NodeSocket, NodeWire 10 | -------------------------------------------------------------------------------- /gsnodegraph/assets/__init__.py: -------------------------------------------------------------------------------- 1 | from .bitmaps import * 2 | -------------------------------------------------------------------------------- /gsnodegraph/assets/bitmaps.py: -------------------------------------------------------------------------------- 1 | from wx.lib.embeddedimage import PyEmbeddedImage 2 | #-------------------- 3 | ICON_ADD_NODE = PyEmbeddedImage( 4 | b'iVBORw0KGgoAAAANSUhEUgAAAC0AAAAtCAYAAAA6GuKaAAAABHNCSVQICAgIfAhkiAAAA9ZJ' 5 | b'REFUWIXtWUF24jgQ/WVpP7lBe04w0cPs6ROEPkHICZqcoDMnSPcJEk7Q5ASh14gncoIxNyB7' 6 | b'yzULZKIHklsmkJkFf0MsS/rfJVVJVQHOOOOM/xx0jEmMMRfW2isiygGAmQcAQEQz91wKIZ6U' 7 | b'Uutj8B0s2gm9BjAEMEgcNgMwFUJM3vMBB4nWWl8D+A7gIvD6lZmXAEBElwD+CPQpAdwVRTE5' 8 | b'hL+TaGPMZVVVD05MgxcAMyHEo1JqGRtnrR1hsyJ/Ne3MvJRSflFKlScRPZ/PB0T0E2/WXQEY' 9 | b'F0Ux7UKotR5is0qfXNOamb/0+/1Z6hxJoheLxYiZH7aDiG57vd73LmIDc46Z+d6b86bX6z2m' 10 | b'jP2taLe0z9hY+BXAqKt1Y3BWf8Rm36+FEJ9jW8xHq2gXIf5xgiGEUCmTdoEzinGPpeNojSxZ' 11 | b'20tr7TZCENFNqmCt9Vet9deUvkqpJRHduMfccbYiaukdC0yKohiliHAO+wwAzPw51cG01lMA' 12 | b'VwAghPizLaJELW2t3TqJEOIuhfg9EEKMPe6Htr5B0caYHO6UY+a/u8bRQ+A4frjHgTEmdHAB' 13 | b'iIiu63p7LEspjxIpUiCE2Ia8uq6HsX5B0czcDFgdO1q0wXGtdjTsIbanr9zvh1nZQ8N5FevQ' 14 | b'GvKY+ShXyS5I4ZS7DcaY3FoLACCi4NaYz+f3O5cmn9R3oHutdVAEMy/7/f7tbrvPaYzJQ0Fg' 15 | b'T3RVVTkRNRMHCYloHGp37/y/gx/m3g0A7Ilm5nUzR1VVOTbX2HbRUsrSs3Qs7Pxg5qAgN6a5' 16 | b'fr60fHhwFX1OKWUw1O6JVkqVWmsAgBO254xFUUQtvXMijrtcORvOxtKx86HVEVssfTJ4nK+x' 17 | b'PjHRT+43GnZOiIYzukJB0UTUbIncGBN1pmPDceU7GvYQFJ1l2fYrq6qKnkzHhrV26ytZlnUT' 18 | b'rZQqmfkXABDRN3eBOikcxzUAMPOvtkQg6ohSyu392b+mngo+h88dQlS0CzdNXWK4WCySkoB+' 19 | b'vz8jolsiuk0Nd27uZhtOfncVTskRS7iCywfkiCshxOW7ckSl1FoIMYCLmdbaZ5dBHwVa66HL' 20 | b'9AHgVQgxTCmXtYoGtoln49UXAH6mJq1tWCwW3wD4xZ9R6ip2rTBN8VabKwHcHlhhuoeLx9hY' 21 | b'eNBl23Wt5eXW2im8ehyAkpmnUspJWy2vqqprIhp6YgHgxW2J09TyfLgy2R3e6nE+1gAa8ZcI' 22 | b'V1ZXRHSXWgbbxbvq03Vdj1wBPfWO8kREsyzLHj+8Pr0L9wHDuq6bpW+y+RkAZFlWZlk2PdZ/' 23 | b'As4444z/Af4FELLNYCJ7/vwAAAAASUVORK5CYII=') 24 | 25 | #---------------------------------------------------------------------- 26 | ICON_BRUSH_CHECKERBOARD = PyEmbeddedImage( 27 | b'iVBORw0KGgoAAAANSUhEUgAAAGQAAABkAQMAAABKLAcXAAAABlBMVEVfYWQnLjUoH6AuAAAA' 28 | b'KElEQVQ4y2NgsP/A/B9GDDDv/wH+PzCCYWB5o+EyGi6j4TIaLrTiAQDYaoSnfkBkDwAAAABJ' 29 | b'RU5ErkJggg==') 30 | 31 | #---------------------------------------------------------------------- 32 | ICON_IMAGE = PyEmbeddedImage( 33 | b'iVBORw0KGgoAAAANSUhEUgAAAA8AAAAPCAYAAAA71pVKAAAACXBIWXMAAA7DAAAOwwHHb6hk' 34 | b'AAAAGXRFWHRTb2Z0d2FyZQB3d3cuaW5rc2NhcGUub3Jnm+48GgAAAHBJREFUKJHVkksOgCAM' 35 | b'RJ/GExDLYUw8vXga4Azjyg0QDcGNs2z6+pkWBjQBpJROSVsHF8xsByDGqJ6Od/7cA5VaGlUz' 36 | b'4Mq4pOy9Xx9hwJnZ1CharTY09k/hyjBJuWWOpPwKl+d40idjh84XPUaaAnAB0HEs5elgUi0A' 37 | b'AAAASUVORK5CYII=') 38 | 39 | -------------------------------------------------------------------------------- /gsnodegraph/constants.py: -------------------------------------------------------------------------------- 1 | # ---------------------------------------------------------------------------- 2 | # gsnodegraph Copyright 2019-2022 by Noah Rahm and contributors 3 | # 4 | # Licensed under the Apache License, Version 2.0 (the "License" ; 5 | # you may not use this file except in compliance with the License. 6 | # You may obtain a copy of the License at 7 | # 8 | # http://www.apache.org/licenses/LICENSE-2.0 9 | # 10 | # Unless required by applicable law or agreed to in writing, software 11 | # distributed under the License is distributed on an "AS IS" BASIS, 12 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | # See the License for the specific language governing permissions and 14 | # limitations under the License. 15 | # ---------------------------------------------------------------------------- 16 | 17 | 18 | SOCKET_INPUT = 0 19 | SOCKET_OUTPUT = 1 20 | 21 | SOCKET_RADIUS = 5 22 | SOCKET_HIT_RADIUS = 11 23 | 24 | SOCKET_BORDER_COLOR = (18, 23, 25, 255) 25 | 26 | WIRE_NORMAL_COLOR = (88, 98, 110, 255) 27 | WIRE_ACTIVE_COLOR = (215, 219, 223, 255) 28 | 29 | DEFAULT_WIRE_CURVATURE = 8 30 | 31 | NODE_HEADER_MUTED_COLOR = (70, 70, 70, 255) 32 | 33 | NODE_NORMAL_COLOR = (45, 54, 62, 255) 34 | NODE_SELECTED_COLOR = (40, 47, 52, 255) 35 | NODE_MUTED_COLOR = (63, 66, 70, 90) 36 | 37 | NODE_BORDER_NORMAL_COLOR = (27, 28, 30, 255) 38 | NODE_BORDER_SELECTED_COLOR = (255, 255, 255, 255) 39 | 40 | NODE_THUMB_BORDER_COLOR = (27, 28, 30, 255) 41 | 42 | NODE_DEFAULT_WIDTH = 154 43 | NODE_DEFAULT_HEIGHT = 154 44 | NODE_THUMB_PADDING = 16 45 | NODE_Y_PADDING = 12 46 | 47 | SELECTION_BOX_BORDER_COLOR = (194, 194, 194, 255) 48 | SELECTION_BOX_COLOR = (100, 100, 100, 56) 49 | 50 | GRAPH_BACKGROUND_COLOR = (30, 36, 41, 255) 51 | 52 | BTN_NORMAL_COLOR = (0, 0, 0, 0) 53 | BTN_CLICKED_COLOR = (90, 127, 200, 255) 54 | BTN_FOCUSED_COLOR = (54, 56, 60, 255) 55 | -------------------------------------------------------------------------------- /gsnodegraph/graph/__init__.py: -------------------------------------------------------------------------------- 1 | from .base import (NodeGraphBase, 2 | ID_CONTEXTMENU_DUPLICATENODE, 3 | ID_CONTEXTMENU_DELETENODES, 4 | EVT_GSNODEGRAPH_NODESELECT, 5 | EVT_GSNODEGRAPH_NODEDISCONNECT, 6 | EVT_GSNODEGRAPH_NODECONNECT, 7 | EVT_GSNODEGRAPH_MOUSEZOOM, 8 | EVT_GSNODEGRAPH_ADDNODEBTN) 9 | -------------------------------------------------------------------------------- /gsnodegraph/graph/base.py: -------------------------------------------------------------------------------- 1 | # ---------------------------------------------------------------------------- 2 | # gsnodegraph Copyright 2019-2022 by Noah Rahm and contributors 3 | # 4 | # Licensed under the Apache License, Version 2.0 (the "License"); 5 | # you may not use this file except in compliance with the License. 6 | # You may obtain a copy of the License at 7 | # 8 | # http://www.apache.org/licenses/LICENSE-2.0 9 | # 10 | # Unless required by applicable law or agreed to in writing, software 11 | # distributed under the License is distributed on an "AS IS" BASIS, 12 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | # See the License for the specific language governing permissions and 14 | # limitations under the License. 15 | # ---------------------------------------------------------------------------- 16 | 17 | import uuid 18 | import wx 19 | import wx.lib.agw.flatmenu as flatmenu 20 | from wx.lib.newevent import NewCommandEvent 21 | 22 | from gsnodegraph.node import NodeWire 23 | from gsnodegraph.constants import (GRAPH_BACKGROUND_COLOR, SOCKET_OUTPUT, 24 | SELECTION_BOX_COLOR, SELECTION_BOX_BORDER_COLOR, 25 | DEFAULT_WIRE_CURVATURE) 26 | from gsnodegraph.assets import ICON_ADD_NODE 27 | from .utils.z_matrix import ZMatrix 28 | from .btn import AddNodeBtn 29 | 30 | gsnodegraph_nodeselect_cmd_event, EVT_GSNODEGRAPH_NODESELECT = NewCommandEvent() 31 | gsnodegraph_nodeconnect_cmd_event, EVT_GSNODEGRAPH_NODECONNECT = NewCommandEvent() 32 | gsnodegraph_nodedisconnect_cmd_event, EVT_GSNODEGRAPH_NODEDISCONNECT = NewCommandEvent() 33 | gsnodegraph_mousezoom_cmd_event, EVT_GSNODEGRAPH_MOUSEZOOM = NewCommandEvent() 34 | gsnodegraph_addnodebtn_cmd_event, EVT_GSNODEGRAPH_ADDNODEBTN = NewCommandEvent() 35 | 36 | ID_CONTEXTMENU_DELETENODE = wx.NewIdRef() 37 | ID_CONTEXTMENU_MUTENODE = wx.NewIdRef() 38 | ID_CONTEXTMENU_UNMUTENODE = wx.NewIdRef() 39 | ID_CONTEXTMENU_DELETENODES = wx.NewIdRef() 40 | ID_CONTEXTMENU_DUPLICATENODE = wx.NewIdRef() 41 | ID_CONTEXTMENU_DESELECTALLNODES = wx.NewIdRef() 42 | ID_CONTEXTMENU_SELECTALLNODES = wx.NewIdRef() 43 | 44 | 45 | class NodeGraphBase(wx.ScrolledCanvas): 46 | def __init__(self, parent, registry, config, *args, **kwds): 47 | kwds["style"] = kwds.get("style", 0) | wx.DEFAULT_FRAME_STYLE 48 | wx.ScrolledCanvas.__init__(self, parent, *args, **kwds) 49 | 50 | self.parent = parent 51 | self.node_registry = registry 52 | self.node_datatypes = config["node_datatypes"] 53 | self.node_categories = config["node_categories"] 54 | self.image_datatype = config["image_datatype"] 55 | self.input_nodes_categories = config["input_nodes_categories"] 56 | 57 | self.matrix = ZMatrix() 58 | self.identity = ZMatrix() 59 | self.matrix.Reset() 60 | self.identity.Reset() 61 | 62 | self.buffer = None 63 | self.bg_img = None 64 | self.zoom = 100 65 | 66 | self.wires = [] 67 | self.nodes = {} 68 | 69 | self.sel_nodes = [] 70 | self.active_node = None 71 | self.last_active_node = None 72 | 73 | self.middle_pnt = None 74 | self.last_pnt = None 75 | 76 | self.tmp_wire = None 77 | self.src_node = None 78 | self.src_socket = None 79 | 80 | self.bbox_rect = None 81 | self.bbox_start = None 82 | 83 | self.wire_curvature = DEFAULT_WIRE_CURVATURE 84 | 85 | # Init the add node button 86 | self.addnode_btn = AddNodeBtn(self) 87 | 88 | # Event bindings 89 | self.Bind(wx.EVT_PAINT, self.OnPaint) 90 | self.Bind(wx.EVT_ERASE_BACKGROUND, lambda x: None) 91 | self.Bind(wx.EVT_SIZE, self.OnSize) 92 | self.Bind(wx.EVT_LEFT_DOWN, self.OnLeftDown) 93 | self.Bind(wx.EVT_LEFT_UP, self.OnLeftUp) 94 | self.Bind(wx.EVT_MOTION, self.OnMotion) 95 | self.Bind(wx.EVT_MOUSEWHEEL, self.OnMousewheel) 96 | self.Bind(wx.EVT_MIDDLE_DOWN, self.OnMiddleDown) 97 | self.Bind(wx.EVT_MIDDLE_UP, self.OnMiddleUp) 98 | 99 | # Context menu bindings 100 | self.Bind(wx.EVT_CONTEXT_MENU, self.OnContextMenu) 101 | self.Bind(wx.EVT_MENU, self.OnDeleteNode, 102 | id=ID_CONTEXTMENU_DELETENODE) 103 | self.Bind(wx.EVT_MENU, self.OnMuteNode, 104 | id=ID_CONTEXTMENU_MUTENODE) 105 | self.Bind(wx.EVT_MENU, self.OnUnmuteNode, 106 | id=ID_CONTEXTMENU_UNMUTENODE) 107 | self.Bind(wx.EVT_MENU, self.OnDeleteNodes, 108 | id=ID_CONTEXTMENU_DELETENODES) 109 | self.Bind(wx.EVT_MENU, self.OnSelectAllNodes, 110 | id=ID_CONTEXTMENU_SELECTALLNODES) 111 | self.Bind(wx.EVT_MENU, self.OnDeselectAllNodes, 112 | id=ID_CONTEXTMENU_DESELECTALLNODES) 113 | self.Bind(wx.EVT_MENU, self.OnDuplicateNode, 114 | id=ID_CONTEXTMENU_DUPLICATENODE) 115 | 116 | # Keyboard shortcut bindings 117 | self.accel_tbl = wx.AcceleratorTable([(wx.ACCEL_SHIFT, ord('D'), 118 | ID_CONTEXTMENU_DUPLICATENODE), 119 | (wx.ACCEL_SHIFT, ord('M'), 120 | ID_CONTEXTMENU_MUTENODE), 121 | (wx.ACCEL_NORMAL, wx.WXK_DELETE, 122 | ID_CONTEXTMENU_DELETENODES), 123 | ]) 124 | self.parent.SetAcceleratorTable(self.accel_tbl) 125 | 126 | def OnPaint(self, event): 127 | dc = wx.BufferedPaintDC(self, self.buffer) 128 | 129 | def OnSize(self, event): 130 | Size = self.ClientSize 131 | self.buffer = wx.Bitmap(*Size) 132 | self.UpdateNodeGraph() 133 | 134 | def OnLeftDown(self, event): 135 | pnt = event.GetPosition() 136 | winpnt = self.CalcMouseCoords(pnt) 137 | 138 | # The node has been clicked 139 | self.src_node = self.HitTest(winpnt) 140 | if self.src_node is not None: 141 | self.HandleNodeSelection() 142 | 143 | # If the user CTRL+Clicks a node, connect it to the 144 | # output node in place of any other connections. 145 | if wx.GetKeyState(wx.WXK_CONTROL) == True: 146 | self.SetNodeAsPreview(self.src_node) 147 | 148 | # Handle sockets and wires 149 | self.src_socket = self.src_node.HitTest(winpnt) 150 | 151 | if self.src_socket is not None: 152 | 153 | # We do not allow connections from anything except 154 | # the output socket. If this is an Output socket, 155 | # we create the temp wire. 156 | if self.src_socket.direction == SOCKET_OUTPUT: 157 | pnt1 = self.src_node.pos + self.src_socket.pos 158 | 159 | self.tmp_wire = NodeWire(self, pnt1, winpnt, None, None, 160 | self.src_socket.direction, 161 | self.wire_curvature) 162 | 163 | # If this is an input socket, we disconnect any already-existing 164 | # sockets and connect the new wire. We do not allow disconnections 165 | # from the output socket 166 | elif self.src_socket.direction != SOCKET_OUTPUT: 167 | 168 | for wire in self.wires: 169 | if wire.dstsocket == self.src_socket: 170 | dst = wire.dstsocket 171 | self.src_socket = wire.srcsocket 172 | self.DisconnectNodes(self.src_socket, dst) 173 | 174 | # Refresh the nodegraph 175 | self.UpdateNodeGraph() 176 | 177 | # Don't allow a wire to be pulled out from an input node 178 | # AKA: backwards 179 | if self.src_socket.direction == SOCKET_OUTPUT: 180 | 181 | # Create the temp wire again 182 | pnt = event.GetPosition() 183 | winpnt = self.CalcMouseCoords(pnt) 184 | pnt1 = self.src_socket.node.pos + self.src_socket.pos 185 | 186 | # Draw the temp wire with the new values 187 | self.tmp_wire = NodeWire(self, pnt1, winpnt, None, None, 188 | self.src_socket.direction, 189 | self.wire_curvature) 190 | 191 | # Important: we re-assign the source node variable 192 | self.src_node = self.src_socket.node 193 | 194 | else: 195 | # Start the box select bbox 196 | self.bbox_start = winpnt 197 | 198 | self.DeselectNodes() 199 | 200 | # Update add node button 201 | pnt = event.GetPosition() 202 | if self.MouseInAddNodeBtn(pnt) is True: 203 | self.addnode_btn.SetClicked(True) 204 | 205 | self.last_pnt = winpnt 206 | 207 | # Refresh the nodegraph 208 | self.UpdateNodeGraph() 209 | 210 | def OnLeftUp(self, event): 211 | pnt = event.GetPosition() 212 | winpnt = self.CalcMouseCoords(pnt) 213 | 214 | # Clear selection bbox and set nodes as selected 215 | if self.bbox_rect != None: 216 | self.sel_nodes = self.BoxSelectHitTest(self.bbox_rect) 217 | for node in self.sel_nodes: 218 | if node.IsSelected() != True and node.IsActive() != True: 219 | node.SetSelected(True) 220 | 221 | # Attempt to make a connection 222 | if self.src_node != None: 223 | dst_node = self.HitTest(winpnt) 224 | 225 | if dst_node is not None: 226 | dst_socket = dst_node.HitTest(winpnt) 227 | 228 | # Make sure not to allow different datatypes or 229 | # the same 'socket type' to be connected! 230 | if dst_socket is not None: 231 | if (self.src_socket.direction != dst_socket.direction 232 | and self.src_socket.datatype == dst_socket.datatype 233 | and self.src_node != dst_node): 234 | 235 | # Only allow a single wire to be connected to any one input. 236 | if self.SocketHasWire(dst_socket) is not True: 237 | self.ConnectNodes(self.src_socket, dst_socket) 238 | 239 | # If there is already a connection, 240 | # but a wire is "dropped" into the socket 241 | # disconnect the last connection and 242 | # connect the current wire. 243 | else: 244 | for wire in self.wires: 245 | if wire.dstsocket == dst_socket: 246 | dst = wire.dstsocket 247 | src = wire.srcsocket 248 | self.DisconnectNodes(src, dst) 249 | 250 | self.ConnectNodes(self.src_socket, dst_socket) 251 | 252 | # Send event to update the properties panel 253 | if self.last_active_node is None: 254 | self.SendNodeSelectEvent() 255 | if self.last_active_node is not self.src_node: 256 | self.SendNodeSelectEvent() 257 | self.last_active_node = self.src_node 258 | 259 | 260 | # Reset all values 261 | self.src_node = None 262 | self.src_socket = None 263 | self.tmp_wire = None 264 | self.bbox_start = None 265 | self.bbox_rect = None 266 | 267 | # Update add node button and send button event if it was clicked 268 | pnt = event.GetPosition() 269 | if self.MouseInAddNodeBtn(pnt) is True: 270 | self.addnode_btn.SetClicked(False) 271 | self.SendAddNodeBtnEvent() 272 | 273 | # Refresh the nodegraph 274 | self.UpdateNodeGraph() 275 | 276 | def OnMotion(self, event): 277 | pnt = event.GetPosition() 278 | winpnt = self.CalcMouseCoords(pnt) 279 | 280 | # Draw box selection bbox 281 | if event.LeftIsDown() is True: 282 | if self.src_node is None and self.bbox_start != None: 283 | rect = wx.Rect(topLeft=self.bbox_start, bottomRight=winpnt) 284 | self.bbox_rect = rect 285 | self.UpdateNodeGraph() 286 | 287 | # If the MMB is down, calculate the scrolling of the graph 288 | if event.MiddleIsDown() is True and event.Dragging(): 289 | dx = int(winpnt[0] - self.middle_pnt[0]) 290 | dy = int(winpnt[1] - self.middle_pnt[1]) 291 | self.ScrollNodeGraph(dx, dy) 292 | self.ScenePostPan(dx, dy) 293 | self.UpdateNodeGraph() 294 | 295 | if event.LeftIsDown() and self.src_node != None and event.Dragging(): 296 | if self.src_socket is None: 297 | 298 | # Traslating the selected nodes 299 | if self.sel_nodes != []: 300 | for node in self.sel_nodes: 301 | dpnt = node.pos + winpnt - self.last_pnt 302 | node.pos = dpnt 303 | else: 304 | # Traslating the active node 305 | dpnt = self.src_node.pos + winpnt - self.last_pnt 306 | self.src_node.pos = dpnt 307 | 308 | self.last_pnt = winpnt 309 | 310 | # Redraw the wires 311 | for wire in self.wires: 312 | wire.pnt1 = wire.srcnode.pos + wire.srcsocket.pos 313 | wire.pnt2 = wire.dstnode.pos + wire.dstsocket.pos 314 | 315 | elif self.tmp_wire != None: 316 | 317 | # Set the wire to be active when it is being edited. 318 | self.tmp_wire.active = True 319 | 320 | if winpnt != None: 321 | self.tmp_wire.pnt2 = winpnt 322 | 323 | self.UpdateNodeGraph() 324 | 325 | else: 326 | pnt = event.GetPosition() 327 | if self.addnode_btn.IsClicked() is not True: 328 | if self.MouseInAddNodeBtn(pnt) is True: 329 | self.addnode_btn.SetFocused(True) 330 | else: 331 | self.addnode_btn.SetFocused(False) 332 | self.UpdateNodeGraph() 333 | 334 | def OnDeleteNodes(self, event): 335 | self.DeleteNodes() 336 | 337 | def OnDeleteNode(self, event): 338 | if (self.active_node != None and 339 | self.active_node.IsOutputNode() != True): 340 | self.DeleteNode(self.active_node) 341 | self.active_node = None 342 | 343 | # Update the properties panel so that the deleted 344 | # nodes' properties are not still shown! 345 | self.SendNodeSelectEvent() 346 | 347 | self.UpdateNodeGraph() 348 | 349 | def OnMuteNode(self, event): 350 | self.active_node.SetMuted(True) 351 | self.UpdateNodeGraph() 352 | 353 | def OnUnmuteNode(self, event): 354 | self.active_node.SetMuted(False) 355 | self.UpdateNodeGraph() 356 | 357 | def OnSelectAllNodes(self, event): 358 | """ Event that selects all the nodes in the Node Graph. """ 359 | for node_id in self.nodes: 360 | node = self.nodes[node_id] 361 | if node.IsActive() is True: 362 | node.SetActive(False) 363 | node.SetSelected(True) 364 | self.sel_nodes.append(node) 365 | self.UpdateNodeGraph() 366 | 367 | def OnDeselectAllNodes(self, event): 368 | """ Event that deselects all the currently selected nodes. """ 369 | self.DeselectNodes() 370 | self.UpdateNodeGraph() 371 | 372 | def OnDuplicateNode(self, event): 373 | """ Event that duplicates the currently selected node. """ 374 | self.DuplicateNode(self.active_node) 375 | 376 | def OnContextMenu(self, event): 377 | # Create the popup menu 378 | self.CreateContextMenu() 379 | 380 | # Position it at the mouse cursor 381 | pnt = event.GetPosition() 382 | self.context_menu.Popup(wx.Point(pnt.x, pnt.y), self) 383 | 384 | def CreateContextMenu(self): 385 | self.context_menu = flatmenu.FlatMenu() 386 | 387 | # If there is an active node, then we know 388 | # that there shouldn't be any other nodes 389 | # selected, thus we handle the active node first. 390 | if self.active_node != None: 391 | 392 | # Do not allow the output node to be 393 | # deleted or duplicated at all. 394 | if self.active_node.IsOutputNode() != True: 395 | duplicate_menuitem = flatmenu.FlatMenuItem(self.context_menu, 396 | ID_CONTEXTMENU_DUPLICATENODE, 397 | "{0}{1}".format(_("Duplicate"), "\tShift+D"), "", 398 | wx.ITEM_NORMAL) 399 | self.context_menu.AppendItem(duplicate_menuitem) 400 | delete_menuitem = flatmenu.FlatMenuItem(self.context_menu, 401 | ID_CONTEXTMENU_DELETENODE, 402 | "{0}{1}".format(_("Delete"), "\tDel"), "", 403 | wx.ITEM_NORMAL) 404 | self.context_menu.AppendItem(delete_menuitem) 405 | 406 | if self.IsInputNode(self.active_node) is not True: 407 | if self.active_node.IsMuted() is not True: 408 | mute_menuitem = flatmenu.FlatMenuItem(self.context_menu, 409 | ID_CONTEXTMENU_MUTENODE, 410 | "{0}{1}".format(_("Mute"), "\tShift+M"), "", 411 | wx.ITEM_NORMAL) 412 | self.context_menu.AppendItem(mute_menuitem) 413 | else: 414 | unmute_menuitem = flatmenu.FlatMenuItem(self.context_menu, 415 | ID_CONTEXTMENU_UNMUTENODE, 416 | _("Unmute"), "", 417 | wx.ITEM_NORMAL) 418 | self.context_menu.AppendItem(unmute_menuitem) 419 | 420 | else: 421 | if self.sel_nodes != []: 422 | deletenodes_menuitem = flatmenu.FlatMenuItem(self.context_menu, 423 | ID_CONTEXTMENU_DELETENODES, 424 | "{0}{1}".format(_("Delete Selected"), "\tDel"), "", 425 | wx.ITEM_NORMAL) 426 | self.context_menu.AppendItem(deletenodes_menuitem) 427 | 428 | selectallnodes_menuitem = flatmenu.FlatMenuItem(self.context_menu, 429 | ID_CONTEXTMENU_SELECTALLNODES, 430 | "Select All", "", 431 | wx.ITEM_NORMAL) 432 | self.context_menu.AppendItem(selectallnodes_menuitem) 433 | 434 | deselectallnodes_menuitem = flatmenu.FlatMenuItem(self.context_menu, 435 | ID_CONTEXTMENU_DESELECTALLNODES, 436 | "Deselect All", "", 437 | wx.ITEM_NORMAL) 438 | self.context_menu.AppendItem(deselectallnodes_menuitem) 439 | 440 | def DrawSelectionBox(self, dc, rect): 441 | dc.SetPen(wx.Pen(wx.Colour(SELECTION_BOX_BORDER_COLOR), 2, 442 | wx.PENSTYLE_SHORT_DASH)) 443 | dc.SetBrush(wx.Brush(wx.Colour(SELECTION_BOX_COLOR))) 444 | dc.DrawRectangle(rect) 445 | 446 | def SetZoomLevel(self, zoom, x=0, y=0): 447 | if x == 0: 448 | x = int(self.Size[0]/2) 449 | y = int(self.Size[1]/2) 450 | self.ScenePostScale(zoom, zoom, x, y) 451 | self.UpdateZoomValue() 452 | self.UpdateNodeGraph() 453 | 454 | def OnMousewheel(self, event): 455 | rotation = event.GetWheelRotation() 456 | mouse = event.GetPosition() 457 | 458 | if rotation > 1 and self.zoom < 310: 459 | self.ScenePostScale(1.1, 1.1, mouse[0], mouse[1]) 460 | 461 | elif rotation < -1 and self.zoom > 60: 462 | self.ScenePostScale(0.9, 0.9, mouse[0], mouse[1]) 463 | 464 | self.UpdateZoomValue() 465 | self.SendMouseZoomEvent() 466 | self.UpdateNodeGraph() 467 | 468 | def OnMiddleDown(self, event): 469 | """ Event that updates the mouse cursor. """ 470 | pnt = event.GetPosition() 471 | winpnt = self.CalcMouseCoords(pnt) 472 | 473 | self.middle_pnt = winpnt 474 | 475 | self.SetCursor(wx.Cursor(wx.CURSOR_SIZING)) 476 | 477 | def OnMiddleUp(self, event): 478 | """ Event that resets the mouse cursor. """ 479 | self.SetCursor(wx.Cursor(wx.CURSOR_ARROW)) 480 | 481 | def GetNodes(self): 482 | return self.nodes 483 | 484 | def UpdateZoomValue(self): 485 | self.zoom = round(self.GetScaleX() * 100) 486 | 487 | def ScrollNodeGraph(self, pos_x, pos_y): 488 | """ Scrolls the scrollbars to the specified position. """ 489 | scrollpos_x = self.GetScrollPos(wx.HORIZONTAL) 490 | scrollpos_y = self.GetScrollPos(wx.VERTICAL) 491 | 492 | self.Scroll(scrollpos_x - pos_x, 493 | scrollpos_y - pos_y) 494 | 495 | def CalcMouseCoords(self, pnt): 496 | """ Calculate the mouse coordinates, taking into account 497 | the current scroll position and zoom level. """ 498 | pnt = self.ConvertWindowToScene(self.ConvertCoords(pnt)) 499 | return wx.Point(int(pnt[0]), int(pnt[1])) 500 | 501 | def ConvertCoords(self, pnt): 502 | """ Convert coords to account for scrolling. 503 | 504 | :param pnt: the given wx.Point coord to convert 505 | :returns: wx.Point 506 | """ 507 | xv, yv = self.GetViewStart() 508 | xd, yd = self.GetScrollPixelsPerUnit() 509 | return wx.Point(pnt[0] + (xv * xd), pnt[1] + (yv * yd)) 510 | 511 | def GetViewableWindowRegion(self): 512 | """ Get the shown scrolled region of the window based on 513 | the current scrolling. 514 | 515 | :returns: wx.Rect 516 | """ 517 | xv, yv = self.GetViewStart() 518 | xd, yd = self.GetScrollPixelsPerUnit() 519 | x, y = (xv * xd, yv * yd) 520 | rgn = self.GetUpdateRegion() 521 | rgn.Offset(x, y) 522 | return rgn.GetBox() 523 | 524 | def UpdateNodeGraph(self): 525 | dc = wx.MemoryDC() 526 | dc.SelectObject(self.buffer) 527 | dc = wx.GCDC(dc) 528 | self.OnDrawBackground(dc) 529 | dc.SetTransformMatrix(self.matrix) 530 | self.OnDrawScene(dc) 531 | dc.SetTransformMatrix(self.identity) 532 | self.OnDrawInterface(dc) 533 | del dc # need to get rid of the MemoryDC before Update() is called. 534 | self.Refresh() 535 | self.Update() 536 | 537 | def OnDrawBackground(self, dc): 538 | dc.SetBackground(wx.Brush(wx.Colour(GRAPH_BACKGROUND_COLOR))) 539 | dc.Clear() 540 | 541 | def OnDrawScene(self, dc): 542 | # Draw background 543 | if self.bg_img != None: 544 | image = self.bg_img 545 | 546 | x = int(self.GetSize()[0]/2.0 - image.Width/2.0) 547 | y = int(self.GetSize()[1]/2.0 - image.Height/2.0) 548 | pnt = self.ConvertCoords(wx.Point(x, y)) 549 | dc.DrawBitmap(image, pnt[0], pnt[1], useMask=False) 550 | 551 | # Draw nodes 552 | for node_id in self.nodes: 553 | self.nodes[node_id].Draw(dc) 554 | 555 | # Draw temporary wires 556 | if self.tmp_wire != None: 557 | self.tmp_wire.Draw(dc) 558 | 559 | # Draw wires 560 | for wire in self.wires: 561 | wire.Draw(dc) 562 | 563 | # Draw selection box 564 | if self.bbox_start != None and self.bbox_rect != None: 565 | self.DrawSelectionBox(dc, self.bbox_rect) 566 | 567 | def OnDrawInterface(self, dc): 568 | # Calculate the position and draw the add node button 569 | padding = 10 570 | x = (padding) 571 | y = int((self.GetSize()[1] - self.addnode_btn.GetHeight()) - padding) 572 | pnt = self.ConvertCoords(wx.Point(x, y)) 573 | self.addnode_btn.Draw(dc, pnt) 574 | 575 | def SetNodeWireCurvature(self, curvature): 576 | self.wire_curvature = curvature 577 | 578 | # Change existing wires 579 | for wire in self.wires: 580 | wire.SetCurvature(curvature) 581 | 582 | def SetBackgroundImage(self, image): 583 | self.bg_img = image 584 | 585 | def HandleNodeSelection(self): 586 | # Set the active node 587 | if self.active_node is None: 588 | self.active_node = self.src_node 589 | self.active_node.SetActive(True) 590 | 591 | else: 592 | # We check to make sure this is not just the same 593 | # node clicked again, then we switch the active states. 594 | if self.src_node != self.active_node: 595 | self.active_node.SetActive(False) 596 | self.active_node = self.src_node 597 | self.active_node.SetActive(True) 598 | 599 | # When a node is active, all the selected nodes 600 | # need to be set to the unselected state. 601 | if self.sel_nodes != []: 602 | [node.SetSelected(False) for node in self.sel_nodes] 603 | 604 | def BoxSelectHitTest(self, bboxrect): 605 | """ Hit-test for box selection. """ 606 | nodehits = [] 607 | for node in self.nodes.values(): 608 | if bboxrect.Intersects(node.GetRect()) == True: 609 | nodehits.append(node) 610 | 611 | if nodehits != []: 612 | return nodehits 613 | else: 614 | self.DeselectNodes() 615 | return [] 616 | 617 | def DeselectNodes(self): 618 | """ Deselect everything that is selected or active. """ 619 | [node.SetSelected(False) for node in self.sel_nodes] 620 | 621 | self.sel_nodes = [] 622 | 623 | if self.active_node != None: 624 | self.active_node.SetActive(False) 625 | self.active_node = None 626 | 627 | def HitTest(self, pnt): 628 | mouse_rect = wx.Rect(pnt[0], pnt[1], 2, 2) 629 | 630 | for node in self.nodes: 631 | # Inflate the rect so that the node sockets are 632 | # highly sensitive to clicks for easier connections. 633 | node_rect = self.nodes[node].GetRect().Inflate(7, 7) 634 | if mouse_rect.Intersects(node_rect): 635 | return self.nodes[node] 636 | 637 | # Refresh the nodegraph 638 | self.UpdateNodeGraph() 639 | 640 | def MouseInAddNodeBtn(self, pnt): 641 | mouse_rect = wx.Rect(pnt[0], pnt[1], 1, 1) 642 | 643 | addnodebtn_rect = self.addnode_btn.GetRect() 644 | if mouse_rect.Intersects(addnodebtn_rect): 645 | return True 646 | else: 647 | return False 648 | 649 | def DeleteNodes(self): 650 | """ Delete the currently selected nodes. This will refuse 651 | to delete the Output Composite node though, for obvious reasons. 652 | """ 653 | for node in self.sel_nodes: 654 | if node.IsOutputNode() != True: 655 | self.DeleteNode(node) 656 | else: 657 | # In the case that this is an output node, we 658 | # want to deselect it, not delete it. :) 659 | node.SetSelected(False) 660 | self.sel_nodes = [] 661 | 662 | if (self.active_node != None and 663 | self.active_node.IsOutputNode() != True): 664 | self.DeleteNode(self.active_node) 665 | self.active_node = None 666 | 667 | # Update the properties panel so that the deleted 668 | # nodes' properties are not still shown! 669 | self.SendNodeSelectEvent() 670 | 671 | self.UpdateNodeGraph() 672 | 673 | def DuplicateNode(self, node): 674 | """ Duplicates the given ``Node`` object with its properties. 675 | :param node: the ``Node`` object to duplicate 676 | :returns: the duplicate ``Node`` object 677 | """ 678 | if node.IsOutputNode() is not True: 679 | duplicate_node = self.AddNode(node.GetIdname(), location="CURSOR") 680 | 681 | # TODO: Assign the same properties to the duplicate node object 682 | 683 | self.UpdateNodeGraph() 684 | return duplicate_node 685 | 686 | def AddNode(self, idname, nodeid=None, pos=(0, 0), location="POSITION"): 687 | if nodeid is None: 688 | node_id = uuid.uuid4().hex 689 | else: 690 | node_id = nodeid 691 | node = self.node_registry[idname](self, node_id) 692 | node.Init(idname) 693 | self.nodes[node.id] = node 694 | 695 | if location == "CURSOR": 696 | node.pos = self.CalcMouseCoords(self.ScreenToClient(wx.GetMousePosition())) 697 | else: 698 | node.pos = wx.Point(pos[0], pos[1]) 699 | return node 700 | 701 | def IsInputNode(self, node) -> bool: 702 | if node.GetCategory() in self.input_nodes_categories: 703 | return True 704 | else: 705 | return False 706 | 707 | def SocketHasWire(self, dst_socket): 708 | for wire in self.wires: 709 | if wire.dstsocket == dst_socket: 710 | return True 711 | return False 712 | 713 | def SetNodeAsPreview(self, current_node): 714 | """ Connect the given node to the the output node in place 715 | of any other connections. 716 | :param current_node: NodeBase subclass object 717 | """ 718 | output_node = self.GetOutputNode() 719 | 720 | # We assume there is only one input (for now) 721 | output_node_socket = output_node.GetSockets()[0] 722 | 723 | # Disconnect any previous connections 724 | wires = output_node_socket.GetWires() 725 | for wire in wires: 726 | if wire.dstsocket == output_node_socket: 727 | dst = wire.dstsocket 728 | src = wire.srcsocket 729 | self.DisconnectNodes(src, dst) 730 | 731 | # Connect the newly selected node to the output 732 | for socket in current_node.GetSockets(): 733 | if socket.direction == SOCKET_OUTPUT: 734 | src_socket = socket 735 | 736 | self.ConnectNodes(src_socket, output_node_socket) 737 | 738 | def GetOutputNode(self): 739 | """ Return the output node object. """ 740 | for node_id in self.nodes: 741 | node = self.nodes[node_id] 742 | if node.IsOutputNode(): 743 | return node 744 | 745 | def GetNodeByTypeId(self, type_id): 746 | """ Return a node object based on the node type identifier. 747 | :param type_id: node type identifier 748 | :returns: NodeBase subclass object 749 | """ 750 | for node_id in self.GetNodes(): 751 | if self.nodes[node_id].idname == type_id: 752 | return self.nodes[node_id] 753 | 754 | def ConnectNodes(self, src_socket, dst_socket): 755 | pt1 = src_socket.node.pos + src_socket.pos 756 | pt2 = dst_socket.node.pos + dst_socket.pos 757 | direction = src_socket.direction 758 | 759 | wire = NodeWire(src_socket, pt1, pt2, src_socket, dst_socket, 760 | direction, self.wire_curvature) 761 | wire.srcnode = src_socket.node 762 | wire.dstnode = dst_socket.node 763 | wire._srcsocket = src_socket 764 | wire._dstsocket = dst_socket 765 | 766 | self.wires.append(wire) 767 | 768 | src_socket.wires.append(wire) 769 | dst_socket.wires.append(wire) 770 | 771 | dst_socket.node.EditConnection(dst_socket.idname, self.nodes[src_socket.node.id], src_socket.idname) 772 | self.SendNodeConnectEvent() 773 | 774 | def DisconnectNodes(self, src_socket, dst_socket): 775 | for wire in self.wires: 776 | if wire.srcsocket is src_socket and wire.dstsocket is dst_socket: 777 | self.wires.remove(wire) 778 | wire.dstsocket.node.EditConnection(wire.dstsocket.idname, None, None) 779 | 780 | self.SendNodeDisconnectEvent() 781 | 782 | def DeleteNode(self, node): 783 | for socket in node.GetSockets(): 784 | for wire in socket.GetWires(): 785 | # Clean up any wires that are 786 | # connected to this node. 787 | self.DisconnectNodes(wire.srcsocket, wire.dstsocket) 788 | del self.nodes[node.id] 789 | self.UpdateNodeGraph() 790 | 791 | def SendNodeSelectEvent(self): 792 | wx.PostEvent(self, 793 | gsnodegraph_nodeselect_cmd_event(id=self.GetId(), 794 | value=self.active_node)) 795 | 796 | def SendNodeConnectEvent(self): 797 | wx.PostEvent(self, 798 | gsnodegraph_nodeconnect_cmd_event(id=self.GetId(), 799 | value=self.active_node)) 800 | 801 | def SendNodeDisconnectEvent(self): 802 | wx.PostEvent(self, 803 | gsnodegraph_nodedisconnect_cmd_event(id=self.GetId(), 804 | value=self.active_node)) 805 | 806 | def SendMouseZoomEvent(self): 807 | wx.PostEvent(self, 808 | gsnodegraph_mousezoom_cmd_event(id=self.GetId(), 809 | value=self.zoom)) 810 | 811 | def SendAddNodeBtnEvent(self): 812 | wx.PostEvent(self, 813 | gsnodegraph_addnodebtn_cmd_event(id=self.GetId(), 814 | value=self.active_node)) 815 | 816 | def SceneMatrixReset(self): 817 | self.matrix.Reset() 818 | 819 | def ScenePostScale(self, sx, sy=None, ax=0, ay=0): 820 | self.matrix.PostScale(sx, sy, ax, ay) 821 | 822 | def ScenePostPan(self, px, py): 823 | self.matrix.PostTranslate(px, py) 824 | 825 | def ScenePostRotate(self, angle, rx=0, ry=0): 826 | self.matrix.PostRotate(angle, rx, ry) 827 | 828 | def ScenePreScale(self, sx, sy=None, ax=0, ay=0): 829 | self.matrix.PreScale(sx, sy, ax, ay) 830 | 831 | def ScenePrePan(self, px, py): 832 | self.matrix.PreTranslate(px, py) 833 | 834 | def ScenePreRotate(self, angle, rx=0, ry=0): 835 | self.matrix.PreRotate(angle, rx, ry) 836 | 837 | def GetScaleX(self): 838 | return self.matrix.GetScaleX() 839 | 840 | def GetScaleY(self): 841 | return self.matrix.GetScaleY() 842 | 843 | def GetSkewX(self): 844 | return self.matrix.GetSkewX() 845 | 846 | def GetSkewY(self): 847 | return self.matrix.GetSkewY() 848 | 849 | def GetTranslateX(self): 850 | return self.matrix.GetTranslateX() 851 | 852 | def GetTranslateY(self): 853 | return self.matrix.GetTranslateY() 854 | 855 | def FocusPositionScene(self, scene_point): 856 | window_width, window_height = self.ClientSize 857 | scale_x = self.GetScaleX() 858 | scale_y = self.GetScaleY() 859 | self.SceneMatrixReset() 860 | self.ScenePostPan(-scene_point[0], -scene_point[1]) 861 | self.ScenePostScale(scale_x, scale_y) 862 | self.ScenePostPan(window_width / 2.0, window_height / 2.0) 863 | 864 | def FocusViewportScene(self, new_scene_viewport, buffer=0, lock=True): 865 | window_width, window_height = self.ClientSize 866 | left = new_scene_viewport[0] 867 | top = new_scene_viewport[1] 868 | right = new_scene_viewport[2] 869 | bottom = new_scene_viewport[3] 870 | viewport_width = right - left 871 | viewport_height = bottom - top 872 | 873 | left -= viewport_width * buffer 874 | right += viewport_width * buffer 875 | top -= viewport_height * buffer 876 | bottom += viewport_height * buffer 877 | 878 | if right == left: 879 | scale_x = 100 880 | else: 881 | scale_x = window_width / float(right - left) 882 | if bottom == top: 883 | scale_y = 100 884 | else: 885 | scale_y = window_height / float(bottom - top) 886 | 887 | cx = int((right + left) / 2) 888 | cy = int((top + bottom) / 2) 889 | self.matrix.Reset() 890 | self.matrix.PostTranslate(-cx, -cy) 891 | if lock: 892 | scale = min(scale_x, scale_y) 893 | if scale != 0: 894 | self.matrix.PostScale(scale) 895 | else: 896 | if scale_x != 0 and scale_y != 0: 897 | self.matrix.PostScale(scale_x, scale_y) 898 | self.matrix.PostTranslate(window_width / 2.0, window_height / 2.0) 899 | 900 | def ConvertSceneToWindow(self, position): 901 | return self.matrix.TransformPoint([position[0], position[1]]) 902 | 903 | def ConvertWindowToScene(self, position): 904 | return self.matrix.InverseTransformPoint([position[0], position[1]]) 905 | -------------------------------------------------------------------------------- /gsnodegraph/graph/btn.py: -------------------------------------------------------------------------------- 1 | 2 | import wx 3 | 4 | from gsnodegraph.constants import (BTN_NORMAL_COLOR, BTN_CLICKED_COLOR, 5 | BTN_FOCUSED_COLOR) 6 | from gsnodegraph.assets import ICON_ADD_NODE 7 | 8 | 9 | class AddNodeBtn(object): 10 | def __init__(self, parent): 11 | self.parent = parent 12 | self.bitmap = ICON_ADD_NODE.GetBitmap() 13 | self.rect = wx.Rect(0, 0, self.bitmap.Width, self.bitmap.Height) 14 | 15 | self.focused = False 16 | self.clicked = False 17 | 18 | def GetRect(self): 19 | return self.rect 20 | 21 | def SetRect(self, rect): 22 | self.rect = rect 23 | 24 | def GetHeight(self): 25 | return self.rect[3] 26 | 27 | def GetBitmap(self): 28 | return self.bitmap 29 | 30 | def IsFocused(self): 31 | return self.focused 32 | 33 | def SetFocused(self, focused): 34 | self.focused = focused 35 | 36 | def IsClicked(self): 37 | return self.clicked 38 | 39 | def SetClicked(self, clicked): 40 | self.clicked = clicked 41 | 42 | def Draw(self, dc, pnt): 43 | rect = wx.Rect(pnt[0], pnt[1], self.bitmap.Width, self.bitmap.Height) 44 | self.SetRect(rect) 45 | 46 | if self.IsClicked(): 47 | btn_color = BTN_CLICKED_COLOR 48 | elif self.IsFocused(): 49 | btn_color = BTN_FOCUSED_COLOR 50 | else: 51 | btn_color = BTN_NORMAL_COLOR 52 | 53 | dc.SetPen(wx.TRANSPARENT_PEN) 54 | dc.SetBrush(wx.Brush(wx.Colour(btn_color))) 55 | dc.DrawRoundedRectangle(self.GetRect(), 4) 56 | dc.DrawBitmap(self.GetBitmap(), pnt[0], pnt[1], useMask=False) -------------------------------------------------------------------------------- /gsnodegraph/graph/utils/__init__.py: -------------------------------------------------------------------------------- 1 | from .z_matrix import ZMatrix 2 | -------------------------------------------------------------------------------- /gsnodegraph/graph/utils/z_matrix.py: -------------------------------------------------------------------------------- 1 | # ---------------------------------------------------------------------------- 2 | # This file is code from EmbroidePy 3 | # which is licensed under the MIT License 4 | # Copyright (c) 2018 Metallicow 5 | # ---------------------------------------------------------------------------- 6 | 7 | from wx import AffineMatrix2D 8 | 9 | 10 | class ZMatrix(AffineMatrix2D): 11 | def __init__(self, *args, **kwds): 12 | AffineMatrix2D.__init__(self) 13 | 14 | def Reset(self): 15 | AffineMatrix2D.__init__(self) 16 | 17 | def PostScale(self, sx, sy=None, ax=0, ay=0): 18 | self.Invert() 19 | if sy is None: 20 | sy = sx 21 | if ax == 0 and ay == 0: 22 | self.Scale(1.0 / sx, 1.0 / sy) 23 | else: 24 | self.Translate(ax, ay) 25 | self.Scale(1.0 / sx, 1.0 / sy) 26 | self.Translate(-ax, -ay) 27 | self.Invert() 28 | 29 | def PostTranslate(self, px, py): 30 | self.Invert() 31 | self.Translate(-px, -py) 32 | self.Invert() 33 | 34 | def PostRotate(self, radians, rx=0, ry=0): 35 | self.Invert() 36 | if rx == 0 and ry == 0: 37 | self.Rotate(-radians) 38 | else: 39 | self.Translate(rx, ry) 40 | self.Rotate(-radians) 41 | self.Translate(-rx, -ry) 42 | self.Invert() 43 | 44 | def PreScale(self, sx, sy=None, ax=0, ay=0): 45 | if sy is None: 46 | sy = sx 47 | if ax == 0 and ay == 0: 48 | self.Scale(sx, sy) 49 | else: 50 | self.Translate(ax, ay) 51 | self.Scale(sx, sy) 52 | self.Translate(-ax, -ay) 53 | 54 | def PreTranslate(self, px, py): 55 | self.Translate(px, py) 56 | 57 | def PreRotate(self, radians, rx=0, ry=0): 58 | if rx == 0 and ry == 0: 59 | self.Rotate(radians) 60 | else: 61 | self.Translate(rx, ry) 62 | self.Rotate(radians) 63 | self.Translate(-rx, -ry) 64 | 65 | def GetScaleX(self): 66 | return self.Get()[0].m_11 67 | 68 | def GetScaleY(self): 69 | return self.Get()[0].m_22 70 | 71 | def GetSkewX(self): 72 | return self.Get()[0].m_12 73 | 74 | def GetSkewY(self): 75 | return self.Get()[0].m_21 76 | 77 | def GetTranslateX(self): 78 | return self.Get()[1].x 79 | 80 | def GetTranslateY(self): 81 | return self.Get()[1].y 82 | 83 | def InverseTransformPoint(self, position): 84 | self.Invert() 85 | converted_point = self.TransformPoint(position) 86 | self.Invert() 87 | return converted_point 88 | -------------------------------------------------------------------------------- /gsnodegraph/node/__init__.py: -------------------------------------------------------------------------------- 1 | from .socket import NodeSocket 2 | from .wire import NodeWire 3 | from .node import NodeBase 4 | from .utils import TruncateText -------------------------------------------------------------------------------- /gsnodegraph/node/node.py: -------------------------------------------------------------------------------- 1 | # ---------------------------------------------------------------------------- 2 | # gsnodegraph Copyright 2019-2022 by Noah Rahm and contributors 3 | # 4 | # Licensed under the Apache License, Version 2.0 (the "License"); 5 | # you may not use this file except in compliance with the License. 6 | # You may obtain a copy of the License at 7 | # 8 | # http://www.apache.org/licenses/LICENSE-2.0 9 | # 10 | # Unless required by applicable law or agreed to in writing, software 11 | # distributed under the License is distributed on an "AS IS" BASIS, 12 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | # See the License for the specific language governing permissions and 14 | # limitations under the License. 15 | # ---------------------------------------------------------------------------- 16 | 17 | import wx 18 | 19 | from gsnodegraph.assets.bitmaps import ICON_IMAGE 20 | 21 | from .socket import NodeSocket 22 | from .utils import TruncateText 23 | from ..constants import (NODE_DEFAULT_WIDTH, NODE_DEFAULT_HEIGHT, 24 | NODE_HEADER_MUTED_COLOR, 25 | SOCKET_INPUT, SOCKET_OUTPUT, NODE_THUMB_PADDING, NODE_Y_PADDING, 26 | NODE_NORMAL_COLOR, NODE_MUTED_COLOR, NODE_THUMB_BORDER_COLOR, 27 | NODE_BORDER_NORMAL_COLOR, NODE_BORDER_SELECTED_COLOR) 28 | from ..assets import (ICON_BRUSH_CHECKERBOARD, ICON_IMAGE) 29 | 30 | 31 | class NodeBase(object): 32 | def __init__(self, nodegraph, id): 33 | self.nodegraph = nodegraph 34 | self.id = id 35 | self.idname = None 36 | self.pos = wx.Point(0, 0) 37 | self.size = wx.Size(NODE_DEFAULT_WIDTH, NODE_DEFAULT_HEIGHT) 38 | self.header_color = wx.Colour("#242424") 39 | 40 | self.expanded = False 41 | self.selected = False 42 | self.active = False 43 | self.muted = False 44 | self.is_output = False 45 | 46 | self.sockets = [] 47 | self.properties = {} 48 | self.outputs = {} 49 | 50 | self.label = "" 51 | self.category = None 52 | self.has_thumbnail = False 53 | 54 | self.thumbnail = self.CreateEmptyBitmap() 55 | self.expandicon_bmp = ICON_IMAGE.GetBitmap() 56 | self.checkerboard_bmp = ICON_BRUSH_CHECKERBOARD.GetBitmap() 57 | 58 | @property 59 | def NodeGraph(self): 60 | return self.nodegraph 61 | 62 | @property 63 | def NodeDatatypes(self): 64 | return self.nodegraph.node_datatypes 65 | 66 | @property 67 | def NodeCategories(self): 68 | return self.nodegraph.node_categories 69 | 70 | @property 71 | def NodeImageDatatype(self): 72 | return self.nodegraph.image_datatype 73 | 74 | def Init(self, idname) -> None: 75 | self.InitSockets() 76 | self.InitHeaderColor() 77 | self.InitSize() 78 | self.InitLabel() 79 | self.SetIdName(idname) 80 | 81 | def CreateEmptyBitmap(self) -> wx.Bitmap: 82 | img = wx.Image(120, 120) 83 | img.SetMaskColour(0,0,0) 84 | img.InitAlpha() 85 | return img.ConvertToBitmap() 86 | 87 | def AddSocket(self, label, color, direction) -> None: 88 | self.ArrangeSockets() 89 | 90 | def HitTest(self, pos: wx.Point) -> None: 91 | # Handle expanding the node to show thumbnail hittest 92 | if self.HasThumbnail() and wx.GetMouseState().LeftIsDown(): 93 | icon_rect = self.expandicon_rect.Inflate(10, 10) 94 | mouse_rect = wx.Rect(pos[0], pos[1], 2, 2) 95 | if mouse_rect.Intersects(icon_rect): 96 | self.ToggleExpand() 97 | 98 | # Handle socket hittest 99 | for socket in self.sockets: 100 | if socket.HitTest(pos - self.pos): 101 | return socket 102 | 103 | def EditConnection(self, name, binding, socket): 104 | pass 105 | 106 | def InitHeaderColor(self) -> None: 107 | self.header_color = wx.Colour(self.NodeCategories[self.GetCategory()]) 108 | 109 | def InitSockets(self) -> None: 110 | sockets = [] 111 | ins = [] 112 | outs = [] 113 | 114 | # Create a list of input and output sockets with the format: 115 | # [(label, idname, datatype), ...] 116 | for prop_id in self.properties: 117 | prop = self.properties[prop_id] 118 | if prop.exposed and prop.can_be_exposed: 119 | ins.append((prop.label, prop.idname, prop.datatype)) 120 | 121 | if self.IsOutputNode() is not True: 122 | for output_id in self.outputs: 123 | output = self.outputs[output_id] 124 | outs.append((output.label, output.idname, output.datatype)) 125 | # If there is an image datatype then we know there 126 | # should be a thumbnail for this node. 127 | if output.datatype == self.NodeImageDatatype: 128 | self.has_thumbnail = True 129 | 130 | x, y, w, h = self.GetRect() 131 | x, y = self.pos 132 | w, h = self.size 133 | 134 | for i, p in enumerate(outs + ins): 135 | socket_type = SOCKET_INPUT # Socket type IN 136 | x = 0 # socket margin 137 | if (p[0], p[1], p[2]) in outs: 138 | x = w - x - 1 139 | socket_type = SOCKET_OUTPUT # Socket type OUT 140 | 141 | # We keep track of where the last socket is placed 142 | self.lastsocket_pos = 60 + 14 * i 143 | 144 | # Create the node sockets 145 | socket = NodeSocket(label=p[0], idname=p[1], datatype=p[2], 146 | node=self, direction=socket_type) 147 | socket.pos = wx.Point(x, int(45 + (19 * i))) 148 | socket.SetColor(self.NodeDatatypes[socket.datatype]) 149 | sockets.append(socket) 150 | 151 | self.sockets = sockets 152 | 153 | def InitSize(self) -> None: 154 | # Calculate the normal size of the node to fit 155 | # the amount of sockets the node has. The expanded size 156 | # is calculated to be the normal size plus the image thumbnail size. 157 | calc_height = self.lastsocket_pos + self.thumbnail.Height + NODE_THUMB_PADDING * 2 158 | self.expanded_size = wx.Size(NODE_DEFAULT_WIDTH, calc_height) 159 | 160 | self.normal_size = wx.Size(NODE_DEFAULT_WIDTH, 161 | self.lastsocket_pos+(NODE_Y_PADDING*2)) 162 | 163 | # Set the initial node size 164 | if self.IsExpanded(): 165 | self.SetSize(self.expanded_size) 166 | else: 167 | self.SetSize(self.normal_size) 168 | 169 | def InitLabel(self): 170 | # Number of chars to truncate from the label is based on 171 | # whether there is a toggle icon taking up space on this node. 172 | if self.HasThumbnail() == True: 173 | chars = 15 174 | else: 175 | chars = 20 176 | self.label = TruncateText(self.GetLabel(), chars) 177 | 178 | def HasThumbnail(self) -> bool: 179 | return self.has_thumbnail 180 | 181 | def IsOutputNode(self) -> bool: 182 | """ Override method to set whether the node is the output or not. """ 183 | return self.is_output 184 | 185 | def GetLabel(self) -> str: 186 | """ Get the node label. """ 187 | return self.label 188 | 189 | def GetCategory(self) -> str: 190 | """ Override method to set the node category. """ 191 | return self.category 192 | 193 | def GetIdname(self) -> str: 194 | return self.idname 195 | 196 | def SetIdName(self, idname) -> None: 197 | self.idname = idname 198 | 199 | def GetPosition(self) -> wx.Point: 200 | return self.pos 201 | 202 | def SetPosition(self, x: int, y: int) -> None: 203 | self.pos = wx.Point(x, y) 204 | 205 | def GetSize(self) -> wx.Size: 206 | return (self.size[0], self.size[1]) 207 | 208 | def SetSize(self, size: wx.Size) -> None: 209 | self.size = size 210 | 211 | def GetRect(self) -> wx.Rect: 212 | return wx.Rect(self.pos[0], self.pos[1], self.size[0], self.size[1]) 213 | 214 | def IsSelected(self) -> bool: 215 | return self.selected 216 | 217 | def SetSelected(self, selected=True) -> None: 218 | self.selected = selected 219 | 220 | def IsActive(self) -> bool: 221 | return self.active 222 | 223 | def SetActive(self, active=True) -> None: 224 | self.active = active 225 | 226 | def IsMuted(self) -> bool: 227 | return self.muted 228 | 229 | def SetMuted(self, muted=True) -> None: 230 | self.muted = muted 231 | self.SetExpanded(False) 232 | self.SetSize(self.normal_size) 233 | 234 | def IsExpanded(self) -> bool: 235 | return self.expanded 236 | 237 | def SetExpanded(self, expanded=True) -> None: 238 | self.expanded = expanded 239 | 240 | def ToggleExpand(self) -> None: 241 | if self.HasThumbnail(): 242 | if self.IsExpanded() is True: 243 | self.SetExpanded(False) 244 | self.SetSize(self.normal_size) 245 | elif self.IsExpanded() is False: 246 | self.SetExpanded(True) 247 | self.SetSize(self.expanded_size) 248 | 249 | def GetSockets(self) -> list: 250 | return self.sockets 251 | 252 | def SetThumbnail(self, thumb) -> None: 253 | if self.HasThumbnail(): 254 | self.thumbnail = thumb 255 | self.UpdateExpandSize() 256 | 257 | def UpdateExpandSize(self) -> None: 258 | calc_height = self.lastsocket_pos + self.thumbnail.Height + NODE_THUMB_PADDING * 2 259 | self.expanded_size = wx.Size(NODE_DEFAULT_WIDTH, calc_height) 260 | self.SetSize(self.expanded_size) 261 | 262 | def FindSocket(self, idname): 263 | """ Return the node socket with the given name. 264 | :param idname: the socket idname as a string 265 | :returns: Socket object 266 | """ 267 | for socket in self.GetSockets(): 268 | if socket.idname == idname: 269 | return socket 270 | 271 | def Draw(self, dc) -> None: 272 | x, y = self.GetPosition() 273 | w, h = self.GetSize() 274 | 275 | # Node body and border 276 | if self.IsSelected() or self.IsActive(): 277 | border_color = NODE_BORDER_SELECTED_COLOR 278 | else: 279 | border_color = NODE_BORDER_NORMAL_COLOR 280 | if self.IsMuted(): 281 | node_color = NODE_MUTED_COLOR 282 | else: 283 | node_color = NODE_NORMAL_COLOR 284 | dc.SetPen(wx.Pen(wx.Colour(border_color), 1)) 285 | dc.SetBrush(wx.Brush(wx.Colour(node_color))) 286 | dc.DrawRoundedRectangle(x, y, w, h, 3) 287 | 288 | # Node header 289 | dc.SetPen(wx.Pen(wx.TRANSPARENT_PEN)) 290 | if self.IsMuted(): 291 | header_color = wx.Colour(NODE_HEADER_MUTED_COLOR) 292 | bottom_color = wx.Colour(NODE_HEADER_MUTED_COLOR).ChangeLightness(80) 293 | else: 294 | header_color = wx.Colour(self.header_color).ChangeLightness(70) 295 | bottom_color = wx.Colour(self.header_color).ChangeLightness(55) 296 | dc.SetBrush(wx.Brush(header_color)) 297 | dc.DrawRoundedRectangle(x+1, y+1, w-2, 25, 3) 298 | 299 | # Bottom border of the node header (to cover up the rounded bottom) 300 | dc.SetBrush(wx.Brush(bottom_color)) 301 | dc.DrawRectangle(x+1, y+24, w-2, 2) 302 | 303 | # Node name label 304 | if self.IsMuted(): 305 | color = wx.Colour('#fff').ChangeLightness(60) 306 | else: 307 | color = wx.Colour('#fff').ChangeLightness(85) 308 | dc.SetTextForeground(color) 309 | dc.DrawText(self.GetLabel(), x+10, y+1) 310 | 311 | # Node sockets 312 | [socket.Draw(dc) for socket in self.sockets] 313 | 314 | # Expand node thumbnail icon 315 | if self.HasThumbnail() == True and self.IsMuted() != True: 316 | self.expandicon_rect = wx.Rect(x+NODE_DEFAULT_WIDTH-28, y+5, 16, 16) 317 | dc.DrawBitmap(self.expandicon_bmp, self.expandicon_rect[0], 318 | self.expandicon_rect[1], True) 319 | 320 | # Thumbnail 321 | if self.IsExpanded() and self.HasThumbnail(): 322 | # Calculate the coords for the placement of the thumbnail 323 | thumb_rect = wx.Rect(int(x+NODE_THUMB_PADDING/2), 324 | int(y+self.lastsocket_pos+(NODE_Y_PADDING*2)), 325 | NODE_DEFAULT_WIDTH-NODE_THUMB_PADDING, 326 | self.thumbnail.Height) 327 | 328 | # Draw thumbnail border and background 329 | dc.SetPen(wx.Pen(wx.Colour(NODE_THUMB_BORDER_COLOR), 1)) 330 | dc.SetBrush(wx.Brush(self.checkerboard_bmp)) 331 | dc.DrawRectangle(thumb_rect) 332 | 333 | # Draw the thumbnail 334 | dc.DrawBitmap(self.thumbnail, thumb_rect[0], thumb_rect[1], True) 335 | -------------------------------------------------------------------------------- /gsnodegraph/node/socket.py: -------------------------------------------------------------------------------- 1 | # ---------------------------------------------------------------------------- 2 | # gsnodegraph Copyright 2019-2022 by Noah Rahm and contributors 3 | # 4 | # Licensed under the Apache License, Version 2.0 (the "License"); 5 | # you may not use this file except in compliance with the License. 6 | # You may obtain a copy of the License at 7 | # 8 | # http://www.apache.org/licenses/LICENSE-2.0 9 | # 10 | # Unless required by applicable law or agreed to in writing, software 11 | # distributed under the License is distributed on an "AS IS" BASIS, 12 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | # See the License for the specific language governing permissions and 14 | # limitations under the License. 15 | # ---------------------------------------------------------------------------- 16 | 17 | import math 18 | import wx 19 | 20 | from ..constants import (SOCKET_BORDER_COLOR, SOCKET_INPUT, SOCKET_HIT_RADIUS, 21 | SOCKET_RADIUS, SOCKET_BORDER_COLOR) 22 | 23 | 24 | class NodeSocket(object): 25 | """ 26 | Node socket showing the datatypes and flow of the node relative to 27 | the graph. Wires are dropped into the socket to connect nodes. 28 | """ 29 | def __init__(self, label, idname, datatype, node, direction): 30 | self.label = label 31 | self.idname = idname 32 | self.node = node 33 | self.direction = direction 34 | self.datatype = datatype 35 | 36 | self.wires = [] 37 | self.pos = wx.Point(0, 0) 38 | self.color = wx.Colour("#fff") 39 | self.tdc = wx.WindowDC(wx.GetApp().GetTopWindow()) 40 | 41 | #self.SetColorByDataType(self.datatype) 42 | 43 | def GetWires(self) -> list: 44 | """ Get the wires for this socket. """ 45 | return self.wires 46 | 47 | def SetColor(self, color) -> None: 48 | """ Set the socket base color based on the datatype. """ 49 | self.color = wx.Colour(color) 50 | 51 | def CurrentSocketPos(self) -> wx.Point: 52 | """ Return the current coords of the node socket. """ 53 | return self.pos + self.node.pos 54 | 55 | def HitTest(self, pos) -> bool: 56 | """ Returns True if the node socket was hit. """ 57 | pnt = pos - self.pos 58 | distance = math.sqrt(math.pow(pnt.x, 2) + math.pow(pnt.y, 2)) 59 | 60 | # Socket hit radius 61 | if math.fabs(distance) < SOCKET_HIT_RADIUS: 62 | return True 63 | 64 | def Draw(self, dc) -> None: 65 | """ Draw the node socket. """ 66 | pos = self.CurrentSocketPos() 67 | w, h = self.tdc.GetTextExtent(self.label) 68 | 69 | # Set the socket color 70 | dc.SetPen(wx.Pen(wx.Colour(SOCKET_BORDER_COLOR), 1)) 71 | dc.SetBrush(wx.Brush(self.color)) 72 | 73 | # Draw the socket 74 | dc.DrawCircle(pos.x, pos.y, SOCKET_RADIUS) 75 | 76 | # Socket label margin 77 | if self.direction == SOCKET_INPUT: 78 | x = int(pos.x + 12) 79 | else: 80 | x = int(pos.x - w - 12) 81 | 82 | # Draw the label 83 | dc.DrawText(self.label, x, int(pos.y - h / 2)) 84 | -------------------------------------------------------------------------------- /gsnodegraph/node/utils.py: -------------------------------------------------------------------------------- 1 | # ---------------------------------------------------------------------------- 2 | # gsnodegraph Copyright 2019-2022 by Noah Rahm and contributors 3 | # 4 | # Licensed under the Apache License, Version 2.0 (the "License"); 5 | # you may not use this file except in compliance with the License. 6 | # You may obtain a copy of the License at 7 | # 8 | # http://www.apache.org/licenses/LICENSE-2.0 9 | # 10 | # Unless required by applicable law or agreed to in writing, software 11 | # distributed under the License is distributed on an "AS IS" BASIS, 12 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | # See the License for the specific language governing permissions and 14 | # limitations under the License. 15 | # ---------------------------------------------------------------------------- 16 | 17 | 18 | def TruncateText(text_string, str_length=18): 19 | """ Truncate the text string after a certain 20 | number of characters. 21 | """ 22 | chars = [] 23 | for char in text_string: 24 | chars.append(char) 25 | 26 | if len(chars) > str_length: 27 | words = chars[:str_length - 1] 28 | text = "".join(words) 29 | return "{}...".format(text) 30 | else: 31 | return text_string -------------------------------------------------------------------------------- /gsnodegraph/node/wire.py: -------------------------------------------------------------------------------- 1 | # ---------------------------------------------------------------------------- 2 | # gsnodegraph Copyright 2019-2022 by Noah Rahm and contributors 3 | # 4 | # Licensed under the Apache License, Version 2.0 (the "License"); 5 | # you may not use this file except in compliance with the License. 6 | # You may obtain a copy of the License at 7 | # 8 | # http://www.apache.org/licenses/LICENSE-2.0 9 | # 10 | # Unless required by applicable law or agreed to in writing, software 11 | # distributed under the License is distributed on an "AS IS" BASIS, 12 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | # See the License for the specific language governing permissions and 14 | # limitations under the License. 15 | # ---------------------------------------------------------------------------- 16 | 17 | import wx 18 | 19 | from ..constants import WIRE_NORMAL_COLOR, WIRE_ACTIVE_COLOR 20 | 21 | 22 | class NodeWire(object): 23 | """ Wire for showing a connection between two nodes. """ 24 | def __init__(self, parent, pnt1, pnt2, srcsocket, dstsocket, direction, curvature): 25 | self.parent = parent 26 | self.pnt1 = pnt1 27 | self.pnt2 = pnt2 28 | self.srcsocket = srcsocket 29 | self.dstsocket = dstsocket 30 | self.curvature = curvature 31 | self.direction = direction 32 | 33 | self.srcnode = None 34 | self.dstnode = None 35 | self.active = False 36 | 37 | def SetCurvature(self, curvature) -> None: 38 | """ Set the curvature of the wire. """ 39 | self.curvature = curvature 40 | 41 | def GetRect(self) -> wx.Rect: 42 | """ Get the bounding box rect of the wire. """ 43 | min_x = min(self.pnt1[0], self.pnt2[0]) 44 | min_y = min(self.pnt1[1], self.pnt2[1]) 45 | size = self.pnt2 - self.pnt1 46 | rect = wx.Rect(min_x - 10, min_y, abs(size[0]) + 20, abs(size[1])) 47 | return rect.Inflate(2, 2) 48 | 49 | def Draw(self, dc) -> None: 50 | """ Draw the node wire. """ 51 | # Direction of wire 52 | sign = 1 53 | if self.direction == 0: 54 | sign = -1 55 | 56 | # Curvature of the wire 57 | curvature = int(self.curvature * 2) 58 | 59 | # Wire color 60 | if self.active is True: 61 | color = WIRE_ACTIVE_COLOR 62 | else: 63 | color = WIRE_NORMAL_COLOR 64 | dc.SetPen(wx.Pen(wx.Colour(color), 3)) 65 | 66 | # If the wire has curvature, use a spline 67 | if self.curvature > 0: 68 | pnts = [] 69 | pnts.append(self.pnt1) 70 | pnts.append(self.pnt1 + wx.Point(curvature * sign, 0)) 71 | pnts.append(self.pnt2 - wx.Point(curvature * sign, 0)) 72 | pnts.append(self.pnt2) 73 | 74 | dc.DrawSpline(pnts) 75 | 76 | else: 77 | # Otherwise, use a line 78 | dc.DrawLine(self.pnt1[0], self.pnt1[1], self.pnt2[0], self.pnt2[1]) 79 | -------------------------------------------------------------------------------- /main.py: -------------------------------------------------------------------------------- 1 | # ---------------------------------------------------------------------------- 2 | # gsnodegraph Copyright 2019-2022 by Noah Rahm and contributors 3 | # 4 | # Licensed under the Apache License, Version 2.0 (the "License"); 5 | # you may not use this file except in compliance with the License. 6 | # You may obtain a copy of the License at 7 | # 8 | # http://www.apache.org/licenses/LICENSE-2.0 9 | # 10 | # Unless required by applicable law or agreed to in writing, software 11 | # distributed under the License is distributed on an "AS IS" BASIS, 12 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | # See the License for the specific language governing permissions and 14 | # limitations under the License. 15 | # ---------------------------------------------------------------------------- 16 | 17 | import sys 18 | import wx 19 | import ctypes 20 | try: 21 | ctypes.windll.shcore.SetProcessDpiAwareness(True) 22 | except Exception: 23 | pass 24 | 25 | from gsnodegraph import EVT_GSNODEGRAPH_ADDNODEBTN 26 | from nodes import OutputNode, MixNode, ImageNode, BlurNode, BlendNode, ValueNode 27 | from nodegraph import NodeGraph 28 | 29 | 30 | # Install a custom displayhook to keep Python from setting the global 31 | # _ (underscore) to the value of the last evaluated expression. 32 | # If we don't do this, our mapping of _ to gettext can get overwritten. 33 | # This is useful/needed in interactive debugging with PyShell. 34 | def _displayHook(obj): 35 | """ Custom display hook to prevent Python stealing '_'. """ 36 | 37 | if obj is not None: 38 | print(repr(obj)) 39 | 40 | # Add translation macro to builtin similar to what gettext does. 41 | import builtins 42 | builtins.__dict__['_'] = wx.GetTranslation 43 | 44 | 45 | class MainApp(wx.App): 46 | 47 | def OnInit(self): 48 | 49 | # Work around for Python stealing "_". 50 | sys.displayhook = _displayHook 51 | 52 | return True 53 | 54 | 55 | class MyFrame(wx.Frame): 56 | def __init__(self, parent, id=wx.ID_ANY, title=wx.EmptyString, 57 | pos=wx.DefaultPosition, size=wx.DefaultSize, 58 | style=wx.DEFAULT_FRAME_STYLE, name="frame"): 59 | wx.Frame.__init__(self, parent, id, title, pos, size, style, name) 60 | 61 | # Setup the node registry 62 | node_registry = { 63 | "image_nodeid": ImageNode, 64 | "mix_nodeid": MixNode, 65 | "blur_nodeid": BlurNode, 66 | "blend_nodeid": BlendNode, 67 | "value_nodeid": ValueNode, 68 | "output_nodeid": OutputNode 69 | } 70 | # Setup the config with datatypes and node categories 71 | config = { 72 | "image_datatype": "IMAGE", 73 | "node_datatypes": { 74 | "IMAGE": "#C6C62D", # Yellow 75 | "INTEGER": "#A0A0A0", # Grey 76 | "FLOAT": "#A0A0A0", # Grey 77 | "VALUE": "#A0A0A0", # Depreciated! 78 | }, 79 | "input_nodes_categories": ["INPUT"], 80 | "node_categories": { 81 | "INPUT": "#E64555", # Burgendy 82 | "DRAW": "#AF4467", # Pink 83 | "MASK": "#084D4D", # Blue-green 84 | "CONVERT": "#564B7C", # Purple 85 | "FILTER": "#558333", # Green 86 | "BLEND": "#498DB8", # Light blue 87 | "COLOR": "#C2AF3A", # Yellow 88 | "TRANSFORM": "#6B8B8B", # Blue-grey 89 | "OUTPUT": "#B33641" # Red 90 | } 91 | } 92 | 93 | # Init the nodegraph 94 | ng = NodeGraph(self, registry=node_registry, config=config) 95 | 96 | # Add nodes to the node graph 97 | node1 = ng.AddNode("image_nodeid", pos=wx.Point(100, 10)) 98 | node2 = ng.AddNode("image_nodeid", pos=wx.Point(450, 400)) 99 | node3 = ng.AddNode("mix_nodeid", pos=wx.Point(400, 100)) 100 | node4 = ng.AddNode("blur_nodeid", pos=wx.Point(700, 100)) 101 | node5 = ng.AddNode("blend_nodeid", pos=wx.Point(720, 300)) 102 | node6 = ng.AddNode("value_nodeid", pos=wx.Point(620, 430)) 103 | node7 = ng.AddNode("output_nodeid", pos=wx.Point(1000, 290)) 104 | 105 | # Maximize the window 106 | self.Maximize(True) 107 | 108 | # Bind events 109 | ng.Bind(EVT_GSNODEGRAPH_ADDNODEBTN, self.OnAddNodeMenuBtn) 110 | self.Bind(wx.EVT_CLOSE, self.OnDestroy) 111 | 112 | def OnAddNodeMenuBtn(self, event): 113 | print("Open add node menu") 114 | 115 | def OnDestroy(self, event): 116 | self.Destroy() 117 | 118 | 119 | if __name__ == "__main__": 120 | app = MainApp() 121 | frame = MyFrame(None, size=(512, 512)) 122 | frame.SetTitle("gsnodegraph demo") 123 | frame.Show() 124 | app.MainLoop() 125 | -------------------------------------------------------------------------------- /nodegraph.py: -------------------------------------------------------------------------------- 1 | # ---------------------------------------------------------------------------- 2 | # gsnodegraph Copyright 2019-2022 by Noah Rahm and contributors 3 | # 4 | # Licensed under the Apache License, Version 2.0 (the "License"); 5 | # you may not use this file except in compliance with the License. 6 | # You may obtain a copy of the License at 7 | # 8 | # http://www.apache.org/licenses/LICENSE-2.0 9 | # 10 | # Unless required by applicable law or agreed to in writing, software 11 | # distributed under the License is distributed on an "AS IS" BASIS, 12 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | # See the License for the specific language governing permissions and 14 | # limitations under the License. 15 | # ---------------------------------------------------------------------------- 16 | 17 | import wx 18 | 19 | from gsnodegraph import NodeGraphBase 20 | from gsnodegraph.constants import * 21 | 22 | 23 | class NodeGraph(NodeGraphBase): 24 | def __init__(self, parent, registry, config, *args, **kwds): 25 | NodeGraphBase.__init__(self, parent, registry, config, *args, **kwds) 26 | 27 | # Do your custom stuff and overrides here 28 | -------------------------------------------------------------------------------- /nodes/__init__.py: -------------------------------------------------------------------------------- 1 | from .nodes import (OutputNode, MixNode, ImageNode, BlurNode, 2 | BlendNode, ValueNode) 3 | -------------------------------------------------------------------------------- /nodes/nodes.py: -------------------------------------------------------------------------------- 1 | # ---------------------------------------------------------------------------- 2 | # gsnodegraph Copyright 2019-2022 by Noah Rahm and contributors 3 | # 4 | # Licensed under the Apache License, Version 2.0 (the "License"); 5 | # you may not use this file except in compliance with the License. 6 | # You may obtain a copy of the License at 7 | # 8 | # http://www.apache.org/licenses/LICENSE-2.0 9 | # 10 | # Unless required by applicable law or agreed to in writing, software 11 | # distributed under the License is distributed on an "AS IS" BASIS, 12 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | # See the License for the specific language governing permissions and 14 | # limitations under the License. 15 | # ---------------------------------------------------------------------------- 16 | 17 | from gsnodegraph import NodeBase 18 | 19 | 20 | class Image(object): 21 | """ An example datatype. """ 22 | # Create your datatype. For this example, we do nothing. 23 | pass 24 | 25 | 26 | class Property(object): 27 | """ Example property base class. """ 28 | def __init__(self, idname, default, label, exposed=True, 29 | can_be_exposed=True, visible=False): 30 | self.idname = idname 31 | self.label = label 32 | self.default = default 33 | self.exposed = exposed 34 | self.can_be_exposed = can_be_exposed 35 | self.visible = visible 36 | self.binding = None 37 | self.datatype = None 38 | 39 | 40 | class ImageProp(Property): 41 | """ Example property. """ 42 | def __init__(self, idname, default=Image(), label="Image", exposed=True, 43 | can_be_exposed=True, visible=True): 44 | Property.__init__(self, idname, default, label, exposed, 45 | can_be_exposed, visible) 46 | self.value = default 47 | self.datatype = "IMAGE" 48 | 49 | def GetValue(self): 50 | return self.value 51 | 52 | def SetValue(self, value): 53 | self.value = value 54 | 55 | 56 | class IntegerProp(Property): 57 | """ Example property. """ 58 | def __init__(self, idname, default=1, label="Integer", exposed=True, 59 | can_be_exposed=True, visible=True): 60 | Property.__init__(self, idname, default, label, exposed, 61 | can_be_exposed, visible) 62 | self.value = default 63 | self.datatype = "INTEGER" 64 | 65 | def GetValue(self): 66 | return self.value 67 | 68 | def SetValue(self, value): 69 | self.value = value 70 | 71 | 72 | class Output(object): 73 | def __init__(self, idname, datatype, label, visible=True): 74 | self.idname = idname 75 | self.datatype = datatype 76 | self.label = label 77 | self.visible = visible 78 | 79 | 80 | class OutputNode(NodeBase): 81 | """ Example output node. Only one of these should exist at a time. 82 | Use ``self._isoutput = True`` to set as the output node. """ 83 | def __init__(self, nodegraph, _id): 84 | NodeBase.__init__(self, nodegraph, _id) 85 | 86 | self.label = "Output" 87 | self.is_output = True 88 | self.category = "OUTPUT" 89 | self.properties = { 90 | "image_socketid": ImageProp("image_socketid", label="Image") 91 | } 92 | 93 | 94 | class ImageNode(NodeBase): 95 | """ Example node showing an input node. """ 96 | def __init__(self, nodegraph, _id): 97 | NodeBase.__init__(self, nodegraph, _id) 98 | 99 | self.label = "Image" 100 | self.category = "INPUT" 101 | self.outputs = { 102 | "image": Output(idname="image", datatype="IMAGE", label="Image") 103 | } 104 | self.properties = {} 105 | 106 | 107 | class MixNode(NodeBase): 108 | """ Example node showing a node with multiple inputs. """ 109 | def __init__(self, nodegraph, _id): 110 | NodeBase.__init__(self, nodegraph, _id) 111 | 112 | self.label = "Mix" 113 | self.category = "BLEND" 114 | self.outputs = { 115 | "image": Output(idname="image", datatype="IMAGE", label="Image"), 116 | "alpha": Output(idname="alpha", datatype="INTEGER", label="Alpha") 117 | } 118 | self.properties = { 119 | "image1_socketid": ImageProp("image1_socketid", label="Overlay"), 120 | "image2_socketid": ImageProp("image2_socketid", label="Image") 121 | } 122 | 123 | 124 | class BlurNode(NodeBase): 125 | """ Example node showing a node with multiple inputs 126 | and different datatypes. """ 127 | def __init__(self, nodegraph, _id): 128 | NodeBase.__init__(self, nodegraph, _id) 129 | 130 | self.label = "Blur" 131 | self.category = "FILTER" 132 | self.outputs = { 133 | "image": Output(idname="image", datatype="IMAGE", label="Image") 134 | } 135 | self.properties = { 136 | "image1_socketid": ImageProp("image1_socketid", label="Image"), 137 | "int_socketid": IntegerProp("int_socketid", label="Integer") 138 | } 139 | 140 | 141 | class BlendNode(NodeBase): 142 | """ Example node showing a node with multiple inputs. """ 143 | def __init__(self, nodegraph, _id): 144 | NodeBase.__init__(self, nodegraph, _id) 145 | 146 | self.label = "Brightness/Contrast" 147 | self.category = "BLEND" 148 | 149 | self.outputs = { 150 | "image": Output(idname="image", datatype="IMAGE", label="Image") 151 | } 152 | self.properties = { 153 | "alphamask_socketid": ImageProp("alphamask_socketid", label="Alpha"), 154 | "image1_socketid": ImageProp("image1_socketid", label="Image"), 155 | "image2_socketid": ImageProp("image2_socketid", label="Image") 156 | } 157 | 158 | 159 | class ValueNode(NodeBase): 160 | """ Example node showing a node with a different datatype output. """ 161 | def __init__(self, nodegraph, _id): 162 | NodeBase.__init__(self, nodegraph, _id) 163 | 164 | self.label = "Value" 165 | self.category = "INPUT" 166 | self.outputs = { 167 | "value": Output(idname="value", datatype="INTEGER", label="Value") 168 | } 169 | 170 | -------------------------------------------------------------------------------- /pyproject.toml: -------------------------------------------------------------------------------- 1 | [build-system] 2 | requires = [ 3 | "setuptools>=42", 4 | "wheel" 5 | ] 6 | build-backend = "setuptools.build_meta" 7 | -------------------------------------------------------------------------------- /setup.cfg: -------------------------------------------------------------------------------- 1 | [metadata] 2 | description-file = README.md 3 | 4 | -------------------------------------------------------------------------------- /setup.py: -------------------------------------------------------------------------------- 1 | from setuptools import setup 2 | 3 | 4 | setup( 5 | name = 'gsnodegraph', 6 | packages = ['gsnodegraph', 7 | 'gsnodegraph.graph', 8 | 'gsnodegraph.graph.utils', 9 | 'gsnodegraph.node', 10 | 'gsnodegraph.assets'], 11 | version = '0.5.6', 12 | license = 'Apache 2.0', 13 | description = 'Powerful node graph widget for wxPython GUIs', 14 | long_description_content_type="text/markdown", 15 | author = 'Noah Rahm and contributors', 16 | author_email = 'correctsyntax@yahoo.com', 17 | url = 'https://github.com/GimelStudio/gsnodegraph', 18 | keywords = ['nodegraph', 'nodes', 'graph', 'node-based', 'widget'], 19 | install_requires = [ 20 | 'wxpython==4.2.1' 21 | ], 22 | classifiers = [ 23 | 'Development Status :: 2 - Pre-Alpha', 24 | 'Intended Audience :: Developers', 25 | 'Operating System :: OS Independent', 26 | 'Topic :: Desktop Environment', 27 | 'Topic :: Multimedia :: Graphics :: Editors', 28 | 'License :: OSI Approved :: Apache Software License', 29 | 'Programming Language :: Python :: 3', 30 | 'Programming Language :: Python :: 3.8', 31 | 'Programming Language :: Python :: 3.9', 32 | 'Programming Language :: Python :: 3.10', 33 | 'Programming Language :: Python :: 3.11', 34 | ], 35 | ) 36 | --------------------------------------------------------------------------------