Created
May 5, 2014 13:07
-
-
Save motoishmz/f97dfedb7653e56bb4a8 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# [usage] | |
# ruby move.rb -i {input_obj_dir} -x {x_move_amount} -y {y_move_amount} -z {z-move_amount} | |
# ruby move.rb -i {input_obj_dir} -s {scale} | |
# --> 全frameの全キーポイントを、 {x, y, z}分移動 | |
# | |
# note: translateとscaleを同時に渡すと、移動 >> scalingの順に実行される | |
# see also: 110行目あたり | |
require 'optparse' | |
src = "" | |
dst = "" | |
x = 0.0 | |
y = 0.0 | |
z = 0.0 | |
scale = 1.0 | |
OptionParser.new {|opt| | |
opt.on('-i VAL') {|v| src = v } | |
opt.on('-x VAL') {|v| x = v.to_f } | |
opt.on('-y VAL') {|v| y = v.to_f } | |
opt.on('-z VAL') {|v| z = v.to_f } | |
opt.on('-s VAL') {|v| scale = v.to_f } | |
opt.parse!(ARGV) | |
} | |
def delete_dir(path) | |
dirlist = Dir::glob(path + "**/").sort { | |
|a,b| b.split('/').size <=> a.split('/').size | |
} | |
dirlist.each {|d| | |
Dir::foreach(d) {|f| | |
File::delete(d+f) if ! (/\.+$/ =~ f) | |
} | |
Dir::rmdir(d) | |
} | |
end | |
if (src == "") | |
p "no source input. aborted" | |
p "usage is written on the top of move.rb" | |
exit | |
end | |
if (x == 0.0 && y == 0.0 && z == 0.0 && scale == 0.0) | |
p "no information for moving || scaling. aborted" | |
p "usage is written on the top of move.rb" | |
exit | |
end | |
dst = src + "_moved" | |
delete_dir(dst) if (File.exist?(dst)) | |
Dir::mkdir(dst, 0700) | |
# ------- | |
# .obj example | |
=begin | |
# Model file name: xxx | |
# Movie file name: xxx | |
# Frame: 0 | |
# TotalFrame: 442 | |
g model | |
v 1000, 71, 0 | |
=end | |
src_aps_path = File.expand_path(src) | |
frames = Dir::entries(src_aps_path) | |
frames.each{|frame| | |
cur_file_path = src+"/"+frame | |
new_file_path = dst+"/"+frame | |
next unless File.extname(cur_file_path) =~ /.obj/ | |
new_file = open(new_file_path, "w") | |
open(cur_file_path) { |file| | |
while line = file.gets | |
if (line =~ /^v/) | |
# removing prefix, surfix | |
line[0..1] = "" # removing "v " | |
line.gsub!("\n", "") | |
# x, y, z | |
vertex = line.split(", ") | |
vertex[0] = (vertex[0].to_f + x) * scale | |
vertex[1] = (vertex[1].to_f + y) * scale | |
vertex[2] = (vertex[2].to_f + z) * scale | |
new_file.puts "v " + vertex.join(", ") | |
else | |
# そのままputs | |
new_file.puts line | |
end | |
end | |
} | |
new_file.close() | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment