Improved pyside_cache
decorator
This one should work properly when multiple properties use the same callback function.
This commit is contained in:
@@ -45,29 +45,36 @@ def redraw(self, context):
|
||||
context.area.tag_redraw()
|
||||
|
||||
|
||||
def pyside_cache(wrapped):
|
||||
"""Stores the result of the callable in Python-managed memory.
|
||||
def pyside_cache(propname):
|
||||
|
||||
This is to work around the warning at
|
||||
https://www.blender.org/api/blender_python_api_master/bpy.props.html#bpy.props.EnumProperty
|
||||
"""
|
||||
if callable(propname):
|
||||
raise TypeError('Usage: pyside_cache("property_name")')
|
||||
|
||||
import functools
|
||||
def decorator(wrapped):
|
||||
"""Stores the result of the callable in Python-managed memory.
|
||||
|
||||
@functools.wraps(wrapped)
|
||||
# We can't use (*args, **kwargs), because EnumProperty explicitly checks
|
||||
# for the number of fixed positional arguments.
|
||||
def wrapper(self, context):
|
||||
result = None
|
||||
try:
|
||||
result = wrapped(self, context)
|
||||
return result
|
||||
finally:
|
||||
wrapped._cached_result = result
|
||||
return wrapper
|
||||
This is to work around the warning at
|
||||
https://www.blender.org/api/blender_python_api_master/bpy.props.html#bpy.props.EnumProperty
|
||||
"""
|
||||
|
||||
import functools
|
||||
|
||||
@functools.wraps(wrapped)
|
||||
# We can't use (*args, **kwargs), because EnumProperty explicitly checks
|
||||
# for the number of fixed positional arguments.
|
||||
def wrapper(self, context):
|
||||
result = None
|
||||
try:
|
||||
result = wrapped(self, context)
|
||||
return result
|
||||
finally:
|
||||
rna_type, rna_info = getattr(self.bl_rna, propname)
|
||||
rna_info['_cached_result'] = result
|
||||
return wrapper
|
||||
return decorator
|
||||
|
||||
|
||||
@pyside_cache
|
||||
@pyside_cache('version')
|
||||
def blender_syncable_versions(self, context):
|
||||
"""Returns the list of items used by SyncStatusProperties.version EnumProperty."""
|
||||
|
||||
@@ -130,7 +137,7 @@ class SyncStatusProperties(PropertyGroup):
|
||||
self['available_blender_versions'] = new_versions
|
||||
|
||||
|
||||
@pyside_cache
|
||||
@pyside_cache('project')
|
||||
def bcloud_available_projects(self, context):
|
||||
"""Returns the list of items used by BlenderCloudProjectGroup.project EnumProperty."""
|
||||
|
||||
|
Reference in New Issue
Block a user