Never been to TextSnippets before?

Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world (or not, you can keep them private!)

« Newer Snippets
Older Snippets »
3 total  XML / RSS feed 

Ruby: Submit Apple Shake Scripts for Batch Render

A quick-n-dirty Ruby script that takes all the Shake scripts in a given folder, checks to make sure they are all set on maximum quality e.g "Base" and then submits them for render.

#! /usr/bin/env ruby

require 'pp'

class Fixnum

def pad(n)
  
  x = self.to_s

  if x.length < n
    pad = "0"*(n - x.length)
    x.insert(0,pad)
  else
    x
  end

end

end

def render(filepath,last_frame)

IO.popen("shake -v xml -exec #{filepath} -t 1-#{last_frame}x1", 'r+')

end

def get_last_frame(file_path)
  
  last_frame = File.open(file_path).grep(/SetTimeRange/).to_s.scan(/\d{2,6}/).first.to_i

end

def get_output_file(file_path)

  out_file = File.open(file_path).grep(/FileOut/).first.gsub(/\"/,"").split(",")[1].lstrip

end

def get_quality(file_path)

  quality = File.open(file_path).grep(/SetUseProxy/).first.match(/\".+\"/).to_a.first

end

# Get location of scripts

$renderQ = {}

puts "Where are the scripts?"
$TARGET = gets.rstrip
$TARGET << "/" if $TARGET !~ /\/$/

$script_list = %x| ls #{$TARGET}*.shk |.split("\n")   # Only files with *.shk extension

# Replace whitespace (if present) in filenames with underscores

$script_list.map! do |f|
    new_name = f.gsub(/\s/,'_')
    unless f == new_name
      puts "Renaming #{f} --> #{new_name}"
      File.rename(f,new_name)
    end
  new_name
end

# Make sure all renders are full-resolution

$script_list.each do |f|

  file,path,filename = File.open(f), File.dirname(f), File.basename(f).split(/\./)

  unless file.read.grep(/SetUseProxy/).first =~ /Base/
    
    new_file = File.open(f).read.gsub!(/^SetUseProxy.+;$/,'SetUseProxy("Base");')
    
    File.open(f,'w') { |f| f.puts new_file }

  end

end

# Get render info on scripts

$script_list.each do |f|
  outfile = get_output_file(f)
  last_frame = get_last_frame(f)
  quality = get_quality(f)
  $renderQ[f] = [last_frame,quality,outfile]
end

# Print report

puts "\n---------| RENDER SCRIPT -- FRAME COUNT -- OUTPUT FILE |---------\n"

$renderQ.sort.each { |k,v| puts "#{k} -- #{v.first} --> #{v.last} | #{v[1]}" }

puts "---------------------------------------------------"

puts "\nPress Enter to continue with render, or force exit."

gets

# Render

$renderQ.each { |k,v| render(k,v.first) }

rendering layout from different controller

If an action in one controller leads to a deadly similar template as in another controller, you can just borrow that controller's action template by calling render at the end of your action.
render :template => ''

Say your in tags controller in the show action. You can use the template found in /apps/views/lists/index.rhtml
def show
  [boring action logic]
  render :template => 'lists/index'
end

default layout for a controller

// description of your code here
In your controller add the name of the .rhtml file in /apps/layouts/ that you want to use, without the suffix (.rhtml).
When rails spits out your view, it will use this given layout as the container for your rendered action.
layout ""
« Newer Snippets
Older Snippets »
3 total  XML / RSS feed