Python Script: Update chf(“parm”) values (default, min, max) from VEX comment

Inspired by great Matt Estela. Now I can comment my VEX like this:

@pscale = chf("pscale"); // 0.2 in -1 to 1

The script reads the comment, and sets these values to the pscale parm:

– default value = 0.2
– slider range min = -1
– slider range max = 1

I will update the script to be a tool with hotkey. But it works even like this:

import re
#parm = kwargs['parms'][0]
for nodes in hou.selectedNodes():
    node = nodes
# I will update the script to be a tool, with hotkey. But it works already like this:

parm = node.parm("snippet")
snippet = parm.unexpandedString()

#

lines = snippet.split('\n')

for line in lines:
    chf = re.findall('chf.+$',line)         #chf = chf("pscale"); // 0.6 in 0.5 to 1.0
    if chf:
        chf = chf[0]                        
        name,range = chf.split('//')        #name = chf("pscale");      range =  5 to 10
        default, minmax = range.split('in')
        min,max = minmax.split('to')
        #
        name = re.findall("chf\(\"(\w+)\"", line)[0]
        default = float(default.strip())
        default = ([default])               #make tuple from float
        min = float(min.strip())
        max = float(max.strip())
        #
        ptg = node.parmTemplateGroup()
        parmedit = ptg.find( name )
        parmedit.setDefaultValue(  default  )
        parmedit.setMinValue(  min  )
        parmedit.setMaxValue(  max  )
        ptg.replace( name, parmedit )
        node.setParmTemplateGroup( ptg )

 

Leave a Reply

Your email address will not be published. Required fields are marked *