Better way for checking a nodes type in python

   2474   2   1
User Avatar
Member
15 posts
Joined: March 2015
Offline
what is the best way to check a nodes type,

I have always done somthing like this :
node  = hou.node('/obj/geo1')
if node.type().name() == "geo":
    print("other code goes here")
but I have always felt that comparing to a hard coded string to be a bit strange,

I suppose what I am looking for is something more along the lines of pythons builtin isinstace or somthing like;
if node.type() == hou.NodeTypes.Geo

I am probably missing somthing...
Thanks in advance
Edited by Roger Wellard - March 4, 2021 09:51:32
User Avatar
Member
1907 posts
Joined: Nov. 2006
Offline
Technically the best way is to explicitly test against actual node types constructed with the operator table and name:

node = hou.node('/obj/geo1')
expected_type = hou.nodeType(hou.objNodeTypeCategory(), "geo")

if node.type() == expected_type:
   ...

You could also get the type from the node type category object:
category = hou.objNodeTypeCategory()

node_type = category.nodeTypes()["geo"]
node_type = category.nodeType("geo")
Graham Thompson, Technical Artist @ Rockstar Games
User Avatar
Member
15 posts
Joined: March 2015
Offline
This is exactly what I was looking for!
Thank you
  • Quick Links