Jump to content

Python-Returning currently selected face and then growing the selectio


Tyfx567

Recommended Posts

Hey guys, trying to create a variable for the currently selected face that is selected in the viewport then I want to grow the selection and use that selection pattern into a group. I know how to do basic method ideas in python but not so much classes. Any help will be greatly appreciated! And if you would please describe so I can learn more about python. 

 

Edited by Tyfx567
Link to comment
Share on other sites

Update: I have it partially working sort of where I say 

import toolutils

viewer = toolutiles.sceneViewer()

hou.GeometrySelection.growSelection( viewer.selectGeometry("selectedges"))

This works but only works with edges and not prims which is super weird. Any help?

 

Another update: 

Slowly working it out. 

 

here is what I have so far to select pieces and put the name into a string

import toolutils
import hou

viewer = toolutils.sceneViewer()

viewer.setGroupPicking(1)
viewer.setGroupListMask("@name *")

current = viewer.currentNode()

label = viewer.selectGeoemtry("doesn't matter")

string = hou.GeometrySelection.selectionStrings( label )
print string
Edited by Tyfx567
Link to comment
Share on other sites

I actually have figured out 99% of my new tool. I do have a question though, I have a python script that was done in the python source editor and it works great. You can't set attribs using the python shell which is sad. But you can make a python sop and set the pythonNode.parm('python').set(''' whatever code you want to put into here to modify your geometry''') 

I need that code to use a variable I had made in the shell and I don't know how to do that. Any ideas?

 

 

Nevermind figured it out it's 

 

pythonNode.parm('python').set('''whatever code''' + str(variable) + '''finish code''')

Edited by Tyfx567
Link to comment
Share on other sites

Does anyone know how to escape characters in python successfully? I have a variable has to be inside quotes like 

fractureNum = int(fractureLast.strip(' '''str(word)''' ') #this fracture 0

But I'm trying to get the word (which is a variable of string piece) while it's in ' ' without spaces though. So still be a variable of piece in there but not have a space?

 

Thanks, 

Hope you guys know what I am talking about, it's super confusing

 

So what I am trying to do is this:

var = 5
emptyString = ''' blah blah blah  'var'  '''

return emptyString 

blah blah blah '5'

but it either gives me an error or it just does var or 'var' I need the variable to be in quotes because I'm putting it into a strip expression

 

Thanks guys!

Edited by Tyfx567
Link to comment
Share on other sites

Ok I got to finally work wow this took literally all day:

pythonNode.parm('python').set(
'''
fractureGeo = hou.pwd().geometry()
fracturePrims = hou.Geometry.prims(fractureGeo)
for i in fracturePrims:
   fractureLast = hou.Prim.stringAttribValue(i, 'name') #'piece7'
   fractureNum = int(fractureLast.lstrip('{piece}')) #this fracture 0
   newNum = fractureNum + {num} #added num
   newName = hou.Prim.setAttribValue(i, 'name', '{piece}'+str(newNum))
'''.format(piece = word, num = pieceNameNum)

)

Link to comment
Share on other sites

Ok guys, I just slaved for 3 days off and on making a cool script that will refracture your geometry piece based on a piece selection. It creates all the nodes needed to do the shatter, but the most important part is it renames the piece attribute, so say your pieces are originally from 'piece0-piece9'  the tool will take the last number which is 9 and add that number to the next fracture + 1 which would be 10 and up. So your new pieces would start from piece10-20 That way it doesn't mess up the sim. Also, it runs through all the prims on the original geo to get the 'piece' string. That way, if your original fracture pieces were named 'whatever' your new pieces would be named 'whatever10-whatever20'. Also you can do this continuously of course over and over. 

Hopefully I thought of everything here is the script. Idk if this is useful in production. Is it? 

#Selecting Geo
import hou
import toolutils
viewer = toolutils.sceneViewer()
viewer.setGroupPicking(1)
viewer.selectGeometry(geometry_types=(hou.geometryType.Primitives,))
viewer.setGroupListMask("@name *")
label = viewer.selectGeometry(geometry_types=(hou.geometryType.Primitives,),initial_selection=None, use_existing_selection=False,)
string = hou.GeometrySelection.selectionStrings( label )
#print string

#Creating nodes in python:


viewer = toolutils.sceneViewer()
currentnode = viewer.currentNode()
objectlevel = hou.SopNode.parent(currentnode)
blast = objectlevel.createNode('blast', 'deleteselection1')
blast.setInput(0, currentnode)
blast.moveToGoodPosition()
grouptype = blast.parm('grouptype').set('prims')
blastgroup = blast.parm('group').set(string[0])

blast2 = objectlevel.createNode('blast', 'deleteNonSelected1')
blast2.setInput(0, currentnode)
blast2.moveToGoodPosition()
blast2grouptype = blast2.parm('grouptype').set('prims')
blast2group = blast2.parm('group').set(string[0])
negate = blast2.parm('negate').set(1)

#Create isooffset

isooffset = objectlevel.createNode('isooffset', 'convertofog1')
isooffset.setInput(0, blast2)
isooffset.moveToGoodPosition()
isooffset.parm('samplediv').set(40)

#Create scatter

scatter = objectlevel.createNode('scatter', 'scatterpoints1')
scatter.setInput(0, isooffset)
scatter.moveToGoodPosition()
scatter.parm('npts').set(30)
scatter.parm('relaxpoints').set(0)

#Get piece or other word if specified
import string
all = string.maketrans('', '')
nodigs = all.translate(all, string.letters)
currentNodeGeo = currentnode.geometry()
currentNodePrims = hou.Geometry.prims(currentNodeGeo)
for i in currentNodePrims:
    pieceNameString = hou.Prim.stringAttribValue(i, 'name') #'piece7'
    word = pieceNameString.translate(all, nodigs) #'piece'
    pieceNameNum = int(pieceNameString.strip(word)) #7 old attrib
    
    
    
#Create fracture
fracture = objectlevel.createNode('voronoifracture', 'voronifracturenew')
fracture.setInput(0, blast2)
fracture.setInput(1, scatter)
fracturePos = fracture.moveToGoodPosition()
fracture.setPosition([fracturePos[0]-1, fracturePos[1]-1 ])
fracture.parm('nameprefix').set(word)

#Create python node
pythonNode = objectlevel.createNode('python', 'pythonEditGeo')
pythonNode.setInput(0, fracture)
pythonNode.moveToGoodPosition()


#Paste python code here

pythonNode.parm('python').set(
'''
fractureGeo = hou.pwd().geometry()
fracturePrims = hou.Geometry.prims(fractureGeo)
for i in fracturePrims:
   fractureLast = hou.Prim.stringAttribValue(i, 'name') #'piece7'
   fractureNum = int(fractureLast.lstrip('{piece}')) #this fracture 0
   newNum = fractureNum + {num}+1 #added num
   newName = hou.Prim.setAttribValue(i, 'name', '{piece}'+str(newNum))
'''.format(piece = word, num = pieceNameNum)

)


mergeNode = objectlevel.createNode('merge', 'merge1')
mergeNode.setInput(0, blast)
mergeNode.setInput(1, pythonNode)
mergeNode.moveToGoodPosition()
mergeNode.setDisplayFlag(1)

currentnode.setSelected(0)
mergeNode.setSelected(1)

#Create subnetwork for all this
subnet = objectlevel.collapseIntoSubnet([blast, blast2, isooffset, scatter, fracture, pythonNode, mergeNode], 'additional_fracture1')
subnet.moveToGoodPosition()
subnet.setSelected(1)
subnet.setDisplayFlag(1)
Edited by Tyfx567
Link to comment
Share on other sites

I have this python functions to grow\shrink current selection.  Works with houdini version >= 15.0

def grow_selection():
    sop = viewer.currentNode()
    if sop.type().category() == hou.sopNodeTypeCategory():
        geo = sop.geometry()  
        sel = geo.selection()
        sel = sel.freeze()
        sel.grow(geo)
        sop.setSelection(sel)


#----------------------------------------------------------------------------------------
        
def shrink_selection():
    sop = viewer.currentNode()
    if sop.type().category() == hou.sopNodeTypeCategory():
        geo = sop.geometry()  
        sel = geo.selection()
        sel = sel.freeze()
        sel.shrink(geo)
        sop.setSelection(sel)
Edited by Alexey Vanzhula
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...