Using Python 'distanceTo' function

   2961   4   0
User Avatar
Member
2048 posts
Joined: Sept. 2015
Offline
Hello,

I was hoping someone might be able to suggest a way to use ( if possible ) the distanceTo python function with 2 created Vector3 variables.

I've tried playing around in Python Shell and get an error message when I try to use the function

Any ideas? - Thank you

>>> Pt1 = hou.Vector3
>>> Pt2 = hou.Vector3
>>> Pt1.x = 1
>>> Pt1.y = 2
>>> Pt1.z = 0
>>> Pt2.x = 1
>>> Pt2.y = 3
>>> Pt2.z = 1
>>> Pt1.distanceTo(Pt2)
Traceback (most recent call last):
  File "<console>", line 1, in <module>
TypeError: unbound method distanceTo() must be called with Vector3 instance as first 
argument (got type instance instead)
>>>
Edited by BabaJ - Nov. 9, 2016 13:43:44
User Avatar
Member
2573 posts
Joined: June 2008
Offline
This worked for me.

Attachments:
Untitled-1.jpg (20.1 KB)

Using Houdini Indie 20.0
Windows 11 64GB Ryzen 16 core.
nVidia 3050RTX 8BG RAM.
User Avatar
Member
2048 posts
Joined: Sept. 2015
Offline
Thanks for the example Enivob,

Made me realize I was still thinking in terms VEX, where I could use x, y or z for a vector.

But also interesting that I see I have to also first ‘initialize’ all 3 components of the vector3 first to at least some values, before I can change them individually ( like in this screenshot )
Edited by BabaJ - Nov. 9, 2016 22:00:54

Attachments:
distanceTo.jpg (51.9 KB)

User Avatar
Member
182 posts
Joined: April 2009
Offline
Actually your first two lines should be:
Pt1 = hou.Vector3()
Pt2 = hou.Vector3()


From the Houdini help about hou.Vector3:
__init__(values=(0.0, 0.0, 0.0))
... If this method is called without parameters, the resulting vector contains the values (0.0, 0.0, 0.0).

What you are doing now is just assigning the class definition to a variable.

This will error out:
Pt = hou.Vector3
print Pt.length()

This works:
Pt = hou.Vector3()
print Pt.length()
>> 0.0

This works too:
Pt = hou.Vector3
mypt = Pt()
print mypt.length()
>> 0.0

Take a look at python classes, objects and instances.
User Avatar
Member
2048 posts
Joined: Sept. 2015
Offline
Not that I don't believe you, because I am not well versed in python classes, objects and instances ( so thanks for the suggestion as I am sure it will improve my using python in Houdini )..but the part you said would error out actually doesn't. Works fine.

Edit: Ok, sorry..your meaning Vector3 with or without braces. Thats an oversight on my part.

My intent wasn't to create Vector3 with using braces. I had some problems with VEX vectors in the past with the erroneous float values getting ‘initialzed’ to the assigned variable, if I didn't assign one myself. Maybe it was the same case and needed braces or something like that. I just carried over that convention here and never thought about braces. Thanks for showing that.
Edited by BabaJ - Nov. 11, 2016 08:15:54

Attachments:
distanceTo_2.jpg (17.3 KB)

  • Quick Links