1
0
mirror of https://github.com/drewcassidy/TexTools-Blender synced 2024-06-11 00:26:53 +00:00
Blender-TexTools/op_meshtex_trim_collapse.py

70 lines
2.0 KiB
Python
Raw Normal View History

2019-06-08 23:42:50 +00:00
import bpy
import bmesh
import operator
from mathutils import Vector
from collections import defaultdict
from math import pi
import math
from . import utilities_meshtex
def is_available():
2019-12-18 20:53:16 +00:00
# If the selection contains a boolean modifier
obj_textures = utilities_meshtex.find_texture_meshes(bpy.context.selected_objects)
for obj in obj_textures:
for modifier in obj.modifiers:
if modifier.type == 'BOOLEAN':
return True
return False
2019-06-08 23:42:50 +00:00
class op(bpy.types.Operator):
2019-12-18 20:53:16 +00:00
bl_idname = "uv.textools_meshtex_trimcollapse"
bl_label = "Collapse"
bl_description = "Trim Mesh Texture"
bl_options = {'REGISTER', 'UNDO'}
2019-06-08 23:42:50 +00:00
2019-12-18 20:53:16 +00:00
@classmethod
def poll(cls, context):
if not bpy.context.active_object or bpy.context.active_object.mode != 'OBJECT':
return False
return is_available()
2019-06-08 23:42:50 +00:00
2019-12-18 20:53:16 +00:00
def execute(self, context):
collapse(self)
return {'FINISHED'}
2019-06-08 23:42:50 +00:00
def collapse(self):
2019-12-18 20:53:16 +00:00
# Collect texture meshes
obj_textures = utilities_meshtex.find_texture_meshes( bpy.context.selected_objects )
2019-06-08 23:42:50 +00:00
2019-12-18 20:53:16 +00:00
previous_selection = bpy.context.selected_objects.copy()
previous_active = bpy.context.view_layer.objects.active
2019-06-08 23:42:50 +00:00
2019-12-18 20:53:16 +00:00
if len(obj_textures) == 0:
self.report({'ERROR_INVALID_INPUT'}, "No meshes found for mesh textures" )
return
2019-06-08 23:42:50 +00:00
2019-12-18 20:53:16 +00:00
# Apply bool modifier to trim
for obj in obj_textures:
bpy.ops.object.select_all(action='DESELECT')
obj.select_set( state = True, view_layer = None)
bpy.context.view_layer.objects.active = obj
bpy.ops.object.convert(target='MESH')
2019-06-08 23:42:50 +00:00
2019-12-18 20:53:16 +00:00
# restore selection
bpy.ops.object.select_all(action='DESELECT')
for obj in previous_selection:
obj.select_set( state = True, view_layer = None)
bpy.context.view_layer.objects.active = previous_active
2019-06-08 23:42:50 +00:00
2019-12-18 20:53:16 +00:00
bpy.ops.ui.textools_popup('INVOKE_DEFAULT', message="{}x objects have been collapsed".format(len(obj_textures)))
2019-06-08 23:42:50 +00:00
bpy.utils.register_class(op)