Jump to content
cgartashish

Add Name Attribute Via Python

Recommended Posts

Hi,

How to add a name attribute to each point in a selected geometry via the python sop?

Suppose I have a box with 4 points and I want each point to be named as piece0, piece1 and so on where the number is its point number. I have written a code, it adds the attribute to the spreadsheet but is unable to add the value. The value is added only when it is not a string. Please Help.

 

The code that I tried:

node = hou.pwd()
geo = node.geometry()

geo.addAttrib(hou.attribType.Point, "name", "piece")

 

Please also add how to add the respective point numbers to the string piece.

Thanks

Share this post


Link to post
Share on other sites

Something like this?

node = hou.pwd()
geo = node.geometry()

geo.addAttrib(hou.attribType.Point, "name", "")
for point in geo.points():
    point.setAttribValue("name", "piece" + str(point.number()))

Then look at this post by Graham explaining why string types does not have a default value.

  • Like 1

Share this post


Link to post
Share on other sites

Another method of string concatenation is to use the format() function.

name = "piece{0}".format(point.number())
  • Like 2
  • Thanks 1

Share this post


Link to post
Share on other sites

Wow, Thanks it works like charm.

Hey can you please help me with python for houdini. Because the documentation for python in Houdini is very wierd. I cannot get enough examples and all the functions are very hard to find. So can you point me to some documentation elsewhere or tutorial? Or can you please tell me how to follow the documentation because I always feel lost in it. Like finding some examples or some function name, etc. 

Share this post


Link to post
Share on other sites

The documentation is the first thing I look through. But I agree, it does kinda lack in the way of examples.
Next I also google stuff, where the results are usually from this forum, or at Sidefx.
For general python syntax and functions, I still use google. :D

Lastly, if I have an issue and all of the above is still lacking.. I would try and post a thread to hopefully get some answers.

As for examples, I try and check out built-in HDA's and peek inside the Scripts tab, sometimes it contains python code.
Also if you explore the Shelf Tools, most of those uses a module in the form of 'tools' that you can find at $HFS\houdini\python2.7libs
They have lots of functions there with descriptions.

I'm also still just learning as I go with my day to day Houdini adventure. Wishing you the best of luck!

  • Like 2

Share this post


Link to post
Share on other sites
On 11/22/2017 at 10:31 PM, galagast said:

The documentation is the first thing I look through. But I agree, it does kinda lack in the way of examples.
Next I also google stuff, where the results are usually from this forum, or at Sidefx.
For general python syntax and functions, I still use google. :D

Lastly, if I have an issue and all of the above is still lacking.. I would try and post a thread to hopefully get some answers.

As for examples, I try and check out built-in HDA's and peek inside the Scripts tab, sometimes it contains python code.
Also if you explore the Shelf Tools, most of those uses a module in the form of 'tools' that you can find at $HFS\houdini\python2.7libs
They have lots of functions there with descriptions.

I'm also still just learning as I go with my day to day Houdini adventure. Wishing you the best of luck!

Thanks alottttt. :)

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×