Skip to content
Snippets Groups Projects
__init__.py 72.3 KiB
Newer Older
Vilem Duha's avatar
Vilem Duha committed
# ##### BEGIN GPL LICENSE BLOCK #####
#
#  This program is free software; you can redistribute it and/or
#  modify it under the terms of the GNU General Public License
#  as published by the Free Software Foundation; either version 2
#  of the License, or (at your option) any later version.
#
#  This program is distributed in the hope that it will be useful,
#  but WITHOUT ANY WARRANTY; without even the implied warranty of
#  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#  GNU General Public License for more details.
#
#  You should have received a copy of the GNU General Public License
#  along with this program; if not, write to the Free Software Foundation,
#  Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
#
# ##### END GPL LICENSE BLOCK #####

bl_info = {
Vilém Duha's avatar
Vilém Duha committed
    "name": "BlenderKit Online Asset Library",
    "author": "Vilem Duha, Petr Dlouhy",
    "version": (2, 93, 0),
    "blender": (2, 93, 0),
Vilem Duha's avatar
Vilem Duha committed
    "location": "View3D > Properties > BlenderKit",
Vilém Duha's avatar
Vilém Duha committed
    "description": "Online BlenderKit library (materials, models, brushes and more). Connects to the internet.",
Vilem Duha's avatar
Vilem Duha committed
    "warning": "",
    "doc_url": "{BLENDER_MANUAL_URL}/addons/3d_view/blenderkit.html",
Vilém Duha's avatar
Vilém Duha committed
    "category": "3D View",
Vilem Duha's avatar
Vilem Duha committed
}

if "bpy" in locals():
    from importlib import reload

    # alphabetically sorted all add-on modules since reload only happens from __init__.
    # modules with _bg are used for background computations in separate blender instance and that's why they don't need reload.
    append_link = reload(append_link)
    asset_bar_op = reload(asset_bar_op)
    asset_inspector = reload(asset_inspector)
    autothumb = reload(autothumb)
    bg_blender = reload(bg_blender)
    bkit_oauth = reload(bkit_oauth)
    categories = reload(categories)
    colors = reload(colors)
    download = reload(download)
    icons = reload(icons)
    image_utils = reload(image_utils)
    oauth = reload(oauth)
    overrides = reload(overrides)
    paths = reload(paths)
    ratings = reload(ratings)
    ratings_utils = reload(ratings_utils)
Vilém Duha's avatar
Vilém Duha committed
    resolutions = reload(resolutions)
    search = reload(search)
    tasks_queue = reload(tasks_queue)
    ui = reload(ui)
    ui_bgl = reload(ui_bgl)
    ui_panels = reload(ui_panels)
    upload = reload(upload)
    upload_bg = reload(upload_bg)
    utils = reload(utils)

    bl_ui_label = reload(bl_ui_label)
    bl_ui_button = reload(bl_ui_button)
    # bl_ui_checkbox = reload(bl_ui_checkbox)
    # bl_ui_slider = reload(bl_ui_slider)
    # bl_ui_up_down = reload(bl_ui_up_down)
    bl_ui_drag_panel = reload(bl_ui_drag_panel)
    bl_ui_draw_op = reload(bl_ui_draw_op)
    # bl_ui_textbox = reload(bl_ui_textbox)

Vilem Duha's avatar
Vilem Duha committed
else:
    from blenderkit import append_link
    from blenderkit import asset_bar_op
    from blenderkit import asset_inspector
    from blenderkit import autothumb
    from blenderkit import bg_blender
    from blenderkit import bkit_oauth
    from blenderkit import categories
    from blenderkit import colors
    from blenderkit import download
    from blenderkit import icons
    from blenderkit import image_utils
    from blenderkit import oauth
    from blenderkit import overrides
    from blenderkit import paths
    from blenderkit import ratings
    from blenderkit import ratings_utils
    from blenderkit import resolutions
    from blenderkit import search
    from blenderkit import tasks_queue
    from blenderkit import ui
    from blenderkit import ui_bgl
    from blenderkit import ui_panels
    from blenderkit import upload
    from blenderkit import upload_bg
    from blenderkit import utils

    from blenderkit.bl_ui_widgets import bl_ui_label
    from blenderkit.bl_ui_widgets import bl_ui_button
    # from blenderkit.bl_ui_widgets import bl_ui_checkbox
    # from blenderkit.bl_ui_widgets import bl_ui_slider
    # from blenderkit.bl_ui_widgets import bl_ui_up_down
    from blenderkit.bl_ui_widgets import bl_ui_drag_panel
    from blenderkit.bl_ui_widgets import bl_ui_draw_op
    # from blenderkit.bl_ui_widgets import bl_ui_textbox

Vilem Duha's avatar
Vilem Duha committed
import os
import math
import time
Vilem Duha's avatar
Vilem Duha committed
import bpy
log = logging.getLogger(__name__)

Vilem Duha's avatar
Vilem Duha committed
from bpy.app.handlers import persistent
import bpy.utils.previews
import mathutils
from mathutils import Vector
from bpy.props import (
    IntProperty,
    FloatProperty,
    FloatVectorProperty,
    StringProperty,
    EnumProperty,
    BoolProperty,
    PointerProperty,
)
from bpy.types import (
    Operator,
    Panel,
    AddonPreferences,
    PropertyGroup,
)

Vilem Duha's avatar
Vilem Duha committed
# logging.basicConfig(filename = 'blenderkit.log', level = logging.INFO,
#                     format = '	%(asctime)s:%(filename)s:%(funcName)s:%(lineno)d:%(message)s')


@persistent
def scene_load(context):
Vilém Duha's avatar
Vilém Duha committed
    if not bpy.app.background:
        search.load_previews()
Vilem Duha's avatar
Vilem Duha committed
    ui_props = bpy.context.scene.blenderkitUI
    ui_props.assetbar_on = False
    ui_props.turn_off = False
Vilem Duha's avatar
Vilem Duha committed
    preferences = bpy.context.preferences.addons['blenderkit'].preferences
    preferences.login_attempt = False
Vilém Duha's avatar
Vilém Duha committed
@bpy.app.handlers.persistent
def check_timers_timer():
    ''' checks if all timers are registered regularly. Prevents possible bugs from stopping the addon.'''
    if not bpy.app.timers.is_registered(search.search_timer):
        bpy.app.timers.register(search.search_timer)
    if not bpy.app.timers.is_registered(download.download_timer):
        bpy.app.timers.register(download.download_timer)
    if not (bpy.app.timers.is_registered(tasks_queue.queue_worker)):
        bpy.app.timers.register(tasks_queue.queue_worker)
    if not bpy.app.timers.is_registered(bg_blender.bg_update):
        bpy.app.timers.register(bg_blender.bg_update)
    return 5.0
Vilém Duha's avatar
Vilém Duha committed

Vilem Duha's avatar
Vilem Duha committed
conditions = (
Vilém Duha's avatar
Vilém Duha committed
    ('UNSPECIFIED', 'Unspecified', ""),
Vilem Duha's avatar
Vilem Duha committed
    ('NEW', 'New', 'Shiny new item'),
    ('USED', 'Used', 'Casually used item'),
    ('OLD', 'Old', 'Old item'),
    ('DESOLATE', 'Desolate', 'Desolate item - dusty & rusty'),
)
model_styles = (
Vilém Duha's avatar
Vilém Duha committed
    ('REALISTIC', 'Realistic', "Photo realistic model"),
    ('PAINTERLY', 'Painterly', 'Hand painted with visible strokes'),
Vilem Duha's avatar
Vilem Duha committed
    ('LOWPOLY', 'Lowpoly', "Lowpoly art -don't mix up with polycount!"),
    ('ANIME', 'Anime', 'Anime style'),
    ('2D_VECTOR', '2D Vector', '2D vector'),
    ('3D_GRAPHICS', '3D Graphics', '3D graphics'),
Vilém Duha's avatar
Vilém Duha committed
    ('OTHER', 'Other', 'Other styles'),
Vilem Duha's avatar
Vilem Duha committed
)
search_model_styles = (
Vilém Duha's avatar
Vilém Duha committed
    ('REALISTIC', 'Realistic', "Photo realistic model"),
    ('PAINTERLY', 'Painterly', 'Hand painted with visible strokes'),
Vilem Duha's avatar
Vilem Duha committed
    ('LOWPOLY', 'Lowpoly', "Lowpoly art -don't mix up with polycount!"),
    ('ANIME', 'Anime', 'Anime style'),
    ('2D_VECTOR', '2D Vector', '2D vector'),
    ('3D_GRAPHICS', '3D Graphics', '3D graphics'),
Vilem Duha's avatar
Vilem Duha committed
    ('OTHER', 'Other', 'Other Style'),
    ('ANY', 'Any', 'Any Style'),
)
material_styles = (
Vilém Duha's avatar
Vilém Duha committed
    ('REALISTIC', 'Realistic', "Photo realistic model"),
    ('NPR', 'Non photorealistic', 'Hand painted with visible strokes'),
Vilem Duha's avatar
Vilem Duha committed
    ('OTHER', 'Other', 'Other style'),
)
search_material_styles = (
Vilém Duha's avatar
Vilém Duha committed
    ('REALISTIC', 'Realistic', "Photo realistic model"),
    ('NPR', 'Non photorealistic', 'Hand painted with visible strokes'),
Vilem Duha's avatar
Vilem Duha committed
    ('ANY', 'Any', 'Any'),
)
engines = (
Vilém Duha's avatar
Vilém Duha committed
    ('CYCLES', 'Cycles', 'Blender Cycles'),
    ('EEVEE', 'Eevee', 'Blender eevee renderer'),
    ('OCTANE', 'Octane', 'Octane render enginge'),
    ('ARNOLD', 'Arnold', 'Arnold render engine'),
Vilem Duha's avatar
Vilem Duha committed
    ('V-RAY', 'V-Ray', 'V-Ray renderer'),
    ('UNREAL', 'Unreal', 'Unreal engine'),
    ('UNITY', 'Unity', 'Unity engine'),
    ('GODOT', 'Godot', 'Godot engine'),
    ('3D-PRINT', '3D printer', 'object can be 3D printed'),
Vilem Duha's avatar
Vilem Duha committed
    ('OTHER', 'Other', 'any other engine'),
Vilem Duha's avatar
Vilem Duha committed
)
pbr_types = (
    ('METALLIC', 'Metallic-Roughness', 'Metallic/Roughness PBR material type'),
    ('SPECULAR', 'Specular  Glossy', ''),
)

mesh_poly_types = (
    ('QUAD', 'quad', ''),
    ('QUAD_DOMINANT', 'quad_dominant', ''),
    ('TRI_DOMINANT', 'tri_dominant', ''),
    ('TRI', 'tri', ''),
    ('NGON', 'ngon_dominant', ''),
    ('OTHER', 'other', ''),
)



Vilém Duha's avatar
Vilém Duha committed
def udate_down_up(self, context):
    """Perform a search if results are empty."""
    s = context.scene
    wm = bpy.context.window_manager
Vilém Duha's avatar
Vilém Duha committed
    props = s.blenderkitUI
    if wm.get('search results') == None and props.down_up == 'SEARCH':
Vilém Duha's avatar
Vilém Duha committed
        search.search()
Vilém Duha's avatar
Vilém Duha committed

Vilem Duha's avatar
Vilem Duha committed
def switch_search_results(self, context):
    s = bpy.context.scene
    wm = bpy.context.window_manager
Vilem Duha's avatar
Vilem Duha committed
    props = s.blenderkitUI
    if props.asset_type == 'MODEL':
        wm['search results'] = wm.get('bkit model search')
        wm['search results orig'] = wm.get('bkit model search orig')
Vilem Duha's avatar
Vilem Duha committed
    elif props.asset_type == 'SCENE':
        wm['search results'] = wm.get('bkit scene search')
        wm['search results orig'] = wm.get('bkit scene search orig')
    elif props.asset_type == 'HDR':
        wm['search results'] = wm.get('bkit hdr search')
        wm['search results orig'] = wm.get('bkit hdr search orig')
Vilem Duha's avatar
Vilem Duha committed
    elif props.asset_type == 'MATERIAL':
        wm['search results'] = wm.get('bkit material search')
        wm['search results orig'] = wm.get('bkit material search orig')
Vilem Duha's avatar
Vilem Duha committed
    elif props.asset_type == 'TEXTURE':
        wm['search results'] = wm.get('bkit texture search')
        wm['search results orig'] = wm.get('bkit texture search orig')
Vilem Duha's avatar
Vilem Duha committed
    elif props.asset_type == 'BRUSH':
        wm['search results'] = wm.get('bkit brush search')
        wm['search results orig'] = wm.get('bkit brush search orig')
        if not (context.sculpt_object or context.image_paint_object):
            ui.add_report(
                'Switch to paint or sculpt mode to search in BlenderKit brushes.')

Vilem Duha's avatar
Vilem Duha committed
    search.load_previews()
    if wm['search results'] == None and props.down_up == 'SEARCH':
Vilém Duha's avatar
Vilém Duha committed
        search.search()
def asset_type_callback(self, context):
    '''
    Returns
    items for Enum property, depending on the down_up property - BlenderKit is either in search or in upload mode.

    '''
    user_preferences = bpy.context.preferences.addons['blenderkit'].preferences

    if self.down_up == 'SEARCH':
        items = (
Vilém Duha's avatar
Vilém Duha committed
            ('MODEL', 'Models', 'Find models', 'OBJECT_DATAMODE', 0),
            ('MATERIAL', 'Materials', 'Find materials', 'MATERIAL', 2),
            # ('TEXTURE', 'Texture', 'Browse textures', 'TEXTURE', 3),
Vilém Duha's avatar
Vilém Duha committed
            ('SCENE', 'Scenes', 'Find scenes', 'SCENE_DATA', 3),
            ('HDR', 'HDRs', 'Find HDRs', 'WORLD', 4),
            ('BRUSH', 'Brushes', 'Find brushes', 'BRUSH_DATA', 5)
Vilém Duha's avatar
Vilém Duha committed
            ('MODEL', 'Model', 'Upload a model', 'OBJECT_DATAMODE', 0),
            # ('SCENE', 'SCENE', 'Browse scenes', 'SCENE_DATA', 1),
Vilém Duha's avatar
Vilém Duha committed
            ('MATERIAL', 'Material', 'Upload a material', 'MATERIAL', 2),
            # ('TEXTURE', 'Texture', 'Browse textures', 'TEXTURE', 3),
Vilém Duha's avatar
Vilém Duha committed
            ('SCENE', 'Scene', 'Upload a scene', 'SCENE_DATA', 3),
            ('HDR', 'HDR', 'Upload a HDR', 'WORLD', 4),
            ('BRUSH', 'Brush', 'Upload a brush', 'BRUSH_DATA', 5)
    return items
Vilém Duha's avatar
Vilém Duha committed

Vilem Duha's avatar
Vilem Duha committed
class BlenderKitUIProps(PropertyGroup):
    down_up: EnumProperty(
        name="Download vs Upload",
        items=(
Vilém Duha's avatar
Vilém Duha committed
            ('SEARCH', 'Search', 'Activate searching', 'VIEWZOOM', 0),
            ('UPLOAD', 'Upload', 'Activate uploading', 'COPYDOWN', 1),
            # ('RATING', 'Rating', 'Activate rating', 'SOLO_ON', 2)
Vilem Duha's avatar
Vilem Duha committed
        ),
Vilém Duha's avatar
Loading
Loading full blame...