How can I use Python to create new geometry?

   Views 27148   Replies 11   Subscribers 1
User Avatar
Member
22 posts
Joined: Aug. 2007
Offline
Apologies if this has been answered else where, but I couldn't find an answer when searching

I'm trying to use python to create some geometry. In the simplest sense, I just need to create edges between existing points, but if that's not possible, then I could always create polygon faces (thus giving me the edges I need - it's just I don't actually need the faces).

Anyway, from reading the help over the HOM I'm fairly certain I need to use hou.Geometry.createPolygon, which unfortunately is undocumented… so I have no idea how to use it!

Anyone know how to use this method to create new polygon geometry, or know of another way to use python to create new geometry? (I've got a fairly complicated algorithm working in python, the last stage for which is to output the newly calculated geometry).

Cheers,
Paddingon
User Avatar
Member
22 posts
Joined: Aug. 2007
Offline
Bump…. surely someone knows how to use python to create new geometry… no?

This is really causing me a headache, so any help would be greatly appreciated
User Avatar
Member
58 posts
Joined: Sept. 2007
Offline
last time i tried python there was to mutch missing.

i was able to create some vertexes tho…
User Avatar
Member
22 posts
Joined: Aug. 2007
Offline
That sucks :roll:

Can the developers confirm if it is indeed possible to create polygonal geometry using python? I guess I'll have to use the HDK if not, but I was really hoping to avoid this option because I'd have to recompile etc. for new versions of Houdini, which I wouldn't need to do if I implemented it using python

Paddington
User Avatar
Member
1390 posts
Joined: July 2005
Offline
PaddingtonBear
That sucks :roll:
Can the developers confirm if it is indeed possible to create polygonal geometry using python? I guess I'll have to use the HDK if not, but I was really hoping to avoid this option because I'd have to recompile etc. for new versions of Houdini, which I wouldn't need to do if I implemented it using python
Paddington

Hi,
last time we had this conversation about creating geo with Python there was an conclusion that this is not what developers would love since it potentially breaks cooking schema in Houdini. This sounds reasonable. Creating points and prims were supposed to be restricted into a PythonSOP context as I remember, could be wrong here though.

On the over hand it's quite easy to follow current Houdini's paradigm and use HOM to drive operators. This appears less elegant than MEL loop at the first glance, but in fact is much more stable, save and coherent in Houdini - using its native operators.

Here you have the simplest example in HOM:

: I added a null to overcome GUI issue in heavy Add SOP.


# CreateGeometry.py
# Create Geometry in HOM


# Main function (shouldn't be a class?):
def createGeometry(node, points, edges=“”, closed=False):

geo = node.createNode(“add”)
null = node.createNode(“null”, “HOM_Geo”)
null.setDisplayFlag(1)

geo.parm(“points”).set(len(points))
geo.parm(“prims”).set(len(edges))

for ptnum in range(len(points)):
usept = “usept” + str(ptnum)
pt = “pt”+str(ptnum)
geo.parm(usept).set(1)
geo.parm(pt+“x”).set(points)
geo.parm(pt+“y”).set(points)
geo.parm(pt+“z”).set(points)


for edge in range(len(edges)):
pr = “prim” + str(edge)
geo.parm(pr).set(“ ”.join([str(x) for x in edges]))
if closed:
pr_closed = “closed”+str(edge)
geo.parm(pr_closed).set(1)


null.setFirstInput(geo)
null.setHardLocked(1)
geo.destroy()

return null

# User interface:
def createEdges(node, points, edges):
return createGeometry(node, points, edges)

def createFaces(node, points, poly):
return createGeometry(node, points, poly, True)

def createPoints(node, points):
return createGeometry(node, points)

# Ugly shortcuts :
cp = createPoints
cf = createFaces
ce = createEdges


So basically, assuming list of points coordinates:
points = ((0, 0, 0), (1, 2, 3), (1, 4, 5)…)
and points order list for edges:
edges = ((0, 1, 3, 4, 5, 6…), …)

above code works like this:

> import CreateGeometry as cg
> obj = hou.node(“/obj/geo1”)
> my_geo = cg.createFaces(obj, points, edges)
or
> my_geo = cg.createEdges(obj, points, edges)
etc

As you see not too much work overall

Hope this helps,
Sy.
Edited by - March 14, 2008 22:22:15
User Avatar
Member
7967 posts
Joined: July 2005
Offline
Offhand, I see hou.Geometry.createPoint(), hou.Geometry.createPolygon(), and hou.Face.addVertex(). That by itself should be enought to create new polygonal geometry.
User Avatar
Member
1390 posts
Joined: July 2005
Offline
edward
Offhand, I see hou.Geometry.createPoint(), hou.Geometry.createPolygon(), and hou.Face.addVertex(). That by itself should be enought to create new polygonal geometry.

Do you know any related voodoo on this Edward? I suspect they are not intended to be used by a user currently since they refuse to work in a standard way. OF course above example will also refuse to work on anything heavier than a few hundred of polys… . One could add point with particles and VEX but how to builds polys on this?
User Avatar
Staff
4537 posts
Joined: July 2005
Offline
Those functions Edward mentioned can only be used within a Python SOP. Go to File->New Operator Type. Choose “Python Operator”. On the Code page of the Type Properties dialog put in your script, which is free to use those functions. As a quick test, I did:

# This code is called when instances of this SOP cook.
geo = hou.pwd().geometry()

# Add code to modify the contents of geo.
for i in range(0, 100):
pt = geo.createPoint()
pt.setPosition(hou.Vector3(i * 0.1, 0, 0))

Which created 100 points. See the python help for more fun and exciting things you can do with hou.Point, hou.Polygon, etc. But anything that modifies geometry will only work within a Python SOP.

Mark
User Avatar
Member
1390 posts
Joined: July 2005
Offline
mtucker
Those functions Edward mentioned can only be used within a Python SOP. Go to File->New Operator Type. Choose “Python Operator”. On the Code page of the Type Properties dialog put in your script, which is free to use those functions. As a quick test, I did:

# This code is called when instances of this SOP cook.
geo = hou.pwd().geometry()

# Add code to modify the contents of geo.
for i in range(0, 100):
pt = geo.createPoint()
pt.setPosition(hou.Vector3(i * 0.1, 0, 0))

Which created 100 points. See the python help for more fun and exciting things you can do with hou.Point, hou.Polygon, etc. But anything that modifies geometry will only work within a Python SOP.

Mark

Thanks Mark, but I was aware of that. It just didn't worked from me yesterday when I tried… although my build is rather old (9.1.140). I would consider upgrade but I was mislead by others problem also… so I need an upgrade anyway.

cheers,
sy.
User Avatar
Member
22 posts
Joined: Aug. 2007
Offline
Hi everyone,

Thanks for the replies. Perhaps I should have been clear earlier, I am fully intending to write a python SOP using my algorithm , meaning the previously mentioned methods are available to me.

The problem I was having was how to use the hou.Geometry.createPolygon() and hou.Face.addVertex() methods. I'm still fairly new to python, and so I'm not sure how I can call these and specify where the points for that face are located, and how many there are. I'm guessing I need to create a polygon, and then add vertices to the polygon - but is there a difference between a polygon object and a face object? Or once I've created a polygon will I have the addVertex method available to me?

The example for geo.createPoint() given by mtucker is fantastic, and shows very clearly how to create and move points - any chance of a similar example (it can be just as simple) to show how to call and use the other methods?

I really appreciate the replies so far, and if anyone can clear up my last couple of queries I'd be extremely grateful.

Cheers!
Paddington
User Avatar
Member
7967 posts
Joined: July 2005
Offline
You just give poly.addVertex() the point objects you created where poly is the result of createPolygon(). hou.Polygon is derived from hou.Face. A quick test shows that the following works for me. Just make sure to set the number of inputs 0 in the Type Properties dialog (first tab) to avoid requiring an input.

geo = hou.pwd().geometry()
pt0 = geo.createPoint()
pt0.setPosition(hou.Vector3(1, 0, 0))
pt1 = geo.createPoint()
pt1.setPosition(hou.Vector3(0, 1, 0))
pt2 = geo.createPoint()
pt2.setPosition(hou.Vector3(0, 0, 1))
poly = geo.createPolygon()
poly.addVertex(pt0);
poly.addVertex(pt1);
poly.addVertex(pt2);
User Avatar
Member
22 posts
Joined: Aug. 2007
Offline
Thank you so much! That's fantastic

I had no idea that hou.Polygon was derived from hou.Face, or that to call .addVertex I had to pass it a previously created point object

You've seriously saved my ass! This is fantastic

Thanks again,
Paddington
  • Quick Links