Skip to content

Commit

Permalink
first working implementation of the newsfeed system
Browse files Browse the repository at this point in the history
  • Loading branch information
samytichadou committed Nov 7, 2020
1 parent 11ea35f commit c51f90e
Show file tree
Hide file tree
Showing 10 changed files with 211 additions and 10 deletions.
6 changes: 6 additions & 0 deletions __init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,11 +55,14 @@
from .op_submit_template import ANTEMPLATES_OT_submit_template
from .op_refresh_addon_version import ANTEMPLATES_OT_refresh_addon_version
from .op_submission_guidelines import ANTEMPLATES_OT_submission_guidelines
from .op_newsfeed_actions import ANTEMPLATES_OT_newsfeed_actions
from .op_search_select_nodetree import ANTEMPLATES_OT_search_select_nodetree

from .gui import (
ANTEMPLATES_PT_templates_panel,
ANTEMPLATES_PT_import_options_subpanel,
ANTEMPLATES_PT_nodetree_infos_subpanel,
ANTEMPLATES_PT_news_panel,
ANTEMPLATES_PT_utilities_panel,
ANTEMPLATES_PT_submission_panel,
ANTEMPLATES_PT_updater_subpanel,
Expand Down Expand Up @@ -101,10 +104,13 @@
ANTEMPLATES_OT_submit_template,
ANTEMPLATES_OT_refresh_addon_version,
ANTEMPLATES_OT_submission_guidelines,
ANTEMPLATES_OT_newsfeed_actions,
ANTEMPLATES_OT_search_select_nodetree,

ANTEMPLATES_PT_templates_panel,
ANTEMPLATES_PT_import_options_subpanel,
ANTEMPLATES_PT_nodetree_infos_subpanel,
ANTEMPLATES_PT_news_panel,
ANTEMPLATES_PT_utilities_panel,
ANTEMPLATES_UL_panel_ui_list,
ANTEMPLATES_PT_submission_panel,
Expand Down
45 changes: 45 additions & 0 deletions gui.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

from .op_create_manifest import get_separated_tags
from .addon_prefs import get_addon_preferences
from .string_formatting_functions import split_string_on_spaces


class ANTEMPLATES_PT_templates_panel(bpy.types.Panel):
Expand Down Expand Up @@ -145,6 +146,50 @@ def draw(self, context):
col.label(text=tag_reformat)


class ANTEMPLATES_PT_news_panel(bpy.types.Panel):
bl_idname = "ANTEMPLATES_PT_news_panel"
bl_label = "News"
bl_space_type = "NODE_EDITOR"
bl_region_type = "UI"
bl_category = "Templates"


@classmethod
def poll(cls, context):
if context.area.ui_type == "an_AnimationNodeTree":
return True


def draw(self, context):

properties_coll = context.window_manager.an_templates_properties

active_news = properties_coll.news[properties_coll.active_news]

count = str(properties_coll.active_news + 1) + "/" + str(len(properties_coll.news))

layout = self.layout

row = layout.row(align=True)
row.label(text=count)
row.operator("antemplates.newsfeed_actions", text="", icon="TRIA_LEFT").action = "PREV"
row.operator("antemplates.newsfeed_actions", text="", icon="TRIA_RIGHT").action = "NEXT"

col = layout.column(align=True)

for line in split_string_on_spaces(active_news.name, 25):
col.label(text=line)

col.separator()

if active_news.url != "":
col.operator("antemplates.open_url", text="URL", icon="URL").url = active_news.url

if active_news.nodetree_name != "":
col.operator("antemplates.search_select_nodetree", text="Select", icon="RESTRICT_SELECT_OFF").name = active_news.nodetree_name
col.operator("antemplates.import_nodetree", text="Import", icon="IMPORT").from_name = active_news.nodetree_name


class ANTEMPLATES_PT_utilities_panel(bpy.types.Panel):
bl_idname = "ANTEMPLATES_PT_utilities_panel"
bl_label = "Utilities"
Expand Down
28 changes: 25 additions & 3 deletions newsfeed_functions.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,37 @@
import bpy
import os

from .addon_prefs import get_addon_preferences
from .global_variables import newsfeed_file, newsfeed_url
from .internet_functions import download_file
from .json_functions import read_json, load_json_in_collection
from .print_functions import print_and_report


# reload newsfeed
def reload_newsfeed(manifest_dataset):
# download newsfeed file if needed
def download_newsfeed(manifest_dataset):
newsfeed_path = os.path.join(get_addon_preferences().download_folder, newsfeed_file)

if not os.path.isfile(newsfeed_path):
download_file(newsfeed_url, newsfeed_path)
print_and_report(None, "Downloading Newsfeed", "INFO") #debug
return True

elif manifest_dataset["newsfeed_hash"] != read_json(newsfeed_path)["newsfeed_hash"]:
download_file(newsfeed_url, newsfeed_path)
print_and_report(None, "Downloading Newsfeed", "INFO") #debug
return True

return False


# reload newsfeed datas
def reload_newsfeed():

print_and_report(None, "Loading Newsfeed", "INFO") #debug

newsfeed_path = os.path.join(get_addon_preferences().download_folder, newsfeed_file)

properties_coll = bpy.data.window_managers[0].an_templates_properties

return
load_json_in_collection(read_json(newsfeed_path), properties_coll.news, 'news')
3 changes: 3 additions & 0 deletions op_create_newsfeed.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ def invoke(self, context, event):
new_news = temp_news.add()
new_news.name = n["name"]
new_news.url = n["url"]
new_news.nodetree_name = n["nodetree_name"]

return context.window_manager.invoke_props_dialog(self)

Expand All @@ -75,6 +76,7 @@ def draw(self, context):
op.action = "DEL"
op.index = idx
col.prop(n, "url", text="", icon="URL")
col.prop(n, "nodetree_name", text="", icon="NODETREE")
idx += 1

layout.operator("antemplates.news_temp_actions", text="Create News", icon="ADD").action = "ADD"
Expand Down Expand Up @@ -107,6 +109,7 @@ def execute(self, context):
new_news = {}
new_news["name"] = n.name
new_news["url"] = n.url
new_news["nodetree_name"] = n.nodetree_name
datas["news"].append(new_news)

datas["newsfeed_hash"] = generate_hash(10)
Expand Down
12 changes: 11 additions & 1 deletion op_import_nodetree.py
Original file line number Diff line number Diff line change
Expand Up @@ -209,6 +209,7 @@ class ANTEMPLATES_OT_import_nodetree(bpy.types.Operator):
bl_label = "Import Nodetree"
bl_options = {'REGISTER', 'INTERNAL'}

from_name : bpy.props.StringProperty(default="")

@classmethod
def poll(cls, context):
Expand All @@ -224,7 +225,16 @@ def execute(self, context):
winman = context.window_manager
nodetree_collection = winman.an_templates_nodetrees
properties_coll = winman.an_templates_properties
nodetree = nodetree_collection[properties_coll.nodetrees_index]

if self.from_name:
try:
nodetree = nodetree_collection[self.from_name]
except KeyError:
print_and_report(self, "Unable to Find Nodetree : " + self.from_name, "WARNING") #debug
return {'FINISHED'}

else:
nodetree = nodetree_collection[properties_coll.nodetrees_index]

prefs = get_addon_preferences()

Expand Down
41 changes: 41 additions & 0 deletions op_newsfeed_actions.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
import bpy


class ANTEMPLATES_OT_newsfeed_actions(bpy.types.Operator):
bl_idname = "antemplates.newsfeed_actions"
bl_label = "Newsfeed Actions"
bl_description = "Go to Previous/Next News"
bl_options = {'REGISTER', 'INTERNAL'}

action: bpy.props.EnumProperty(
items=(
('PREV', "Previous", ""),
('NEXT', "Next", ""),
)
)


@classmethod
def poll(cls, context):
return len(context.window_manager.an_templates_properties.news) not in {0,1}


def invoke(self, context, event):

properties_coll = context.window_manager.an_templates_properties

idx = properties_coll.active_news
total = len(properties_coll.news)

if self.action == 'PREV':
if idx - 1 in range(0, total):
context.window_manager.an_templates_properties.active_news -= 1
else:
context.window_manager.an_templates_properties.active_news = total - 1
else:
if idx + 1 in range(0, total):
context.window_manager.an_templates_properties.active_news += 1
else:
context.window_manager.an_templates_properties.active_news = 0

return {"FINISHED"}
14 changes: 9 additions & 5 deletions op_refresh_templates.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
from .print_functions import print_and_report
from .op_create_manifest import get_global_k
from .op_submit_template import get_addon_version
from .newsfeed_functions import reload_newsfeed
from .newsfeed_functions import download_newsfeed, reload_newsfeed


# check offline available nodetrees
Expand Down Expand Up @@ -73,11 +73,15 @@ def load_manifest(self, internet_connection):
print_and_report(None, "Checking Downloaded Nodetrees", "INFO") #debug
check_downloaded_nodetrees()

# reload k
reload_global_k(manifest_dataset)
if internet_connection:

# get newsfeed
reload_newsfeed(manifest_dataset)
# reload k
reload_global_k(manifest_dataset)

# get newsfeed
print_and_report(None, "Checking Newsfeed", "INFO") #debug
download_newsfeed(manifest_dataset)
reload_newsfeed()

return True

Expand Down
46 changes: 46 additions & 0 deletions op_search_select_nodetree.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
import bpy

from .print_functions import print_and_report


class ANTEMPLATES_OT_search_select_nodetree(bpy.types.Operator):
bl_idname = "antemplates.search_select_nodetree"
bl_label = "Select Nodetree"
bl_description = "Select Nodetree in Templates Panel"
bl_options = {'REGISTER', 'INTERNAL'}

name : bpy.props.StringProperty(default="")


@classmethod
def poll(cls, context):
return True


def execute(self, context):

winman = context.window_manager

properties_coll = winman.an_templates_properties
nodetree_coll = winman.an_templates_nodetrees

try:
nodetree_coll[self.name]
except KeyError:
print_and_report(self, "Unable to Find Nodetree : " + self.from_name, "WARNING") #debug
return {'FINISHED'}

properties_coll.nodetree_search = self.name

idx = 0
for n in nodetree_coll:

if n.name == self.name:
properties_coll.nodetrees_index = idx
break

idx += 1

print_and_report(self, "Nodetree Selected in Templates Panel", "INFO") #debug

return {"FINISHED"}
4 changes: 3 additions & 1 deletion properties.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ class ANTemplatesTags(bpy.types.PropertyGroup) :
class ANTemplatesNews(bpy.types.PropertyGroup) :
'''name : StringProperty() '''
url : bpy.props.StringProperty(name="News URL")
nodetree_name : bpy.props.StringProperty(name="Nodetree Name")


def get_categories_callback(scene, context):
Expand Down Expand Up @@ -124,4 +125,5 @@ class ANTemplatesProperties(bpy.types.PropertyGroup) :
#newsfeed
newsfeed_hash : bpy.props.StringProperty(name="Newsfeed Hash")
news : bpy.props.CollectionProperty(type = ANTemplatesNews, name="News")
temp_news : bpy.props.CollectionProperty(type = ANTemplatesNews, name="News")
temp_news : bpy.props.CollectionProperty(type = ANTemplatesNews, name="Temp News")
active_news : bpy.props.IntProperty()
22 changes: 22 additions & 0 deletions string_formatting_functions.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
# split string on spaces
def split_string_on_spaces(string, char_limit):

lines = []

words = string.split()

print(words)

line = ""
for w in words:
if len(line) < char_limit:
line += w + " "
else:
line = line[:-1]
lines.append(line)
line = w + " "

if line not in lines:
lines.append(line)

return lines

0 comments on commit c51f90e

Please sign in to comment.