Skip to content

Instantly share code, notes, and snippets.

@denetii
Created August 14, 2020 23:02
Show Gist options
  • Save denetii/6e66891505b32fc7c64c22031288d7f4 to your computer and use it in GitHub Desktop.
Save denetii/6e66891505b32fc7c64c22031288d7f4 to your computer and use it in GitHub Desktop.
import bpy
from bpy.props import *
# New texture slot class
class LegacyTextureSlot(bpy.types.PropertyGroup):
texture = PointerProperty(type=bpy.types.Texture)
name = StringProperty(name="Name", description="Name for this texture slot")
uv_layer = StringProperty(name="UV Slot")
bpy.utils.register_class(LegacyTextureSlot)
# Define new material slot properties (used in 2.8x addon)
bpy.types.Material.th_texture_slots = CollectionProperty(type=LegacyTextureSlot)
bpy.types.Material.th_texture_slot_index = IntProperty(default=-1)
# Copy data from BI texture slots into new properties
for mat in bpy.data.materials:
for tex_slot in mat.texture_slots:
if tex_slot == None or not tex_slot.use: continue
new_texslot = mat.th_texture_slots.add()
new_texslot.uv_layer = tex_slot.uv_layer
new_texslot.name = tex_slot.name
new_texslot.texture = tex_slot.texture
import bpy
import bmesh
import io_thps_scene.material
# Move the 'alpha' vertex color values to the actual alpha channel in 'color'
bm = bmesh.new()
meshes = [o for o in bpy.data.objects if o.type == 'MESH']
for ob in meshes:
bm.clear()
bm.from_mesh(ob.data)
vc_layer = bm.loops.layers.color.get("color")
bake_layer = bm.loops.layers.color.get("bake")
alpha_layer = bm.loops.layers.color.get("alpha")
if alpha_layer:
# Create color layer if it none exist, or use the first layer
if not vc_layer:
if len(bm.loops.layers.color):
#ob.data.vertex_colors[0].name = "color"
#bm.loops.layers.color[0].name = "color"
vc_layer = bm.loops.layers.color[0]
else:
vc_layer = bm.loops.layers.color.new("color")
for face in bm.faces:
for loop in face.loops:
if bake_layer:
loop[bake_layer][3] = loop[alpha_layer][0]
loop[vc_layer][3] = loop[alpha_layer][0]
# Remove alpha layer
bm.loops.layers.color.remove(alpha_layer)
bm.to_mesh(ob.data)
bm.free()
# Finally, make sure shader nodes are generated for the materials
for mat in bpy.data.materials:
io_thps_scene.material.update_node_tree(None, None, mat)
# Your scene should now be fully migrated to work with the 2.8x addon!
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment