Skip to content

Instantly share code, notes, and snippets.

@aarthificial
Created December 18, 2023 06:01
Show Gist options
  • Save aarthificial/4b1a44bfb1ffba23062665cd46568bff to your computer and use it in GitHub Desktop.
Save aarthificial/4b1a44bfb1ffba23062665cd46568bff to your computer and use it in GitHub Desktop.
bl_info = {
"name": "UV Palette",
"author": "aarthificial",
"version": (1, 0),
"blender": (3, 6, 0),
"location": "View3D > Object",
"description": "Modifies the UV to fit a palette.",
"category": "Object",
}
import bpy
def modify_uv(obj, texture, material):
bpy.context.view_layer.objects.active = obj
bpy.ops.object.mode_set(mode='EDIT')
bpy.ops.mesh.select_all(action='SELECT')
bpy.ops.uv.reset()
bpy.ops.object.mode_set(mode='OBJECT')
uv_map = obj.data.uv_layers["UVMap"]
for index in range(len(uv_map.data)):
uv = uv_map.data[index].uv
uv.x /= 2 * texture
uv.y /= 2 * texture
uv.y += 1 - (0.75 / texture)
uv.x += (material + 0.25) / texture
def custom_context_menu(self, context):
layout = self.layout
layout.separator()
layout.operator_context = "INVOKE_DEFAULT"
layout.operator("object.custom_operator", text="Apply UV Palette")
class CustomOperator(bpy.types.Operator):
bl_idname = "object.custom_operator"
bl_label = "UV Palette Operator"
material_prop: bpy.props.IntProperty(name="Material index", default=0)
size_prop: bpy.props.IntProperty(name="Texture size", default=32)
def execute(self, context):
for obj in bpy.context.selected_objects:
if obj and obj.type == 'MESH':
modify_uv(obj, self.size_prop, self.material_prop)
return {'FINISHED'}
def invoke(self, context, event):
return context.window_manager.invoke_props_dialog(self)
def register():
bpy.utils.register_class(CustomOperator)
bpy.types.VIEW3D_MT_object_context_menu.append(custom_context_menu)
def unregister():
bpy.utils.unregister_class(CustomOperator)
bpy.types.VIEW3D_MT_object_context_menu.remove(custom_context_menu)
if __name__ == "__main__":
register()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment