Jump to content

evaluating a string as an expression


Adam Ferestad

Recommended Posts

I am looking for a way to type a math expression into a string parameter, then evaluate it in the function.  Specifically I am working on writing a Monte Carlo numerical integrator for a project I am doing that needs some integrals, but I am wanting to be able to use it for any expression.  I want to be able to pass it the expression and bounds to evaluate, but I am unsure of how to actually make it evaluate the string as anything but.  The old chrsraw() expression function did what I needed it to on an old project, but I am aiming to do this purely with VEX.

 

This will eventually turn into a random variable node with more control over the distributions, but I need the gamma function for several of them, which requires an integral calculation, as do several others.

 

As a side note, I got the Monte Carlo working for the gamma function, but it just couldn't get an accurate number in a good enough time, but I was able to get a rectangular rule that gets close enough.  

Edited by Adam Ferestad
Link to comment
Share on other sites

Something like this:
 

sop

something(string expression, {variables})

{

addattribute("result",evaluate(expression))

}

 

obviously this is grossly oversimplified, but I want to have a parameter on my UI in the parameters pane that accepts me typing in something like pow(x,x)-3*x-2 and then actually processes it in the code.  

 

the end goal for this thread is that I can pass a math expression to a function call and have the function execute the expression with given variables.

Edited by Adam Ferestad
Link to comment
Share on other sites

  • 3 weeks later...

if you are using python

eg. you want to set expression in tx of point node

 

my_node = hou.node('/obj/geo1/point1')
my_tx = my_node.parm('tx')
my_tx.deleteAllKeyframes() # to delete any existing expression

my_tx.setExpression ('$TX + rand($PT)')

Edited by mitul
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...