validator

This commit is contained in:
Ondrej Samohel 2021-06-25 19:15:53 +02:00 committed by Ondřej Samohel
parent f5b5c94487
commit 5254a53d03
No known key found for this signature in database
GPG key ID: 02376E18990A97C6
3 changed files with 46 additions and 18 deletions

View file

@ -1,8 +1,13 @@
# -*- coding: utf-8 -*-
"""Validate model nodes names."""
from maya import cmds
import pyblish.api
import openpype.api
import openpype.hosts.maya.api.action
from openpype.lib.mongo import OpenPypeMongoConnection
import gridfs
import re
import os
class ValidateModelName(pyblish.api.InstancePlugin):
@ -19,18 +24,18 @@ class ValidateModelName(pyblish.api.InstancePlugin):
families = ["model"]
label = "Model Name"
actions = [openpype.hosts.maya.api.action.SelectInvalidAction]
# path to shader names definitions
# TODO: move it to preset file
material_file = None
regex = '(.*)_(\\d)*_(.*)_(GEO)'
database_file = "maya/shader_definition.txt"
@classmethod
def get_invalid(cls, instance):
"""Get invalid nodes."""
use_db = instance.context.data["project_settings"]["maya"]["publish"]["ValidateModelName"]["database"] # noqa: E401
# find out if supplied transform is group or not
def is_group(groupName):
def is_group(group_name):
"""Find out if supplied transform is group or not."""
try:
children = cmds.listRelatives(groupName, children=True)
children = cmds.listRelatives(group_name, children=True)
for child in children:
if not cmds.ls(child, transforms=True):
return False
@ -49,24 +54,41 @@ class ValidateModelName(pyblish.api.InstancePlugin):
fullPath=True) or []
descendants = cmds.ls(descendants, noIntermediate=True, long=True)
trns = cmds.ls(descendants, long=False, type=('transform'))
trns = cmds.ls(descendants, long=False, type='transform')
# filter out groups
filter = [node for node in trns if not is_group(node)]
filtered = [node for node in trns if not is_group(node)]
# load shader list file as utf-8
if cls.material_file:
shader_file = open(cls.material_file, "r")
shaders = shader_file.readlines()
shaders = []
if not use_db:
if cls.material_file:
if os.path.isfile(cls.material_file):
shader_file = open(cls.material_file, "r")
shaders = shader_file.readlines()
shader_file.close()
else:
cls.log.error("Missing shader name definition file.")
return True
else:
client = OpenPypeMongoConnection.get_mongo_client()
fs = gridfs.GridFS(client[os.getenv("OPENPYPE_DATABASE_NAME")])
shader_file = fs.find_one({"filename": cls.database_file})
if not shader_file:
cls.log.error("Missing shader name definition in database.")
return True
shaders = shader_file.read().splitlines()
shader_file.close()
# strip line endings from list
shaders = map(lambda s: s.rstrip(), shaders)
# compile regex for testing names
r = re.compile(cls.regex)
regex = instance.context.data["project_settings"]["maya"]["publish"]["ValidateModelName"]["regex"] # noqa: E401
r = re.compile(regex)
for obj in filter:
for obj in filtered:
cls.log.info("testing: {}".format(obj))
m = r.match(obj)
if m is None:
cls.log.error("invalid name on: {}".format(obj))
@ -74,7 +96,7 @@ class ValidateModelName(pyblish.api.InstancePlugin):
else:
# if we have shader files and shader named group is in
# regex, test this group against names in shader file
if 'shader' in r.groupindex and shaders:
if "shader" in r.groupindex and shaders:
try:
if not m.group('shader') in shaders:
cls.log.error(
@ -90,8 +112,8 @@ class ValidateModelName(pyblish.api.InstancePlugin):
return invalid
def process(self, instance):
"""Plugin entry point."""
invalid = self.get_invalid(instance)
if invalid:
raise RuntimeError("Model naming is invalid. See log.")
raise RuntimeError("Model naming is invalid. See the log.")

View file

@ -164,12 +164,13 @@
},
"ValidateModelName": {
"enabled": false,
"database": true,
"material_file": {
"windows": "",
"darwin": "",
"linux": ""
},
"regex": "(.*)_(\\\\d)*_(.*)_(GEO)"
"regex": "(.*)_(\\d)*_(?P<shader>.*)_(GEO)"
},
"ValidateTransformNamingSuffix": {
"enabled": true,

View file

@ -147,9 +147,14 @@
"key": "enabled",
"label": "Enabled"
},
{
"type": "boolean",
"key": "database",
"label": "Use database shader name definitions"
},
{
"type": "label",
"label": "Path to material file defining list of material names to check. This is material name per line simple text file.<br/>It will be checked against named group <b>shader</b> in your <em>Validation regex</em>.<p>For example: <br/> <code>^.*(?P=&lt;shader&gt;.+)_GEO</code></p>"
"label": "Path to material file defining list of material names to check. This is material name per line simple text file.<br/>It will be checked against named group <b>shader</b> in your <em>Validation regex</em>.<p>For example: <br/> <code>^.*(?P=&lt;shader&gt;.+)_GEO</code></p>This is used instead of database definitions if they are disabled."
},
{
"type": "path",