Jump to content

Python - Correct Me If I'm Wrong


Tamis

Recommended Posts

So i'm tryng to work with python in houdini.

so far i understand that hou is the houdini modual that you use to controll houdini itself.

hou.pwd means use the incomming geomatry when creating yout own operator(node)

hou.pwd.geometry means acces the geometry from the incomming object.

and hou.pwd.geometry.points means acces the points from that geometry.

so no error this works oke.

but when i type hou.pwd.geometry.points *2 it gives an error: unsuported operants type for: * 'instandcemethod' and 'int'

i expect it to just multiply the position by 2 for each point ?

Link to comment
Share on other sites

I think you'll find that points() just returns a point type which consists of more than just the position, plus it will return a list of all the points.

I haven't done this in python only the hdk but it seems very similar.

I would expect that you would have to loop through all the points returned by points() and access their individual position method, pos() maybe, and then multiple those values by 2.

Link to comment
Share on other sites

In the Windows > Python Shell window, type these to get more info:

>>> help('hou.Geometry.points')

>>> help('hou.Geometry.iterPoints')

>>> help('hou.Point')

Notice how there's a position() method in hou.Point that returns the position and a setPosition() method that allows you to set it. Putting all this together we get:

geo = hou.pwd().geometry()
for pt in geo.points():
  pos = pt.position()
  pt.setPosition([pos[0]+2, pos[1], pos[2]])

Link to comment
Share on other sites

First thing you should do is study concepts of object oriented programming and dynamic languages. Go through Python tutorial - the one supplied in python's Help. Then read "Dive into Python" - free book you can download from internet or buy printed. It will help you understand the nature of Python and tackle down the difference between dynamic and C-like languages (one of whom is VEX).

There is also another story about HOM itself - this API is not very "pythonic" - because it really can't be like that. Fortunately once you get used to its weird functions() nature, you will use it with please ;).

good luck!

sy.

Edited by SYmek
Link to comment
Share on other sites

First thing you should do is study concepts of object oriented programming and dynamic languages. Go through Python tutorial - the one supplied in python's Help. Then read "Dive into Python" - free book you can download from internet or buy printed. It will help you understand the nature of Python and tackle down the difference between dynamic and C-like languages (one of whom is VEX).

There is also another story about HOM itself - this API is not very "pythonic" - because it really can't be like that. Fortunately once you get used to its weird functions() nature, you will use it with please ;).

good luck!

sy.

Thank you SYmek,

It's always nice to have someone lay that bit of road for you so you don't need to go threw unnecessary pain.

Link to comment
Share on other sites

Hey, i'm back :D

so i got a bit further but i am stuck once more.

so a couple of questions:

i have been playng with the python shell, by using the dir command i can go into the structures.

so dir(hou.Geometry.points) gives me:

['__call__', '__class__', '__cmp__', '__delattr__', '__doc__', '__get__', '__getattribute__', '__hash__', '__init__', '__new__',

'__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__str__', 'im_class', 'im_func', 'im_self']

so i assume these are input variables and hou.Geometry.points is a function.

but if this is so then were can i find pt ?

geo = hou.pwd().geometry()

for pt in geo.points():

pos = pt.position()

pt.setPosition([pos[0]+2, pos[1], pos[2]])

it's not in there, so what are these __bla__ ?

and were can i find the function(or structure) pt ?

thank you for your pations

edit:

Oke so i found out that pt is a variable that contains geo.points.

but now the question were did you get .postion from ? is it some kind of global function ?

Edited by Tamis
Link to comment
Share on other sites

The dir() function will return a list of attributes for that object. The '__' generally means that it's a private attribute and won't really mean much to you.

'pt' is a variable which the point is assigned to. So the 'for pt in geo.points():' will loop through the points and assign them to 'pt'. You can then access specific information about that point by using pt.position() etc.

Does this make sense?

M

Link to comment
Share on other sites

wow, i was just making an edit and already 2 posts !! brilliant !.

let me brake that down marc:

what your sayng is that the variable pt gets overitten in the loop.

the loop loops thrue .points() so it basicly dose the same operation on every single point in the array.

now what i don't understand is were edward got this .position function(structure?)

is it some sort of global function?

Edited by Tamis
Link to comment
Share on other sites

If you look back at Ed's post you'll see that in hou.Point there is a position() method and a setPosition() method. The first will return the position and the second will set the position.

I highly recommend reading the books Symek mentioned too. I know a few programming languages and Python was a little tricky to get my head around. Mainly, I think, because its syntax and names for things are so different than any other language.

But if you read those then this will all be a lot easier, I promise that :).

M

Link to comment
Share on other sites

thnx marc, python is indeed tricky defenatly because alot of things arn't clear because its all automatic.

like create a variable you don't specifie if its a float int vector ect.

also i'm confused by the . things not clear if something is a structure function variable ect.

i'l be looking at that book again as you and symek mentiont didn't make mutch sense the first time tho :P.

thnx for the help, hope to return the favor.

Cheers:

cheers-three-mugs.jpg

Link to comment
Share on other sites

Sure. Feel free to ask questions here though, I'm sure there are many people who have the same problems with it that could benefit from a little explanation :).

Personally I'm just looking forward to the day that they document all of the python methods in Houdini.

Link to comment
Share on other sites

also i'm confused by the . things not clear if something is a structure function variable ect.

There is something in Python called "introspection". It basically means that every single python object has a knowledge about itself and that we (or Python) have an access to this knowledge. This is possible because every single python object is... an object not only a statement or expression - thus can posses attributes (which is quite different from variable, func() or operator in C - they state, express or do something but don't posses attributes).

If you find your self in trouble to recognize some object's nature ask it to introduce itself. Simple writing its name in interpreter will show you its soul. object class, method, attribute etc. You can use type() build-in method also.

Another handy note is that HOM consists almost exclusively with methods (and objects they belong to). An access to object's attributes is possible only with one, unique method() (function). This is HOM specific and has nothing to do with Python.

Knowing this you can be sure that whenever you want to access/modify some object's attributes in HOM you should look for a method() for this. So non standard "=" assignment will apply. Methods names are very consistent and descriptive.

good luck in diving ;)

sy.

Edited by SYmek
Link to comment
Share on other sites

once more i'm a bit confused:

# This code is called when instances of this SOP cook.

geo = hou.pwd().geometry()

# Add code to modify the contents of geo.

geo.createPoint()

p = geo.points()

p.setPosition(0,0,-2)

it is crashing on p.setPosition(0,0,-2) error: object has no attribute setPosition.

im tryng to do the same as in SYmek example only for one point(i deleted other incomming points first).

but houdini interpertates setPosition as a atribute and not as a method.

do i have to declare these methods ? clearly missing something.

Link to comment
Share on other sites

Your problem is that p is a list object and does not have a setPosition method. You have to index into the list then do setPosition.

p[0].setPosition((0,0,-2)) for example, for the first/only point.

I should also mention that your position value needs to be a tuple/hou.Vector3.

Edited by Hammy
Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...