Skip to content

Commit

Permalink
Release - 2.4.1
Browse files Browse the repository at this point in the history
  • Loading branch information
simon50keda committed Oct 25, 2023
1 parent b4dd465 commit 34dff52
Show file tree
Hide file tree
Showing 8 changed files with 228 additions and 154 deletions.
2 changes: 1 addition & 1 deletion addon/io_scs_tools/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
"name": "SCS Tools",
"description": "Setup models, Import-Export SCS data format",
"author": "Simon Lusenc (50keda), Milos Zajic (4museman)",
"version": (2, 4, "1909305e"),
"version": (2, 4, "aeadde03"),
"blender": (3, 2, 0),
"location": "File > Import-Export",
"doc_url": "http://modding.scssoft.com/wiki/Documentation/Tools/SCS_Blender_Tools",
Expand Down
2 changes: 1 addition & 1 deletion addon/io_scs_tools/exp/pim/piece.py
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ def __calc_vertex_hash(index, normal, uvs, rgba, tangent):
:return: calculated vertex hash
:rtype: str
"""
fprec = 10 * 4
fprec = 10 ** 4

if tangent:
vertex_hash = (index,
Expand Down
2 changes: 1 addition & 1 deletion addon/io_scs_tools/internals/containers/parsers/sii.py
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ def parse_token(self):

else:

lprint("W No included SII file found, ignoring include: %r\n\t from: %r",
lprint("D No included SII file found, ignoring include: %r\n\t from: %r",
(match.group(1), self.filepath))

new_array = self.input[:self.current_line]
Expand Down
25 changes: 20 additions & 5 deletions addon/io_scs_tools/operators/mesh.py
Original file line number Diff line number Diff line change
Expand Up @@ -416,14 +416,15 @@ def initialize(self, context):
vcolor = mesh.color_attributes.new(name=layer_name, type='FLOAT_COLOR', domain='CORNER')

buffer = None
color = list(Color((0.5,) * 3).from_srgb_to_scene_linear()) + [1.0, ] # our default is 0.5, even for alpha where 0.5 is also max
if layer_name == _VCT_consts.ColoringLayersTypes.Color:
buffer = numpy.array([0.5] * (len(mesh.loops) * 4))
buffer = numpy.array(color * len(mesh.loops))
elif layer_name == _VCT_consts.ColoringLayersTypes.Decal:
buffer = numpy.array([1.0] * (len(mesh.loops) * 4))
buffer = numpy.array(color * len(mesh.loops))
elif layer_name == _VCT_consts.ColoringLayersTypes.AO:
buffer = numpy.array([0.5] * (len(mesh.loops) * 4))
buffer = numpy.array(color * len(mesh.loops))
elif layer_name == _VCT_consts.ColoringLayersTypes.AO2:
buffer = numpy.array([0.5] * (len(mesh.loops) * 4))
buffer = numpy.array(color * len(mesh.loops))

if buffer is not None:
vcolor.data.foreach_set("color", buffer)
Expand Down Expand Up @@ -534,6 +535,19 @@ def execute(self, context):
if VertexColorTools.SCS_TOOLS_OT_StartVColoring.__static_is_active:
return {'CANCELLED'}

# ensure our output layers definition
#
# NOTE: here is the deal: when switching to vertex paint mode
# blender creates first color attribute if none is present.
# As it happens it's name is the same as we use, so to ensure it creates proper
# color type and domain we rather create our output layers beforehand.
mesh_vcolors = context.active_object.data.color_attributes
if _MESH_consts.default_vcol not in mesh_vcolors:
mesh_vcolors.new(name=_MESH_consts.default_vcol, type='FLOAT_COLOR', domain='CORNER')

if _MESH_consts.default_vcol + _MESH_consts.vcol_a_suffix not in mesh_vcolors:
mesh_vcolors.new(name=_MESH_consts.default_vcol + _MESH_consts.vcol_a_suffix, type='FLOAT_COLOR', domain='CORNER')

bpy.ops.object.mode_set(mode="VERTEX_PAINT")

# NOTE: We have to push undo event otherwise undo was just
Expand Down Expand Up @@ -629,7 +643,8 @@ def execute(self, context):
lprint(message)
self.report({'INFO'}, message[2:])

_view3d_utils.tag_redraw_all_view3d() # trigger view update to see rebaked colors
# trigger view update to see rebaked colors, with fake reassignment.
mesh.color_attributes.active_color = mesh.color_attributes.active_color

else:

Expand Down
251 changes: 143 additions & 108 deletions addon/io_scs_tools/operators/scene.py

Large diffs are not rendered by default.

6 changes: 5 additions & 1 deletion addon/io_scs_tools/properties/object.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
#
# ##### END GPL LICENSE BLOCK #####

# Copyright (C) 2013-2021: SCS Software
# Copyright (C) 2013-2022: SCS Software

import bpy
from bpy.props import (StringProperty,
Expand Down Expand Up @@ -293,6 +293,10 @@ def update_empty_object_type(self, context):
obj.empty_display_size = 5.0
obj.empty_display_type = "ARROWS"
obj.show_name = True

# ensure default part
part_inventory = obj.scs_object_part_inventory
_inventory.add_item(part_inventory, _PART_consts.default_name, conditional=True)
else:
obj.empty_display_size = 1.0
obj.empty_display_type = "PLAIN_AXES"
Expand Down
74 changes: 42 additions & 32 deletions addon/io_scs_tools/utils/convert.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,12 @@
#
# ##### END GPL LICENSE BLOCK #####

# Copyright (C) 2013-2021: SCS Software
# Copyright (C) 2013-2022: SCS Software

import struct
import math
import re
from numpy import vectorize
from mathutils import Matrix, Quaternion, Vector, Color
from io_scs_tools.consts import Colors as _COL_consts
from io_scs_tools.utils.printout import lprint
Expand All @@ -32,9 +33,44 @@
_BYTE_STRUCT = struct.Struct(">I")


def __linear_to_srgb(x):
"""Converts value from linear to srgb
NOTE: taken from game and blender code
:param x: value to convert
:type x: float
"""

if x <= 0.0031308:
return 12.92 * x
else:
return (x ** (1.0 / 2.4)) * 1.055 - 0.055


def __srgb_to_linear(x):
"""Converts value from linear to srgb
NOTE: taken from game and blender code
:param x: value to convert
:type x: float
"""

if x <= 0.04045:
return x / 12.92
else:
return ((x + 0.055) / 1.055) ** 2.4


np_linear_to_srgb = vectorize(__linear_to_srgb)
"""Vectorizes linear to srgb function to be used with numpy arrays."""


np_srgb_to_linear = vectorize(__srgb_to_linear)
"""Vectorizes srgb to linear function to be used with numpy arrays."""


def linear_to_srgb(value):
"""Converts linear color to srgb colorspace. Function can convert single float or list of floats.
NOTE: taken from game code
:param value: list of floats or float
:type value: float | collections.Iterable[float]
Expand All @@ -44,26 +80,13 @@ def linear_to_srgb(value):

is_float = isinstance(value, float)
if is_float:
vals = [value]
else:
vals = list(value)

for i, v in enumerate(vals):
if v <= 0.0031308:
vals[i] = 12.92 * v
else:
a = 0.055
vals[i] = (v ** (1.0 / 2.4) * (1.0 + a)) - a

if is_float:
return vals[0]
return __linear_to_srgb(value)
else:
return vals
return [__linear_to_srgb(v) for v in list(value)]


def srgb_to_linear(value):
"""Converts srgb color to linear colorspace. Function can convert single float or list of floats.
NOTE: taken from game code
:param value: list of floats or float
:type value: float | collections.Iterable[float]
Expand All @@ -73,22 +96,9 @@ def srgb_to_linear(value):

is_float = isinstance(value, float)
if is_float:
vals = [value]
else:
vals = list(value)

for i, v in enumerate(vals):

if v <= 0.04045:
vals[i] = v / 12.92
else:
a = 0.055
vals[i] = ((v + a) / (1.0 + a)) ** 2.4

if is_float:
return vals[0]
return __srgb_to_linear(value)
else:
return vals
return [__srgb_to_linear(v) for v in list(value)]


def pre_gamma_corrected_col(color):
Expand Down
20 changes: 15 additions & 5 deletions addon/io_scs_tools/utils/mesh.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
#
# ##### END GPL LICENSE BLOCK #####

# Copyright (C) 2013-2019: SCS Software
# Copyright (C) 2013-2022: SCS Software

import bpy
import bmesh
Expand Down Expand Up @@ -386,13 +386,14 @@ def vcoloring_rebake(mesh, vcolor_arrays, old_array_hash):
mesh_vcolors.new(name=_MESH_consts.default_vcol + _MESH_consts.vcol_a_suffix, type='FLOAT_COLOR', domain='CORNER')

# get vertex color data for hash calculation
if mesh_vcolors.active.name == _VCT_consts.ColoringLayersTypes.Color:
active_color_name = mesh_vcolors.active_color.name
if active_color_name == _VCT_consts.ColoringLayersTypes.Color:
color_loops.foreach_get("color", vcolor_arrays[0])
elif mesh_vcolors.active.name == _VCT_consts.ColoringLayersTypes.Decal:
elif active_color_name == _VCT_consts.ColoringLayersTypes.Decal:
decal_loops.foreach_get("color", vcolor_arrays[0])
elif mesh_vcolors.active.name == _VCT_consts.ColoringLayersTypes.AO:
elif active_color_name == _VCT_consts.ColoringLayersTypes.AO:
ao_loops.foreach_get("color", vcolor_arrays[0])
elif mesh_vcolors.active.name == _VCT_consts.ColoringLayersTypes.AO2:
elif active_color_name == _VCT_consts.ColoringLayersTypes.AO2:
ao2_loops.foreach_get("color", vcolor_arrays[0])

new_array_hash = hash(vcolor_arrays[0].tobytes())
Expand All @@ -406,7 +407,16 @@ def vcoloring_rebake(mesh, vcolor_arrays, old_array_hash):
ao_loops.foreach_get("color", vcolor_arrays[2])
ao2_loops.foreach_get("color", vcolor_arrays[3])

# convert to srgb
for i in (0, 2, 3):
vcolor_arrays[i] = _convert.np_linear_to_srgb(vcolor_arrays[i])

# combine
vcolor_arrays[0] = vcolor_arrays[0] * vcolor_arrays[2] * vcolor_arrays[3] * 4.0

# convert back to scene linear
vcolor_arrays[0] = _convert.np_srgb_to_linear(vcolor_arrays[0])

# alpha is donated only by decal layer color, thus we just comment it out
# vcolor_arrays[1] = vcolor_arrays[1]

Expand Down

0 comments on commit 34dff52

Please sign in to comment.