Chore: Create plugin auto-apply settings (#5908)

* implemented default logic 'apply_settings' to auto-apply values

* added docstring to 'apply_settings'

* replace auto apply settings logic in maya

* add missing quote

Co-authored-by: Roy Nieterau <roy_nieterau@hotmail.com>

---------

Co-authored-by: Roy Nieterau <roy_nieterau@hotmail.com>
This commit is contained in:
Jakub Trllo 2023-11-14 14:57:53 +01:00 committed by GitHub
parent 05bbff0790
commit b382b7c776
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 89 additions and 20 deletions

View file

@ -271,7 +271,7 @@ class MayaCreatorBase(object):
@six.add_metaclass(ABCMeta)
class MayaCreator(NewCreator, MayaCreatorBase):
settings_name = None
settings_category = "maya"
def create(self, subset_name, instance_data, pre_create_data):
@ -317,24 +317,6 @@ class MayaCreator(NewCreator, MayaCreatorBase):
default=True)
]
def apply_settings(self, project_settings):
"""Method called on initialization of plugin to apply settings."""
settings_name = self.settings_name
if settings_name is None:
settings_name = self.__class__.__name__
settings = project_settings["maya"]["create"]
settings = settings.get(settings_name)
if settings is None:
self.log.debug(
"No settings found for {}".format(self.__class__.__name__)
)
return
for key, value in settings.items():
setattr(self, key, value)
class MayaAutoCreator(AutoCreator, MayaCreatorBase):
"""Automatically triggered creator for Maya.
@ -343,6 +325,8 @@ class MayaAutoCreator(AutoCreator, MayaCreatorBase):
any arguments.
"""
settings_category = "maya"
def collect_instances(self):
return self._default_collect_instances()
@ -360,6 +344,8 @@ class MayaHiddenCreator(HiddenCreator, MayaCreatorBase):
arguments for 'create' method.
"""
settings_category = "maya"
def create(self, *args, **kwargs):
return MayaCreator.create(self, *args, **kwargs)

View file

@ -1,3 +1,4 @@
# -*- coding: utf-8 -*-
import copy
import collections
@ -193,6 +194,12 @@ class BaseCreator:
# QUESTION make this required?
host_name = None
# Settings auto-apply helpers
# Root key in project settings (mandatory for auto-apply to work)
settings_category = None
# Name of plugin in create settings > class name is used if not set
settings_name = None
def __init__(
self, project_settings, system_settings, create_context, headless=False
):
@ -233,14 +240,90 @@ class BaseCreator:
" need to keep system settings."
).format(self.__class__.__name__))
@staticmethod
def _get_settings_values(project_settings, category_name, plugin_name):
"""Helper method to get settings values.
Args:
project_settings (dict[str, Any]): Project settings.
category_name (str): Category of settings.
plugin_name (str): Name of settings.
Returns:
Union[dict[str, Any], None]: Settings values or None.
"""
settings = project_settings.get(category_name)
if not settings:
return None
create_settings = settings.get("create")
if not create_settings:
return None
return create_settings.get(plugin_name)
def apply_settings(self, project_settings):
"""Method called on initialization of plugin to apply settings.
Default implementation tries to auto-apply settings values if are
in expected hierarchy.
Data hierarchy to auto-apply settings:
{self.settings_category} - Root key in settings
"create" - Hardcoded key
{self.settings_name} | {class name} - Name of plugin
... attribute values... - Attribute/value pair
It is mandatory to define 'settings_category' attribute. Attribute
'settings_name' is optional and class name is used if is not defined.
Example data:
ProjectSettings {
"maya": { # self.settings_category
"create": { # Hardcoded key
"CreateAnimation": { # self.settings_name / class name
"enabled": True, # --- Attributes to set ---
"optional": True,#
"active": True, #
"fps": 25, # -------------------------
},
...
},
...
},
...
}
Args:
project_settings (dict[str, Any]): Project settings.
"""
pass
settings_category = self.settings_category
if not settings_category:
return
cls_name = self.__class__.__name__
settings_name = self.settings_name or cls_name
settings = self._get_settings_values(
project_settings, settings_category, settings_name
)
if settings is None:
self.log.debug("No settings found for {}".format(cls_name))
return
for key, value in settings.items():
# Log out attributes that are not defined on plugin object
# - those may be potential dangerous typos in settings
if not hasattr(self, key):
self.log.debug((
"Applying settings to unknown attribute '{}' on '{}'."
).format(
key, cls_name
))
setattr(self, key, value)
@property
def identifier(self):