code cleanup: lazy init enum for node search

This commit is contained in:
Campbell Barton 2012-08-08 17:02:14 +00:00
parent c21bf16c46
commit de131177b0

@ -20,11 +20,7 @@
import bpy
from bpy.types import Operator
from bpy.props import (EnumProperty,
FloatVectorProperty,
StringProperty,
CollectionProperty
)
from bpy.props import EnumProperty
# XXX These node item lists should actually be generated by a callback at operator execution time (see node_type_items below),
# using the active node tree from the context. Due to a difficult bug in bpy this is not possible (item list memory gets freed too early),
@ -33,13 +29,11 @@ from bpy.props import (EnumProperty,
# In the custom_nodes branch, the static per-tree-type node items are replaced by a single independent type list anyway (with a poll function
# to limit node types to the respective trees). So this workaround is only temporary.
# lazy init
node_type_items_dict = {}
node_type_items_dict['SHADER'] = [(item.identifier, item.name, item.description, item.value) for item in bpy.types.ShaderNode.bl_rna.properties['type'].enum_items]
node_type_items_dict['COMPOSITING'] = [(item.identifier, item.name, item.description, item.value) for item in bpy.types.CompositorNode.bl_rna.properties['type'].enum_items]
node_type_items_dict['TEXTURE'] = [(item.identifier, item.name, item.description, item.value) for item in bpy.types.TextureNode.bl_rna.properties['type'].enum_items]
# Returns the enum item list for the edited tree in the context
def node_type_items(self, context):
def node_type_items_cb(self, context):
snode = context.space_data
if not snode:
return []
@ -47,6 +41,16 @@ def node_type_items(self, context):
if not tree:
return []
if not node_type_items_dict:
node_type_items_dict.update({
'SHADER': [(item.identifier, item.name, item.description, item.value)
for item in bpy.types.ShaderNode.bl_rna.properties['type'].enum_items],
'COMPOSITING': [(item.identifier, item.name, item.description, item.value)
for item in bpy.types.CompositorNode.bl_rna.properties['type'].enum_items],
'TEXTURE': [(item.identifier, item.name, item.description, item.value)
for item in bpy.types.TextureNode.bl_rna.properties['type'].enum_items],
})
# XXX Does not work correctly, see comment above
#return [(item.identifier, item.name, item.description, item.value) for item in tree.nodes.bl_rna.functions['new'].parameters['type'].enum_items]
@ -55,14 +59,21 @@ def node_type_items(self, context):
else:
return []
class NODE_OT_add_search(bpy.types.Operator):
class NODE_OT_add_search(Operator):
'''Add a node to the active tree'''
bl_idname = "node.add_search"
bl_label = "Search and Add Node"
bl_options = {'REGISTER', 'UNDO'}
# XXX this should be called 'node_type' but the operator search property is hardcoded to 'type' by a hack in bpy_operator_wrap.c ...
type = EnumProperty(items=node_type_items, name="Node Type", description="Node type")
type = EnumProperty(
name="Node Type",
description="Node type",
items=node_type_items_cb,
)
_node_type_items_dict = None
def create_node(self, context):
space = context.space_data
@ -70,7 +81,7 @@ class NODE_OT_add_search(bpy.types.Operator):
node = tree.nodes.new(type=self.type)
for n in tree.nodes:
if n==node:
if n == node:
node.select = True
tree.nodes.active = node
else:
@ -97,4 +108,3 @@ class NODE_OT_add_search(bpy.types.Operator):
context.window_manager.invoke_search_popup(self)
return {'CANCELLED'}