diff options
Diffstat (limited to 'uv_magic_uv/legacy/op/texture_projection.py')
-rw-r--r-- | uv_magic_uv/legacy/op/texture_projection.py | 124 |
1 files changed, 9 insertions, 115 deletions
diff --git a/uv_magic_uv/legacy/op/texture_projection.py b/uv_magic_uv/legacy/op/texture_projection.py index ffcd0baf..bb73138b 100644 --- a/uv_magic_uv/legacy/op/texture_projection.py +++ b/uv_magic_uv/legacy/op/texture_projection.py @@ -23,12 +23,9 @@ __status__ = "production" __version__ = "5.2" __date__ = "17 Nov 2018" -from collections import namedtuple - import bpy import bgl import bmesh -import mathutils from bpy_extras import view3d_utils from bpy.props import ( BoolProperty, @@ -39,110 +36,7 @@ from bpy.props import ( from ... import common from ...utils.bl_class_registry import BlClassRegistry from ...utils.property_class_registry import PropertyClassRegistry - - -__all__ = [ - 'Properties', - 'MUV_OT_TextureProjection', - 'MUV_OT_TextureProjection_Project', -] - - -Rect = namedtuple('Rect', 'x0 y0 x1 y1') -Rect2 = namedtuple('Rect2', 'x y width height') - - -def get_loaded_texture_name(_, __): - items = [(key, key, "") for key in bpy.data.images.keys()] - items.append(("None", "None", "")) - return items - - -def get_canvas(context, magnitude): - """ - Get canvas to be renderred texture - """ - sc = context.scene - prefs = context.preferences.addons["uv_magic_uv"].preferences - - region_w = context.region.width - region_h = context.region.height - canvas_w = region_w - prefs.texture_projection_canvas_padding[0] * 2.0 - canvas_h = region_h - prefs.texture_projection_canvas_padding[1] * 2.0 - - img = bpy.data.images[sc.muv_texture_projection_tex_image] - tex_w = img.size[0] - tex_h = img.size[1] - - center_x = region_w * 0.5 - center_y = region_h * 0.5 - - if sc.muv_texture_projection_adjust_window: - ratio_x = canvas_w / tex_w - ratio_y = canvas_h / tex_h - if sc.muv_texture_projection_apply_tex_aspect: - ratio = ratio_y if ratio_x > ratio_y else ratio_x - len_x = ratio * tex_w - len_y = ratio * tex_h - else: - len_x = canvas_w - len_y = canvas_h - else: - if sc.muv_texture_projection_apply_tex_aspect: - len_x = tex_w * magnitude - len_y = tex_h * magnitude - else: - len_x = region_w * magnitude - len_y = region_h * magnitude - - x0 = int(center_x - len_x * 0.5) - y0 = int(center_y - len_y * 0.5) - x1 = int(center_x + len_x * 0.5) - y1 = int(center_y + len_y * 0.5) - - return Rect(x0, y0, x1, y1) - - -def rect_to_rect2(rect): - """ - Convert Rect1 to Rect2 - """ - - return Rect2(rect.x0, rect.y0, rect.x1 - rect.x0, rect.y1 - rect.y0) - - -def region_to_canvas(rg_vec, canvas): - """ - Convert screen region to canvas - """ - - cv_rect = rect_to_rect2(canvas) - cv_vec = mathutils.Vector() - cv_vec.x = (rg_vec.x - cv_rect.x) / cv_rect.width - cv_vec.y = (rg_vec.y - cv_rect.y) / cv_rect.height - - return cv_vec - - -def is_valid_context(context): - obj = context.object - - # only edit mode is allowed to execute - if obj is None: - return False - if obj.type != 'MESH': - return False - if context.object.mode != 'EDIT': - return False - - # only 'VIEW_3D' space is allowed to execute - for space in context.area.spaces: - if space.type == 'VIEW_3D': - break - else: - return False - - return True +from ...impl import texture_projection_impl as impl @PropertyClassRegistry(legacy=True) @@ -183,7 +77,7 @@ class Properties: scene.muv_texture_projection_tex_image = EnumProperty( name="Image", description="Texture Image", - items=get_loaded_texture_name + items=impl.get_loaded_texture_name ) scene.muv_texture_projection_tex_transparency = FloatProperty( name="Transparency", @@ -237,7 +131,7 @@ class MUV_OT_TextureProjection(bpy.types.Operator): # we can not get area/space/region from console if common.is_console_mode(): return False - return is_valid_context(context) + return impl.is_valid_context(context) @classmethod def is_running(cls, _): @@ -270,7 +164,7 @@ class MUV_OT_TextureProjection(bpy.types.Operator): img = bpy.data.images[sc.muv_texture_projection_tex_image] # setup rendering region - rect = get_canvas(context, sc.muv_texture_projection_tex_magnitude) + rect = impl.get_canvas(context, sc.muv_texture_projection_tex_magnitude) positions = [ [rect.x0, rect.y0], [rect.x0, rect.y1], @@ -336,7 +230,7 @@ class MUV_OT_TextureProjection_Project(bpy.types.Operator): return True if not MUV_OT_TextureProjection.is_running(context): return False - return is_valid_context(context) + return impl.is_valid_context(context) def execute(self, context): sc = context.scene @@ -345,7 +239,7 @@ class MUV_OT_TextureProjection_Project(bpy.types.Operator): self.report({'WARNING'}, "No textures are selected") return {'CANCELLED'} - _, region, space = common.get_space( + _, region, space = common.get_space_legacy( 'VIEW_3D', 'WINDOW', 'VIEW_3D') # get faces to be texture projected @@ -380,10 +274,10 @@ class MUV_OT_TextureProjection_Project(bpy.types.Operator): # transform screen region to canvas v_canvas = [ - region_to_canvas( + impl.region_to_canvas( v, - get_canvas(bpy.context, - sc.muv_texture_projection_tex_magnitude) + impl.get_canvas(bpy.context, + sc.muv_texture_projection_tex_magnitude) ) for v in v_screen ] |