Jump to content

Python How To: Get Point Position, Store It In A String


pclaes

Recommended Posts

Hi guys,

I came across a little problem and I am trying to get through it with python. I have experience with scripting but not with python in houdini.

I have to convert a polygon curve to a nurbs curve, but... the polygon points need to be used as breakpoints (curve points), not as Cv's. The only way I see of doing it is of attaching a "curve sop" to the polygon curve. But the curve sop takes a string as input which is like: "x1,y1,z1 x2,y2,z2 x3,y3,z3 x4,y4,z4 ...." so basically it takes in the translation values of each point. I need to generate that string. I am currently looking into python as it seems like a simple for-loop and in mel I would write it a bit like:

string result = "";

for( i=0, i < $NPT, i++)

{

result += getTranslateX + "," ;

result += getTranslateY + "," ;

result += getTranslateZ + "," ;

result += " ";

}

return result;

So far I got an new python sop that does:

geo = hou.pwd().geometry()

pos_string = geo.addAttrib(hou.attribType.Point, "posString", "")

for point in geo.points():
	print point.position()

Now I think it would be best to store the resulting string on the "detail" level rather then on point, but I haven't figured that out yet.

Also I find navigating the help files not that easy (as in Maya), but that is probably because I am not used to it yet.

thanks for any help!

Peter

Link to comment
Share on other sites

To get the positions into a string you just need to do:

result = ""
for point in geo.points():
	result += str(point.position()[0]) + ','
	result += str(point.position()[1]) + ','
	result += str(point.position()[2]) + ' '

geo.addAttrib(hou.attribType.Global, "posString", "")
geo.setGlobalAttribValue("posString", result)

Detail attributes are referred to as Global attributes in HOM.

Edited by graham
Link to comment
Share on other sites

thanks again !

I have tried out all solutions and they are all very interesting!

I love some of the creative expression use!

Never though of using the partition sop as a way of generating a string and not actually using the groups, just their names. The fit operator works too, but gives slightly different results.

rdg: that is some very cool name linking, it took me a bit to figure out your expression but using opinput(".", 0) to refer to the previous node and combining it with $OS to get to that group is a very nice way!

I've put all these different solutions in one file for my own personal future reference. I thought I attach it so other people can learn from this if they come across similar challenge.

python_curve_string_01.hip

Link to comment
Share on other sites

  • 2 months later...

Hi Peter,

I tried loading up your file and I'm getting this error message for the curve_test1 sop:

# Couldn't find Python code. Please check that

# oplib:/Sop/curve_test?Sop/curve_test.py is in the proper location.

thanks again !

I have tried out all solutions and they are all very interesting!

I love some of the creative expression use!

Never though of using the partition sop as a way of generating a string and not actually using the groups, just their names. The fit operator works too, but gives slightly different results.

rdg: that is some very cool name linking, it took me a bit to figure out your expression but using opinput(".", 0) to refer to the previous node and combining it with $OS to get to that group is a very nice way!

I've put all these different solutions in one file for my own personal future reference. I thought I attach it so other people can learn from this if they come across similar challenge.

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...