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