Created
June 30, 2022 18:50
-
-
Save Marc-Ducret/65defa494458cfe5d73142379cc53adb to your computer and use it in GitHub Desktop.
Blender Addon to setup Unity LOD variations
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
bl_info = { | |
"name": "Unity LOD Setup", | |
"blender": (3, 0, 0), | |
"category": "Object", | |
} | |
import bpy | |
class SetupUnityLOD(bpy.types.Operator): | |
"""Setup Unity LOD""" | |
bl_idname = "object.setup_unity_lod" | |
bl_label = "Setup Unity LOD" | |
bl_options = {'REGISTER', 'UNDO'} | |
def duplicate(self, object): | |
duplicated_object = object.copy() | |
object.users_collection[0].objects.link(duplicated_object) | |
return duplicated_object | |
def execute(self, context): | |
scene = context.scene | |
object = context.active_object | |
if object is None: | |
return {'FINISHED'} | |
name = bpy.path.basename(bpy.context.blend_data.filepath).removesuffix('.blend') | |
if object.users_collection[0].name.startswith('export-'): | |
sub_name = object.users_collection[0].name[7:] | |
name = f'{name}-{sub_name}' | |
context.view_layer.objects.active = object | |
for i in [0, 1, 2]: | |
lod = self.duplicate(object) if i > 0 else object | |
lod.name = f'{name}_LOD{i}' | |
decimate = lod.modifiers.get('LOD-Decimate') or lod.modifiers.new('LOD-Decimate', 'DECIMATE') | |
decimate.decimate_type = 'COLLAPSE' | |
decimate.use_collapse_triangulate = True | |
decimate.ratio = 1 / (2 ** i) | |
return {'FINISHED'} | |
def menu_func(self, context): | |
self.layout.operator(SetupUnityLOD.bl_idname) | |
def register(): | |
bpy.utils.register_class(SetupUnityLOD) | |
bpy.types.VIEW3D_MT_object.append(menu_func) # Adds the new operator to an existing menu. | |
def unregister(): | |
bpy.utils.unregister_class(SetupUnityLOD) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment