A bit more pythonic way of using the items callback in node categories. The category.items attribute is now a function taking a context parameter instead of a property. This can be used for checking validity, e.g. for doing node group recursion checks, and filter out unusable items.
This commit is contained in:
@@ -26,21 +26,17 @@ class NodeCategory():
|
||||
def poll(cls, context):
|
||||
return True
|
||||
|
||||
@property
|
||||
def items(self):
|
||||
if hasattr(self, '_items'):
|
||||
return self._items
|
||||
elif hasattr(self, '_itemfunc'):
|
||||
return self._itemfunc(self)
|
||||
|
||||
def __init__(self, identifier, name, description="", items=[]):
|
||||
def __init__(self, identifier, name, description="", items=None):
|
||||
self.identifier = identifier
|
||||
self.name = name
|
||||
self.description = description
|
||||
if callable(items):
|
||||
self._itemfunc = items
|
||||
|
||||
if items is None:
|
||||
self.items = lambda context: []
|
||||
elif callable(items):
|
||||
self.items = items
|
||||
else:
|
||||
self._items = items
|
||||
self.items = lambda context: items
|
||||
|
||||
class NodeItem():
|
||||
def __init__(self, nodetype, label=None, settings={}):
|
||||
@@ -69,7 +65,7 @@ def register_node_categories(identifier, cat_list):
|
||||
layout = self.layout
|
||||
col = layout.column()
|
||||
default_context = bpy.app.translations.contexts.default
|
||||
for item in self.category.items:
|
||||
for item in self.category.items(context):
|
||||
op = col.operator("node.add_node", text=item.label, text_ctxt=default_context)
|
||||
op.type = item.nodetype
|
||||
op.use_transform = True
|
||||
|
||||
Reference in New Issue
Block a user