Execute string in Python ?

   6076   7   3
User Avatar
Member
39 posts
Joined: Sept. 2013
Offline
is there any way to execute string in python ? for example i want to execute this code to get texture path from disk , and then use it to set string.

myPath = '/obj/test3_FBX/materials/Google_Hybrid___7/Google_Hybrid___7_surface/map1'
hou.parm(myPath)

here is my full code :
myNodes = hou.selectedNodes()
mySize = len(myNodes)
for i in range(mySize):
    myNode = '/obj/geo1/'+myNodes[i-1].name()
    myShop = (myNodes[i-1].geometry().primAttribs()[0]).name()
    myShopATT = myNodes[i-1].geometry().primStringAttribValues(myShop)[0]
    
    myPath = 'hou.parm('+'"'+myShopATT+'/'+myShopATT.replace('/obj/test3_FBX/materials/','')+'/map1")'
    print myPath    
    hou.node(myNode).parm('Texture').set(myPath)

its return this now :


but i want to get this :


in 3dsmax maxscript you can using a execute syntax before string to do it. is there a similar way in python for that ?



.
Edited by vrman - Sept. 14, 2017 06:23:40

Attachments:
ret1.jpg (10.6 KB)
ret2.jpg (12.1 KB)

User Avatar
Staff
1255 posts
Joined: July 2005
Offline
Hello,

Python has an eval()function for evaluating code in strings.

For example:
eval(myPath)

That would return a hou.Parm object using your code example above.

But why even build up a string containing hou.parm(..)? Why not just call hou.parm(..)directly?

For example:
myParm = hou.parm(myShopATT+'/'+myShopATT.replace('/obj/test3_FBX/materials/','')+'/map1')
myPath = myParm.eval()

Cheers,
Rob
User Avatar
Staff
476 posts
Joined: April 2014
Offline
I think what you are after is how to install a python parameter expression on a string parameter.

Right-click in the Parameter label, and do Edit Expression. Write anything in there and click ok. You should see the parameter text background turning green. This tells you you have an hscript expression installed. Right-click again and Set Expression Language to Python. The text background should turn purple.
Now, you have the choice of writing a single line expression, or a multinline one.
You won't need to call eval() or hou.parm().set() to change the parameter value.

You just need to write python code that returns a string for a single line expression.

For multiline expression, you can use the return statement to return a string from any lines. It acts a bit like a function without the def line.
User Avatar
Member
2041 posts
Joined: Sept. 2015
Offline
I was wondering if someone could help me.

I thought I would post here in this recent thread since what I am asking is similar but am having problems implementing.

I've tried doing what rvinluan suggested. But in my case the parameter I am referencing is an operator path that is referencing the desired paramter.

I have it set up this way as it reflects something I want to do with a digital asset. In my hip attachment I've simplified the set up with a subnet in the material network.

I've tried the following which doesn't work:

Result = 1

if(ch("../toggle_use_uv_node") == 0):
Result = ch("../uv_alt_constant")

if(ch("../toggle_use_uv_node") == 1):
My_Ref = hou.parm(expandString('../uv_node_reference'))
Result = My_Ref.eval()


return Result

I've done some net searches but can't seem to find enough info and getting this to work.

So help is much appreciated - thank you.
Edited by BabaJ - Sept. 20, 2017 10:44:27

Attachments:
Python String Eval.hiplc (1.5 MB)

User Avatar
Staff
476 posts
Joined: April 2014
Offline
Try using: hou.chsop(“../uv_node_reference”)
It evaluates the string parameter as a node path and performs relative path resolution.
User Avatar
Member
2041 posts
Joined: Sept. 2015
Offline
Hmmmm, thanks for the reply but it doesn't seem to work.

On a side note I noticed I was only referencing the uv texture node and forgot to include the goal parameter.

So in the file it should be:

../../uvtexture1/su

not ../../uvtexture1

But even so. No results - not erroring out either.

Result = 1

if(ch("../toggle_use_uv_node") == 0):
Result = ch("../uv_alt_constant")

if(ch("../toggle_use_uv_node") == 1):
Result = hou.chsop('../uv_node_reference')


return Result
User Avatar
Staff
476 posts
Joined: April 2014
Offline
I think this should work.

Result = 1

if(ch("../toggle_use_uv_node") == 0):
    Result = ch("../uv_alt_constant")
    
if(ch("../toggle_use_uv_node") == 1):
    My_Ref = hou.node(hou.chsop('../uv_node_reference'))
    Result = My_Ref.parm('su').eval()


return Result
User Avatar
Member
2041 posts
Joined: Sept. 2015
Offline
Thank you very much Guillaume - This works now.

Had a bit of trouble to get it to work, but when I realized I should have removed the ‘su’ reference from the operator path parameter, ie. changing it back to

../../uvtexture1

from

../../uvtexture1/su

..then it worked.

Will have to keep this file with code for future reference until I am much better at knowing how to navigate through the hou docs.

Thanks again.
  • Quick Links