text
stringlengths
1
93.6k
# a copy of this software and associated documentation files (the
# "Software"), to deal in the Software without restriction, including
# without limitation the rights to use, copy, modify, merge, publish,
# distribute, sublicense, and/or sell copies of the Software, and to
# permit persons to whom the Software is furnished to do so, subject to
# the following conditions:
# The above copyright notice and this permission notice shall be
# included in all copies or substantial portions of the Software.
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
# MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
# NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
# LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
# OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
# WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
# <FILESEP>
from pathlib import Path
import bgl
import bpy
import bpy_extras
import gpu
import logging
import numpy as np
import os
import time
from gpu_extras.batch import batch_for_shader
from mathutils import Vector
from .quicksnap_utils import State
from .quicksnap_utils import dump
if bpy.app.version >= (3, 4, 0):
from .quicksnap_shader_gpu_module import shader_2d_image_color, shader_2d_uniform_color, shader_3d_uniform_color, shader_3d_smooth_color, shader_3d_polyline_smooth_color
else:
from .quicksnap_shader_legacy import shader_2d_image_color, shader_2d_uniform_color, shader_3d_uniform_color, shader_3d_smooth_color, shader_3d_polyline_smooth_color
__name_addon__ = '.'.join(__name__.split('.')[:-1])
logger = logging.getLogger(__name_addon__)
square_indices = ((0, 1), (1, 2), (2, 3), (3, 0))
icons = {}
def draw_square_2d(position_x, position_y, size, color=(1, 1, 0, 1), line_width=1, point_width=4):
"""
Draw a 2D square of size {size}, color {color}, line_width, and draw 2D point of width {point_width}
if line_width==0: Only draw point.
if point_width==0: Only draw square.
"""
if line_width != 1:
gpu.state.line_width_set(line_width)
gpu.state.blend_set("ALPHA")
if line_width > 0:
vertices = (
(position_x - size, position_y - size),
(position_x + size, position_y - size),
(position_x + size, position_y + size),
(position_x - size, position_y + size))
batch = batch_for_shader(shader_2d_uniform_color, 'LINES', {"pos": vertices}, indices=square_indices)
shader_2d_uniform_color.bind()
shader_2d_uniform_color.uniform_float("color", color)
batch.draw(shader_2d_uniform_color)
if point_width > 0:
gpu.state.point_size_set(point_width)
batch = batch_for_shader(shader_2d_uniform_color, 'POINTS', {"pos": [(position_x, position_y)]})
shader_2d_uniform_color.bind()
shader_2d_uniform_color.uniform_float("color", color)
batch.draw(shader_2d_uniform_color)
gpu.state.point_size_set(5)
if line_width != 1:
gpu.state.line_width_set(1)
gpu.state.blend_set("NONE")
def draw_image(self, position_x=100, position_y=100, size=20, image='MISSING', color=(1, 1, 1, 1),
color_bg=(0, 0, 0, 1), fade=1):
"""
Draw an icon in the viewport.
"""
halfsize = size * .5
vertices = (
(position_x - halfsize, position_y - halfsize),
(position_x + halfsize, position_y - halfsize),
(position_x + halfsize, position_y + halfsize),
(position_x - halfsize, position_y + halfsize))
previous_blend_state = gpu.state.blend_get()
gpu.state.blend_set("ALPHA")
if image not in icons:
texture_name = f'QUICKSNAP_{image}.tif'
texture_path = get_icons_dir() / texture_name
if not texture_path.exists():
texture_path = get_icons_dir() / f'QUICKSNAP_MISSING.tif'
if not texture_path.exists():
return
if texture_name in bpy.data.images:
bpy.data.images.remove(bpy.data.images[f'QUICKSNAP_{image}.tif'])