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

74 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
class op(bpy.types.Operator):
2019-12-18 20:53:16 +00:00
bl_idname = "uv.textools_meshtex_trim"
bl_label = "Trim"
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
2019-12-24 19:59:46 +00:00
2019-12-18 20:53:16 +00:00
if len(bpy.context.selected_objects) >= 1:
# Find a UV mesh
if utilities_meshtex.find_uv_mesh(bpy.context.selected_objects):
# Find 1 or more meshes to wrap
2019-12-24 19:59:46 +00:00
if len(utilities_meshtex.find_texture_meshes(bpy.context.selected_objects)) > 0:
2019-12-18 20:53:16 +00:00
return True
2019-06-08 23:42:50 +00:00
2019-12-18 20:53:16 +00:00
return False
2019-06-08 23:42:50 +00:00
2019-12-18 20:53:16 +00:00
def execute(self, context):
trim(self)
return {'FINISHED'}
2019-06-08 23:42:50 +00:00
def trim(self):
2019-12-24 19:59:46 +00:00
# Wrap the mesh texture around the
2019-12-18 20:53:16 +00:00
print("Trim Mesh Texture :)")
2019-06-08 23:42:50 +00:00
2019-12-18 20:53:16 +00:00
# Collect UV mesh
obj_uv = utilities_meshtex.find_uv_mesh(bpy.context.selected_objects)
if not obj_uv:
2019-12-24 19:59:46 +00:00
self.report({'ERROR_INVALID_INPUT'}, "No UV mesh found")
2019-12-18 20:53:16 +00:00
return
2019-06-08 23:42:50 +00:00
2019-12-18 20:53:16 +00:00
# Collect texture meshes
2019-12-24 19:59:46 +00:00
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
if len(obj_textures) == 0:
2019-12-24 19:59:46 +00:00
self.report({'ERROR_INVALID_INPUT'},
"No meshes found for mesh textures")
2019-12-18 20:53:16 +00:00
return
2019-06-08 23:42:50 +00:00
2019-12-18 20:53:16 +00:00
# Setup Thickness
utilities_meshtex.uv_mesh_fit(obj_uv, obj_textures)
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:
name = "Trim UV"
2019-06-08 23:42:50 +00:00
2019-12-18 20:53:16 +00:00
if name in obj.modifiers:
2019-12-24 19:59:46 +00:00
obj.modifiers.remove(obj.modifiers[name])
2019-06-08 23:42:50 +00:00
2019-12-18 20:53:16 +00:00
modifier_bool = obj.modifiers.new(name=name, type='BOOLEAN')
modifier_bool.object = obj_uv
2019-06-08 23:42:50 +00:00
2019-12-24 19:59:46 +00:00
bpy.ops.ui.textools_popup(
'INVOKE_DEFAULT', message="Collapse modifiers before wrapping")
2019-06-08 23:42:50 +00:00
bpy.utils.register_class(op)