Jump to content

Using python to copy objects randomly upon points

Recommended Posts


I was wondering if there is a way with a Python script to be able to choose the source geometry library in Houdini (not from disk) and then choose the geometry randomly and copy to the points. 

To clarify,  I have a source of multiple geometries. Geo1, geo2, geo3, etc, which contains a different version of said object. So in this case I have 12 geometry nodes each with a different variation of a oak leaf. I know I can do this with for loop sops and such but I feel that having a script to do this would be quite nice.


Share this post

Link to post
Share on other sites

Hey, why not use a copy stamp with a switch node with a variable called copy with this expression : round(fit01(rand($PT), 0, 11)) and then reference stamp("copy1", "copy", 0) in the switch node ? May be much simpler

Otherwise check this video : 


Share this post

Link to post
Share on other sites

Thanks for directing me to the video.

It would seem that I lack the coding expertise to actually do my own version of the script. The script from the video is doing way more than what I would need. As all I would need is to be able pick the source library via a directory input in the python and then "simply" scatter the objects randomly across the points in which are input to the python node. 

I will try to work something out during the day. 


Share this post

Link to post
Share on other sites

An easier way would be to use VEX to build up a list of the object paths that you want to randomly copy, then write one of those paths to a point attribute on each point. This might be simpler to do than using Python, since you have the shortcut syntax available to bind attributes... it's a bit cumbersome to do that in Python.

Your VEX would look something like this:

string paths[] = {"/obj/objects/obj1", 
int randnum = int(rand(@ptnum+ch("seed"))*len(paths));
s@instancepath = "op:"+paths[randnum];

You can then pass the result directly into an Instance SOP. Inserting the "op:" there in front of the path will allow the File SOP inside Instance to reference the correct geometry.

Incidentally, this "instancepath" attribute is going to be added to the Copy SOP in H18, so you might want to get yourself used to this sort of workflow.

  • Like 1

Share this post

Link to post
Share on other sites

Ah very nice toadstorm.
Yes this made much more sense, I was trying yesterday with Python but couldn't get my head around it really. 
What I am trying to do now is to make a list automatically of the source library so I don't need to manually type in every single variation of the object. 

I added a operator path as the source input of the wrangle:

string paths[] = {"`chs("source")`/"};

That makes me able to choose any object I desire, but the issue is I am not getting the whole list of available objects in the source. 
So this will be todays little fiddle!

Share this post

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now