Skip to content
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

Max: Implementation of the validator for model name #59

Merged
merged 26 commits into from
Mar 7, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
26 commits
Select commit Hold shift + click to select a range
45bb933
implement validator for model name in 3dsmax
moonyuet Jan 25, 2024
05e7c66
Merge branch 'develop' into enhancement/OP-7076_Validate-Model-Name
moonyuet Jan 29, 2024
1d83c6a
add validate model name settings for ayon
moonyuet Jan 29, 2024
43aadb2
resolve hound
moonyuet Jan 29, 2024
b822dd0
add docstring
moonyuet Jan 29, 2024
5e8af09
update regex name
moonyuet Jan 29, 2024
0e33857
Merge branch 'develop' into enhancement/OP-7076_Validate-Model-Name
moonyuet Feb 2, 2024
6b9da9b
update docstring
moonyuet Feb 2, 2024
12f78b1
update regex in ayon settings
moonyuet Feb 5, 2024
8ee2d8c
Merge branch 'develop' into enhancement/OP-7076_Validate-Model-Name
moonyuet Feb 5, 2024
09342f5
fix action.py
moonyuet Feb 5, 2024
1a63065
fix action.py
moonyuet Feb 5, 2024
bfd9520
hound
moonyuet Feb 5, 2024
05b9743
fix Libor's mentioned bug on action.py
moonyuet Feb 8, 2024
c9db644
hound shut
moonyuet Feb 8, 2024
19461e5
ported Validate Model Name from OP to ayon_core
moonyuet Feb 14, 2024
4a85b2e
bug fix the action.py
moonyuet Feb 21, 2024
1d07c2f
Merge branch 'develop' into enhancement/OP-7076_Validate-Model-Name
moonyuet Feb 21, 2024
c25161d
coverting asset to folderPath
moonyuet Feb 22, 2024
6f8e797
resolve conflict
moonyuet Feb 23, 2024
e5f5925
Merge branch 'develop' into enhancement/OP-7076_Validate-Model-Name
LiborBatek Feb 26, 2024
ff904e3
resolve conflict
moonyuet Feb 27, 2024
ff05252
Merge branch 'develop' into enhancement/OP-7076_Validate-Model-Name
LiborBatek Feb 28, 2024
fadf820
improve the code and the error message & docstring
moonyuet Feb 28, 2024
91a7c37
resolve conflict
moonyuet Mar 5, 2024
d35d719
Merge branch 'develop' into enhancement/OP-7076_Validate-Model-Name
moonyuet Mar 7, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?xml version="1.0" encoding="UTF-8"?>
<root>
<error id="main">
<title>Invalid Model Name</title>
<description>## Nodes found with Invalid Model Name

Nodes were detected in your scene which have invalid model name which does not
match the regex you preset in AYON setting.
### How to repair?
Make sure the model name aligns with validation regex in your AYON setting.

</description>
<detail>
### Invalid nodes

{nodes}


### How could this happen?

This often happens if you have mesh with the model naming does not match
with regex in the setting.

</detail>
</error>
</root>
120 changes: 120 additions & 0 deletions client/ayon_core/hosts/max/plugins/publish/validate_model_name.py
moonyuet marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
@@ -0,0 +1,120 @@
# -*- coding: utf-8 -*-
"""Validate model nodes names."""
import re

import pyblish.api

from ayon_core.hosts.max.api.action import SelectInvalidAction

from ayon_core.pipeline.publish import (
OptionalPyblishPluginMixin,
PublishXmlValidationError,
ValidateContentsOrder
)

class ValidateModelName(pyblish.api.InstancePlugin,
OptionalPyblishPluginMixin):
"""Validate Model Name.

Validation regex is `(.*)_(?P<subset>.*)_(GEO)` by default.
The setting supports the following regex group name:
- project
- asset
- subset

Examples:
`{SOME_RANDOM_NAME}_{YOUR_SUBSET_NAME}_GEO` should be your
default model name.
The regex of `(?P<subset>.*)` can be replaced by `(?P<asset>.*)`
and `(?P<project>.*)`.
`(.*)_(?P<asset>.*)_(GEO)` check if your model name is
`{SOME_RANDOM_NAME}_{CURRENT_ASSET_NAME}_GEO`
`(.*)_(?P<project>.*)_(GEO)` check if your model name is
`{SOME_RANDOM_NAME}_{CURRENT_PROJECT_NAME}_GEO`

"""
optional = True
order = ValidateContentsOrder
hosts = ["max"]
families = ["model"]
label = "Validate Model Name"
actions = [SelectInvalidAction]
# defined by settings
regex = r"(.*)_(?P<subset>.*)_(GEO)"
# cache
regex_compiled = None

def process(self, instance):
if not self.is_active(instance.data):
return

invalid = self.get_invalid(instance)
if invalid:
names = "\n".join(
"- {}".format(node.name) for node in invalid
)
raise PublishXmlValidationError(
plugin=self,
message="Nodes found with invalid model names: {}".format(invalid),
formatting_data={"nodes": names}
)

@classmethod
def get_invalid(cls, instance):
if not cls.regex:
cls.log.warning("No regex pattern set. Nothing to validate.")
return

members = instance.data.get("members")
if not members:
cls.log.error("No members found in the instance.")
return

cls.regex_compiled = re.compile(cls.regex)

invalid = []
for obj in members:
if cls.invalid_name(instance, obj):
invalid.append(obj)
return invalid

@classmethod
def invalid_name(cls, instance, obj):
"""Function to check the object has invalid name
regarding to the validation regex in the AYON setttings

Args:
instance (pyblish.api.instance): Instance
obj (str): object name

Returns:
str: invalid object
"""
regex = cls.regex_compiled
name = obj.name
match = regex.match(name)

if match is None:
cls.log.error("Invalid model name on: %s", name)
cls.log.error("Name doesn't match regex {}".format(regex.pattern))
return obj

# Validate regex groups
invalid = False
compare = {
"project": instance.context.data["projectName"],
"asset": instance.context.data["folderPath"],
"subset": instance.context.data["subset"],
}
for key, required_value in compare.items():
if key in regex.groupindex:
if match.group(key) != required_value:
cls.log.error(
"Invalid %s name for the model %s, "
"required name is %s",
key, name, required_value
)
invalid = True

if invalid:
return obj
24 changes: 24 additions & 0 deletions server_addon/max/server/settings/publishers.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,20 @@ class FamilyMappingItemModel(BaseSettingsModel):
)


class ValidateModelNameModel(BaseSettingsModel):
enabled: bool = SettingsField(title="Enabled")
optional: bool = SettingsField(title="Optional")
active: bool = SettingsField(title="Active")
regex: str = SettingsField(
"(.*)_(?P<subset>.*)_(GEO)",
title="Validation regex",
description=(
"Regex for validating model name. You can use named "
" capturing groups:(?P<asset>.*) for Asset name"
)
)


class ValidateLoadedPluginModel(BaseSettingsModel):
enabled: bool = SettingsField(title="Enabled")
optional: bool = SettingsField(title="Optional")
Expand Down Expand Up @@ -98,6 +112,10 @@ class PublishersModel(BaseSettingsModel):
default_factory=BasicValidateModel,
title="Validate Mesh Has UVs"
)
ValidateModelName: ValidateModelNameModel = SettingsField(
default_factory=ValidateModelNameModel,
title="Validate Model Name"
)
ExtractModelObj: BasicValidateModel = SettingsField(
default_factory=BasicValidateModel,
title="Extract OBJ",
Expand Down Expand Up @@ -146,6 +164,12 @@ class PublishersModel(BaseSettingsModel):
"nearclip": 1.0,
"farclip": 1000.0
},
"ValidateModelName": {
"enabled": True,
"optional": True,
"active": False,
"regex": "(.*)_(?P<subset>.*)_(GEO)"
},
"ValidateLoadedPlugin": {
"enabled": False,
"optional": True,
Expand Down