-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Attribute converter nodes #3161
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
Alxiice
wants to merge
14
commits into
develop
Choose a base branch
from
feat/attribute_converter_nodes
base: develop
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
46ca813
Add attribute converter base class
Alxiice cb28554
Add base convert nodes
Alxiice f32d807
Add AttributeConverterList registry and impement it inside the plugin…
Alxiice 417ae34
fix typo in meshroom_info
Alxiice 5123337
Move attribute converter to core instead of desc
Alxiice bd10604
Authorize edges on incompatible connections as long as we find an Att…
Alxiice 0d43047
UI: display attribute converter on the edge
Alxiice ac080f5
Add context menu to select the converter
Alxiice 8a28246
Attribute Converter : fix ChoiceParam values issue and remove the isV…
Alxiice 79a3390
Attribute Converter : handle deserialization of converters
Alxiice 38c482d
Attribute Converter: fix issues raised in review
Alxiice adb4e98
Remove attributeConverter from desc
Alxiice 7e2f548
Fix convert attr on list attributes
Alxiice c78b245
test_submit: fix rez issues
Alxiice File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,102 @@ | ||
| """ | ||
| attributeConverter: base descriptors class for AttributeConverter nodes | ||
| """ | ||
|
|
||
| import logging | ||
| from abc import ABC, abstractmethod | ||
| from typing import TYPE_CHECKING, ClassVar | ||
| from collections import defaultdict | ||
| from itertools import chain | ||
|
|
||
| if TYPE_CHECKING: | ||
| from meshroom.core.desc.attribute import Attribute | ||
|
|
||
|
|
||
| class AttributeConverter(ABC): | ||
| """ | ||
| Base class for converting the value of a source Attribute | ||
| into a value for a destination Attribute of a different type, | ||
| so a connection can be made between them. | ||
| """ | ||
|
|
||
| name: ClassVar[str] = "" | ||
| description: ClassVar[str] = "" | ||
| priority: ClassVar[int] = 10 # Put a higher number to prioritize specific converters | ||
|
|
||
| # Input / Output classes | ||
| srcType: ClassVar["Attribute"] = None | ||
| dstType: ClassVar["Attribute"] = None | ||
|
|
||
| def __init__(self): | ||
| if not all ((self.srcType, self.dstType)): | ||
| raise TypeError( | ||
| f"Class '{self.__class__.__name__}' must define srcType and dstType." | ||
| ) | ||
|
|
||
| @classmethod | ||
| def getName(cls): | ||
| return cls.name or cls.__name__ | ||
|
|
||
| def canConvert(self, srcType, dstType): | ||
| """ Check if this converter corresponds to a source/destination attribute pair. | ||
| """ | ||
| return isinstance(srcType, self.srcType) and isinstance(dstType, self.dstType) | ||
|
|
||
| @abstractmethod | ||
| def convert(self, value): | ||
| """ Convert a value from the source attribute's type to a value for | ||
| the destination attribute's type. | ||
| """ | ||
| return value | ||
|
|
||
| def __repr__(self): | ||
| return f"<AttributeConverter {self.getName()} ({self.srcType.__name__} -> {self.dstType.__name__})>" | ||
|
|
||
|
|
||
| class AttributeConverterRegistry: | ||
| """ | ||
| Registry of available converters | ||
| """ | ||
|
|
||
| # { (srcType, dstType): [converters] } | ||
| _converters: dict[tuple["Attribute", "Attribute"], list[AttributeConverter]] = defaultdict(list) | ||
|
|
||
| @classmethod | ||
| def add(cls, converter: AttributeConverter): | ||
| if not issubclass(converter.__class__, AttributeConverter): | ||
| raise TypeError(f"{converter} parent class must subclass AttributeConverter") | ||
| logging.info( | ||
| f"Add converter class: {converter.getName()} " | ||
| f"({converter.srcType.__name__} -> {converter.dstType.__name__})" | ||
| ) | ||
| cls._converters[(converter.srcType.__name__, converter.dstType.__name__)].append(converter) | ||
|
|
||
| @classmethod | ||
| def getAllConverters(cls) -> list[AttributeConverter]: | ||
| return list(chain.from_iterable(cls._converters.values())) | ||
|
|
||
| @classmethod | ||
| def getConverterByName(cls, name): | ||
| for c in cls.getAllConverters(): | ||
| if c.getName() == name: | ||
| return c | ||
| return None | ||
|
|
||
| @classmethod | ||
| def hasConverter(cls, srcType: "Attribute", dstType: "Attribute") -> list[AttributeConverter]: | ||
| return ((srcType, dstType)) in cls._converters | ||
|
|
||
| @classmethod | ||
| def getConverters(cls, srcType: "Attribute", dstType: "Attribute") -> list[AttributeConverter]: | ||
| """ Get priority-ordered converters. | ||
| """ | ||
| converters = cls._converters.get((srcType, dstType), []) | ||
| return sorted(converters, key=lambda c: -c.priority) | ||
|
|
||
| @classmethod | ||
| def getConverter(cls, srcType: "Attribute", dstType: "Attribute") -> AttributeConverter: | ||
| """ Get highest priority converter. """ | ||
| converters = cls.getConverters(srcType, dstType) | ||
| if not converters: | ||
| return None | ||
| return converters[0] | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.