Skip to content

Instantly share code, notes, and snippets.

@KelSolaar
Created July 9, 2018 10:23
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save KelSolaar/3b0c3810b680eef4a2dea59dc348fa67 to your computer and use it in GitHub Desktop.
Save KelSolaar/3b0c3810b680eef4a2dea59dc348fa67 to your computer and use it in GitHub Desktop.
Colour - Blender
import bpy
from colour.models import RGB_COLOURSPACES, RGB_to_XYZ, \
XYZ_to_colourspace_model
def RGB_identity_cube(subdivisions=32):
bpy.ops.mesh.primitive_cube_add(location=(0.5, 0.5, 0.5))
bpy.ops.transform.resize(value=(0.5, 0.5, 0.5))
bpy.ops.object.transform_apply(location=True, scale=True)
object_ = bpy.context.active_object
mesh = bpy.context.active_object.data
vertex_colours = mesh.vertex_colors.new()
i = 0
for polygon in mesh.polygons:
for loop_index in polygon.loop_indices:
vertex_index = mesh.loops[loop_index].vertex_index
vertex_colours.data[i].color = mesh.vertices[vertex_index].co
i += 1
mesh.vertex_colors.active = vertex_colours
bpy.ops.object.mode_set(mode='EDIT')
bpy.ops.mesh.subdivide(number_cuts=subdivisions)
bpy.ops.object.mode_set(mode='OBJECT')
bpy.ops.object.convert(target='MESH')
bpy.context.scene.objects.active.name = 'RGB_identity_cube'
return bpy.context.scene.objects.active
def RGB_colourspaces_gamuts_plot(colourspaces=None,
reference_colourspace='CIE xyY'):
for i, colourspace in enumerate(colourspaces):
colourspace = RGB_COLOURSPACES[colourspace]
cube = RGB_identity_cube()
RGB = [vertex.co for vertex in cube.data.vertices]
XYZ = RGB_to_XYZ(RGB, colourspace.whitepoint, colourspace.whitepoint,
colourspace.RGB_to_XYZ_matrix)
ijk = XYZ_to_colourspace_model(XYZ, colourspace.whitepoint,
reference_colourspace)
for i, vertex in enumerate(cube.data.vertices):
vertex.co = ijk[i]
bpy.context.scene.objects.active.name = '{0}_{1}'.format(colourspace.name, reference_colourspace.replace(' ', '_'))
RGB_colourspaces_gamuts_plot(['sRGB'])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment