工作需要,在同事的帮助下写了一段代码用于批量转换bip文件。之前在网上查了很久都没找到相应的教程或者代码,把自己的代码发上来帮助有需要的人。
-- https://cganimator.com/uiaccessor-mini-tutorial-how-to-control-make-preview-dialog/
-- 获取bip动画的长度,控制fbx导出时候的长度
fn getLastKeyTime bipCtl = (
-- get subAnim controllers
-- maybe only three subAnim controller, i don't know
vertical_subAnim = bipCtl.vertical
horizontal_subAnim = bipCtl.horizontal
turning_subAnim = bipCtl.turning
last_key_index = vertical_subAnim.keys.count
if last_key_index != 0 then
(
key = vertical_subAnim.keys[last_key_index]
)
if horizontal_subAnim.keys.count > last_key_index then(
last_key_index = horizontal_subAnim.keys.count
key = horizontal_subAnim.keys[last_key_index]
)
if turning_subAnim.keys.count > last_key_index then(
last_key_index = turning_subAnim.keys.count
key = turning_subAnim.keys[last_key_index]
)
if last_key_index <=1 then(
return 1f
)
return key.time
)
-- 转换
fn batchConvertBipToFBX bipCtl srcDir dstDir = (
--file_list = getFiles(srcDir + "\\*.bip")
file_list = getFiles (srcDir + "\\*.bip") recurse:true
--file_list = getFiles (srcDir + "\\*.bip") recurse:false
for file in file_list do
(
-- load bip file
try
biped.loadBipFile bipCtl file
catch
(
print "?????????:" + file
continue
)
--file_length = biped.getl
FBXExporterSetParam "Animation" true
FBXExporterSetParam "BakeAnimation" true
FBXExporterSetParam "BakeFrameStart" 0
last_key_time = getLastKeyTime bipCtl
animationRange = interval 0f last_key_time
print last_key_time.frame
FBXExporterSetParam "BakeFrameEnd" (last_key_time.frame as integer)
FBXExporterSetParam "BakeResampleAnimation" true
FBXExporterSetParam "UpAxis" "Y"
srcDir_list = GetDirectories (srcDir + "\\*")
if srcDir_list.count >= 1 then
(
file_dir_array = filterString (getFilenamePath file) "\\"
local output_path = (dstDir + "\\" +file_dir_array[file_dir_array.count] + "-" + getFilenameFile file + ".fbx")
)
else
(
local output_path = (dstDir + "\\" + getFilenameFile file + ".fbx")
)
exportFile output_path #noPrompt selectedOnly:false using:FBXEXP
print ("convert '" + file + "' to '" + output_path + "'")
)
)
-- 导入时可能会弹出帧率转换确认对话框,这个函数用来处理该对话框
fn frameRateChangeHook = (
local WindowHandle = DialogMonitorOPS.GetWindowHandle()
local WindowTitle = (UIAccessor.GetWindowText WindowHandle)
if WindowTitle == "Frame Rate Change" then (
-- print "'Frame Rate Change' dialog is detected"
-- Get the handles of all controls in the dialog
local dialogChildren = (windows.getChildrenHWND WindowHandle)
-- print (dialogChildren) -- uncomment this line to see available buttons
UIAccessor.PressButton dialogChildren[3][1] -- choose "quantize"
-- UIAccessor.PressButton dialogChildren[6][1] -- choose "map frames"
-- click OK
UIAccessor.PressButtonByName WindowHandle "OK"
)
True
)
-- start monitoring dialogs and register hook
DialogMonitorOPS.enabled = true
DialogMonitorOPS.RegisterNotification frameRateChangeHook id:#frameRateChangeHook
pluginManager.loadClass FbxExporter
-- the real convert job
-- 这里写源目录
srcDir = "C:\\Users\\XPS\\Downloads\\Motion\\"
-- 这里写目标目录
dstDir = "C:\\Users\\XPS\\Downloads\\Motion\\result\\"
-- 用于加载bip文件的biped骨骼名字
bip = $Bip001
bip_ctl = bip.transform.controller
batchConvertBipToFBX bip_ctl srcDir dstDir
-- stop monitoring dialogs and unregister hook
DialogMonitorOPS.unRegisterNotification id:#frameRateChangeHook
DialogMonitorOPS.enabled = false