prop_factory enum property tested/working.

This commit is contained in:
filmfreedom-org 2021-05-29 01:44:21 -05:00
parent 32bb691bc4
commit 361de218ca
2 changed files with 57 additions and 39 deletions

View File

@ -21,6 +21,16 @@ import yaml
def EnumFromList(schema, listname): def EnumFromList(schema, listname):
return [(e, e.capitalize(), e.capitalize()) for e in schema[listname]] return [(e, e.capitalize(), e.capitalize()) for e in schema[listname]]
def ExpandEnumList(schema, options):
blender_options = []
for option in options:
if type(option) is str:
blender_options.append((option, option, option))
elif isinstance(option, tuple) or isinstance(option, list):
option = tuple(option[0:3] + ([option[-1]]*(3-len(option))))
blender_options.append(option)
return blender_options
class PropertyGroupFactory(bpy.types.PropertyGroup): class PropertyGroupFactory(bpy.types.PropertyGroup):
""" """
Metadata property group factory for attachment to Blender object types. Metadata property group factory for attachment to Blender object types.
@ -33,31 +43,32 @@ class PropertyGroupFactory(bpy.types.PropertyGroup):
'keywords': { 'name', 'description', 'default', 'maxlen', 'keywords': { 'name', 'description', 'default', 'maxlen',
'options', 'subtype'}, 'options', 'subtype'},
'translate': { 'translate': {
'desc': ('description', None)}}, 'desc': (None, 'description', None)}},
'enum': { 'enum': {
'property': bpy.props.EnumProperty, 'property': bpy.props.EnumProperty,
'keywords': { 'items', 'name', 'description', 'default', 'options'}, 'keywords': { 'items', 'name', 'description', 'default', 'options'},
'translate': { 'translate': {
'desc': ('description', None), 'desc': (None, 'description', None),
'items_from': ('items', EnumFromList)}}, 'items_from': (EnumFromList, 'items'),
'items': (ExpandEnumList, 'items')}},
'int': { 'int': {
'property': bpy.props.IntProperty, 'property': bpy.props.IntProperty,
'keywords': { 'name', 'description', 'default', 'min', 'max', 'keywords': { 'name', 'description', 'default', 'min', 'max',
'soft_min', 'soft_max', 'step', 'options', 'subtype'}, 'soft_min', 'soft_max', 'step', 'options', 'subtype'},
'translate': { 'translate': {
'desc': ('description', None)}}, 'desc': (None, 'description')}},
'float': { 'float': {
'property': bpy.props.FloatProperty, 'property': bpy.props.FloatProperty,
'keywords': { 'name', 'description', 'default', 'min', 'max', 'keywords': { 'name', 'description', 'default', 'min', 'max',
'soft_min', 'soft_max', 'step', 'options', 'soft_min', 'soft_max', 'step', 'options',
'subtype', 'precision', 'unit'}, 'subtype', 'precision', 'unit'},
'translate': { 'translate': {
'desc': ('description', None)}}, 'desc': (None, 'description')}},
'bool': { 'bool': {
'property': bpy.props.BoolProperty, 'property': bpy.props.BoolProperty,
'keywords': { 'name', 'description', 'default', 'options', 'subtype'}, 'keywords': { 'name', 'description', 'default', 'options', 'subtype'},
'translate': { 'translate': {
'desc': ('description', None)}} 'desc': (None, 'description')}}
} }
def __new__(cls, name, schema): def __new__(cls, name, schema):
@ -74,13 +85,13 @@ class PropertyGroupFactory(bpy.types.PropertyGroup):
filtered = {} filtered = {}
for param in definition: for param in definition:
if 'translate' in propmap and param in propmap['translate']: if 'translate' in propmap and param in propmap['translate']:
translator = propmap['translate'][param][1] translator = propmap['translate'][param][0]
if callable(translator): if callable(translator):
# Filtered translation # Filtered translation
filtered[propmap['translate'][param][0]] = translator(schema, definition[param]) filtered[propmap['translate'][param][1]] = translator(schema, definition[param])
else: else:
# Simple translation # Simple translation
filtered[propmap['translate'][param][0]] = definition[param] filtered[propmap['translate'][param][1]] = definition[param]
else: else:
filtered[param] = definition[param] filtered[param] = definition[param]

View File

@ -75,6 +75,25 @@ class TestPropertyGroupFactory(unittest.TestCase):
default: 'unknown' default: 'unknown'
desc: An enumerated property using a list reference. desc: An enumerated property using a list reference.
- code: enum_prop2
name: MyEnum2
type: enum
items:
- op1
- op2
- op3
default: op1
desc: An enumerated property using a direct list of strings.
- code: enum_prop3
name: MyEnum3
items:
- op1
- ('op2', 'Option2')
- ('op3', 'Option3', 'Option Three')
default: op1
desc: An enumerated property using direct list with mixed types.
enum1_list: enum1_list:
- unknown - unknown
- option1 - option1
@ -103,47 +122,35 @@ class TestPropertyGroupFactory(unittest.TestCase):
bpy.context.scene.my_custom_property_group.prop1, bpy.context.scene.my_custom_property_group.prop1,
'default') 'default')
def test_creating_complex_sampler(self): def test_creating_and_attaching_complex_sampler(self):
schema = yaml.safe_load(io.StringIO(self.SAMPLER)) schema = yaml.safe_load(io.StringIO(self.SAMPLER))
sampler = prop_factory.PropertyGroupFactory( sampler = prop_factory.PropertyGroupFactory(
'sampler', schema) 'sampler', schema)
bpy.types.Scene.sampler = bpy.props.PointerProperty(type=sampler) bpy.types.Scene.sampler = bpy.props.PointerProperty(type=sampler)
self.assertEqual( self.assertEqual(bpy.context.scene.sampler.str_prop, 'str_prop')
bpy.context.scene.sampler.str_prop, 'str_prop') self.assertEqual(bpy.context.scene.sampler.int_prop, 1)
self.assertEqual(bpy.context.scene.sampler.float_prop, 1.0)
self.assertEqual( self.assertEqual(bpy.context.scene.sampler.bool_prop, False)
bpy.context.scene.sampler.int_prop, 1) self.assertEqual(bpy.context.scene.sampler.enum_prop1, 'unknown')
self.assertEqual(bpy.context.scene.sampler.enum_prop2, 'op1')
self.assertEqual( self.assertEqual(bpy.context.scene.sampler.enum_prop3, 'op1')
bpy.context.scene.sampler.float_prop, 1.0)
self.assertEqual(
bpy.context.scene.sampler.bool_prop, False)
self.assertEqual(
bpy.context.scene.sampler.enum_prop1, 'unknown')
bpy.context.scene.sampler.str_prop = 'my string' bpy.context.scene.sampler.str_prop = 'my string'
bpy.context.scene.sampler.int_prop = 2 bpy.context.scene.sampler.int_prop = 2
bpy.context.scene.sampler.float_prop = 0.7 bpy.context.scene.sampler.float_prop = 0.7
bpy.context.scene.sampler.bool_prop = True bpy.context.scene.sampler.bool_prop = True
bpy.context.scene.sampler.enum_prop1 = 'option1' bpy.context.scene.sampler.enum_prop1 = 'option1'
bpy.context.scene.sampler.enum_prop2 = 'op2'
bpy.context.scene.sampler.enum_prop3 = 'op3'
self.assertEqual( self.assertEqual(bpy.context.scene.sampler.str_prop, 'my string')
bpy.context.scene.sampler.str_prop, 'my string') self.assertEqual(bpy.context.scene.sampler.int_prop, 2)
self.assertAlmostEqual(bpy.context.scene.sampler.float_prop, 0.7)
self.assertEqual( self.assertEqual(bpy.context.scene.sampler.bool_prop, True)
bpy.context.scene.sampler.int_prop, 2) self.assertEqual(bpy.context.scene.sampler.enum_prop1, 'option1')
self.assertEqual(bpy.context.scene.sampler.enum_prop2, 'op2')
self.assertAlmostEqual( self.assertEqual(bpy.context.scene.sampler.enum_prop3, 'op3')
bpy.context.scene.sampler.float_prop, 0.7)
self.assertEqual(
bpy.context.scene.sampler.bool_prop, True)
self.assertEqual(
bpy.context.scene.sampler.enum_prop1, 'option1')