Jump to content

Create Material in Python


mabulazm

Recommended Posts

Hey :lol:

I am totally new to Python, so I have an idea and want to know if it possible to achieve or not.

creating Box and Material ... I have done this part :D

then I want to copy the material ... say 4 times and assign it to different primitive, but just 1 material holding 4 shaders.

material 1 go to primitive 0 ... and so on.

and of course change the color of the shader.

cheers

Link to comment
Share on other sites

Guest Swann

I give it a shot.

Just create new shelf tool and place this code in scriptTAB.

It creates box and material node also so you don't need create them manually.

# create geo node for your box
this = hou.node('/obj').createNode('geo', run_init_scripts=False)

# create box inside of geo
box = this.createNode('box')

# create material node
material = this.createNode('material')

# setup material
# find number of box faces
numFaces = len(box.geometry().prims())

# setup material tabs    
tabs = material.parm('num_materials')
tabs.set(numFaces)

# connect material to box and correct position
material.setFirstInput(box)
material.moveToGoodPosition()

# make material node active node
material.setDisplayFlag(True)
material.setRenderFlag(True)

# create shop node and correct position
shop = this.createNode('shopnet', 'SHOP')
shop.moveToGoodPosition()

# create shaders in shop
# create colors
color = ((0.9, 0.45, 0.0), (1.0, 1.0, 1.0), (0.9, 0.0, 0.0),
         (0.0, 0.45, 0.9), (0.0, 0.9, 0.0), (0.9, 0.9, 0.0))


# here we create loop using xrange instead of range
# range gives you string not real iterator, 
# you can use both but xrange is better suited for this because it's faster
# just remember that xrange is obsolete in python3 
for i in xrange(numFaces):
    # create your shaders
    shader = shop.createNode('v_clay')

    # setup shaders
    shader.parmTuple('diff').set(color[i])

    # correct nodes positions 
    shader.moveToGoodPosition()

    # assign colors to faces
    material.parm('group%s' % (i+1)).set(str(i))
    material.parm('shop_materialpath%s' % (i+1)).set(material.relativePathTo(shader))

Link to comment
Share on other sites

WawWooooo :blink:

this is exactly what am looking for, I was struggling a bit to find the name of shader, How can I find the shaders name, What about choosing from the Gallery for example Basic Surface, I have tried basic_surface but it gave me error, or it MUST be VEX.

len(box.geometry().prims())

feel a bit confused about how to get this kind of informations through the help command, could you guide me in how to use it.

is moveToGoodPosition() same like layoutChildren(), when to use it.

sorry for being a pain :rolleyes:

Edit :which editor you are using ?

cheers

Edited by mabulazm
Link to comment
Share on other sites

Items in the Material Palette aren't their own node types. They are gallery entries. The Basic Surface entry gets applied to a Material SHOP and creates a basic_surface material. To create it you need to do something like this:

mat_shop = hou.node("/shop").createNode("material")

try:
    gal = hou.galleries.galleryEntries(name_pattern="basic_surface")[0]
    gal.applyToNode(mat_shop)
 except IndexError:
    pass

There's no direct way, such as something like hou.Geometry.numPrims(), numPoints(), etc, to get the number of primitives or points in some geometry. That leaves you with getting the number of them by actually requesting them then finding the length of the resultant list/tuple. In the case of having a lot of points/prims you should consider using hou.Geometry.iterPrims() or iterPoints() if you just need to get the number of them. These functions return a generator object that doesn't allocate all the actual elements and waste time/memory.

moveToGoodPosition() basically just lays out a node based on it's inputs where as layoutChildren() lays out child nodes. If you are creating a single node then you just need to move it so you can tell it to move itself rather than telling the parent node to lay out all the children.

Edited by graham
Link to comment
Share on other sites

graham ... thanks a lot for your reply, things getting clear now.

so am thinking about, making number of colors depends on the numFaces, but to to make it vector and give it random values or can I use ... readInput(), selectFromList() to insert the color values.

cheers

Link to comment
Share on other sites

Guest Swann
... but to to make it vector and give it random values or can I use ... readInput(), selectFromList() to insert the color values.

If you wan't choose color before creation you need to make your own GUI for this with WX module or something simmilar.

But if you don't need this maybe just add float SEED parameter to you materialSOP node that will drive random generator in the code. For randomising use hou.hmath.rand(SEED), try adding primitive num, point num or maybe actual time, anything that will generate random SEED for each face, and make your own vector3 for color from those data.

Link to comment
Share on other sites

  • 9 years later...
On 17/6/2010 at 12:28 AM, Guest Swann said:

I give it a shot.

Just create new shelf tool and place this code in scriptTAB.

It creates box and material node also so you don't need create them manually.

 


# create geo node for your box
this = hou.node('/obj').createNode('geo', run_init_scripts=False)

# create box inside of geo
box = this.createNode('box')

# create material node
material = this.createNode('material')

# setup material
# find number of box faces
numFaces = len(box.geometry().prims())

# setup material tabs    
tabs = material.parm('num_materials')
tabs.set(numFaces)

# connect material to box and correct position
material.setFirstInput(box)
material.moveToGoodPosition()

# make material node active node
material.setDisplayFlag(True)
material.setRenderFlag(True)

# create shop node and correct position
shop = this.createNode('shopnet', 'SHOP')
shop.moveToGoodPosition()

# create shaders in shop
# create colors
color = ((0.9, 0.45, 0.0), (1.0, 1.0, 1.0), (0.9, 0.0, 0.0),
         (0.0, 0.45, 0.9), (0.0, 0.9, 0.0), (0.9, 0.9, 0.0))


# here we create loop using xrange instead of range
# range gives you string not real iterator, 
# you can use both but xrange is better suited for this because it's faster
# just remember that xrange is obsolete in python3 
for i in xrange(numFaces):
    # create your shaders
    shader = shop.createNode('v_clay')

    # setup shaders
    shader.parmTuple('diff').set(color[i])

    # correct nodes positions 
    shader.moveToGoodPosition()

    # assign colors to faces
    material.parm('group%s' % (i+1)).set(str(i))
    material.parm('shop_materialpath%s' % (i+1)).set(material.relativePathTo(shader))
 

 

oh man... many thanks for your sharing !!! that is a beautiful code for learning purpose a python in houdini. Thanks of lot ! (^_^)

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