"""Utility methods that parses names.
:created: 27/02/2017
:author: Benoit GIELLY <benoit.gielly@gmail.com>
"""
from __future__ import absolute_import
import logging
import re
import bgdev.utils.decorator
from maya import cmds
LOG = logging.getLogger(__name__)
[docs]def find_conflicts(query=False):
"""Create two selection sets to store name conflicts.
Args:
query (bool): Only query the nodes without creating sets.
Returns:
tuple: Two separate lists for conflicting transforms and shapes.
"""
conflict_nodes = []
conflict_shapes = []
for each in cmds.ls(transforms=True, shapes=True):
if "|" in each:
if cmds.objectType(each, isAType="shape"):
conflict_shapes.append(each)
else:
conflict_nodes.append(each)
# return here if query flag is True
if query:
return conflict_nodes, conflict_shapes
# delete existing set_
for set_ in ["NODES_NAME_CONFLICTS", "SHAPES_NAME_CONFLICTS"]:
if cmds.objExists(set_):
cmds.delete(set_)
# create set if conflicts
if conflict_nodes:
cmds.sets(conflict_nodes, name="NODES_NAME_CONFLICTS")
if conflict_shapes:
cmds.sets(conflict_shapes, name="SHAPES_NAME_CONFLICTS")
if not conflict_nodes and not conflict_shapes:
LOG.warning("No conflincting names detected!")
return conflict_nodes, conflict_shapes
[docs]def generate_unique_name(name):
"""Generate a unique name based on the given one."""
count = 1
while cmds.objExists(name):
name = "{0}{1}".format(remove_end_digits(name, False), count)
count += 1
return name
[docs]@bgdev.utils.decorator.UNDO_REPEAT
def remove_end_digits_callback():
"""Call back :func:`remove_end_digits`."""
selection = cmds.ls(selection=True)
if not selection:
LOG.warning("Please select something!")
return
for each in selection:
remove_end_digits(each)
[docs]def remove_end_digits(node, rename=True):
"""Remove digits at the end of a name.
Args:
node (str): Name of the node to "cleanup"
rename (bool): If True, rename the node, else just return the cleaned up name
Returns:
str: Name of the node stripped ouf ot end digits.
"""
regex = re.compile(r"\w+\D+")
name = node.rpartition("|")[2]
new = regex.findall(name)[0]
if rename:
cmds.rename(node, new)
return new