1
$\begingroup$

I am trying to create interface for this code (look bottom) in Blender.

Python code helps find dominant colors in image. It would be great if color data from program send to colorpicker elements on panel (Blender interface). But I couldn't find information about colorpicker elements and how send data to it. I am using sys.path.append() to import Pillow (PIL for Pithon 3.X), NumPy and SciPy from Python 3.3 dir.

import struct
from PIL import Image
import scipy
import codecs
import scipy.misc
import scipy.cluster

NUM_CLUSTERS = 5
print ('reading image')
im = Image.open("C:\\Users\\User\\Desktop\\maxresdefault.jpg") #path to your pics
im = im.resize((150, 150))      # resize pics, to reduce time
ar = scipy.misc.fromimage(im)
shape = ar.shape
ar = ar.reshape(scipy.product(shape[:2]), shape[2])

print ('finding clusters')
codes, dist = scipy.cluster.vq.kmeans(ar, NUM_CLUSTERS)
print ('cluster centres:\n', (codes))

vecs, dist = scipy.cluster.vq.vq(ar, codes)         # assign codes
counts, bins = scipy.histogram(vecs, len(codes))    # count occurrences

index_max = scipy.argmax(counts)                    # find most frequent
peak = codes[index_max]
colour = codecs.encode(peak, 'hex_codec').decode('ascii')
print ('most frequent is %s (#%s)' % (peak, colour))

Summing up I want to ask you:

  1. How to create 5 or more colorpicker elements on panel?
  2. How to send data from program to colorpickers? In hex for example...

I would be grateful for any help!


Thank for help! I have one more question: How create button to execute python code

#Analogue colors
m = bpy.context.scene.color_prop.h + 0.125
n = m - int (m)
bpy.context.scene.color_prop1.h = n

m = bpy.context.scene.color_prop1.h + 0.125
n = m - int (m)
bpy.context.scene.color_prop2.h = n

m = bpy.context.scene.color_prop2.h + 0.125
n = m - int (m)
bpy.context.scene.color_prop3.h = n

m = bpy.context.scene.color_prop3.h + 0.125
n = m - int (m)
bpy.context.scene.color_prop4.h = n
$\endgroup$
2
$\begingroup$
import bpy

class ColorItem(bpy.types.PropertyGroup):
    color = bpy.props.FloatVectorProperty(
                 name = "Color Picker",
                 subtype = "COLOR",
                 size = 4,
                 min = 0.0,
                 max = 1.0,
                 default = (0.75,0.0,0.8,1.0)
                 )

class ColorPanel(bpy.types.Panel):
    """Creates a Panel in the Scene properties window"""
    bl_label = "Colors"
    bl_idname = "SCENE_PT_colors"
    bl_space_type = 'PROPERTIES'
    bl_region_type = 'WINDOW'
    bl_context = "scene"

    def draw(self, context):
        for i in range(5):
            row = self.layout.row()
            row.prop(context.scene.colors[i], "color", text=str(i))

def register():
    bpy.utils.register_class(ColorPanel)
    bpy.utils.register_class(ColorItem)
    bpy.types.Scene.colors = \
        bpy.props.CollectionProperty(type=ColorItem)

    for i in range(5):   
        bpy.context.scene.colors.add()

def unregister():
    bpy.utils.unregister_class(ColorPanel)
    bpy.utils.unregister_class(ColorItem)
    del bpy.types.Scene.colors

if __name__ == "__main__":
    register()

Adapted the SimplePanel Template to show 5 color pickers. Access and set their values via

bpy.context.scene.colors[index].color = (0.0, 0.0, 0.0, 1.0)


Additional infos:

Here is the link to the official api documentation. In the section quickstart introduction you will find an example of an operator.

You can find a template in the text editor -> Templates -> Python -> SimpleOperator. templates

In your case you simply wrap your code in a function which will be called from the operators execute method:

def main(context):
    context.scene.color_prop.h += 0.125
    context.scene.color_prop.h %= 1.0
    #...

Define an operator (see the SimpleOperator template), and extend the panel's draw method:

def draw(self, context):
    #...
    row = self.layout.row()
    row.operator("object.simple_operator")

Here is a full listing copy & pasted together.

These sections #1, #2 from the docs might be helpful.

$\endgroup$
  • $\begingroup$ You are my hero! Thank you very much ! It was very helpful. $\endgroup$ – Natasha May 25 '14 at 8:08

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.