handle permissions and root more gracefully

This commit is contained in:
Ondrej Samohel 2022-02-10 17:31:32 +01:00
parent 816eb71393
commit 390c6d15a5
No known key found for this signature in database
GPG key ID: 02376E18990A97C6

View file

@ -403,33 +403,46 @@ def imprint(node, data):
node.setParmTemplateGroup(parm_group)
def lsattr(attr, value=None):
def lsattr(attr, value=None, root="/"):
"""Return nodes that have `attr`
When `value` is not None it will only return nodes matching that value
for the given attribute.
Args:
attr (str): Name of the attribute (hou.Parm)
value (object, Optional): The value to compare the attribute too.
When the default None is provided the value check is skipped.
root (str): The root path in Houdini to search in.
Returns:
list: Matching nodes that have attribute with value.
"""
if value is None:
nodes = list(hou.node("/obj").allNodes())
nodes += list(hou.node("/stage").allNodes())
# Use allSubChildren() as allNodes() errors on nodes without
# permission to enter without a means to continue of querying
# the rest
nodes = hou.node(root).allSubChildren()
return [n for n in nodes if n.parm(attr)]
return lsattrs({attr: value})
def lsattrs(attrs):
def lsattrs(attrs, root="/"):
"""Return nodes matching `key` and `value`
Arguments:
attrs (dict): collection of attribute: value
root (str): The root path in Houdini to search in.
Example:
>> lsattrs({"id": "myId"})
["myNode"]
>> lsattr("id")
["myNode", "myOtherNode"]
Returns:
list
list: Matching nodes that have attribute with value.
"""
matches = set()
nodes = list(hou.node("/obj").allNodes()) # returns generator object
nodes += list(hou.node("/stage").allNodes())
# Use allSubChildren() as allNodes() errors on nodes without
# permission to enter without a means to continue of querying
# the rest
nodes = hou.node(root).allSubChildren()
for node in nodes:
for attr in attrs:
if not node.parm(attr):