Skip to content

Commit

Permalink
color can now be choosen with a color picker
Browse files Browse the repository at this point in the history
  • Loading branch information
Ocupe committed Jul 30, 2019
1 parent b3cf922 commit ba14f74
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 23 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
__pycache__
docs/
docs/
.vscode/
5 changes: 3 additions & 2 deletions helper.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,14 @@
ADDON_ID = 'protor_{}'


def random_color():
def random_color(alpha=False):
""" Return a high contrast random color. """
h = random()
s = 1
v = 1
rgb = list(colorsys.hsv_to_rgb(h, s, v))
rgb.append(1)
if alpha:
rgb.append(1)
return rgb


Expand Down
27 changes: 19 additions & 8 deletions projector.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,17 @@
]


def change_texture_color(context, projector):
projector.children[0].data.node_tree.nodes['Checker Texture'].inputs['Color2'].default_value = random_color()
def on_color_change(proj_settings, context):
c = proj_settings.projected_color
change_texture_color([c.r, c.g, c.b, 1], context.object)


class PROJECTOR_OT_change_color(Operator):
def change_texture_color(rgba, projector):
""" Update the color on the spotlight nodetree. """
projector.children[0].data.node_tree.nodes['Checker Texture'].inputs['Color2'].default_value = rgba


class PROJECTOR_OT_change_color_randomly(Operator):
""" Randomly change the color of the projected texture."""
bl_idname = 'projector.change_color'
bl_label = 'Change color of projection texture'
Expand All @@ -46,8 +52,10 @@ def poll(cls, context):

def execute(self, context):
projectors = get_projectors(context, only_selected=True)
new_color = random_color(alpha=True)
for projector in projectors:
change_texture_color(context, projector)
projector.proj_settings['projected_color'] = new_color[:-1]
change_texture_color(new_color, projector)
return {'FINISHED'}


Expand Down Expand Up @@ -127,7 +135,7 @@ def add_projector_node_tree_to_spot(spot):

# b) Generated checker texture.
checker_tex = nodes.new('ShaderNodeTexChecker')
checker_tex.inputs['Color2'].default_value = random_color()
checker_tex.inputs['Color2'].default_value = random_color(alpha=True)
checker_tex.inputs[3].default_value = 8
checker_tex.location = auto_pos(y=-300)

Expand Down Expand Up @@ -279,6 +287,7 @@ def create_projector(context):
cam.proj_settings.use_img_texture = False
cam.proj_settings.h_shift = 0.0
cam.proj_settings.v_shift = 0.0
# cam.proj_settings.resolution = resolutions[]

update_throw_ratio(cam)

Expand All @@ -300,7 +309,7 @@ class PROJECTOR_OT_create(Operator):

def execute(self, context):
projector = create_projector(context)
update_projector(projector, context)
update_projector(projector.proj_settings, context)
return {'FINISHED'}


Expand Down Expand Up @@ -351,20 +360,22 @@ class ProjectorSettings(bpy.types.PropertyGroup):
name="Horizontal Shift", min=-5, max=5, update=update_projector)
v_shift: bpy.props.FloatProperty(
name="Vertical Shift", min=-5, max=5, update=update_projector)
projected_color: bpy.props.FloatVectorProperty(
subtype='COLOR', update=on_color_change)


def register():
bpy.utils.register_class(ProjectorSettings)
bpy.utils.register_class(PROJECTOR_OT_create)
bpy.utils.register_class(PROJECTOR_OT_delete)
bpy.utils.register_class(PROJECTOR_OT_change_color)
bpy.utils.register_class(PROJECTOR_OT_change_color_randomly)

bpy.types.Object.proj_settings = bpy.props.PointerProperty(
type=ProjectorSettings, update=update_projector)


def unregister():
bpy.utils.unregister_class(PROJECTOR_OT_change_color)
bpy.utils.unregister_class(PROJECTOR_OT_change_color_randomly)
bpy.utils.unregister_class(PROJECTOR_OT_delete)
bpy.utils.unregister_class(PROJECTOR_OT_create)
bpy.utils.unregister_class(ProjectorSettings)
50 changes: 38 additions & 12 deletions ui.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,26 +20,29 @@ class PROJECTOR_PT_panel(Panel):
bl_category = "Projector"

def draw(self, context):
layout = self.layout
layout.use_property_split = True
layout.use_property_decorate = False

self.layout.use_property_split = True

row = self.layout.row(align=True)
row = layout.row(align=True)
row.operator('projector.create',
icon='ADD', text="New")
row.operator('projector.delete',
text='Remove', icon='REMOVE')

if context.scene.render.engine == 'BLENDER_EEVEE':
box = self.layout.box()
box = layout.box()
box.label(text='Image Projection only works in Cycles.', icon='ERROR')
box.operator('projector.switch_to_cycles')

selected_projectors = get_projectors(context, only_selected=True)
if len(selected_projectors) == 1:
projector = selected_projectors[0]

self.layout.label(text='Projector Settings:')
box = self.layout.box()
layout.separator()

layout.label(text='Projector Settings:')
box = layout.box()
box.prop(projector.proj_settings, 'throw_ratio')
box.prop(projector.proj_settings, 'power', text='Power')
box.prop(projector.proj_settings, 'resolution',
Expand All @@ -50,11 +53,32 @@ def draw(self, context):
'h_shift', text='Horizontal Shift')
col.prop(projector.proj_settings, 'v_shift', text='Vertical Shift')
# Projected Texture
box.prop(projector.proj_settings, 'use_img_texture',
text='Use Image Texture')
if not projector.proj_settings.use_img_texture:
box.operator('projector.change_color',
icon='MODIFIER_ON', text='Random Color')
layout.prop(projector.proj_settings, 'use_img_texture',
text='Use Image Texture')


class PROJECTOR_PT_color(Panel):
bl_label = "Projected Color"
bl_parent_id = "OBJECT_PT_projector_n_panel"
bl_option = {'DEFAULT_CLOSED'}
bl_space_type = 'VIEW_3D'
bl_region_type = 'UI'

@classmethod
def poll(self, context):
""" Only draw menu when no image texture is used. """
projector = context.object
return bool(get_projectors(context, only_selected=True)) and not projector.proj_settings.use_img_texture

def draw(self, context):
projector = context.object
layout = self.layout
layout.use_property_decorate = False
col = layout.column()
col.use_property_split = True
col.prop(projector.proj_settings, 'projected_color', text='Color')
col.operator('projector.change_color',
icon='MODIFIER_ON', text='Random Color')


def add_to_blender_add_menu(self, context):
Expand All @@ -64,11 +88,13 @@ def add_to_blender_add_menu(self, context):

def register():
bpy.utils.register_class(PROJECTOR_PT_panel)
bpy.utils.register_class(PROJECTOR_PT_color)
# Register create in the blender add menu.
bpy.types.VIEW3D_MT_light_add.append(add_to_blender_add_menu)


def unregister():
# Register create in the blender add menu.
# Register create in the blender add menu.
bpy.types.VIEW3D_MT_light_add.remove(add_to_blender_add_menu)
bpy.utils.unregister_class(PROJECTOR_PT_color)
bpy.utils.unregister_class(PROJECTOR_PT_panel)

0 comments on commit ba14f74

Please sign in to comment.