Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save p2or/827267cc2949457f4ffe53ca107d5954 to your computer and use it in GitHub Desktop.
Save p2or/827267cc2949457f4ffe53ca107d5954 to your computer and use it in GitHub Desktop.
Import ascii data table for hue correct modifiers (sequencer) based on http://blender.stackexchange.com/a/64461 #Blender
# ##### BEGIN GPL LICENSE BLOCK #####
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software Foundation,
# Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
#
# ##### END GPL LICENSE BLOCK #####
import bpy
from bpy_extras.io_utils import ImportHelper
from bpy.props import StringProperty, EnumProperty
from bpy.types import Operator
from mathutils import Vector
bl_info = {
"name": "Import Ascii Hue Correct Curves",
"author": "p2or",
"version": (0, 1),
"blender": (2, 78, 0),
"location": "Sequencer",
"description": "Apply custom curve data to Hue Correct modifiers",
"warning": "",
"wiki_url": "",
"tracker_url": "",
"category": "Import-Export"
}
def read_ascii_table(filepath):
data = []
with open(filepath, "rt") as f:
for line in f:
if not line.startswith('#'):
x,y = line.strip().split()
data.append(Vector((float(x), float(y))))
return data
def apply_curve_data(curvemap, data):
if len(curvemap.points) > len(data): # remove waste
n = 0
while len(curvemap.points) > len(data):
curvemap.points.remove(curvemap.points[n])
n += 1
if len(curvemap.points) < len(data): # generate missing points
n = 0
while len(curvemap.points) < len(data):
curvemap.points.new(0.1, 0.5)
n +=1
# set values to default
for c, point in enumerate(curvemap.points):
point.location = data[c]
class ImportCurveDataOperator(Operator, ImportHelper):
bl_idname = "sequencer.import_hue_curve"
bl_label = "Import Ascii Hue Curve"
bl_options = {'REGISTER', 'UNDO'}
# ImportHelper mixin class uses this
filename_ext = ".txt"
filter_glob = bpy.props.StringProperty(
default="*.txt",
options={'HIDDEN'},
maxlen=255, # Max internal buffer length, longer would be clamped.
)
# List of operator properties, the attributes will be assigned
# to the class instance from the operator settings before calling.
curve = bpy.props.EnumProperty(
name="Apply Data to",
description="Apply Data to Hue, Saturation or Value",
items=(('HUE', "Hue", ""),
('SAT', "Saturation", ""),
('VAL', "Value", "")),
default='HUE',
)
def modifier_callback(self, context):
sel_strip = context.scene.sequence_editor.active_strip
hue_modifiers = [(str(c), item.name.title(), '') for
c, item in enumerate(sel_strip.modifiers) if
item.type == "HUE_CORRECT"]
return hue_modifiers
mod_list = EnumProperty(name="Modifier", items=modifier_callback)
@classmethod
def poll(cls, context):
spc = context.space_data
act_clip = context.scene.sequence_editor.active_strip
hue_mod = bool([m for m in act_clip.modifiers if m.type == "HUE_CORRECT"])
return (spc.type == 'SEQUENCE_EDITOR' and act_clip.select and hue_mod)
def draw(self, context):
row = self.layout
sel_strip = context.scene.sequence_editor.active_strip
row.prop(self, "curve")
if len([m for m in sel_strip.modifiers if m.type == "HUE_CORRECT"]) > 1:
row.prop(self, "mod_list")
def execute(self, context):
# get the active strip
sel_strip = context.scene.sequence_editor.active_strip
# tagging the area for redraw to update the curve edits
context.region.tag_redraw() #context.area.tag_redraw()
# read the file
try:
data = read_ascii_table(self.filepath)
except:
self.report({'ERROR'}, "Error reading the file")
return {'FINISHED'}
# check the data
if (len(data) < 2):
self.report({'ERROR'}, "2 points are required at least")
return {'FINISHED'}
else:
m = sel_strip.modifiers[int(self.mod_list)]
mapping = m.curve_mapping
h, s, v = mapping.curves
# apply the data
if self.curve == "HUE":
apply_curve_data(h, data)
if self.curve == "SAT":
apply_curve_data(s, data)
if self.curve == "VAL":
apply_curve_data(v, data)
m.curve_mapping.update()
# force viewer update (optional)
m.mute = not m.mute # toggle twice to respect the state
m.mute = not m.mute
self.report({'INFO'}, "Data applied to {} modifier".format(m.name))
return {'FINISHED'}
def register():
bpy.utils.register_class(ImportCurveDataOperator)
def unregister():
bpy.utils.unregister_class(ImportCurveDataOperator)
if __name__ == "__main__":
register()
0.0000000000 0.0000000000
0.2500000000 0.2500000000
0.5000000000 0.5000000000
0.7500000000 0.7500000000
1.0000000000 1.0000000000
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment