Random value in Python

   7556   5   0
User Avatar
Member
210 posts
Joined: Jan. 2014
Offline
I'm struggling to create a stamp variable with a random value with Python.

First I tried:
hou.hscriptExpression('rand($PR)/4')
No error but also no result.

Then I tried:
import random
random.random()*20
No error but also no result.


What is going wrong here?
For the second version I would additionally like to know, if I manage to get a random value how do get to evaluate per point or primitiv?
User Avatar
Member
210 posts
Joined: Jan. 2014
Offline
Aaaaahhhhh! I found it, sorry, sorry! I forgot to return a value in a multiline expression.

import random
return random.random()


works fine now.


But actually I have a different question now:
The help says that hou gets imported always when Houdini starts, so you don't have to explicitly call it in your parameter expressions.
Can I do the same with random? Maybe like the menu.py file for Nuke. Where you can store your own scripts and they always get loaded on startup. This would be so convenient.
User Avatar
Member
387 posts
Joined: Nov. 2008
Offline
http://www.sidefx.com/docs/houdini14.0/hom/expressions [sidefx.com]

From the manual:
Houdini implicitly runs from hou import * and from hou.session import * in the Python expression namespaces.

Note that you can also access the Python expression namespace from the global namespace with hou.expressionGlobals.

So put it in your hou.session module or pythonrc.py file. Examples are in the help linked above.
User Avatar
Member
210 posts
Joined: Jan. 2014
Offline
pezetko
So put it in your hou.session module or pythonrc.py file.

Cool thanks, that sounds like what I'm after but I have yet another problem.

About the pythonrc.py the help says
$HOME/houdiniX.Y/python2.7libs/pythonrc.py
As well an automatic global search on my pc as well as manually looking into that path couldn't find that pythonrc file.

About the hou.session the help says
This module is used to define custom classes, functions and variables…
but it doesn't tell me where to find it and once again an automatic search on my pc couldn't find it.

Any hints?

Edit:
I did find a hou.py is that in anyway helpful?
User Avatar
Member
387 posts
Joined: Nov. 2008
Offline
$HOME/houdiniX.Y/python2.7libs/pythonrc.py

You have to create that pythonrc.py file in that path (or script path).

hou.session module is in Houdini->Windows->Python Source Editor, content of this window is content of hou.session module (it could be different per file or you can append your code to it with help of pythonrc.py or 456.py)

If help is not enough for you I would recommend to you to get some basic/introduction houdini python training, good one is here in Learning from SESI or e.g. some from Digital Tutors, CMIVFX, 3DBuzz etc…
User Avatar
Member
210 posts
Joined: Jan. 2014
Offline
If help is not enough for you I would recommend to you to get some basic/introduction houdini python training, good one is here in Learning from SESI…

No thanks, I'm already watching a good amount of these tuts, I was just confused about the missing file. It seems to work so far now.
Thanks a lot
  • Quick Links