Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Automatically generate a model attribution file #134

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -65,6 +65,10 @@ If this fits your usecase better, you can also select the "Import from url" opti

<p align="center"><img style="max-width:100%" src="https://user-images.githubusercontent.com/52042414/158480653-568f6a91-bcd4-4009-b927-4d5ffc400658.png"></p>

When importing publicly available models, a text file named **sf_attributions** will automatically be created inside Blender. The creator credits and license informations for the downloaded models will be appended to this file. It can be accessed in the **Text Editor** workspace.

<p align="center"><img style="max-width:100%" src="https://dl.dropbox.com/scl/fi/wbj15y2wfekxof06r9v0a/Screenshot-2024-06-15-155042.png?rlkey=8pk0yk768znwu7pxc8yk7e0cv&st=17d46hjr&dl=0"></p>

<br>

## Export a model to Sketchfab
39 changes: 39 additions & 0 deletions __init__.py
Original file line number Diff line number Diff line change
@@ -1780,6 +1780,8 @@ class SketchfabDownloadModel(bpy.types.Operator):
def execute(self, context):
skfb_api = context.window_manager.sketchfab_browser.skfb_api
skfb_api.download_model(self.model_uid)
attribution = SF_Attributions()
attribution.request_model_attributions(self.model_uid)
return {'FINISHED'}


@@ -2405,6 +2407,43 @@ def register():
# If a cache path was set in preferences, use it
updateCacheDirectory(None, context=bpy.context)

class SF_Attributions:

def append_to_attributions(self, text):
# Check if "sf_attributions" text file already exists
if "sf_attributions" not in bpy.data.texts:
# Create a new text file named "sf_attributions"
text_block = bpy.data.texts.new(name="sf_attributions")
print("Text file 'sf_attributions' created.")
else:
# Get the existing text file
text_block = bpy.data.texts["sf_attributions"]

# Move the cursor to the end of the text block
text_block.cursor_set(len(text_block.as_string()))

# Append the new text
text_block.write(text + "\n")
print("Credits appended to 'sf_attributions' file.")

def request_model_attributions(self, uid, callback=None):
self.uid = uid
callback = self.handle_model_attributions if callback is None else callback
url = Config.SKETCHFAB_MODEL + '/' + uid

model_infothr = GetRequestThread(url, callback)
model_infothr.start()

def handle_model_attributions(self, r, *args, **kwargs):
json_data = r.json()
try:
self.append_to_attributions(f"\"{json_data['name']}\" ({json_data['viewerUrl']}) by {json_data['user']['username']} is licensed under {json_data['license']['fullName']} ({json_data['license']['url']}).")
except KeyError as err:
try:
self.append_to_attributions(f"Could not get attribution for UID: {self.uid}.")
except Exception as inner_err:
print(inner_err)

def unregister():
for cls in classes:
bpy.utils.unregister_class(cls)