armory/blender/arm/nodes_logic.py

204 lines
7.3 KiB
Python
Raw Normal View History

2015-10-30 13:23:09 +01:00
import bpy
2017-03-06 02:29:03 +01:00
from bpy.types import NodeTree
2015-10-30 13:23:09 +01:00
from bpy.props import *
2017-03-06 02:29:03 +01:00
import nodeitems_utils
2020-06-25 12:14:57 +02:00
from nodeitems_utils import NodeCategory, NodeItem
2017-03-15 12:30:14 +01:00
from arm.logicnode import *
2017-04-10 21:17:17 +02:00
import webbrowser
2017-03-06 02:29:03 +01:00
registered_nodes = []
2016-10-19 13:28:06 +02:00
2017-03-06 02:29:03 +01:00
class ArmLogicTree(NodeTree):
"""Logic nodes"""
bl_idname = 'ArmLogicTreeType'
2018-12-19 15:47:58 +01:00
bl_label = 'Logic Node Editor'
bl_icon = 'DECORATE'
2015-10-30 13:23:09 +01:00
2017-03-06 02:29:03 +01:00
class LogicNodeCategory(NodeCategory):
@classmethod
def poll(cls, context):
return context.space_data.tree_type == 'ArmLogicTreeType'
2016-08-14 21:08:01 +02:00
2017-03-06 02:29:03 +01:00
def register_nodes():
global registered_nodes
2016-08-14 21:08:01 +02:00
2017-03-06 02:29:03 +01:00
# Re-register all nodes for now..
if len(registered_nodes) > 0:
unregister_nodes()
2015-10-30 13:23:09 +01:00
2017-03-06 02:29:03 +01:00
for n in arm_nodes.nodes:
registered_nodes.append(n)
bpy.utils.register_class(n)
node_categories = []
for category in sorted(arm_nodes.category_items):
if category == 'Layout':
# Handled separately
continue
sorted_items = sorted(arm_nodes.category_items[category], key=lambda item: item.nodetype)
node_categories.append(
2018-10-29 18:09:46 +01:00
LogicNodeCategory('Logic' + category + 'Nodes', category, items=sorted_items)
)
2017-03-06 02:29:03 +01:00
# Add special layout nodes known from Blender's node editors
if 'Layout' in arm_nodes.category_items:
# Clone with [:] to prevent double entries
layout_items = arm_nodes.category_items['Layout'][:]
else:
layout_items = []
layout_items += [NodeItem('NodeReroute'), NodeItem('NodeFrame')]
layout_items = sorted(layout_items, key=lambda item: item.nodetype)
node_categories.append(
LogicNodeCategory('LogicLayoutNodes', 'Layout', description='Layout Nodes', items=layout_items)
)
2017-04-04 23:11:31 +02:00
nodeitems_utils.register_node_categories('ArmLogicNodes', node_categories)
2015-10-30 13:23:09 +01:00
2017-03-06 02:29:03 +01:00
def unregister_nodes():
global registered_nodes
for n in registered_nodes:
bpy.utils.unregister_class(n)
registered_nodes = []
2017-04-04 23:11:31 +02:00
nodeitems_utils.unregister_node_categories('ArmLogicNodes')
2015-10-30 13:23:09 +01:00
2019-04-29 13:14:32 +02:00
class ARM_PT_LogicNodePanel(bpy.types.Panel):
2018-06-12 00:26:52 +02:00
bl_label = 'Armory Logic Node'
2019-04-29 13:14:32 +02:00
bl_idname = 'ARM_PT_LogicNodePanel'
2017-04-10 21:17:17 +02:00
bl_space_type = 'NODE_EDITOR'
bl_region_type = 'UI'
2018-12-19 15:47:58 +01:00
bl_category = 'Node'
2017-04-10 21:17:17 +02:00
def draw(self, context):
layout = self.layout
2018-12-19 13:33:17 +01:00
layout.use_property_split = True
2018-12-19 20:10:34 +01:00
layout.use_property_decorate = False
2017-04-10 21:17:17 +02:00
if context.active_node != None and context.active_node.bl_idname.startswith('LN'):
2018-06-15 00:30:40 +02:00
layout.prop(context.active_node, 'arm_logic_id')
layout.prop(context.active_node, 'arm_watch')
2017-04-10 21:17:17 +02:00
layout.operator('arm.open_node_source')
class ArmOpenNodeSource(bpy.types.Operator):
'''Expose Haxe source'''
bl_idname = 'arm.open_node_source'
bl_label = 'Open Node Source'
def execute(self, context):
if context.active_node != None and context.active_node.bl_idname.startswith('LN'):
name = context.active_node.bl_idname[2:]
webbrowser.open('https://github.com/armory3d/armory/tree/master/Sources/armory/logicnode/' + name + '.hx')
return{'FINISHED'}
2019-04-29 13:14:32 +02:00
# node replacement code
replacements = {}
def add_replacement(item):
replacements[item.from_node] = item
def get_replaced_nodes():
return replacements.keys()
def get_replacement_for_node(node):
return replacements[node.bl_idname]
class Replacement:
# represents a single replacement rule, this can replace exactly one node with another
#
# from_node: the node type to be removed
# to_node: the node type which takes from_node's place
# *SocketMapping: a map which defines how the sockets of the old node shall be connected to the new node
# {1: 2} means that anything connected to the socket with index 1 on the original node will be connected to the socket with index 2 on the new node
def __init__(self, from_node, to_node, in_socket_mapping, out_socket_mapping, property_mapping):
2019-04-29 13:14:32 +02:00
self.from_node = from_node
self.to_node = to_node
self.in_socket_mapping = in_socket_mapping
self.out_socket_mapping = out_socket_mapping
self.property_mapping = property_mapping
2019-04-29 13:14:32 +02:00
# actual replacement code
def replace(tree, node):
replacement = get_replacement_for_node(node)
newnode = tree.nodes.new(replacement.to_node)
newnode.location = node.location
newnode.parent = node.parent
parent = node.parent
while parent is not None:
newnode.location[0] += parent.location[0]
newnode.location[1] += parent.location[1]
parent = parent.parent
# map properties
for prop in replacement.property_mapping.keys():
setattr(newnode, replacement.property_mapping.get(prop), getattr(node, prop))
# map unconnected inputs
for in_socket in replacement.in_socket_mapping.keys():
if not node.inputs[in_socket].is_linked:
newnode.inputs[replacement.in_socket_mapping.get(in_socket)].default_value = node.inputs[in_socket].default_value
# map connected inputs
2019-04-29 13:14:32 +02:00
for link in tree.links:
if link.from_node == node:
# this is an output link
for i in range(0, len(node.outputs)):
# check the outputs
# i represents the socket index
# do we want to remap it & is it the one referenced in the current link
if i in replacement.out_socket_mapping.keys() and node.outputs[i] == link.from_socket:
tree.links.new(newnode.outputs[replacement.out_socket_mapping.get(i)], link.to_socket)
if link.to_node == node:
# this is an input link
for i in range(0, len(node.inputs)):
# check the inputs
# i represents the socket index
# do we want to remap it & is it the one referenced socket in the current link
if i in replacement.in_socket_mapping.keys() and node.inputs[i] == link.to_socket:
tree.links.new(newnode.inputs[replacement.in_socket_mapping.get(i)], link.from_socket)
tree.nodes.remove(node)
def replaceAll():
for tree in bpy.data.node_groups:
2019-05-01 11:04:15 +02:00
if tree.bl_idname == "ArmLogicTreeType":
for node in tree.nodes:
if node.bl_idname in get_replaced_nodes():
print("Replacing "+ node.bl_idname+ " in Tree "+tree.name)
replace(tree, node)
2019-04-29 13:14:32 +02:00
class ReplaceNodesOperator(bpy.types.Operator):
'''Automatically replaces deprecated nodes.'''
bl_idname = "node.replace"
bl_label = "Replace Nodes"
def execute(self, context):
replaceAll()
2019-04-29 13:14:32 +02:00
return {'FINISHED'}
@classmethod
def poll(cls, context):
return context.space_data != None and context.space_data.type == 'NODE_EDITOR'
2019-05-01 11:04:15 +02:00
# TODO: deprecated
# Input Replacement Rules
2019-09-06 18:48:21 +02:00
# add_replacement(Replacement("LNOnGamepadNode", "LNMergedGamepadNode", {0: 0}, {0: 0}, {"property0": "property0", "property1": "property1"}))
2015-10-30 13:23:09 +01:00
def register():
2017-03-15 12:30:14 +01:00
bpy.utils.register_class(ArmLogicTree)
2019-04-29 13:14:32 +02:00
bpy.utils.register_class(ARM_PT_LogicNodePanel)
2017-04-10 21:17:17 +02:00
bpy.utils.register_class(ArmOpenNodeSource)
2019-04-29 13:14:32 +02:00
bpy.utils.register_class(ReplaceNodesOperator)
2017-03-06 02:29:03 +01:00
register_nodes()
2015-10-30 13:23:09 +01:00
def unregister():
2019-04-29 13:14:32 +02:00
bpy.utils.unregister_class(ReplaceNodesOperator)
2017-03-06 02:29:03 +01:00
unregister_nodes()
2017-03-15 12:30:14 +01:00
bpy.utils.unregister_class(ArmLogicTree)
2019-04-29 13:14:32 +02:00
bpy.utils.unregister_class(ARM_PT_LogicNodePanel)
2017-04-10 21:17:17 +02:00
bpy.utils.unregister_class(ArmOpenNodeSource)