Difference between revisions of "IC Python API:Color Picker"

From Reallusion Wiki!
Jump to: navigation, search
m (Hooking up the Color Picker)
m
 
(4 intermediate revisions by the same user not shown)
Line 1: Line 1:
 
{{TOC}}
 
{{TOC}}
{{Parent|IC_Python_API:RL_Python_Samples|RL Python Samples}}
+
{{Parent|IC_Python_API#Python_of_the_Month|Python of the Month}}
  
 
This article will go over the creation of a color picker and how we can leverage Qt in dealing with the color hex data.
 
This article will go over the creation of a color picker and how we can leverage Qt in dealing with the color hex data.
Line 19: Line 19:
 
[[File:Ic_python_api_color_picker_01.png|frame]]
 
[[File:Ic_python_api_color_picker_01.png|frame]]
  
For this article, we'll be reading in a Qt UI file for the sake of convenience. You can download '''Color_Picker.ui''' [[File:Color_Picker.ui | here]].
+
For this article, we'll be reading in a Qt UI file for the sake of convenience. You can download '''Color_Picker.ui''' [[Media:Color_Picker.ui | here]].
  
 
<syntaxhighlight lang="python">
 
<syntaxhighlight lang="python">
Line 55: Line 55:
 
</syntaxhighlight>
 
</syntaxhighlight>
  
Now when the color picker button is pressed a color request dialog window appears.
+
Now when the color picker button is pressed a color request dialog window appears and the button will change its background color according to the value that is picked with the hexadecimal representation.
 +
 
 +
{{Images3|image1=Ic_python_api_color_picker_03.png|image2=Ic_python_api_color_picker_04.png|image3=Ic_python_api_color_picker_05.png}}
  
 
== Everything Put Together ==
 
== Everything Put Together ==

Latest revision as of 00:12, 19 October 2020

Main article: Python of the Month.

This article will go over the creation of a color picker and how we can leverage Qt in dealing with the color hex data.

Required Modules

Besides the Reallusion Python API will need os to read from the system file directory and PySide2 to build the user interface.

import RLPy
import os
from PySide2 import *
from PySide2.shiboken2 import wrapInstance

Importing the Qt UI File

Ic python api color picker 01.png

For this article, we'll be reading in a Qt UI file for the sake of convenience. You can download Color_Picker.ui here.

window = RLPy.RUi.CreateRDialog()
window.SetWindowTitle("Color Picker")

dialog = wrapInstance(int(window.GetWindow()), QtWidgets.QDialog)
dialog.setFixedWidth(250)

ui = QtCore.QFile(os.path.dirname(__file__) + "/Color_Picker.ui")
ui.open(QtCore.QFile.ReadOnly)
widget = QtUiTools.QUiLoader().load(ui)
ui.close()

dialog.layout().addWidget(widget)

window.Show()

Hooking up the Color Picker

Ic python api color picker 02.png

The color picker button will also be our color preview simply by changing it's background style-sheet.

def pick_color():
    color = QtWidgets.QColorDialog().getColor()
    if color.isValid():
        widget.button.setStyleSheet("background-color: %s" % color.name())
        widget.hex.setText(color.name())


widget.button.clicked.connect(pick_color)

Now when the color picker button is pressed a color request dialog window appears and the button will change its background color according to the value that is picked with the hexadecimal representation.

  • Ic python api color picker 03.png
  • Ic python api color picker 04.png
  • Ic python api color picker 05.png

Everything Put Together

import RLPy
import os
from PySide2 import *
from PySide2.shiboken2 import wrapInstance

window = RLPy.RUi.CreateRDialog()
window.SetWindowTitle("Color Picker")

dialog = wrapInstance(int(window.GetWindow()), QtWidgets.QDialog)
dialog.setFixedWidth(250)

ui = QtCore.QFile(os.path.dirname(__file__) + "/Color_Picker.ui")
ui.open(QtCore.QFile.ReadOnly)
widget = QtUiTools.QUiLoader().load(ui)
ui.close()

dialog.layout().addWidget(widget)

window.Show()


def pick_color():
    color = QtWidgets.QColorDialog().getColor()
    if color.isValid():
        widget.button.setStyleSheet("background-color: %s" % color.name())
        widget.hex.setText(color.name())


widget.button.clicked.connect(pick_color)

APIs Used

You can research the following references for the APIs deployed in this code.