Get primvar values in parameter fields and wrangle nodes

   1537   9   1
User Avatar
Member
39 posts
Joined: Oct. 2011
Offline
I'm trying to access data stored in a primvar. For a simple test, I'm just trying to get this primvar to show up in the translate y axis of a transform node but I also want to know how to access it in a wrangle node.


Based on another thread, it seems like the following python expression on the y axis parameter should work, and it does compile, but returns 0 when it definitely should not:

hou.pwd().input(0).stage().GetPrimAtPath("/myObject").GetAttribute("myPrimvar").Get()


Please help if you can.
Edited by fbb - April 22, 2024 14:10:41
User Avatar
Staff
4523 posts
Joined: July 2005
Offline
If you call `Get()` without an argument, what you get back will be the "default value" of the attribute. If the attribute you are fetching has time samples (or really even if it doesn't have time samples), it's always best to pass a time value to the Get() method. Passing in `hou.frame()` is always a good option but will make your node time dependent. Passing in `0.0` will work if there is a single time sample or if the value is only set as a default value, and will avoid creating a time dependency. But it won't give you the expected result if there are multiple time samples available on the input's stage at the same time.
User Avatar
Member
39 posts
Joined: Oct. 2011
Offline
@mtucker - thank you for your reply. However, I must be misunderstanding something 'cause when I try formulating the "Get()" method as
Get(0.0)
as you suggest. This compiles but still returns "0" instead of the correct value. Perhaps there are multiple time samples available on the input stage as you said that could break it, but I don't think so (I'm really not sure how that condition would arise, actually, but the input is pretty simple). Also, is there really no way to make this work without sampling the time?
Edited by fbb - April 22, 2024 17:02:20
User Avatar
Member
39 posts
Joined: Oct. 2011
Offline
Figured it out, at least in the case of using it in a parameter. I had to place 'primvars:' before 'myPrimvar' in the "GetAttribute" method, like so:

hou.pwd().input(0).stage().GetPrimAtPath("/myObject").GetAttribute("primvars:myPrimvar").Get()

I didn't need to put anything in the "Get" method.


Of course, this doesn't answer the question of how to get such attributes in a wrangle, so please chime in if you know how to do that.

Also, if anyone knows how to get a primvar without so much code and without needing to specify the object in the "GetPrimAtPath" method, so that it just uses the input geo, that would be really helpful as well.
User Avatar
Member
39 posts
Joined: Oct. 2011
Offline
Update: if you put "ch("primpattern")" into the "GetPrimAtPath" method, that will use whatever primitive you specify in the "Primitives" field of the node which means you can use `lopinputprims('.', 0)` in the "Primitives" field to get the geo that is plugged into the input.
User Avatar
Member
39 posts
Joined: Oct. 2011
Offline
Something else I'm stuck on: how to fetch a value from an array type primvar.
User Avatar
Member
199 posts
Joined: Nov. 2013
Offline
Here's how you'd grab an element from an array and assign it to a new primvar. You can prob use this to find a way to drive a parm I'd imagine.

vector value = usd_primvarelement(0, @primpath, "displayColor", 3); //Gets the third element in the array
usd_addprimvar(0, @primpath, "myval", "vector");
usd_setprimvar(0, @primpath, "myval", value);
User Avatar
Member
39 posts
Joined: Oct. 2011
Offline
Hamilton Meathouse
vector value = usd_primvarelement(0, @primpath, "displayColor", 3); //Gets the third element in the array
usd_addprimvar(0, @primpath, "myval", "vector");
usd_setprimvar(0, @primpath, "myval", value);

That's not python. Is it hscript or vex? I've tried implementing this "usd_addprimvar" as hscript without success. I suspect this is vex meant for use in a wrangle.

... found the doc's for it - yeah, that's vex. This will be useful for when I need to work with a wrangle. Thank you.

Of course, I still haven't found a way to get a value from an array primvar into a parameter. At least not directly. I did come up with a workaround. The attribute I'm trying to access was created in a sopmodify node, so I just split the array into individual float attributes - these I know how to fetch. If you have a pre-existing array primvar whose values you need to fetch, I expect you could push this into a sopmodify node and, there, split the array to individual floats so that they are accessible downstream but... this really shouldn't be necessary.
Edited by fbb - April 23, 2024 13:10:41
User Avatar
Member
199 posts
Joined: Nov. 2013
Offline
Here I have a sopcreate that is making a sphere and assigning a random color to each point. Then in the transform lop below it I am adding a python expression in the 'tx' parm that reads a primvar value (in this case displayColor) and sets the 'tz' parm to that value.

def newfunc():
    myvallist = []
    blah = hou.node("/stage/sopcreate1").stage().GetPrimAtPath("/sopcreate1/mesh_0").GetAttribute("primvars:displayColor").Get(0)
    for i in [blah[0]]:
        myval = i[0]
        myvallist.append(myval)
        b = myvallist[0]
        hou.parm("/stage/transform2/tz").set(b)
newfunc()

Attachments:
hou_forums_python.JPG (105.9 KB)

User Avatar
Member
199 posts
Joined: Nov. 2013
Offline
FYI the displayColor primvar is an array of tuples, so that code is getting the first tuple in the array and getting the first value in that tuple.
  • Quick Links