Added start of image sharing.
Sharing an image datablock works, if it has been saved and not packed. Directly sharing a file, and dirty/packed datablocks are for a future commit.
This commit is contained in:
@@ -70,15 +70,17 @@ def register():
|
||||
sys.modules[modname] = module
|
||||
return module
|
||||
|
||||
reload_mod('blendfile')
|
||||
reload_mod('home_project')
|
||||
|
||||
blender = reload_mod('blender')
|
||||
gui = reload_mod('gui')
|
||||
async_loop = reload_mod('async_loop')
|
||||
settings_sync = reload_mod('settings_sync')
|
||||
|
||||
reload_mod('blendfile')
|
||||
reload_mod('home_project')
|
||||
image_sharing = reload_mod('image_sharing')
|
||||
else:
|
||||
from . import blender, gui, async_loop, settings_sync, blendfile, home_project
|
||||
from . import (blender, gui, async_loop, settings_sync, blendfile, home_project,
|
||||
image_sharing)
|
||||
|
||||
async_loop.setup_asyncio_executor()
|
||||
async_loop.register()
|
||||
@@ -86,6 +88,7 @@ def register():
|
||||
gui.register()
|
||||
blender.register()
|
||||
settings_sync.register()
|
||||
image_sharing.register()
|
||||
|
||||
|
||||
def _monkey_patch_requests():
|
||||
@@ -106,8 +109,9 @@ def _monkey_patch_requests():
|
||||
|
||||
|
||||
def unregister():
|
||||
from . import blender, gui, async_loop, settings_sync
|
||||
from . import blender, gui, async_loop, settings_sync, image_sharing
|
||||
|
||||
image_sharing.unregister()
|
||||
settings_sync.unregister()
|
||||
blender.unregister()
|
||||
gui.unregister()
|
||||
|
@@ -12,7 +12,9 @@ from bpy.props import StringProperty, EnumProperty, PointerProperty
|
||||
from . import pillar, gui
|
||||
|
||||
PILLAR_SERVER_URL = 'https://cloudapi.blender.org/'
|
||||
PILLAR_WEB_SERVER_URL = 'https://cloudapi.blender.org/'
|
||||
# PILLAR_SERVER_URL = 'http://localhost:5000/'
|
||||
# PILLAR_WEB_SERVER_URL = 'http://pillar_web:5001/'
|
||||
|
||||
ADDON_NAME = 'blender_cloud'
|
||||
log = logging.getLogger(__name__)
|
||||
|
188
blender_cloud/image_sharing.py
Normal file
188
blender_cloud/image_sharing.py
Normal file
@@ -0,0 +1,188 @@
|
||||
import logging
|
||||
import pathlib
|
||||
import tempfile
|
||||
|
||||
import bpy
|
||||
import pillarsdk
|
||||
from pillarsdk import exceptions as sdk_exceptions
|
||||
from .pillar import pillar_call
|
||||
from . import async_loop, pillar, home_project
|
||||
from .blender import PILLAR_WEB_SERVER_URL
|
||||
|
||||
REQUIRES_ROLES_FOR_IMAGE_SHARING = {'subscriber', 'demo'}
|
||||
IMAGE_SHARING_GROUP_NODE_NAME = 'Image sharing'
|
||||
log = logging.getLogger(__name__)
|
||||
|
||||
|
||||
async def find_image_sharing_group_id(home_project_id, user_id):
|
||||
# Find the top-level image sharing group node.
|
||||
try:
|
||||
share_group, created = await pillar.find_or_create_node(
|
||||
where={'project': home_project_id,
|
||||
'node_type': 'group',
|
||||
'parent': None,
|
||||
'name': IMAGE_SHARING_GROUP_NODE_NAME},
|
||||
additional_create_props={
|
||||
'user': user_id,
|
||||
'properties': {},
|
||||
},
|
||||
projection={'_id': 1},
|
||||
may_create=True)
|
||||
except pillar.PillarError:
|
||||
log.exception('Pillar error caught')
|
||||
raise pillar.PillarError('Unable to find image sharing folder on the Cloud')
|
||||
|
||||
return share_group['_id']
|
||||
|
||||
|
||||
class PILLAR_OT_image_share(pillar.PillarOperatorMixin,
|
||||
async_loop.AsyncModalOperatorMixin,
|
||||
bpy.types.Operator):
|
||||
bl_idname = 'pillar.image_share'
|
||||
bl_label = 'Share an image via Blender Cloud'
|
||||
bl_description = 'Uploads an image for sharing via Blender Cloud'
|
||||
|
||||
log = logging.getLogger('bpy.ops.%s' % bl_idname)
|
||||
|
||||
home_project_id = None
|
||||
share_group_id = None # top-level share group node ID
|
||||
user_id = None
|
||||
|
||||
target = bpy.props.EnumProperty(
|
||||
items=[
|
||||
('FILE', 'File', 'Upload an image file'),
|
||||
('DATABLOCK', 'Datablock', 'Upload an image datablock'),
|
||||
],
|
||||
name='target')
|
||||
|
||||
name = bpy.props.StringProperty(name='name',
|
||||
description='File or datablock name to sync')
|
||||
|
||||
def invoke(self, context, event):
|
||||
async_loop.AsyncModalOperatorMixin.invoke(self, context, event)
|
||||
|
||||
self.log.info('Starting sharing')
|
||||
self._new_async_task(self.async_execute(context))
|
||||
return {'RUNNING_MODAL'}
|
||||
|
||||
async def async_execute(self, context):
|
||||
"""Entry point of the asynchronous operator."""
|
||||
|
||||
self.report({'INFO'}, 'Communicating with Blender Cloud')
|
||||
|
||||
try:
|
||||
# Refresh credentials
|
||||
try:
|
||||
self.user_id = await self.check_credentials(context,
|
||||
REQUIRES_ROLES_FOR_IMAGE_SHARING)
|
||||
self.log.debug('Found user ID: %s', self.user_id)
|
||||
except pillar.NotSubscribedToCloudError:
|
||||
self.log.exception('User not subscribed to cloud.')
|
||||
self.report({'ERROR'}, 'Please subscribe to the Blender Cloud.')
|
||||
self._state = 'QUIT'
|
||||
return
|
||||
except pillar.CredentialsNotSyncedError:
|
||||
self.log.exception('Error checking/refreshing credentials.')
|
||||
self.report({'ERROR'}, 'Please log in on Blender ID first.')
|
||||
self._state = 'QUIT'
|
||||
return
|
||||
|
||||
# Find the home project.
|
||||
try:
|
||||
self.home_project_id = await home_project.get_home_project_id()
|
||||
except sdk_exceptions.ForbiddenAccess:
|
||||
self.log.exception('Forbidden access to home project.')
|
||||
self.report({'ERROR'}, 'Did not get access to home project.')
|
||||
self._state = 'QUIT'
|
||||
return
|
||||
except sdk_exceptions.ResourceNotFound:
|
||||
self.report({'ERROR'}, 'Home project not found.')
|
||||
self._state = 'QUIT'
|
||||
return
|
||||
|
||||
try:
|
||||
gid = await find_image_sharing_group_id(self.home_project_id,
|
||||
self.user_id)
|
||||
self.share_group_id = gid
|
||||
self.log.debug('Found group node ID: %s', self.share_group_id)
|
||||
except sdk_exceptions.ForbiddenAccess:
|
||||
self.log.exception('Unable to find Group ID')
|
||||
self.report({'ERROR'}, 'Unable to find sync folder.')
|
||||
self._state = 'QUIT'
|
||||
return
|
||||
|
||||
await self.share_image(context)
|
||||
except Exception as ex:
|
||||
self.log.exception('Unexpected exception caught.')
|
||||
self.report({'ERROR'}, 'Unexpected error: %s' % ex)
|
||||
|
||||
self._state = 'QUIT'
|
||||
|
||||
async def share_image(self, context):
|
||||
"""Sends files to the Pillar server."""
|
||||
|
||||
self.report({'INFO'}, 'Uploading %s %s' % (self.target.lower(), self.name))
|
||||
if self.target == 'FILE':
|
||||
await self.upload_file(pathlib.Path(self.name))
|
||||
else:
|
||||
await self.upload_datablock()
|
||||
|
||||
async def upload_file(self, filename: pathlib.Path):
|
||||
"""Uploads a file to the cloud, attached to the image sharing node."""
|
||||
|
||||
self.log.info('Uploading file %s', filename)
|
||||
node = await pillar_call(pillarsdk.Node.create_asset_from_file,
|
||||
self.home_project_id,
|
||||
self.share_group_id,
|
||||
'image',
|
||||
str(filename),
|
||||
extra_where={'user': self.user_id})
|
||||
self.log.info('Created node %s', node['_id'])
|
||||
self.report({'INFO'}, 'File succesfully uploaded to the cloud!')
|
||||
|
||||
import webbrowser
|
||||
import urllib.parse
|
||||
url = urllib.parse.urljoin(PILLAR_WEB_SERVER_URL, '/p/p-home/%s' % node['_id'])
|
||||
self.log.info('Opening browser at %s', url)
|
||||
webbrowser.open_new_tab(url)
|
||||
|
||||
async def upload_datablock(self):
|
||||
"""Saves a datablock to file if necessary, then upload."""
|
||||
|
||||
self.log.info('Uploading datablock %s' % self.name)
|
||||
datablock = bpy.data.images[self.name]
|
||||
|
||||
if datablock.is_dirty:
|
||||
# TODO: support dirty datablocks.
|
||||
self.report({'ERROR'}, 'Datablock is dirty, save it first.')
|
||||
return
|
||||
|
||||
if datablock.packed_file is not None:
|
||||
# TODO: support packed files.
|
||||
self.report({'ERROR'}, 'Packed files are not supported yet.')
|
||||
return
|
||||
|
||||
filepath = pathlib.Path(bpy.path.abspath(datablock.filepath))
|
||||
await self.upload_file(filepath)
|
||||
|
||||
|
||||
def image_editor_menu(self, context):
|
||||
image = context.space_data.image
|
||||
|
||||
if image and image.has_data:
|
||||
props = self.layout.operator(PILLAR_OT_image_share.bl_idname,
|
||||
text='Share on Blender Cloud')
|
||||
props.target = 'DATABLOCK'
|
||||
props.name = context.space_data.image.name
|
||||
|
||||
|
||||
def register():
|
||||
bpy.utils.register_class(PILLAR_OT_image_share)
|
||||
|
||||
bpy.types.IMAGE_HT_header.append(image_editor_menu)
|
||||
|
||||
|
||||
def unregister():
|
||||
bpy.utils.unregister_class(PILLAR_OT_image_share)
|
||||
|
||||
bpy.types.IMAGE_HT_header.remove(image_editor_menu)
|
@@ -720,32 +720,31 @@ async def find_or_create_node(where: dict,
|
||||
|
||||
found_node = await pillar_call(pillarsdk.Node.find_first, params, caching=False)
|
||||
|
||||
created = False
|
||||
if found_node is None:
|
||||
if not may_create:
|
||||
return None, False
|
||||
if found_node is not None:
|
||||
return found_node, False
|
||||
|
||||
log.info('Creating new sync group node')
|
||||
if not may_create:
|
||||
return None, False
|
||||
|
||||
# Augment the node properties to form a complete node.
|
||||
node_props = where.copy()
|
||||
if additional_create_props:
|
||||
node_props.update(additional_create_props)
|
||||
# Augment the node properties to form a complete node.
|
||||
node_props = where.copy()
|
||||
if additional_create_props:
|
||||
node_props.update(additional_create_props)
|
||||
|
||||
found_node = pillarsdk.Node.new(node_props)
|
||||
created_ok = await pillar_call(found_node.create)
|
||||
if not created_ok:
|
||||
log.error('Blender Cloud addon: unable to create node on the Cloud.')
|
||||
raise PillarError('Unable to create node on the Cloud')
|
||||
created = True
|
||||
log.debug('Creating new node %s', node_props)
|
||||
created_node = pillarsdk.Node.new(node_props)
|
||||
created_ok = await pillar_call(created_node.create)
|
||||
if not created_ok:
|
||||
log.error('Blender Cloud addon: unable to create node on the Cloud.')
|
||||
raise PillarError('Unable to create node on the Cloud')
|
||||
|
||||
return found_node, created
|
||||
return created_node, True
|
||||
|
||||
|
||||
async def attach_file_to_group(file_path: pathlib.Path,
|
||||
home_project_id: str,
|
||||
group_node_id: str,
|
||||
user_id: str=None) -> pillarsdk.Node:
|
||||
user_id: str = None) -> pillarsdk.Node:
|
||||
"""Creates an Asset node and attaches a file document to it."""
|
||||
|
||||
node = await pillar_call(pillarsdk.Node.create_asset_from_file,
|
||||
|
Reference in New Issue
Block a user