Wrapping my head around Python classes

   1629   2   0
User Avatar
Member
10 posts
Joined: May 2017
Offline
I've been learning Python in my spare time this year, which has been (mostly) a joy.
Now that I'm finally putting it into use in Houdini, I'm finding one thing interesting which perhaps someone here can better explain to me.

When learning about basic OOP in Python, generally the process of instantiating a class would go as follows:

1. Define the class in a module:

class My_Class():
	pass
	
    def class_method(self):
	    pass

2. Instantiate the class, run a method

import module

x = module.My_Class()
x.class_method()


What I think I notice is that in Houdini, the process is a bit different to this. Creating a class object is not done by directly calling the class, but through a method. So instead of...
x = hou.Node("path")
y = x.Geometry()
...we instead call a method which seems to then in turn create an object of that class? Is this understanding correct?
x = hou.node("path") 
y = x.geometry()

I'm curious as to the logic of why this is done this way, if that's not an answer too complicated to bother explaining?


>> On a side note, I have to mention that so far, I'm finding the experience of using Python in Houdini to be excellent, particularly the documentation is comprehensive and really easy to read. Thank you SideFX!
Edited by Heks12 - Dec. 28, 2020 20:00:21
User Avatar
Member
10 posts
Joined: May 2017
Offline
Searching further for answers I've been diving into the hou.py file. All the function definitions seem to just contain the function declaration, followed by a long docstring, and the returns the same function with a leading underscore. For example:

def node(path: "char const *") -> "HOM_Node *":
    r"""

    hou.node

    Given a path string, return a Node object. Return None if the path does
    not refer to a node.

    USAGE
      node(path) -> hou.Node or None

    If the path starts with a /, Houdini will look for a node with that
    exact path. Otherwise, the Houdini searches relative to the current
    path. See hou.pwd for more information about Houdini's current path. For
    each occurrence of .. in the path, Houdini will move up one node from
    the current location.

    Raises hou.NotAvailable if you call it from MPlay.

    Be careful not to confuse this function with the class hou.Node.

    > >>> hou.node(\"/obj\")
    > <hou.Node at /obj>
    > >>> hou.node(\"/obj\").createNode(\"geo\")
    > <hou.ObjNode of type geo at /obj/geo1>
    > >>> hou.node(\"/obj\").createNode(\"geo\")
    > <hou.ObjNode of type geo at /obj/geo2>
    > >>> hou.node(\"/obj/geo1\")
    > <hou.ObjNode of type geo at /obj/geo1>
    > >>> hou.cd(\"/obj\")
    > >>> hou.node(\"geo1\")
    > <hou.ObjNode of type geo at /obj/geo1>
    > >>> hou.cd(\"/obj/geo2\")
    > >>> hou.node(\"../geo1\")
    > <hou.ObjNode of type geo at /obj/geo1>
    > >>> print hou.node(\"../geo3\")
    > None

    etc etc..

    """
    return _hou.node(path)


What is going on here, how is this function creating a hou.Node() instance? Is it something to do with function being hidden inside the _hou.pyd file?

Thanks for the python lesson
Edited by Heks12 - Dec. 29, 2020 04:32:41
User Avatar
Member
577 posts
Joined: Nov. 2005
Offline
the function does not really create a node, it just gives You back a reference to a existing node.

there is a function node.createNode() that actually creates a node, but it also returns a object reference.
Edited by sanostol - Dec. 29, 2020 11:05:00
  • Quick Links