OpenShot Video Editor  2.0.0
blender_model.py
Go to the documentation of this file.
1 ##
2 #
3 # @file
4 # @brief This file contains the blender model, used by the 3d animated titles screen
5 # @author Jonathan Thomas <jonathan@openshot.org>
6 #
7 # @section LICENSE
8 #
9 # Copyright (c) 2008-2018 OpenShot Studios, LLC
10 # (http://www.openshotstudios.com). This file is part of
11 # OpenShot Video Editor (http://www.openshot.org), an open-source project
12 # dedicated to delivering high quality video editing and animation solutions
13 # to the world.
14 #
15 # OpenShot Video Editor is free software: you can redistribute it and/or modify
16 # it under the terms of the GNU General Public License as published by
17 # the Free Software Foundation, either version 3 of the License, or
18 # (at your option) any later version.
19 #
20 # OpenShot Video Editor is distributed in the hope that it will be useful,
21 # but WITHOUT ANY WARRANTY; without even the implied warranty of
22 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
23 # GNU General Public License for more details.
24 #
25 # You should have received a copy of the GNU General Public License
26 # along with OpenShot Library. If not, see <http://www.gnu.org/licenses/>.
27 #
28 
29 import os
30 import xml.dom.minidom as xml
31 
32 from PyQt5.QtCore import Qt
33 from PyQt5.QtGui import *
34 from PyQt5.QtWidgets import QMessageBox
35 import openshot # Python module for libopenshot (required video editing module installed separately)
36 
37 from classes import info
38 from classes.logger import log
39 from classes.app import get_app
40 
41 
42 class BlenderModel():
43  def update_model(self, clear=True):
44  log.info("updating effects model.")
45  app = get_app()
46 
47  # Get window to check filters
48  win = app.window
49  _ = app._tr
50 
51  # Clear all items
52  if clear:
53  self.model_paths = {}
54  self.model.clear()
55 
56  # Add Headers
57  self.model.setHorizontalHeaderLabels([_("Thumb"), _("Name")])
58 
59  # get a list of files in the OpenShot /effects directory
60  effects_dir = os.path.join(info.PATH, "blender")
61  icons_dir = os.path.join(effects_dir, "icons")
62 
63  for file in os.listdir(effects_dir):
64  if os.path.isfile(os.path.join(effects_dir, file)) and ".xml" in file:
65  # Split path
66  path = os.path.join(effects_dir, file)
67  (fileBaseName, fileExtension) = os.path.splitext(path)
68 
69  # load xml effect file
70  xmldoc = xml.parse(path)
71 
72  # Get all attributes
73  title = xmldoc.getElementsByTagName("title")[0].childNodes[0].data
74  description = xmldoc.getElementsByTagName("description")[0].childNodes[0].data
75  icon_name = xmldoc.getElementsByTagName("icon")[0].childNodes[0].data
76  icon_path = os.path.join(icons_dir, icon_name)
77  category = xmldoc.getElementsByTagName("category")[0].childNodes[0].data
78  service = xmldoc.getElementsByTagName("service")[0].childNodes[0].data
79 
80  # Generate thumbnail for file (if needed)
81  thumb_path = os.path.join(info.CACHE_PATH, icon_name)
82 
83  # Check if thumb exists
84  if not os.path.exists(thumb_path):
85 
86  try:
87  # Reload this reader
88  clip = openshot.Clip(icon_path)
89  reader = clip.Reader()
90 
91  # Open reader
92  reader.Open()
93 
94  # Determine scale of thumbnail
95  scale = 95.0 / reader.info.width
96 
97  # Save thumbnail
98  reader.GetFrame(0).Save(thumb_path, scale)
99  reader.Close()
100 
101  except:
102  # Handle exception
103  msg = QMessageBox()
104  msg.setText(_("{} is not a valid image file.".format(icon_path)))
105  msg.exec_()
106  continue
107 
108  row = []
109 
110  # Append thumbnail
111  col = QStandardItem()
112  col.setIcon(QIcon(thumb_path))
113  col.setText(self.app._tr(title))
114  col.setToolTip(self.app._tr(title))
115  col.setFlags(Qt.ItemIsSelectable | Qt.ItemIsEnabled | Qt.ItemIsUserCheckable)
116  row.append(col)
117 
118  # Append Name
119  col = QStandardItem("Name")
120  col.setData(self.app._tr(title), Qt.DisplayRole)
121  col.setText(self.app._tr(title))
122  col.setFlags(Qt.ItemIsSelectable | Qt.ItemIsEnabled | Qt.ItemIsUserCheckable)
123  row.append(col)
124 
125  # Append Path
126  col = QStandardItem("Path")
127  col.setData(path, Qt.DisplayRole)
128  col.setText(path)
129  col.setFlags(Qt.ItemIsSelectable | Qt.ItemIsEnabled | Qt.ItemIsUserCheckable)
130  row.append(col)
131 
132  # Append Service
133  col = QStandardItem("Service")
134  col.setData(service, Qt.DisplayRole)
135  col.setText(service)
136  col.setFlags(Qt.ItemIsSelectable | Qt.ItemIsEnabled | Qt.ItemIsUserCheckable)
137  row.append(col)
138 
139  # Append ROW to MODEL (if does not already exist in model)
140  if not path in self.model_paths:
141  self.model.appendRow(row)
142  self.model_paths[path] = path
143 
144  # Process events in QT (to keep the interface responsive)
145  app.processEvents()
146 
147  def __init__(self, *args):
148 
149  # Create standard model
150  self.app = get_app()
151  self.model = QStandardItemModel()
152  self.model.setColumnCount(4)
153  self.model_paths = {}
def get_app
Returns the current QApplication instance of OpenShot.
Definition: app.py:55