Skip to content

Instantly share code, notes, and snippets.

@MattOstgard
Created December 14, 2015 22:53
Show Gist options
  • Save MattOstgard/245617e60d8079c22bb6 to your computer and use it in GitHub Desktop.
Save MattOstgard/245617e60d8079c22bb6 to your computer and use it in GitHub Desktop.
This script works around pixi-spine issue 68 by multiplying all additive slots rgb values by alpha.
#!/usr/bin/python3
import os
import json
import math
from collections import OrderedDict
def multiply_rgb_by_alpha_and_set_alpha_to_1(hex_color):
# Turn hex to float values
rgba = []
for i in range(0,8,2):
rgba.append(int('0x' + hex_color[i:i + 2], 0) / 255)
# Multiply rgb values by alpha
for i in range(3):
rgba[i] *= rgba[3]
# Set alpha to 1
rgba[3] = 1.0
# Convert back to hex
new_hex_color = ''
for i in range(4):
hex_value = hex(int(rgba[i] * 255))[2:]
if len(hex_value) == 1:
hex_value = '0' + hex_value
new_hex_color += hex_value
return new_hex_color
if __name__ == '__main__':
import argparse
parser = argparse.ArgumentParser(description='This script works around pixi-spine issue 68 by multiplying all'
'additive slots rgb by alpha.')
parser.add_argument('spine_files', metavar='Spine .json files', nargs='+',
help='an integer for the accumulator')
args = parser.parse_args()
for spine_file in args.spine_files:
spine_file = os.path.join(os.getcwd(), spine_file)
print('Input file: ' + spine_file)
file_split_ext = os.path.splitext(spine_file)
output_file = file_split_ext[0] + '_-_issue_68_work_around' + file_split_ext[1]
with open(spine_file) as f:
spine_obj = json.load(f, object_pairs_hook=OrderedDict)
slot_defaults = spine_obj['slots']
slot_anims = spine_obj['animations']['animation']['slots']
for slot_default in slot_defaults:
name = slot_default['name']
# Perform first on all the non animated values (defaults)
if not 'blend' in slot_default:
continue
if not slot_default['blend'] == 'additive':
continue
if 'color' in slot_default:
slot_default['color'] = multiply_rgb_by_alpha_and_set_alpha_to_1(slot_default['color'])
# Do the same to animated values
if not name in slot_anims:
continue
if not 'color' in slot_anims[name]:
continue
for keyframe in slot_anims[name]['color']:
keyframe['color'] = multiply_rgb_by_alpha_and_set_alpha_to_1(keyframe['color'])
with open(output_file, 'w') as f:
json.dump(spine_obj, f)
print('Output file: ' + output_file)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment