Skip to content

Commit b70623a

Browse files
committed
🐛 Fix models converted to Blueprints becoming unsavable
If the default variant couldn't be found, the codec compile function would fail. I've changed the `Variant.getDefault` function to create a default variant if one does not already exist.
1 parent 7b08802 commit b70623a

File tree

5 files changed

+21
-15
lines changed

5 files changed

+21
-15
lines changed

src/components/keyframePanels/variantKeyframePanel.svelte

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
let executeCondition = keyframe?.execute_condition ?? ''
1212
1313
$: {
14-
keyframe.variant = Variant.all.find(v => v.uuid === variantUuid) ?? Variant.getDefault()
14+
keyframe.variant = Variant.getByUUID(variantUuid) ?? Variant.getDefault()
1515
keyframe.execute_condition = executeCondition
1616
Animator.preview()
1717
}

src/formats/blueprint/codec.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -309,8 +309,8 @@ export const BLUEPRINT_CODEC = registerCodec(
309309
}
310310

311311
model.variants = {
312-
default: Variant.all.find(v => v.isDefault)!.toJSON(),
313-
list: Variant.all.filter(v => !v.isDefault).map(v => v.toJSON()),
312+
default: Variant.getDefault().toJSON(),
313+
list: Variant.allExcludingDefault().map(v => v.toJSON()),
314314
}
315315

316316
const animationOptions = { bone_names: true, absolute_paths: options.absolute_paths }

src/formats/blueprint/index.ts

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -281,13 +281,6 @@ export const BLUEPRINT_FORMAT = registerModelFormat(
281281

282282
project.pluginMode = new Valuable(project.animated_java.enable_plugin_mode)
283283

284-
// project.variants ??= []
285-
// if (Variant.all.length === 0) {
286-
// console.warn('No variants found, creating default variant')
287-
// new Variant('Default', true)
288-
// }
289-
// Variant.selectDefault()
290-
291284
initializeRenderBoxPreview()
292285

293286
requestAnimationFrame(() => {

src/mods/customKeyframes.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ registerConditionalPropertyOverrideMod({
4444
const uuid = this.data_points.at(0)?.[EFFECT_ANIMATOR_CHANNELS.VARIANT] as
4545
| string
4646
| undefined
47-
if (uuid) return Variant.all.find(v => v.uuid === uuid)
47+
if (uuid) return Variant.getByUUID(uuid)
4848
console.error('Keyframe variant', uuid, 'not found!')
4949
},
5050

src/variants.ts

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -93,11 +93,13 @@ export class Variant {
9393
this.textureMap = new TextureMap()
9494
this.id = Variant.all.length
9595
if (this.isDefault) {
96+
if (Variant.hasDefault()) {
97+
throw new Error('There can only be one default variant!')
98+
}
9699
this.displayName = 'Default'
97100
this.name = 'default'
98101
}
99102
Variant.all.push(this)
100-
// this.select()
101103
EVENTS.CREATE_VARIANT.publish(this)
102104
}
103105

@@ -225,12 +227,23 @@ export class Variant {
225227
}
226228

227229
static selectDefault() {
228-
const variant = Variant.all.find(v => v.isDefault)
229-
if (variant) variant.select()
230+
Variant.getDefault().select()
231+
}
232+
233+
static getByUUID(uuid: string): Variant | undefined {
234+
return Variant.all.find(v => v.uuid === uuid)
235+
}
236+
237+
static allExcludingDefault(): Variant[] {
238+
return Variant.all.filter(v => !v.isDefault)
239+
}
240+
241+
static hasDefault(): boolean {
242+
return Variant.all.some(v => v.isDefault)
230243
}
231244

232245
static getDefault(): Variant {
233-
return Variant.all.find(v => v.isDefault) ?? Variant.all[0]
246+
return Variant.all.find(v => v.isDefault) ?? new Variant('Default', true)
234247
}
235248
}
236249

0 commit comments

Comments
 (0)