Jump to content

python expressions


kubabuk

Recommended Posts

Hello all,

I am trying to replace functionality of some hscripts commands used in my ROP, like strcat, strreplace, substr etc. with python expressions.

Basically what I am up to is to source the path, get rid of the extension, create a directory and save a file with a different extension in that directory. I have a python script which works fine up to the creation of the folder in the houdini python shell. So I thought the only thing I need to do is to past the script into the one of the filed of the ROP to have the job done. BUT it isn't so simple. I am definetly missing something here that's why I have some question to you guys. I haven't found much about python expressions in the help, so please help! :)

Is it something different to Python I use in python shell?

Does python expressions have some limitations are they restricted to one line or so?

Have they been implemented yet?

Do I have to brace python scripts from python shell with backticks the same it is with hscript and expressions to use them properly?

As you can see I haven't had luck with them yet.

thanks for help

kuba

Link to comment
Share on other sites

Python expressions are not limited to one line. To do more than a single expression you have to make sure to return a value to a parameter that is expecting one.

For example, the ty of a geo node can be set using a sin wave from python's math library.

import math
return math.sin(math.radians(frame()))

The python in a parameter only differs from that in the shell in that it automatically does a 'from hou import *' for you. Also, any modules that you import in the shell are not imported in parameters so you have to import any modules in the parameter code.

No sort of bracing or anything is needed. There really are no 'Python Expressions'. Python is python. It's not like the Hscript / Hscript Expressions. No sort of bracing is needed.

Edited by graham
Link to comment
Share on other sites

Thanks, it is pretty straightforward once one knows how it works.

Love it!

:lol:

This goes into the rop_geometry file field, for anyone who wants to push it further:

import os,hou

# store the path to the loaded jpg
fullpath = hou.evalParm('../trace6/file')

path = fullpath.split(".")
print path

# declare new extension
obj = ".obj"

# declare folder name to store new files in
folder = "geo2"

print path[0]
corepath = path[0].split("/")

# store the name of the jpg file
file_name = corepath.pop(-1)
print file_name

# remove the last element - the file name from the list
del corepath[-1]

print corepath

print file_name


new_core_path = '/'.join(corepath)
print new_core_path

# set the new path to the folder
new_file_path = new_core_path+'/'+folder
print new_file_path


if os.path.exists(new_file_path):
	print 'warning - folder already exists'
else:
	os.mkdir(new_file_path)

# set the final path fot the obj file
final_path = new_file_path+'/'+ file_name + obj
print final_path

return final_path

have a nice day

kuba

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