|
| 1 | +type NodeType = 'bone' | 'camera' | 'locator' |
| 2 | + |
| 3 | +interface ISerializedVariantModel { |
| 4 | + custom_model_data: number |
| 5 | + resource_location: string |
| 6 | +} |
| 7 | + |
| 8 | +interface ISerializedVariant { |
| 9 | + name: string |
| 10 | + uuid: string |
| 11 | + models: Record<string, ISerializedVariantModel> |
| 12 | + affected_bones: string[] |
| 13 | + affected_bones_is_a_whitelist: boolean |
| 14 | +} |
| 15 | + |
| 16 | +interface ISerializedAnimationFrame { |
| 17 | + nodes: ISerializedNodeAnimationFrameEntry[] |
| 18 | + time: number |
| 19 | + variant?: { |
| 20 | + uuid: string |
| 21 | + execute_condition?: string |
| 22 | + } |
| 23 | + commands?: { |
| 24 | + commands: string |
| 25 | + execute_condition?: string |
| 26 | + } |
| 27 | +} |
| 28 | + |
| 29 | +interface ISerializedNodeAnimationFrameEntry { |
| 30 | + uuid: string |
| 31 | + matrix: number[] |
| 32 | +} |
| 33 | + |
| 34 | +interface ISerealizedAnimation { |
| 35 | + frames: ISerializedAnimationFrame[] |
| 36 | + duration: number |
| 37 | + loop_mode: 'once' | 'hold' | 'loop' |
| 38 | + affected_bones: string[] |
| 39 | + affected_bones_is_a_whitelist: boolean |
| 40 | +} |
| 41 | + |
| 42 | +interface ISerializedNode { |
| 43 | + type: NodeType |
| 44 | + name: string |
| 45 | + uuid: string |
| 46 | + custom_model_data: number |
| 47 | + resource_location: string |
| 48 | +} |
| 49 | + |
| 50 | +interface ISerializedVariant { |
| 51 | + name: string |
| 52 | + uuid: string |
| 53 | + models: Record<string, ISerializedVariantModel> |
| 54 | + affected_bones: string[] |
| 55 | + affected_bones_is_a_whitelist: boolean |
| 56 | +} |
| 57 | + |
| 58 | +export interface IJSONExport { |
| 59 | + project_settings: Record<string, any> |
| 60 | + exporter_settings: Record<string, any> |
| 61 | + rig: { |
| 62 | + default_pose: ISerializedNodeAnimationFrameEntry[] |
| 63 | + node_map: Record<string, ISerializedNode> |
| 64 | + } |
| 65 | + variants: Record<string, ISerializedVariant> |
| 66 | + animations: Record<string, ISerealizedAnimation> |
| 67 | +} |
| 68 | + |
| 69 | +function serializeProjectSettings(): Record<string, any> { |
| 70 | + const project_settings = {} |
| 71 | + for (const [name, setting] of Object.entries(Project!.animated_java_settings)) { |
| 72 | + project_settings[name] = setting._save() |
| 73 | + } |
| 74 | + return project_settings |
| 75 | +} |
| 76 | + |
| 77 | +function serializeExporterSettings(exporterSettings: Record<string, any>): Record<string, any> { |
| 78 | + const exporter_settings = {} |
| 79 | + for (const [name, setting] of Object.entries(exporterSettings)) { |
| 80 | + exporter_settings[name] = setting._save() |
| 81 | + } |
| 82 | + return exporter_settings |
| 83 | +} |
| 84 | + |
| 85 | +function serializeNodeAnimationFrameEntry( |
| 86 | + node: AnimatedJava.IAnimationNode |
| 87 | +): ISerializedNodeAnimationFrameEntry { |
| 88 | + const { type, uuid, matrix } = node |
| 89 | + return { |
| 90 | + uuid, |
| 91 | + matrix: matrix.toArray(), |
| 92 | + } |
| 93 | +} |
| 94 | + |
| 95 | +function serializeNodeMap(nodeMap: Record<string, any>): Record<string, ISerializedNode> { |
| 96 | + const serializedNodeMap: Record<string, ISerializedNode> = {} |
| 97 | + for (const uuid in nodeMap) { |
| 98 | + const node = nodeMap[uuid] |
| 99 | + const type = node.type |
| 100 | + const name = node.name |
| 101 | + const custom_model_data = node.customModelData |
| 102 | + const resource_location = node.resourceLocation |
| 103 | + |
| 104 | + serializedNodeMap[uuid] = { |
| 105 | + type, |
| 106 | + name, |
| 107 | + uuid, |
| 108 | + custom_model_data, |
| 109 | + resource_location, |
| 110 | + } |
| 111 | + } |
| 112 | + return serializedNodeMap |
| 113 | +} |
| 114 | + |
| 115 | +function serializeVariant( |
| 116 | + exportOptions: AnimatedJava.IAnimatedJavaExportData<unknown>, |
| 117 | + variant: AnimatedJava.Variant |
| 118 | +): ISerializedVariant { |
| 119 | + const name = variant.name |
| 120 | + const uuid = variant.uuid |
| 121 | + const models = {} |
| 122 | + const affected_bones = variant.affectedBones.map(v => v.value) |
| 123 | + const affected_bones_is_a_whitelist = variant.affectedBonesIsAWhitelist |
| 124 | + |
| 125 | + for (const [uuid, model] of Object.entries(exportOptions.rig.variantModels[name])) { |
| 126 | + models[uuid] = { |
| 127 | + custom_model_data: model.customModelData, |
| 128 | + resource_location: model.resourceLocation, |
| 129 | + } |
| 130 | + } |
| 131 | + |
| 132 | + return { |
| 133 | + name, |
| 134 | + uuid, |
| 135 | + models, |
| 136 | + affected_bones, |
| 137 | + affected_bones_is_a_whitelist, |
| 138 | + } |
| 139 | +} |
| 140 | + |
| 141 | +function serializeAnimationFrame( |
| 142 | + frame: AnimatedJava.IRenderedAnimation['frames'][any] |
| 143 | +): ISerializedAnimationFrame { |
| 144 | + const nodes = frame.nodes.map(serializeNodeAnimationFrameEntry) |
| 145 | + const time = frame.time |
| 146 | + const variant = frame.variant |
| 147 | + const commands = frame.commands |
| 148 | + |
| 149 | + return { |
| 150 | + nodes, |
| 151 | + time, |
| 152 | + variant, |
| 153 | + commands, |
| 154 | + } |
| 155 | +} |
| 156 | + |
| 157 | +function serializeAnimation(animation: AnimatedJava.IRenderedAnimation): ISerealizedAnimation { |
| 158 | + const frames = animation.frames.map(serializeAnimationFrame) |
| 159 | + const duration = animation.duration |
| 160 | + const loop_mode = animation.loopMode |
| 161 | + const bbAnimation = Blockbench.Animation.all.find( |
| 162 | + v => v instanceof Blockbench.Animation && v.name === animation.name |
| 163 | + ) as _Animation |
| 164 | + const affected_bones = bbAnimation.affected_bones.map(v => v.value) |
| 165 | + const affected_bones_is_a_whitelist = bbAnimation.affected_bones_is_a_whitelist |
| 166 | + |
| 167 | + return { |
| 168 | + frames, |
| 169 | + duration, |
| 170 | + loop_mode, |
| 171 | + affected_bones, |
| 172 | + affected_bones_is_a_whitelist, |
| 173 | + } |
| 174 | +} |
| 175 | + |
| 176 | +export function constructJSON( |
| 177 | + exportOptions: AnimatedJava.IAnimatedJavaExportData<unknown> |
| 178 | +): IJSONExport { |
| 179 | + const {} = AnimatedJava.API |
| 180 | + const project_settings = serializeProjectSettings() |
| 181 | + const exporter_settings = serializeExporterSettings(exportOptions.exporterSettings) |
| 182 | + const rig = { |
| 183 | + default_pose: exportOptions.rig.defaultPose.map(serializeNodeAnimationFrameEntry), |
| 184 | + node_map: serializeNodeMap(exportOptions.rig.nodeMap), |
| 185 | + } |
| 186 | + const variants = {} |
| 187 | + const animations = {} |
| 188 | + |
| 189 | + for (const variant of Project!.animated_java_variants.variants) { |
| 190 | + if (variant.default) continue |
| 191 | + variants[variant.uuid] = serializeVariant(exportOptions, variant) |
| 192 | + } |
| 193 | + |
| 194 | + for (const animation of exportOptions.renderedAnimations) { |
| 195 | + animations[animation.name] = serializeAnimation(animation) |
| 196 | + } |
| 197 | + |
| 198 | + return { |
| 199 | + project_settings, |
| 200 | + exporter_settings, |
| 201 | + rig, |
| 202 | + variants, |
| 203 | + animations, |
| 204 | + } |
| 205 | +} |
0 commit comments