Skip to content

Instantly share code, notes, and snippets.

@fillmember
Created September 19, 2016 20:09
Show Gist options
  • Save fillmember/432ff9be9422a9bb7ec8798b30867385 to your computer and use it in GitHub Desktop.
Save fillmember/432ff9be9422a9bb7ec8798b30867385 to your computer and use it in GitHub Desktop.
gulp task for export animation data in dae to a JSON file
gulp = require 'gulp'
yargs = require 'yargs'
chalk = require 'chalk'
gulp.task 'dae' , (cb) ->
# Arguments
argv = yargs
#
.alias 'input' , 'i'
.default 'input' , 'example.dae'
#
.alias 'output' , 'o'
.default 'output' , 'output/'
#
.boolean 'animation'
.default 'animation' , true
#
.boolean 'scene'
.default 'scene' , true
#
.alias 'pretty' , 'p'
.boolean 'pretty'
.default 'pretty' , true
#
.argv
input = argv.input
output = argv.output
console.log chalk.blue '// dae conversion process start //'
console.log chalk.underline('input file') + ' : ' + input
console.log chalk.underline('output to ') + ' : ' + output
# process content for scene.js
if argv.scene
console.log chalk.bold '[ generate scene.js ]'
spawn = require('child_process').spawn
args = [ 'python/convert2threejs.py' , input , output+'scene.js' ]
if argv.pretty then args.push '-p'
pyp = spawn 'python' , args
pyp.stdout.on 'data' , (data) -> process.stdout.write chalk.gray data
pyp.stderr.on 'data' , (data) -> process.stdout.write "#{chalk.red(stderr)}: #{data}"
pyp.on 'close' , (code, signal) ->
if code is 0
console.log chalk.green "child process exited with code #{code}"
if taskFinish() then allDone()
# process content for animation.js
if argv.animation
console.log chalk.bold '[ generate animation.js ]'
DAEProcess.parse input , (err,result,$) ->
content = JSON.stringify { animation:result }
fs = require 'fs'
_opath = output+'animation.js'
fs.writeFile _opath , content , (err) ->
if err then console.log "fs error: #{err}"
console.log chalk.green('[ animation data expoted to:'),_opath,chalk.green(']')
if taskFinish() then allDone()
# task & task finish counter
tasks = 0
count = 0
taskFinish = ->++count is tasks
tasks += 1 if argv.scene
tasks += 1 if argv.animation
allDone = ->
console.log chalk.blue '// dae conversion process complete //'
cb()
class DAEProcess
@parse = (path, callback = ->) ->
fs = require 'fs'
fs.readFile path , 'utf-8' , (err, data) ->
if err
callback err , null
throw err
cheerio = require 'cheerio'
$ = cheerio.load data , xmlMode : true
result = []
$('library_animations animation channel')
.each (index) ->
$this = $(this)
# Get Target Name
[ targetID , track ] = $this.attr('target').split('/')
targetName = $("library_visual_scenes node[id=#{targetID}]").attr('name')
# Get Keyframes
$sampler = $this.siblings('sampler')
inputID = $sampler.find('input[semantic=INPUT]').attr('source')
outputID = $sampler.find('input[semantic=OUTPUT]').attr('source')
input = $(inputID).children('float_array').html().split(' ')
output = $(outputID).children('float_array').html().split(' ')
animation =
obj : targetName
trk : DAEProcess.getTrackName track
in : DAEProcess.parseTrack input
out : DAEProcess.parseTrack output
result.push animation
callback null , result , $
@parseTrack = (input, precision=1000) ->
input.map (x) -> Math.round( parseFloat(x) * precision ) / precision
@getTrackName = (input) ->
dictionary =
# Position
'translate.X' : 'px'
'translate.Y' : 'py'
'translate.Z' : 'pz'
# Scale
'scale.X' : 'sx'
'scale.Y' : 'sy'
'scale.Z' : 'sz'
# Rotation (Angle)
'rotateX.ANGLE' : 'rx'
'rotateY.ANGLE' : 'ry'
'rotateZ.ANGLE' : 'rz'
dictionary[input]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment