Use the new Blender ID API
This commit is contained in:
parent
5396fd765d
commit
2c4c102302
@ -3,8 +3,6 @@
|
|||||||
Separated from __init__.py so that we can import & run from non-Blender environments.
|
Separated from __init__.py so that we can import & run from non-Blender environments.
|
||||||
"""
|
"""
|
||||||
|
|
||||||
import os.path
|
|
||||||
|
|
||||||
import bpy
|
import bpy
|
||||||
from bpy.types import AddonPreferences, Operator, WindowManager, Scene
|
from bpy.types import AddonPreferences, Operator, WindowManager, Scene
|
||||||
from bpy.props import StringProperty
|
from bpy.props import StringProperty
|
||||||
@ -50,7 +48,7 @@ class BlenderCloudPreferences(AddonPreferences):
|
|||||||
blender_id_help = "To login, go to the Blender ID add-on preferences."
|
blender_id_help = "To login, go to the Blender ID add-on preferences."
|
||||||
else:
|
else:
|
||||||
blender_id_icon = 'WORLD_DATA'
|
blender_id_icon = 'WORLD_DATA'
|
||||||
blender_id_text = "You are logged in as %s." % blender_id_profile['username']
|
blender_id_text = "You are logged in as %s." % blender_id_profile.username
|
||||||
blender_id_help = "To logout or change profile, " \
|
blender_id_help = "To logout or change profile, " \
|
||||||
"go to the Blender ID add-on preferences."
|
"go to the Blender ID add-on preferences."
|
||||||
|
|
||||||
@ -82,8 +80,12 @@ class PillarCredentialsUpdate(Operator):
|
|||||||
|
|
||||||
@classmethod
|
@classmethod
|
||||||
def is_logged_in(cls, context):
|
def is_logged_in(cls, context):
|
||||||
active_user_id = getattr(context.window_manager, 'blender_id_active_profile', None)
|
try:
|
||||||
return bool(active_user_id)
|
import blender_id
|
||||||
|
except ImportError:
|
||||||
|
return False
|
||||||
|
|
||||||
|
return blender_id.is_logged_in()
|
||||||
|
|
||||||
def execute(self, context):
|
def execute(self, context):
|
||||||
# Only allow activation when the user is actually logged in.
|
# Only allow activation when the user is actually logged in.
|
||||||
|
@ -34,12 +34,16 @@ def cache_directory(*subdirs) -> str:
|
|||||||
|
|
||||||
from . import pillar
|
from . import pillar
|
||||||
|
|
||||||
profile = pillar.blender_id_profile() or {'username': 'anonymous'}
|
profile = pillar.blender_id_profile()
|
||||||
|
if profile:
|
||||||
|
username = profile.username
|
||||||
|
else:
|
||||||
|
username = 'anonymous'
|
||||||
|
|
||||||
# TODO: use bpy.utils.user_resource('CACHE', ...)
|
# TODO: use bpy.utils.user_resource('CACHE', ...)
|
||||||
# once https://developer.blender.org/T47684 is finished.
|
# once https://developer.blender.org/T47684 is finished.
|
||||||
user_cache_dir = appdirs.user_cache_dir(appname='Blender', appauthor=False)
|
user_cache_dir = appdirs.user_cache_dir(appname='Blender', appauthor=False)
|
||||||
cache_dir = os.path.join(user_cache_dir, 'blender_cloud', profile['username'], *subdirs)
|
cache_dir = os.path.join(user_cache_dir, 'blender_cloud', username, *subdirs)
|
||||||
|
|
||||||
os.makedirs(cache_dir, mode=0o700, exist_ok=True)
|
os.makedirs(cache_dir, mode=0o700, exist_ok=True)
|
||||||
|
|
||||||
|
@ -58,19 +58,13 @@ def save_as_json(pillar_resource, json_filename):
|
|||||||
json.dump(pillar_resource, outfile, sort_keys=True, cls=pillarsdk.utils.PillarJSONEncoder)
|
json.dump(pillar_resource, outfile, sort_keys=True, cls=pillarsdk.utils.PillarJSONEncoder)
|
||||||
|
|
||||||
|
|
||||||
def blender_id_profile() -> dict:
|
def blender_id_profile() -> 'blender_id.BlenderIdProfile':
|
||||||
"""Returns the Blender ID profile of the currently logged in user."""
|
"""Returns the Blender ID profile of the currently logged in user."""
|
||||||
|
|
||||||
# Allow overriding before we import the bpy module.
|
# Allow overriding before we import the bpy module.
|
||||||
if _testing_blender_id_profile is not None:
|
if _testing_blender_id_profile is not None:
|
||||||
return _testing_blender_id_profile
|
return _testing_blender_id_profile
|
||||||
|
|
||||||
import bpy
|
|
||||||
|
|
||||||
active_user_id = getattr(bpy.context.window_manager, 'blender_id_active_profile', None)
|
|
||||||
if not active_user_id:
|
|
||||||
return None
|
|
||||||
|
|
||||||
import blender_id
|
import blender_id
|
||||||
return blender_id.get_active_profile()
|
return blender_id.get_active_profile()
|
||||||
|
|
||||||
@ -100,9 +94,9 @@ def pillar_api(pillar_endpoint: str = None) -> pillarsdk.Api:
|
|||||||
pillarsdk.Api.requests_session = cache.requests_session()
|
pillarsdk.Api.requests_session = cache.requests_session()
|
||||||
|
|
||||||
_pillar_api = pillarsdk.Api(endpoint=pillar_endpoint,
|
_pillar_api = pillarsdk.Api(endpoint=pillar_endpoint,
|
||||||
username=profile['username'],
|
username=profile.username,
|
||||||
password=None,
|
password=None,
|
||||||
token=profile['token'])
|
token=profile.token)
|
||||||
|
|
||||||
return _pillar_api
|
return _pillar_api
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user