Feature/factory refacto #44
Open
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.
Hello GarmentCode team, @maria-korosteleva
I discovered your awesome project, I tested it for my personal project and experienced some inconvenience about adding new types of clothes, which are mainly implemented in
àssets.garment_programs. In fact, adding a new cloth obliges me to directly modify the code source of the GarmentCode project.Then I would like to contribute a little to your project GarmentCode, to make it modular and easily extensible as a python library via factory creational pattern.
Introduction of PR
This Pull Request aims to refactorize the module
assets.garment_programsin order to make it extensible and modular.I suggest using a factory creational pattern to create garment modules. The files in
assets.garment_programsare split into 5 main modules:Details
Here is the code structure after modification.
To make use of the factory creational pattern, I implement 3 main points:
A
Registry utilityin thepygarment/registry.py(a utility registry in a Tensorflow Google project), this file has 2 important functions:Restructure the module
assets.garment_programsinto modules and sub-modules. For example, in thesleevemodule, there is anarmhole shapeas its submodule. For more details, please consult the above folder structure.In each module and submodule, implement
factory.pywith 2 global variables and 4 utility functions:_REGISTERED_<CMP>_CLS = {}: Registry (e.g. Python dictionary) to register component classes_REGISTERED_<CMP>_CFG = {}(placeholder for later update on configuration): Registry (e.g. Python dictionary) to register config for component classesregister_builder()to register classes/ build functionsbuild()to lookup into registry by name and pass input argumentsregister_config()to register config for classes/ build functions (placeholder only)get_config()to get default config from config registryIn this PR, I also make a change of importing modules in
meta_garment.pyaccording to the new module structures, and the approach of calling class fromglobal()["<class_name>"](**args)tofactory.build(name=<class_name>, **args).How to add new classes
It's very simple! Just place
@factory.register_builder("<name_of_your_new_class>")ahead a new class. For instance, I just need to add a decorator function register_builder ahead of class ``Sleeve`. Remember for the shake of simplicity (and as the intended class calling way in GarmentCode), I use the identical string name as class.For instance, in my project, I've just created a new custom class, called
MyNewSleeve, put it inassets/garment_programs/sleeves/my_sleeves.py.This will add into
_REGISTERED_SLEEVES_CLSan element with "MyNewSleeve" as key and classMyNewSleeveas value, while importing the modulesleevesRegisters will be updated via the use of decorator
@factory.register_builder(), whenever we import modules.Explanation of importing python module mechanism: As we import a module (via import details can be declared in
__init__.pyin the folder of module), python will import also its submodules and sub-submodule recursively. Therefore, within this module, every class headed with@factory.register_builder()will be registered/stored in registries_REGISTERED_<CMP>_CLSas global variables.After importing modules we need, these registries are accessible indirectly, by convention, through a factory (
factory.py) in their corresponding module.A good practice to import and build class is for example,
For more examples, see the change in importing in
assets/garment_programs/meta_garment.pyI welcome any suggestion to make it more user-friendly, please directly reply in the comment section.
If you would like me to change the PR, please let me know. I would love to contribute to your project.