Skip to content
This repository has been archived by the owner on Sep 20, 2024. It is now read-only.

Commit

Permalink
Chore: Create plugin auto-apply settings (#5908)
Browse files Browse the repository at this point in the history
* 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>
  • Loading branch information
iLLiCiTiT and BigRoy authored Nov 14, 2023
1 parent 05bbff0 commit b382b7c
Show file tree
Hide file tree
Showing 2 changed files with 89 additions and 20 deletions.
24 changes: 5 additions & 19 deletions openpype/hosts/maya/api/plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -271,7 +271,7 @@ def _default_remove_instances(self, instances):
@six.add_metaclass(ABCMeta)
class MayaCreator(NewCreator, MayaCreatorBase):

settings_name = None
settings_category = "maya"

def create(self, subset_name, instance_data, pre_create_data):

Expand Down Expand Up @@ -317,24 +317,6 @@ def get_pre_create_attr_defs(self):
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.
Expand All @@ -343,6 +325,8 @@ class MayaAutoCreator(AutoCreator, MayaCreatorBase):
any arguments.
"""

settings_category = "maya"

def collect_instances(self):
return self._default_collect_instances()

Expand All @@ -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)

Expand Down
85 changes: 84 additions & 1 deletion openpype/pipeline/create/creator_plugins.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
# -*- coding: utf-8 -*-
import copy
import collections

Expand Down Expand Up @@ -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
):
Expand Down Expand Up @@ -233,14 +240,90 @@ def __init__(
" 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):
Expand Down

0 comments on commit b382b7c

Please sign in to comment.