[PYTHON] How to implement a gradient picker in Houdini

Introduction

I'll show you how to implement a gradient picker like Substance Designer in Houdini that Ando announced at the PySide Study Group the other day. Introduction of Ando's gradation picker, commentary article

When actually used, the gradation picked up by the picker is applied to the lamp parameters as shown in the figure below. In the figure below, the image is loaded into the network editor, but you can get the color anywhere on the screen.

Implementation method

  1. Download the code from the URL below. https://snippets.cacher.io/snippet/2c7496d1dbbdc093033c
  2. Move the downloaded ** gradationPicker.py ** to a folder where Houdini can read Python. If you don't know, copy it to % HOMEDRIVE %% HOMEPATH% \ Documents \ houdini version \ scripts \ python and it will be recognized automatically (create it if it doesn't exist).
  3. From here, there is a slight difference between implementing in HDA and implementing in the parameters of an existing node.

-** When implementing on HDA ** Go to the HDA ** Type Properties **> ** Scripts tab ** and change the ** Event Handler ** to ** Python Module **. This will activate the Python code field on the right, where you can enter the code __ to apply the gradient to the __ramp parameters below.

-** When implementing in the parameter of the existing node ** Click ** Windows **> ** Python Source Editor ** in the Houdini menu to launch the Python Source Editor and enter the code ** to apply the gradient to the ** ramp parameters below in the code entry field.

Code that applies a gradient to the ramp parameters

# -*- coding: utf-8 -*-

import hou
from PySide2.QtCore import Qt

from gradationPicker import ColorPick

class _GCProtector(object):
    widgets = []

def rampColorPicker(parm, threshold=0.05):
    def setRampColor(colors):
        #The argument colors are[Gradient position,color]List containing
        curveKeys = []
        curveValues = []
        #Divide the two-dimensional list into two parts, curveKeys and curveValues.
        for pos, color in colors:
            curveKeys.append(pos)
            #2 because the color space of PySide is sRGB and Houdini is linear.Square and convert
            curveValues.append([(ch / 255.0) ** 2.2 for ch in color])
        linears = [hou.rampBasis.Linear] * len(colors)
        ramp = hou.Ramp(linears, curveKeys, curveValues)
        parm.set(ramp)

    picker = ColorPick()
    #Displayed in the foreground so that the focus does not shift
    picker.setWindowFlags(Qt.WindowStaysOnTopHint)
    #Set a threshold for how much color difference is identified
    picker.threshold = threshold
    #Make the picker's main window fill the screen
    picker.showFullScreen()
    #Execute a function when the mouse is released
    picker.getGradation.connect(setRampColor)
    #Processing to avoid freeing memory and disappearing immediately when displayed normally
    _GCProtector.widgets.append(picker)
  1. Finally, create a button to execute this code.
  2. Enter the following code in the Callback Script field of the created button.

-** For HDA ** hou.phm().rampColorPicker(kwargs['node'].parm('ramp'), threshold=0.05)

-** For nodes that already exist ** hou.session.rampColorPicker(kwargs['node'].parm('ramp'), threshold=0.05)

If you want to set an icon for a button, enter BUTTONS_secondary_colors in the Button Icon field and an icon like that will be added.

Now when you press the button, you'll be in gradient picker mode, so drag and pick a color.

Finally

That's how to implement a gradient picker in Houdini. PySide can do many things and is fun. By the way, by modifying the gradation picker, you can also create a function to write the curve of the lamp parameter by hand as shown below.

Recommended Posts

How to implement a gradient picker in Houdini
How to temporarily implement a progress bar in a scripting language
How to get a stacktrace in python
How to implement Scroll View in pythonista 1
How to clear tuples in a list (Python)
How to implement Rails helper-like functionality in Django
How to embed a variable in a python string
How to implement Discord Slash Command in Python
How to implement shared memory in Python (mmap.mmap)
How to create a JSON file in Python
How to notify a Discord channel in Python
How to Implement a new CPUFreq Processor Driver
[Python] How to draw a histogram in Matplotlib
How to create a Rest Api in Django
How to write a named tuple document in 2020
How to count numbers in a specific range
How to read a file in a different directory
How to Mock a Public function in Pytest
How to specify a schema in Django's database settings
How to convert / restore a string with [] in python
I want to easily implement a timeout in python
[Python] How to expand variables in a character string
A memorandum on how to use keras.preprocessing.image in Keras
How to display DataFrame as a table in Markdown
How to execute a command using subprocess in Python
How to reference static files in a Django project
[Linux] How to put your IP in a variable
How to call a function
How to hack a terminal
How to develop in Python
I tried to implement a one-dimensional cellular automaton in Python
How to slice a block multiple array from a multiple array in Python
How to import NoteBook as a module in Jupyter (IPython)
How to output a document in pdf format with Sphinx
A story about how to specify a relative path in python.
How to use the __call__ method in a Python class
How to import a file anywhere you like in Python
How to define multiple variables in a python for statement
A note on how to load a virtual environment in PyCharm
I tried "How to get a method decorated in Python"
How to develop in a virtual environment of Python [Memo]
How to generate a query using the IN operator in Django
How to check if a value exists in an enum
How to get the last (last) value in a list in Python
How to implement Python EXE for Windows in Docker container
How to get a list of built-in exceptions in python
How to import NoteBook as a module in Jupyter (IPython)
A story about trying to implement a private variable in Python.
How to get a quadratic array of squares in a spiral!
How to make a Japanese-English translation
How to write a Python class
[Python] How to do PCA in Python
How to handle session in SQLAlchemy
How to put a symbolic link
How to write soberly in pandas
Implement a date setter in Tkinter
How to use SQLite in Python
How to make a slack bot
How to create a Conda package
How to make a crawler --Advanced
How to make a recursive function