Skip to content

Commit

Permalink
Scene publishing support
Browse files Browse the repository at this point in the history
  • Loading branch information
keianhzo committed Dec 13, 2023
1 parent f172034 commit 6dca7d7
Show file tree
Hide file tree
Showing 5 changed files with 791 additions and 180 deletions.
92 changes: 92 additions & 0 deletions addons/io_hubs_addon/api.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
import requests


def create_room(endpoint, token=None, scene_name=None, scene_id=None):
payload = {}
if scene_id:
payload = {
"hub": {
"name": scene_name,
"scene_id": scene_id
}
}

headers = {}
if token:
headers = {
"content-type": "application/json",
"authorization": f'Bearer {token}'
}

import json
body = json.dumps(payload)

url = f'{endpoint}/api/v1/hubs'
resp = requests.post(url, body, headers=headers)

return resp.json()


def upload_media(endpoint, file):
resp = requests.post(f'{endpoint}/api/v1/media', files={'media': (
'glb', file, 'application/octet-stream')}, verify=False)
json = resp.json()

json = resp.json()
if "error" in json:
raise Exception(f'Unknown error')

return {
"file_id": json.get("file_id"),
"access_token": json.get("meta").get("access_token")
}


def publish_scene(endpoint, token, scene_data, scene_id=None):
headers = {
"content-type": "application/json",
"authorization": f'Bearer {token}'
}

import json
body = json.dumps({"scene": scene_data})

url = f'{endpoint}/api/v1/scenes{"/" + scene_id if scene_id else ""}'
if scene_id:
resp = requests.put(url, body, headers=headers)
else:
resp = requests.post(url, body, headers=headers)

json = resp.json()
if "error" in json:
error = json.get("error")
if error == "invalid_token":
raise Exception("Authentication error")
else:
raise Exception(f'Unknown error: {error}')

return json


def get_projects(endpoint, token):
headers = {
"content-type": "application/json",
"authorization": f'Bearer {token}'
}

resp = requests.get(
f'{endpoint}/api/v1/scenes/projectless', headers=headers)

import json
json = resp.json()
if "error" in json:
error = json.get("error")
if error == "invalid_token":
raise Exception("Authentication error")
else:
raise Exception(f'Unknown error: {error}')

if not "scenes" in json:
raise Exception(f'Projects request error')
scenes = json.get("scenes")
return scenes
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ def draw(self, context, layout, panel):
sub_row.enabled = False
add_link_indicator(sub_row, self.backgroundTexture)

row.context_pointer_set("hubs_component", self)
row.context_pointer_set("target", self)
row.context_pointer_set("host", context.scene)
op = row.operator("image.hubs_open_image", text='', icon='FILE_FOLDER')
op.target_property = "backgroundTexture"
Expand All @@ -112,7 +112,7 @@ def draw(self, context, layout, panel):
sub_row.enabled = False
add_link_indicator(sub_row, self.envMapTexture)

row.context_pointer_set("hubs_component", self)
row.context_pointer_set("target", self)
row.context_pointer_set("host", context.scene)
op = row.operator("image.hubs_open_image", text='', icon='FILE_FOLDER')
op.target_property = "envMapTexture"
Expand Down
6 changes: 3 additions & 3 deletions addons/io_hubs_addon/components/operators.py
Original file line number Diff line number Diff line change
Expand Up @@ -646,14 +646,14 @@ def execute(self, context):
self.report({'INFO'}, "Open image cancelled. No image selected.")
return {'CANCELLED'}

old_img = self.hubs_component[self.target_property]
old_img = getattr(self.target, self.target_property)

# Load/Reload the first image and assign it to the target property, then load the rest of the images if they're not already loaded. This mimics Blender's default open files behavior.
primary_filepath = os.path.join(dirname, self.files[0].name)
primary_img = bpy.data.images.load(
filepath=primary_filepath, check_existing=True)
primary_img.reload()
self.hubs_component[self.target_property] = primary_img
setattr(self.target, self.target_property, primary_img)

for f in self.files[1:]:
bpy.data.images.load(filepath=os.path.join(
Expand All @@ -665,7 +665,7 @@ def execute(self, context):

def invoke(self, context, event):
self.filepath = ""
self.hubs_component = context.hubs_component
self.target = context.target
context.window_manager.fileselect_add(self)
return {'RUNNING_MODAL'}

Expand Down
Loading

0 comments on commit 6dca7d7

Please sign in to comment.