UNPKG

@picode/fbx

Version:

Provides an interface to use FBX data.

168 lines (167 loc) 8.2 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.FBX = exports.FBXAxes = void 0; var fbx_parser_1 = require("fbx-parser"); //https://help.autodesk.com/view/FBX/2015/ENU/ var FBXAxes; (function (FBXAxes) { FBXAxes[FBXAxes["X"] = 0] = "X"; FBXAxes[FBXAxes["Y"] = 1] = "Y"; FBXAxes[FBXAxes["Z"] = 2] = "Z"; })(FBXAxes = exports.FBXAxes || (exports.FBXAxes = {})); var FBXAnimationCurve = /** @class */ (function () { function FBXAnimationCurve(node) { this.node = node; } FBXAnimationCurve.from = function (node) { var _a, _b; if (!((_a = node.node('KeyTime')) === null || _a === void 0 ? void 0 : _a.prop(0, 'number[]')) || !((_b = node.node('KeyValueFloat')) === null || _b === void 0 ? void 0 : _b.prop(0, 'number[]'))) return undefined; return new FBXAnimationCurve(node); }; FBXAnimationCurve.prototype.getTime = function () { var _a; // no undefined, object should only exist if those values are available return (_a = this.node.node('KeyTime')) === null || _a === void 0 ? void 0 : _a.prop(0, 'number[]'); }; FBXAnimationCurve.prototype.getValue = function () { var _a; // no undefined, object should only exist if those values are available return (_a = this.node.node('KeyValueFloat')) === null || _a === void 0 ? void 0 : _a.prop(0, 'number[]'); }; return FBXAnimationCurve; }()); var FBXAnimationCurveNode = /** @class */ (function () { function FBXAnimationCurveNode(node) { this.node = node; } FBXAnimationCurveNode.from = function (node) { return new FBXAnimationCurveNode(node); }; FBXAnimationCurveNode.prototype.getX = function () { var _a, _b; var props70 = (_a = this.node) === null || _a === void 0 ? void 0 : _a.node('Properties70'); if (typeof props70 === 'undefined') return undefined; var value = (_b = props70.node('P', { 0: 'd|X' })) === null || _b === void 0 ? void 0 : _b.prop(4, 'number'); if (typeof value === 'undefined') return undefined; return value; }; FBXAnimationCurveNode.prototype.getY = function () { var _a, _b; var props70 = (_a = this.node) === null || _a === void 0 ? void 0 : _a.node('Properties70'); if (typeof props70 === 'undefined') return undefined; var value = (_b = props70.node('P', { 0: 'd|Y' })) === null || _b === void 0 ? void 0 : _b.prop(4, 'number'); if (typeof value === 'undefined') return undefined; return value; }; FBXAnimationCurveNode.prototype.getZ = function () { var _a, _b; var props70 = (_a = this.node) === null || _a === void 0 ? void 0 : _a.node('Properties70'); if (typeof props70 === 'undefined') return undefined; var value = (_b = props70.node('P', { 0: 'd|Z' })) === null || _b === void 0 ? void 0 : _b.prop(4, 'number'); if (typeof value === 'undefined') return undefined; return value; }; return FBXAnimationCurveNode; }()); var FBXModel = /** @class */ (function () { function FBXModel(node, root) { this.node = node; this.root = root; } FBXModel.prototype.getAnimCurveNode = function (type) { var _a; var modelId = this.node.prop(0, 'number'); // Get the own model id to find connections var objects = this.root.node('Objects'); // Get root Objects node to look for the animation curve node var connections = this.root.node('Connections'); // Get root Connections node to look for the connections if (typeof modelId === 'undefined' || !connections) return undefined; var animCurveNodeId = (_a = connections.node({ 2: modelId, 3: type })) === null || _a === void 0 ? void 0 : _a.prop(1); // Find the connection to the model with the according type, and get the id of the node if (typeof animCurveNodeId === 'undefined') return undefined; var animCurveObj = objects === null || objects === void 0 ? void 0 : objects.node({ 0: animCurveNodeId }); // Find the node by id in objects if (typeof animCurveObj === 'undefined') return undefined; return FBXAnimationCurveNode.from(animCurveObj); }; FBXModel.prototype.getAnimCurve = function (type, axes) { var _a, _b; var modelId = this.node.prop(0, 'number'); var objects = this.root.node('Objects'); var connections = this.root.node('Connections'); if (typeof modelId === 'undefined' || !connections) return undefined; var animCurveNodeId = (_a = connections.node({ 2: modelId, 3: type })) === null || _a === void 0 ? void 0 : _a.prop(1); if (typeof animCurveNodeId === 'undefined') return undefined; var axesStr = ['d|X', 'd|Y', 'd|Z'][axes]; var animCurveId = (_b = connections.node({ 2: animCurveNodeId, 3: axesStr })) === null || _b === void 0 ? void 0 : _b.prop(1); if (typeof animCurveId === 'undefined') return undefined; var animCurveObj = objects === null || objects === void 0 ? void 0 : objects.node({ 0: animCurveId }); if (typeof animCurveObj === 'undefined') return undefined; return FBXAnimationCurve.from(animCurveObj); }; FBXModel.prototype.getTranslationNode = function () { return this.getAnimCurveNode('Lcl Rotation'); }; FBXModel.prototype.getTranslationKey = function (axes) { return this.getAnimCurve('Lcl Translation', axes); }; FBXModel.prototype.getRotationNode = function () { return this.getAnimCurveNode('Lcl Rotation'); }; FBXModel.prototype.getRotationKey = function (axes) { return this.getAnimCurve('Lcl Rotation', axes); }; return FBXModel; }()); var FBXGlobalSettings = /** @class */ (function () { function FBXGlobalSettings(node) { this.node = node; } FBXGlobalSettings.prototype.getUpAxes = function () { var _a, _b, _c; var axes = (_c = (_b = (_a = this.node) === null || _a === void 0 ? void 0 : _a.node('Properties70')) === null || _b === void 0 ? void 0 : _b.node('P', { 0: 'UpAxis' })) === null || _c === void 0 ? void 0 : _c.prop(4, 'number'); if (typeof axes == 'undefined') return undefined; return axes >= 0 && axes <= 2 ? axes : undefined; }; FBXGlobalSettings.prototype.getFrontAxes = function () { var _a, _b, _c; var axes = (_c = (_b = (_a = this.node) === null || _a === void 0 ? void 0 : _a.node('Properties70')) === null || _b === void 0 ? void 0 : _b.node('P', { 0: 'FrontAxis' })) === null || _c === void 0 ? void 0 : _c.prop(4, 'number'); if (typeof axes == 'undefined') return undefined; return axes >= 0 && axes <= 2 ? axes : undefined; }; FBXGlobalSettings.prototype.getCoordAxes = function () { var _a, _b, _c; var axes = (_c = (_b = (_a = this.node) === null || _a === void 0 ? void 0 : _a.node('Properties70')) === null || _b === void 0 ? void 0 : _b.node('P', { 0: 'CoordAxis' })) === null || _c === void 0 ? void 0 : _c.prop(4, 'number'); if (typeof axes == 'undefined') return undefined; return axes >= 0 && axes <= 2 ? axes : undefined; }; return FBXGlobalSettings; }()); var FBX = /** @class */ (function () { function FBX(fbxData) { this.root = new fbx_parser_1.FBXReader(fbxData); this.globalSettings = new FBXGlobalSettings(this.root.node('GlobalSettings')); } FBX.prototype.getModel = function (name) { var _a; var node = (_a = this.root.node('Objects')) === null || _a === void 0 ? void 0 : _a.node({ 1: "Model::" + name }); if (typeof node === 'undefined') return undefined; return new FBXModel(node, this.root); }; return FBX; }()); exports.FBX = FBX;