Skip to content

Commit

Permalink
Add, remove, and open Edit Textures presets
Browse files Browse the repository at this point in the history
Also fixed empty list/set bug when loading a Transmogrifier preset.
  • Loading branch information
SapwoodStudio committed Mar 16, 2024
1 parent a523d92 commit 4494874
Show file tree
Hide file tree
Showing 4 changed files with 72 additions and 124 deletions.
71 changes: 57 additions & 14 deletions Functions.py
Original file line number Diff line number Diff line change
Expand Up @@ -610,6 +610,7 @@ def get_propertygroups():
],
"imports": [bpy.context.scene.transmogrifier_imports, True, ["files", "show_settings"]],
"exports": [bpy.context.scene.transmogrifier_exports, True, ["show_settings"]],
"textures": [bpy.context.scene.transmogrifier_textures, True, ["show_settings"]],
"scripts": [bpy.context.scene.transmogrifier_scripts, True, ["show_settings"]],
}

Expand Down Expand Up @@ -698,6 +699,31 @@ def get_settings_dict(self, context, use_absolute_paths, write_imports_list):
settings_dict.update(get_propertygroups_properties(key, value[0], value[1], value[2], use_absolute_paths, write_imports_list, settings_dict))

return settings_dict


# Create settings_dict dictionary from PropertyGroups to pass to write_json function later.
def get_edit_textures_settings_dict(self, context):
settings = bpy.context.scene.transmogrifier_settings
textures = bpy.context.scene.transmogrifier_textures

settings_dict = {}

# Global Edit Textures settings to record.
global_edit_textures_settings = {
"link_texture_settings": settings.link_texture_settings,
"regex_textures": settings.regex_textures,
"texture_resolution": settings.texture_resolution,
"texture_format": settings.texture_format,
"image_quality": settings.image_quality,
}

# Update the dictionary with the global Edit Textures settings.
settings_dict.update(global_edit_textures_settings)

# Loop through each PropertyGroup and update the dictionary of settings.
settings_dict.update(get_propertygroups_properties("textures", textures, True, [], False, False, settings_dict))

return settings_dict



Expand All @@ -722,8 +748,13 @@ def load_propertygroup_settings(property_groups, properties, group, properties_t
value = repr(value)

# If a value is a list that contains strings, then change the list to a set.
elif isinstance(value, list) and isinstance(value[0], str):
value = set(value)
elif isinstance(value, list):
# If list is not empty and contains strings, convert the list to a set.
if value and isinstance(value[0], str):
value = set(value)
# If list is empty, convert the list to an empty set.
elif not value:
value = repr(set())

# If an integer is an option of a certain EnumProperty drop down, make it a string.
elif key in ("texture_resolution", "resize_textures_limit", "uv_resolution") and isinstance(value, int):
Expand All @@ -749,9 +780,6 @@ def instantiate_propertygroups(property_groups, properties_list, propertygroup,
# Update settings when a Transmogrifier preset is selected.
def set_settings(self, context):
settings = bpy.context.scene.transmogrifier_settings
imports = bpy.context.scene.transmogrifier_imports
exports = bpy.context.scene.transmogrifier_exports
scripts = bpy.context.scene.transmogrifier_scripts

# Get PropertyGroups.
property_groups = get_propertygroups()
Expand All @@ -760,15 +788,6 @@ def set_settings(self, context):
# Load selected Transmogrifier preset as a dictionary.
transmogrifier_preset_dict = load_transmogrifier_preset('transmogrifier', settings.transmogrifier_preset)

# Clear any existing imports instances.
imports.clear()

# Clear any existing exports instances.
exports.clear()

# Clear any existing custom script instances.
scripts.clear()

# Loop through each PropertyGroup ("settings", "imports", etc.)
for key, value in property_groups.items():
try:
Expand All @@ -778,13 +797,37 @@ def set_settings(self, context):

# If PropertyGroup is a CollectionProperty, instantiate and load settings for each instance.
else:
# Clear any existing CollectionProperty instances.
value[0].clear()
instantiate_propertygroups(property_groups, transmogrifier_preset_dict[key], value[0], value[2])

# If using an old Transmogrifier preset (i.e. without new properties such as "imports" and "scripts"), skip.
except KeyError:
continue


# Update settings when an Edit Textures preset is selected.
def set_texture_settings(self, context):
settings = bpy.context.scene.transmogrifier_settings
textures = bpy.context.scene.transmogrifier_textures

# Get PropertyGroups.
property_groups = get_propertygroups()

if settings.edit_textures_preset != "NO_PRESET":
# Load selected Edit Textures preset as a dictionary.
transmogrifier_preset_dict = load_transmogrifier_preset('transmogrifier/edit_textures', settings.edit_textures_preset)

# Clear any existing textures instances.
textures.clear()

# Load the global Edit Textures settings from the dictionary (e.g. link_texture_settings, regex_textures, etc.).
load_propertygroup_settings(property_groups, transmogrifier_preset_dict, settings, [])

# Instantiate each texture instance and load settings for each.
instantiate_propertygroups(property_groups, transmogrifier_preset_dict["textures"], property_groups["textures"][0], property_groups["textures"][2])



# ░▀▀█░█▀▀░█▀█░█▀█░░░█░█░▀█▀░▀█▀░█░░░▀█▀░▀█▀░▀█▀░█▀▀░█▀▀
# ░░░█░▀▀█░█░█░█░█░░░█░█░░█░░░█░░█░░░░█░░░█░░░█░░█▀▀░▀▀█
Expand Down
6 changes: 3 additions & 3 deletions Operators.py
Original file line number Diff line number Diff line change
Expand Up @@ -480,7 +480,7 @@ def execute(self, context):
preset_json = edit_textures_preset_dir / add_preset_name

# Get current Edit Textures settings.
settings_dict = Functions.get_settings_dict(self, context, False, False)
settings_dict = Functions.get_edit_textures_settings_dict(self, context)

# Save new Edit Textures operator preset as JSON file.
Functions.write_json(settings_dict, preset_json)
Expand All @@ -501,7 +501,7 @@ class TRANSMOGRIFIER_OT_edit_textures_remove_preset(Operator):
def execute(self, context):
# Get selected Edit Textures operator preset.
settings = bpy.context.scene.transmogrifier_settings
remove_preset_name = f"{settings.transmogrifier_preset_enum}.json"
remove_preset_name = f"{settings.edit_textures_preset_enum}.json"

# Set Edit Textures operator preset directory and preset file to be removed.
edit_textures_preset_dir = Path(bpy.utils.user_resource('SCRIPTS', path="presets/operator")) / "transmogrifier" / "edit_textures"
Expand Down Expand Up @@ -559,7 +559,7 @@ def execute(self, context):
exec(property_assignment)

# Load preset (Update settings and UI from preset file).
Functions.set_settings(self, context)
Functions.set_texture_settings(self, context)

self.report({'INFO'}, f"Added Edit Textures preset: {preset_name}")
return {'FINISHED'}
Expand Down
102 changes: 5 additions & 97 deletions Settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -189,14 +189,14 @@ class TRANSMOGRIFIER_PG_TransmogrifierSettings(PropertyGroup):
description="Modify image textures with regular expressions, resizing, and/or reformatting.",
default=True,
)
edit_textures_preset: StringProperty(default='FBX_to_GLB')
edit_textures_preset: StringProperty(default='PBR_Standard')
edit_textures_preset_enum: EnumProperty(
name="", options={'SKIP_SAVE'},
description="Use texture edit settings from a preset.\n(Create by clicking '+' after adjusting settings in the Edit Textures menu)",
items=lambda self, context: Functions.get_transmogrifier_presets('transmogrifier'),
get=lambda self: Functions.get_transmogrifier_preset_index('transmogrifier', self.transmogrifier_preset),
set=lambda self, value: setattr(self, 'transmogrifier_preset', Functions.transmogrifier_preset_enum_items_refs['transmogrifier'][value][0]),
update=Functions.set_settings,
items=lambda self, context: Functions.get_transmogrifier_presets('transmogrifier/edit_textures'),
get=lambda self: Functions.get_transmogrifier_preset_index('transmogrifier/edit_textures', self.edit_textures_preset),
set=lambda self, value: setattr(self, 'edit_textures_preset', Functions.transmogrifier_preset_enum_items_refs['transmogrifier/edit_textures'][value][0]),
update=Functions.set_texture_settings,
)
link_texture_settings: BoolProperty(
name="Link Texture Settings",
Expand Down Expand Up @@ -241,11 +241,6 @@ class TRANSMOGRIFIER_PG_TransmogrifierSettings(PropertyGroup):
description="Use textures already linked to .blend file",
default=False,
)
resize_textures: BoolProperty(
name="Resize Textures",
description="Set a custom image texture resolution for exported models without affecting resolution of original/source texture files\nTextures will not be upscaled",
default=True,
)
texture_resolution: EnumProperty(
name="Resolution",
description="Set a custom image texture resolution for exported models without affecting resolution of original/source texture files",
Expand All @@ -261,43 +256,6 @@ class TRANSMOGRIFIER_PG_TransmogrifierSettings(PropertyGroup):
default="1024",
update=lambda self, context: Functions.update_texture_settings(self, context),
)
# Which textures to include in resizing.
texture_resolution_include: EnumProperty(
name="Included Textures",
options={'ENUM_FLAG'},
items=[
('BaseColor', "BaseColor", "", 1),
('Subsurface', "Subsurface", "", 2),
('Metallic', "Metallic", "", 4),
('Specular', "Specular", "", 16),
('Roughness', "Roughness", "", 32),
('Normal', "Normal", "", 64),
('Bump', "Bump", "", 128),
('Displacement', "Displacement", "", 256),
('Emission', "Emission", "", 512),
('Opacity', "Opacity", "", 1024),
('Occlusion', "Occlusion", "", 2048),
],
description="Filter texture maps to resize",
default={
'BaseColor',
'Subsurface',
'Metallic',
'Specular',
'Roughness',
'Normal',
'Bump',
'Displacement',
'Emission',
'Opacity',
'Occlusion'
},
)
reformat_textures: BoolProperty(
name="Reformat Textures",
description="Set a custom texture image type for exported models without affecting resolution of original/source texture files",
default=True,
)
texture_format: EnumProperty(
name="Format",
description="Set a custom texture image type for exported models without affecting resolution of original/source texture files",
Expand All @@ -321,38 +279,6 @@ class TRANSMOGRIFIER_PG_TransmogrifierSettings(PropertyGroup):
soft_max=100,
step=5,
)
# Which textures to include in converting.
texture_format_include: EnumProperty(
name="Included Textures",
options={'ENUM_FLAG'},
items=[
('BaseColor', "BaseColor", "", 1),
('Subsurface', "Subsurface", "", 2),
('Metallic', "Metallic", "", 4),
('Specular', "Specular", "", 16),
('Roughness', "Roughness", "", 32),
('Normal', "Normal", "", 64),
('Bump', "Bump", "", 128),
('Displacement', "Displacement", "", 256),
('Emission', "Emission", "", 512),
('Opacity', "Opacity", "", 1024),
('Occlusion', "Occlusion", "", 2048),
],
description="Filter texture maps to convert",
default={
'BaseColor',
'Subsurface',
'Metallic',
'Specular',
'Roughness',
'Normal',
'Bump',
'Displacement',
'Emission',
'Opacity',
'Occlusion'
},
)
# Set all UV map names to "UVMap". This prevents a material issue with USDZ's - when object A and object B share the same material, but their UV
# map names differ, the material has to pick one UVMap in the UV Map node inputs connected to each texture channel. So if object A's UV map is called
# "UVMap" but object B's UV map is called "UV_Channel", then the shared material may pick "UV_Channel" as the UV inputs, thus causing object A to appear
Expand Down Expand Up @@ -1023,12 +949,6 @@ class TRANSMOGRIFIER_PG_TransmogrifierExports(PropertyGroup):

class TRANSMOGRIFIER_PG_TransmogrifierTextures(PropertyGroup):

show_settings: BoolProperty(
name="Show/Hide texture settings",
description="",
default=True,
)

texture_map: EnumProperty(
name="PBR Map",
items=[
Expand All @@ -1048,12 +968,6 @@ class TRANSMOGRIFIER_PG_TransmogrifierTextures(PropertyGroup):
default='BaseColor',
)

resize_texture: BoolProperty(
name="Resize Textures",
description="Set a custom image texture resolution for exported models without affecting resolution of original/source texture files\nTextures will not be upscaled",
default=True,
)

texture_resolution: EnumProperty(
name="Resolution",
description="Set a custom image texture resolution for exported models without affecting resolution of original/source texture files",
Expand All @@ -1069,12 +983,6 @@ class TRANSMOGRIFIER_PG_TransmogrifierTextures(PropertyGroup):
default="1024",
)

reformat_texture: BoolProperty(
name="Reformat Textures",
description="Set a custom texture image type for exported models without affecting resolution of original/source texture files",
default=True,
)

texture_format: EnumProperty(
name="Format",
description="Set a custom texture image type for exported models without affecting resolution of original/source texture files",
Expand Down
17 changes: 7 additions & 10 deletions UI.py
Original file line number Diff line number Diff line change
Expand Up @@ -359,16 +359,13 @@ def draw_settings_textures(self, context):
props = row.operator('transmogrifier.remove_texture', text = "", icon = 'PANEL_CLOSE')
props.index = index

if len(textures) > 1 or (len(textures) == 1 and settings.link_texture_settings):
if settings.link_texture_settings and (settings.resize_textures or settings.reformat_textures):
box_edit_textures.use_property_split = True
col = box_edit_textures.column(align=True)
if settings.resize_textures:
col.prop(settings, 'texture_resolution')
if settings.reformat_textures:
col.prop(settings, 'texture_format')
if settings.texture_format in lossy_compression_support:
col.prop(settings, 'image_quality')
if len(textures) >= 1 and settings.link_texture_settings:
box_edit_textures.use_property_split = True
col = box_edit_textures.column(align=True)
col.prop(settings, 'texture_resolution')
col.prop(settings, 'texture_format')
if settings.texture_format in lossy_compression_support:
col.prop(settings, 'image_quality')

self.layout.separator(factor = 0.25)

Expand Down

0 comments on commit 4494874

Please sign in to comment.