Skip to content

Commit

Permalink
feature updates
Browse files Browse the repository at this point in the history
and code cleanups
  • Loading branch information
wenchien authored Dec 28, 2022
1 parent fabff54 commit c58dab6
Show file tree
Hide file tree
Showing 3 changed files with 78 additions and 91 deletions.
35 changes: 19 additions & 16 deletions Material_To_VTF/GUI.py
Original file line number Diff line number Diff line change
@@ -1,18 +1,13 @@
import bpy
from . import VTFLibOperators as vtf_operator
from bpy.props import BoolVectorProperty
from bpy.props import (StringProperty,
BoolProperty,
IntProperty,
PointerProperty,
CollectionProperty,
EnumProperty
)
from bpy.types import (Panel,
Operator,
AddonPreferences,
PropertyGroup,
)
from bpy.types import (PropertyGroup)

# ------------------------------------------------------------------------
# Property classes
Expand All @@ -31,20 +26,16 @@ class UI_Settings(PropertyGroup):
maxlen=1024,
subtype='DIR_PATH')

# ------------------------------------------------------------------------
# GUI
# ------------------------------------------------------------------------

# GUI Utility
class VTF_UIList(bpy.types.UIList):
def draw_item(self, context, layout, data, item, icon, active_data, active_propname, index):
#split = layout.split(factor=0.5)
layout.prop(item, "material_checkbox", text=item.material_name, icon='MATERIAL_DATA')

def invoke(self, context, event):
pass


# Main
# GUI Main
class VTFLibConverter(bpy.types.Panel):
"""Creates a Panel for streamlining material to vtf process in the scene context of the properties editor"""
bl_label = "VTFLibConverter"
Expand All @@ -71,12 +62,12 @@ def draw(self, context):

# VTFCMD Path textbox
row = layout.row()
row.label(text="VTFCmd Path: ")
row.label(text="VTFCmd Path *: ")
row = layout.row()
row.prop(scene.VTFLibCmd, "path")

row = layout.row()
row.label(text="Material Ouptut Path:")
row.label(text="Material Ouptut Path *:")
row = layout.row()
row.prop(scene.material_path, "path")

Expand Down Expand Up @@ -111,6 +102,14 @@ def draw(self, context):
row = layout.row(align=True)
row.prop(scene, "vmt_shader_op")

row = layout.row(align=True)
row.prop(scene, "vmt_param_additive", text="Additive?")
row.prop(scene, "vmt_param_translucent", text="Translucent?")

row = layout.row(align=True)
row.prop(scene, "vmt_param_nocull", text="No Cull?")

# Convert button
row = layout.row()
row.operator("object.vtf_operator")

Expand All @@ -128,12 +127,14 @@ def register():
for cls in classes:
bpy.utils.register_class(cls)
bpy.types.Scene.mats_collection = CollectionProperty(type=Material_List)
#bpy.types.Scene.mats_collection_ptr = PointerProperty(type=Material_List)
bpy.types.Scene.mats_index = IntProperty()
bpy.types.Scene.VTFLibCmd = PointerProperty(type=UI_Settings)
bpy.types.Scene.material_path = PointerProperty(type=UI_Settings)
bpy.types.Scene.resize_bool = BoolProperty(name="")
bpy.types.Scene.vmt_shader_bool = BoolProperty(name="")
bpy.types.Scene.vmt_param_additive = BoolProperty(default=False)
bpy.types.Scene.vmt_param_translucent = BoolProperty(default=False)
bpy.types.Scene.vmt_param_nocull = BoolProperty(default=False)

bpy.types.Scene.clamp_op = EnumProperty(
name="Clamp: ",
Expand Down Expand Up @@ -212,7 +213,6 @@ def unregister():
bpy.utils.unregister_class(cls)
del bpy.types.Scene.VTFLibCmd
del bpy.types.Scene.mats_collection
#del bpy.types.Scene.mats_collection_ptr
del bpy.types.Scene.mats_index
del bpy.types.Scene.material_path
del bpy.types.Scene.resize_bool
Expand All @@ -224,5 +224,8 @@ def unregister():
del bpy.types.Scene.format_op
del bpy.types.Scene.vmt_shader_bool
del bpy.types.Scene.vmt_shader_op
del bpy.types.Scene.vmt_param_additive
del bpy.types.Scene.vmt_param_translucent
del bpy.types.Scene.vmt_param_nocull


132 changes: 58 additions & 74 deletions Material_To_VTF/VTFLibOperators.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,22 +9,6 @@
PointerProperty,
)

# To-Do
class find_objects_by_selected_material(bpy.types.Operator):
bl_idname = "object.find_objects_by_selected_material"
bl_icon = 'ANIM_DATA'
bl_label = ""

@classmethod
def poll(cls, context):
return True

def execute(self, context):
# for object in context.scene.objects:
# for material_slot in object.material_slots:
return {'FINISHED'}


class update_materials_list(bpy.types.Operator):
bl_idname = "object.update_materials_list"
bl_label = "Refresh Materials List"
Expand Down Expand Up @@ -57,20 +41,12 @@ def add_all_materials_to_set(self, context):
else:
return True

#def tag_redraw(context, space_type="PROPERTIES", region_type="WINDOW"):
# for window in context.window_manager.windows:
# for area in window.screen.areas:
# if area.spaces[0].type == space_type:
# for region in area.regions:
# if region.type == region_type:
# region.tag_redraw()

def execute(self, context):
# Clean collection first
bpy.context.scene.mats_collection.clear()
if self.add_all_materials_to_set(context):
print("Stuff")
#self.tag_redraw(context)
return {'FINISHED'}

class vtf_operator(bpy.types.Operator):
Expand All @@ -87,60 +63,58 @@ def poll(cls, context):
return context.scene.VTFLibCmd != "" and context.scene.material_path != ""

def execute(self, context):
#main(context)
scene = context.scene

image_path_list = list()
image_name_to_material_name = dict()
for mat_object in scene.mats_collection:
material = mat_object.material
if material.node_tree.nodes["Principled BSDF"] is None:
self.report({'ERROR'}, "Please change your material type to Principled BSDF")
return {'CANCELLED'}

# Get Principled BSDF node
# and check if a link exists
if material.node_tree.nodes["Principled BSDF"].inputs["Base Color"].is_linked:
node = None

# if a link exists, then there is a image texture, presumably
linked_nodes = material.node_tree.nodes["Principled BSDF"].inputs["Base Color"].links

for node_link in linked_nodes:
node = node_link.from_node
if "Image Texture" == node.bl_label:
print(node.bl_label)
break
if node is None:
self.report({'ERROR'}, "No Image Texture node is found")
return {'CANCELLED'}

if node.image is None:
self.report({'ERROR'}, "No source image found")
if mat_object.material_checkbox:
material = mat_object.material
if material.node_tree.nodes["Principled BSDF"] is None:
self.report({'ERROR'}, "Please change your material type to Principled BSDF")
return {'CANCELLED'}

if node.image.is_dirty:
self.report({'ERROR'}, "There are unsaved changes to your source image. Please save it first")
# Get Principled BSDF node
# and check if a link / user exists
if material.node_tree.nodes["Principled BSDF"].inputs["Base Color"].is_linked:
node = None

# if a link exists, then there is a image texture, presumably
linked_nodes = material.node_tree.nodes["Principled BSDF"].inputs["Base Color"].links

for node_link in linked_nodes:
node = node_link.from_node
if "Image Texture" == node.bl_label:
print(node.bl_label)
break
if node is None:
self.report({'ERROR'}, "No Image Texture node is found")
return {'CANCELLED'}

if node.image is None:
self.report({'ERROR'}, "No source image found")
return {'CANCELLED'}

if node.image.is_dirty:
self.report({'ERROR'}, "There are unsaved changes to your source image. Please save it first")
return {'CANCELLED'}

image_path_raw = node.image.filepath_raw

# Convert to readable abs path
full_abs_path = bpy.path.abspath(image_path_raw)
real_abs_path = os.path.realpath(full_abs_path)
image_path_list.append(real_abs_path)
image_name_to_material_name[real_abs_path] = mat_object.material_name

else:
# No img texture found
# bake color?
print()
self.report({'ERROR'}, "Unsupported operation. Base color isn't linked to a TEX_IMAGE")
return {'CANCELLED'}

image_path_raw = node.image.filepath_raw

# Convert to readable abs path
full_abs_path = bpy.path.abspath(image_path_raw)
real_abs_path = os.path.realpath(full_abs_path)
image_path_list.append(real_abs_path)
image_name_to_material_name[real_abs_path] = mat_object.material_name

else:
# No img texture found
# bake color?
print()
self.report({'ERROR'}, "Unsupported operation. Base color isn't linked to a TEX_IMAGE")
return {'CANCELLED'}
#print(color[0], color[1], color[2], color[3])


# Get VTFLibCmd
VTFLibCmdFilePath = scene.VTFLibCmd.path + "VTFCmd.exe"
MaterialOutputPath = scene.material_path.path
if not os.path.exists(VTFLibCmdFilePath) or not os.path.exists(MaterialOutputPath):
Expand All @@ -150,9 +124,9 @@ def execute(self, context):
command_line = list()
command_line.append(VTFLibCmdFilePath)

file_count = 0
# Get all materials and add to a args list
# Requires cleanup later
file_count = 0
for image_path in image_path_list:
file_count += 1
# Copy image first, keeping the original
Expand Down Expand Up @@ -191,26 +165,36 @@ def execute(self, context):
command_line.append("-shader")
command_line.append("\"" + context.scene.vmt_shader_op + "\"")

if context.scene.vmt_param_additive:
command_line.append("-param")
command_line.append("\"additive\"" + " \"1\"")

if context.scene.vmt_param_translucent:
command_line.append("-param")
command_line.append("\"translucent\"" + " \"1\"")

if context.scene.vmt_param_nocull:
command_line.append("-param")
command_line.append("\"nocull\"" + " \"1\"")

# Define output folder
command_line.append("-output")
command_line.append("\"" + MaterialOutputPath[:-1] + "\"")

self.report({'INFO'}, ' '.join(command_line))
# Continue with execution with subprocess

# Continue with subprocess execution
VTFLibCmd_subprocess = subprocess.Popen(' '.join(command_line), stdin=subprocess.DEVNULL, stderr=subprocess.PIPE,stdout=subprocess.PIPE, shell=True)
(out, error) = VTFLibCmd_subprocess.communicate()
print(out, error)

if VTFLibCmd_subprocess.returncode == 0:
self.report({'INFO'}, "Done processing " + str(file_count) + " files")


##VTFLibCmd_subprocess.stdout.close()
##VTFLibCmd_subprocess.wait()
return {'FINISHED'}

classes = (
update_materials_list, vtf_operator, find_objects_by_selected_material
update_materials_list, vtf_operator
)

def register():
Expand Down
2 changes: 1 addition & 1 deletion Material_To_VTF/__init__.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
bl_info = {
'name': 'Materials_To_VTF',
'category': 'All',
'version': (1, 0, 0),
'version': (1, 1, 0),
'blender': (2, 93, 5)
}

Expand Down

0 comments on commit c58dab6

Please sign in to comment.