properly add rotmode to animation .json spec, including import/export

This commit is contained in:
George Paton 2024-12-23 12:33:21 +11:00
parent 67eb2be5a7
commit d56f30a4fa
5 changed files with 61 additions and 27 deletions

View File

@ -51,7 +51,6 @@ public class AnimationLoader {
// The offsets are only required when sequences are played for an object, which is why we don't globally offset! The obj rendering handles the non-animated case fine // The offsets are only required when sequences are played for an object, which is why we don't globally offset! The obj rendering handles the non-animated case fine
// Effectively, this removes double translation AND ensures that rotations occur around the individual object origin, rather than the weapon origin // Effectively, this removes double translation AND ensures that rotations occur around the individual object origin, rather than the weapon origin
HashMap<String, double[]> offsets = new HashMap<String, double[]>(); HashMap<String, double[]> offsets = new HashMap<String, double[]>();
HashMap<String, double[]> rotModes = new HashMap<String, double[]>();
for(Map.Entry<String, JsonElement> root : json.getAsJsonObject("offset").entrySet()) { for(Map.Entry<String, JsonElement> root : json.getAsJsonObject("offset").entrySet()) {
JsonArray array = root.getValue().getAsJsonArray(); JsonArray array = root.getValue().getAsJsonArray();
@ -61,16 +60,21 @@ public class AnimationLoader {
} }
offsets.put(root.getKey(), offset); offsets.put(root.getKey(), offset);
}
// Rotation modes, swizzled into our local space. YZX in blender becomes XYZ due to:
// * rotation order reversed in blender (XYZ -> ZYX)
// * dimensions Y and Z are swapped in blender (ZYX -> YZX)
HashMap<String, double[]> rotModes = new HashMap<String, double[]>();
if(json.has("rotmode")) {
for(Map.Entry<String, JsonElement> root : json.getAsJsonObject("rotmode").entrySet()) {
String mode = root.getValue().getAsString();
if(array.size() >= 6) {
double[] rotMode = new double[3]; double[] rotMode = new double[3];
for(int i = 0; i < 3; i++) { rotMode[0] = getRot(mode.charAt(1));
rotMode[i] = array.get(i + 3).getAsDouble(); rotMode[1] = getRot(mode.charAt(2));
rotMode[2] = getRot(mode.charAt(0));
} }
rotModes.put(root.getKey(), rotMode);
}
} }
@ -95,6 +99,15 @@ public class AnimationLoader {
return animations; return animations;
} }
private static double getRot(char value) {
switch(value) {
case 'X': return 0;
case 'Y': return 1;
case 'Z': return 2;
default: return 0;
}
}
private static BusAnimationSequence loadSequence(JsonObject json, double[] offset, double[] rotMode) { private static BusAnimationSequence loadSequence(JsonObject json, double[] offset, double[] rotMode) {
BusAnimationSequence sequence = new BusAnimationSequence(); BusAnimationSequence sequence = new BusAnimationSequence();

File diff suppressed because one or more lines are too long

View File

@ -41,7 +41,7 @@ class ExportJSONAnimation(Operator, ExportHelper):
print("writing JSON data to file...") print("writing JSON data to file...")
f = open(self.filepath, 'w', encoding='utf-8') f = open(self.filepath, 'w', encoding='utf-8')
collection = {"anim": {}, "offset": {}, "hierarchy": {}} collection = {"anim": {}, "offset": {}, "hierarchy": {}, "rotmode": {}}
dimensions = ["x", "z", "y"] # Swizzled to X, Z, Y dimensions = ["x", "z", "y"] # Swizzled to X, Z, Y
mult = [1, -1, 1] # +X, -Z, +Y mult = [1, -1, 1] # +X, -Z, +Y
@ -51,6 +51,7 @@ class ExportJSONAnimation(Operator, ExportHelper):
animations = collection['anim'] animations = collection['anim']
offsets = collection['offset'] offsets = collection['offset']
modes = collection['rotmode']
hierarchy = collection['hierarchy'] hierarchy = collection['hierarchy']
actions = bpy.data.actions actions = bpy.data.actions
@ -112,11 +113,12 @@ class ExportJSONAnimation(Operator, ExportHelper):
if object.parent: if object.parent:
hierarchy[object.name] = object.parent.name hierarchy[object.name] = object.parent.name
if object.location == mathutils.Vector(): # don't export 0,0,0 if object.location != mathutils.Vector(): # don't export 0,0,0
continue
offsets[object.name] = [object.location.x, object.location.z, -object.location.y] offsets[object.name] = [object.location.x, object.location.z, -object.location.y]
if object.rotation_mode != 'YZX':
modes[object.name] = object.rotation_mode
f.write(json.dumps(collection)) f.write(json.dumps(collection))
@ -229,14 +231,19 @@ class ImportJSONAnimation(Operator, ImportHelper):
bpy.ops.object.transform_apply(location=False, rotation=True, scale=False) bpy.ops.object.transform_apply(location=False, rotation=True, scale=False)
object.rotation_mode = 'YZX' object.rotation_mode = 'YZX'
if 'rotmode' in collection:
modes = collection['rotmode']
for mode in modes:
bpy.data.objects[mode].rotation_mode = modes[mode]
if 'hierarchy' in collection: if 'hierarchy' in collection:
hierarchy = collection["hierarchy"] hierarchy = collection['hierarchy']
for name in hierarchy: for name in hierarchy:
parent = hierarchy[name] parent = hierarchy[name]
bpy.data.objects[name].parent = bpy.data.objects[parent] bpy.data.objects[name].parent = bpy.data.objects[parent]
offsets = collection["offset"] offsets = collection['offset']
for name in offsets: for name in offsets:
offset = offsets[name] offset = offsets[name]

View File

@ -41,7 +41,7 @@ class ExportJSONAnimation(Operator, ExportHelper):
print("writing JSON data to file...") print("writing JSON data to file...")
f = open(self.filepath, 'w', encoding='utf-8') f = open(self.filepath, 'w', encoding='utf-8')
collection = {"anim": {}, "offset": {}, "hierarchy": {}} collection = {"anim": {}, "offset": {}, "hierarchy": {}, "rotmode": {}}
dimensions = ["x", "z", "y"] # Swizzled to X, Z, Y dimensions = ["x", "z", "y"] # Swizzled to X, Z, Y
mult = [1, -1, 1] # +X, -Z, +Y mult = [1, -1, 1] # +X, -Z, +Y
@ -51,6 +51,7 @@ class ExportJSONAnimation(Operator, ExportHelper):
animations = collection['anim'] animations = collection['anim']
offsets = collection['offset'] offsets = collection['offset']
modes = collection['rotmode']
hierarchy = collection['hierarchy'] hierarchy = collection['hierarchy']
actions = bpy.data.actions actions = bpy.data.actions
@ -112,11 +113,12 @@ class ExportJSONAnimation(Operator, ExportHelper):
if object.parent: if object.parent:
hierarchy[object.name] = object.parent.name hierarchy[object.name] = object.parent.name
if object.location == mathutils.Vector(): # don't export 0,0,0 if object.location != mathutils.Vector(): # don't export 0,0,0
continue
offsets[object.name] = [object.location.x, object.location.z, -object.location.y] offsets[object.name] = [object.location.x, object.location.z, -object.location.y]
if object.rotation_mode != 'YZX':
modes[object.name] = object.rotation_mode
f.write(json.dumps(collection)) f.write(json.dumps(collection))
@ -230,14 +232,19 @@ class ImportJSONAnimation(Operator, ImportHelper):
bpy.ops.object.transform_apply(location=False, rotation=True, scale=False, properties=False) bpy.ops.object.transform_apply(location=False, rotation=True, scale=False, properties=False)
object.rotation_mode = 'YZX' object.rotation_mode = 'YZX'
if 'rotmode' in collection:
modes = collection['rotmode']
for mode in modes:
bpy.data.objects[mode].rotation_mode = modes[mode]
if 'hierarchy' in collection: if 'hierarchy' in collection:
hierarchy = collection["hierarchy"] hierarchy = collection['hierarchy']
for name in hierarchy: for name in hierarchy:
parent = hierarchy[name] parent = hierarchy[name]
bpy.data.objects[name].parent = bpy.data.objects[parent] bpy.data.objects[name].parent = bpy.data.objects[parent]
offsets = collection["offset"] offsets = collection['offset']
for name in offsets: for name in offsets:
offset = offsets[name] offset = offsets[name]

View File

@ -41,7 +41,7 @@ class ExportJSONAnimation(Operator, ExportHelper):
print("writing JSON data to file...") print("writing JSON data to file...")
f = open(self.filepath, 'w', encoding='utf-8') f = open(self.filepath, 'w', encoding='utf-8')
collection = {"anim": {}, "offset": {}, "hierarchy": {}} collection = {"anim": {}, "offset": {}, "hierarchy": {}, "rotmode": {}}
dimensions = ["x", "z", "y"] # Swizzled to X, Z, Y dimensions = ["x", "z", "y"] # Swizzled to X, Z, Y
mult = [1, -1, 1] # +X, -Z, +Y mult = [1, -1, 1] # +X, -Z, +Y
@ -51,6 +51,7 @@ class ExportJSONAnimation(Operator, ExportHelper):
animations = collection['anim'] animations = collection['anim']
offsets = collection['offset'] offsets = collection['offset']
modes = collection['rotmode']
hierarchy = collection['hierarchy'] hierarchy = collection['hierarchy']
actions = bpy.data.actions actions = bpy.data.actions
@ -112,11 +113,12 @@ class ExportJSONAnimation(Operator, ExportHelper):
if object.parent: if object.parent:
hierarchy[object.name] = object.parent.name hierarchy[object.name] = object.parent.name
if object.location == mathutils.Vector(): # don't export 0,0,0 if object.location != mathutils.Vector(): # don't export 0,0,0
continue
offsets[object.name] = [object.location.x, object.location.z, -object.location.y] offsets[object.name] = [object.location.x, object.location.z, -object.location.y]
if object.rotation_mode != 'YZX':
modes[object.name] = object.rotation_mode
f.write(json.dumps(collection)) f.write(json.dumps(collection))
@ -227,14 +229,19 @@ class ImportJSONAnimation(Operator, ImportHelper):
bpy.ops.object.transform_apply(location=False, rotation=True, scale=False, properties=False) bpy.ops.object.transform_apply(location=False, rotation=True, scale=False, properties=False)
object.rotation_mode = 'YZX' object.rotation_mode = 'YZX'
if 'rotmode' in collection:
modes = collection['rotmode']
for mode in modes:
bpy.data.objects[mode].rotation_mode = modes[mode]
if 'hierarchy' in collection: if 'hierarchy' in collection:
hierarchy = collection["hierarchy"] hierarchy = collection['hierarchy']
for name in hierarchy: for name in hierarchy:
parent = hierarchy[name] parent = hierarchy[name]
bpy.data.objects[name].parent = bpy.data.objects[parent] bpy.data.objects[name].parent = bpy.data.objects[parent]
offsets = collection["offset"] offsets = collection['offset']
for name in offsets: for name in offsets:
offset = offsets[name] offset = offsets[name]