lib/import/flame_stabilizer.rb in tracksperanto-2.6.0 vs lib/import/flame_stabilizer.rb in tracksperanto-2.6.1

- old
+ new

@@ -1,6 +1,5 @@ - class Tracksperanto::Import::FlameStabilizer < Tracksperanto::Import::Base # Flame setups contain clear size indications def self.autodetects_size? true @@ -12,67 +11,10 @@ def self.human_name "Flame .stabilizer file" end - class ChannelBlock < Array - include ::Tracksperanto::Casts - cast_to_string :name - cast_to_float :base_value - - def <=>(o) - @name <=> o.name - end - - def initialize(io, channel_name) - @name = channel_name.strip - - base_value_matcher = /Value ([\-\d\.]+)/ - keyframe_count_matcher = /Size (\d+)/ - indent = nil - - while line = io.gets - - unless indent - indent = line.scan(/^(\s+)/)[1] - end_mark = "#{indent}End" - end - - if line =~ keyframe_count_matcher - $1.to_i.times { push(extract_key_from(io)) } - elsif line =~ base_value_matcher && empty? - self.base_value = $1 - elsif line.strip == end_mark - break - end - end - raise "Parsed a channel #{@name} with no keyframes" if (empty? && !base_value) - end - - def extract_key_from(io) - frame = nil - frame_matcher = /Frame ([\-\d\.]+)/ - value_matcher = /Value ([\-\d\.]+)/ - - until io.eof? - line = io.gets - if line =~ frame_matcher - frame = $1.to_i - elsif line =~ value_matcher - return [frame, $1.to_f] - end - end - - raise "Did not detect any keyframes!" - end - - # Hack - prevents the channel to be flattened into keyframes - # when it gets Array#flatten'ed - def to_ary; end - private :to_ary - end - def each report_progress("Extracting setup size") self.width, self.height = extract_width_and_height_from_stream(@io) report_progress("Extracting all animation channels") channels, names = extract_channels_from_stream(@io) @@ -104,22 +46,12 @@ end end def extract_channels_from_stream(io) - channels = Tracksperanto::Accumulator.new - names = [] - channel_matcher = /Channel (.+)\n/ - until io.eof? - line = io.gets - if line =~ channel_matcher && channel_is_useful?(line) - report_progress("Extracting channel #{$1}") - channels << ChannelBlock.new(io, $1) - names << $1 - end - end - [channels, names] + channels = FlameChannelParser.parse(io) + [channels, channels.map{|c| c.path }] end USEFUL_CHANNELS = %w( /shift/x /shift/y /ref/x /ref/y ).map(&Regexp.method(:new)) # This method tells the importer whether a channel that has been found in the source @@ -132,21 +64,24 @@ end REF_CHANNEL = "ref" # or "track" - sometimes works sometimes don't def scavenge_trackers_from_channels(channels, names) - - channels.each do |c| next unless c.name =~ /\/#{REF_CHANNEL}\/x/ report_progress("Detected reference channel #{c.name}") - yield grab_tracker(channels, c, names) + t = grab_tracker(channels, c, names) + yield(t) if t end end + def channel_to_frames_and_values(chan) + chan.map{|key| [key.frame, key.value]} + end + def grab_tracker(channels, track_x, names) t = Tracksperanto::Tracker.new(:name => track_x.name.split('/').shift) report_progress("Extracting tracker #{t.name}") @@ -158,13 +93,16 @@ track_y = channels[ref_idx] shift_x = channels[shift_x_idx] shift_y = channels[shift_y_idx] - shift_tuples = zip_curve_tuples(shift_x, shift_y) - track_tuples = zip_curve_tuples(track_x, track_y) + shift_tuples = zip_curve_tuples(channel_to_frames_and_values(shift_x), channel_to_frames_and_values(shift_y)) + track_tuples = zip_curve_tuples(channel_to_frames_and_values(track_x), channel_to_frames_and_values(track_y)) + # If the channels are just empty go to next tracker + return if shift_tuples.empty? || track_tuples.empty? + report_progress("Detecting base value") base_x, base_y = find_base_x_and_y(track_tuples, shift_tuples) total_kf = 1 t.keyframes = shift_tuples.map do | (at, x, y) | @@ -183,43 +121,6 @@ base_track_tuple = track_tuples.find do | track_tuple | shift_tuples.find { |shift_tuple| shift_tuple[0] == track_tuple [0] } end || track_tuples[0] base_track_tuple[1..2] end -end - -__END__ - -Here's how a Flame channel looks like -The Size will not be present if there are no keyframes - -Channel tracker1/ref/x - Extrapolation constant - Value 770.41 - Size 4 - KeyVersion 1 - Key 0 - Frame 1 - Value 770.41 - Interpolation constant - End - Key 1 - Frame 44 - Value 858.177 - Interpolation constant - RightSlope 2.31503 - LeftSlope 2.31503 - End - Key 2 - Frame 74 - Value 939.407 - Interpolation constant - RightSlope 2.24201 - LeftSlope 2.24201 - End - Key 3 - Frame 115 - Value 1017.36 - Interpolation constant - End - Colour 50 50 50 - End +end \ No newline at end of file