Skip to content

Commit f27e77e

Browse files
authored
Merge pull request #149 from drichardson/EnumProperty_fix
Workaround for known bug in bpy.props.EnumProperty
2 parents e453c2f + 8d7384b commit f27e77e

File tree

2 files changed

+72
-5
lines changed

2 files changed

+72
-5
lines changed

ue2rigify/addon/functions/templates.py

+68
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,15 @@
66
import json
77
import shutil
88

9+
from . import scene
910
from . import utilities
1011
from ..settings.tool_tips import *
1112

13+
_result_reference_get_starter_metarig_templates = []
14+
_result_reference_populate_templates_dropdown = []
15+
_result_reference_get_modes = []
16+
_result_reference_get_rig_templates = []
17+
1218

1319
# -------------- functions that handle the rig templating --------------
1420
def get_rig_templates_path():
@@ -339,3 +345,65 @@ def export_zip(zip_file_path, properties):
339345
# zip up the folder and save it to the given path
340346
template_folder_path = os.path.join(properties.rig_templates_path, properties.selected_export_template)
341347
shutil.make_archive(no_extension_file_path, 'zip', template_folder_path)
348+
349+
#
350+
# Dynamic EnumProperty item list workaround:
351+
# https://docs.blender.org/api/current/bpy.props.html?highlight=bpy%20props%20enumproperty#bpy.props.EnumProperty
352+
#
353+
# There is a known bug with using a callback, Python must keep a reference to
354+
# the strings returned by the callback or Blender will misbehave or even crash.
355+
# For more information, see:
356+
#
357+
358+
def safe_get_starter_metarig_templates(self, context):
359+
"""
360+
This function is an EnumProperty safe wrapper for get_starter_metarig_templates.
361+
362+
:param object self: This is a reference to the class this functions in appended to.
363+
:param object context: The context of the object this function is appended to.
364+
:return list: Result of get_starter_metarig_templates.
365+
"""
366+
items = get_starter_metarig_templates()
367+
global _result_reference_get_starter_metarig_templates
368+
_result_reference_get_starter_metarig_templates = items
369+
return items
370+
371+
def safe_populate_templates_dropdown(self, context):
372+
"""
373+
This function is an EnumProperty safe wrapper for populate_templates_dropdown.
374+
375+
:param object self: This is a reference to the class this functions in appended to.
376+
:param object context: The context of the object this function is appended to.
377+
:return list: Result of populate_templates_dropdown.
378+
"""
379+
items = populate_templates_dropdown()
380+
global _result_reference_populate_templates_dropdown
381+
_result_reference_populate_templates_dropdown = items
382+
return items
383+
384+
385+
def safe_get_modes(self, context):
386+
"""
387+
This function is an EnumProperty safe wrapper for scene.get_modes.
388+
389+
:param object self: This is a reference to the class this functions in appended to.
390+
:param object context: The context of the object this function is appended to.
391+
:return list: Result of scene.get_modes.
392+
"""
393+
items = scene.get_modes()
394+
global _result_reference_get_modes
395+
_result_reference_get_modes = items
396+
return items
397+
398+
def safe_get_rig_templates(self, context):
399+
"""
400+
This function is an EnumProperty safe wrapper for get_rig_templates.
401+
402+
:param object self: This is a reference to the class this functions in appended to.
403+
:param object context: The context of the object this function is appended to.
404+
:return list: Result of get_rig_templates.
405+
"""
406+
items = get_rig_templates()
407+
global _result_reference_get_rig_templates
408+
_result_reference_get_rig_templates = items
409+
return items

ue2rigify/addon/properties.py

+4-5
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
from .functions import utilities
77
from .settings import tool_tips
88

9-
109
class UE2RigifyProperties(bpy.types.PropertyGroup):
1110
"""
1211
This class defines a property group that can be accessed through the blender api.
@@ -83,22 +82,22 @@ class UE2RigifyProperties(bpy.types.PropertyGroup):
8382
selected_starter_metarig_template: bpy.props.EnumProperty(
8483
name="Metarig",
8584
description=tool_tips.starter_metarig_template_tool_tip,
86-
items=templates.get_starter_metarig_templates,
85+
items=templates.safe_get_starter_metarig_templates,
8786
update=scene.set_meta_rig
8887
)
8988

9089
selected_rig_template: bpy.props.EnumProperty(
9190
name="Rig Template",
9291
description=tool_tips.rig_template_tool_tip,
93-
items=templates.populate_templates_dropdown,
92+
items=templates.safe_populate_templates_dropdown,
9493
options={'ANIMATABLE'},
9594
update=templates.set_template
9695
)
9796

9897
selected_mode: bpy.props.EnumProperty(
9998
name="Modes",
10099
description=tool_tips.mode_tool_tip,
101-
items=scene.get_modes,
100+
items=templates.safe_get_modes,
102101
options={'ANIMATABLE'},
103102
update=scene.switch_modes
104103
)
@@ -107,7 +106,7 @@ class UE2RigifyProperties(bpy.types.PropertyGroup):
107106
selected_export_template: bpy.props.EnumProperty(
108107
name="Export Rig Template",
109108
description=tool_tips.export_template_tool_tip,
110-
items=templates.get_rig_templates,
109+
items=templates.safe_get_rig_templates,
111110
options={'ANIMATABLE'}
112111
)
113112

0 commit comments

Comments
 (0)