def __init__(self, builder): self.form = Gui.PySideUic.loadUi(get_ui('scripts', 'CreatePanel.ui')) self.form.setWindowTitle(tr('Create Script')) self.form.label.setText(tr('Name:')) self.form.label_descr.setText( tr('The name will be used in expressions, it must be unique and cannot be changed later.' )) self.form.errorMessage.setVisible(False) self.builder = builder
def __init__(self, obj): super(ScriptObject, self).__init__(obj) self.mtime = 0 obj.addProperty(Property.Integer, 'Time', 'Base', tr('Current tick')).Time = 0 obj.addProperty(Property.File, 'File', "Base", tr("Python script")).File = obj.Name + ".py" obj.addProperty(Property.String, 'RealName', "Base", tr("Real name usable in expressions"), Property.Flag_ReadOnly).RealName = obj.Name obj.addProperty(Property.StringList, 'Symbols', "Base", "", Property.Flag_Hidden).Symbols = []
def __init__(self, obj): self.form = Gui.PySideUic.loadUi( get_ui('timers', 'TimerSliderPanel.ui')) self.object = obj minval = min(obj.Start, obj.End) maxval = max(obj.Start, obj.End) slider = self.form.timeSlider slider.setMinimum(minval) slider.setMaximum(maxval) slider.setSingleStep(obj.Step) slider.setValue(obj.Time) slider.valueChanged.connect(self.value_changed) self.form.setWindowTitle(tr("Manual control: {}").format(obj.Label)) self.form.label.setText(tr("Time: {}").format(obj.Time))
def init_gui_scripts(): toolbars.add_global_action( name="CreatePyScript", icon=Icons.variables, action=ScriptObject.create, menu=tr("Create Python Script"), activation=lambda: App.ActiveDocument and App.ActiveDocument.FileName)
def load_script(file_path): try: return file_path.read_text() except BaseException: error_dialog(tr("Script file '{}' cannot be read").format( str(file_path)), raise_exception=True)
def init_gui_pov(): add_global_action(name="CreatePointOfView", icon=Icons.camera, action=show, menu=tr("Points of View (Cameras)"), accel="Shift+Ctrl+V", activation=True)
def __init__(self, current_name=None): form = Gui.PySideUic.loadUi(get_ui('camera', 'PointsOfViewPanel.ui')) form.setWindowTitle(tr("Saved Points of View")) self.form = form items = form.listWidget current = None for pov in find_python_objects_by_class(PointOfViewObject): item = BasicListItem(pov.Label, Icons.camera, data=pov.Name, editable=True) items.addItem(item) if pov.Name == current_name: current = item items.itemSelectionChanged.connect(self.item_selection_changed) items.itemChanged.connect(self.update) if current: items.setCurrentItem(current) save = form.saveCamera save.setIcon(BasicQIcon(Icons.save_camera)) save.clicked.connect(self.save) add = form.addCamera add.setIcon(BasicQIcon(Icons.add_camera)) add.clicked.connect(self.add)
def init_hyperc(): try: import serial serial_available = True except ImportError: serial_available = False log( tr("8D Hyper Controller is disabled because the python module 'pyserial' was not found." )) if serial_available: add_global_action(name="ToggleHyperController", icon=Icons.hyperc, action=lambda: controller.toggle(), menu=tr("Activate/Deactivate 8D Hyper Controller"), activation=lambda: App.ActiveDocument)
def start(self, obj): self.stop() log(tr("Starting timer {}").format(obj.Name)) obj.Time = obj.Start self.timer = QtCore.QTimer() speed = int(1000 / obj.TPS) self.timer.setInterval(speed) self.timer.timeout.connect(self.timeout) self.timer.start()
def build_module(file_path, obj): script = load_script(file_path) try: module = Script(obj.Name, script) ModuleCache[obj] = module except BaseException as e: error_dialog(tr( "Error parsing your script file: {0}: {1}. Check report window for details" ).format(obj.Name, obj.File), exception=e) else: try: module.exec() return module except BaseException as e: error_dialog(tr( "Error executing your script file: {0}: {1}. Check report window for details" ).format(obj.Name, obj.File), exception=e)
def add_additional_python_paths(): keys = pref.get_user_pref_keys(pref.PLUGIN_KEY, "Platform", "PythonPath", "Path", root="Plugins") if keys: for key in keys: path = pref.get_mnesarco_pref("Platform", "PythonPath", key) log(tr("[Python Path] Adding {}").format(path)) add_python_path(path)
def create(self): if not App.ActiveDocument: App.newDocument() group = DocumentObject.create_group('GroupPointsOfView', tr("Points of view"), unique=True) obj = DocumentObject.create('Camera', PointOfViewObject, PointOfViewObjectGui) obj.Proxy.move_to_group(group) return obj
def __init__(self, port_name=None, timeout=None, baud_rate=None): self.port = port_name or pref.get_mnesarco_pref( Driver.PREF_KEY, "Port", default=Driver.DEFAULT_PORT_NAME) self.baud_rate = baud_rate or pref.get_mnesarco_pref( Driver.PREF_KEY, "BaudRate", default=Driver.DEFAULT_BAUD_RATE) self.timeout = timeout or pref.get_mnesarco_pref( Driver.PREF_KEY, "Timeout", default=Driver.DEFAULT_TIMEOUT) try: self.is_open = False log( tr("Connecting to port {0} at {1}Bd with a timeout of {2}s"). format(self.port, self.baud_rate, self.timeout)) import serial self.stream = serial.Serial(self.port, self.baud_rate, timeout=self.timeout) self.is_open = True except ImportError: log_err(tr("pyserial module not found")) except Exception as e: log_err(str(e))
def execute(self, obj): # Fix label to avoid confusions obj.Label = obj.Name if not App.ActiveDocument.FileName: error_dialog( tr("You must save the current document before adding scripts"), raise_exception=True) if not obj.File: return # Make File Relative if possible try: obj.File = make_path_relative(obj.File, App.ActiveDocument.FileName) except ValueError: pass file_path = get_real_path(obj) # Manage cache if self.mtime < file_path.stat().st_mtime: self.mtime = file_path.stat().st_mtime ModuleCache[obj] = None # Big Bang time = obj.Time globals()['TIME'] = time # Execute module = ModuleCache.get(obj, None) if module: log(tr("Executing {} from cache").format(obj.Name)) else: log(tr("Executing {} from {}").format(obj.Name, str(file_path))) module = ScriptObject.build_module(file_path, obj) ScriptObject.update_symbols(module, obj, time)
def accept(self): self.form.errorMessage.setVisible(False) name = self.form.nameInput.text() if name and CreatePanel.VALID_NAME.match(name): if not App.ActiveDocument: App.newDocument() if not App.ActiveDocument.getObject(name): if App.ActiveDocument.FileName: Gui.Control.closeDialog() self.create(name) else: self.form.errorMessage.setVisible(True) self.form.errorMessage.setText( tr("The Actvive Document must be saved before adding scripts" )) else: self.form.errorMessage.setVisible(True) self.form.errorMessage.setText( tr("Duplicated name is not valid")) else: self.form.errorMessage.setVisible(True) self.form.errorMessage.setText(tr("Invalid name"))
def create(self, name): obj = self.builder(name) file = resolve_path(name + ".py", App.ActiveDocument.FileName) if not file.exists(): try: file.write_text( get_template("scripts", "default_script.py.txt")) except BaseException: error_dialog(tr("Script file '{}' cannot be created").format( str(file)), raise_exception=True) return show_task_panel(CodeEditorPanel(obj.Name, file))
def set_user_pref(*path, root="BaseApp"): group_key = "User parameter:" + root + "/" + "/".join(path[:-2]) key = path[-2] value = path[-1] kind = type(value) group = App.ParamGet(group_key) if kind == bool: group.SetBool(key, value) elif kind == int: group.SetInt(key, value) elif kind == float: group.SetFloat(key, value) elif kind == str: group.SetString(key, value) else: log_err(tr("Error writing {} of type {}").format(key, str(kind)))
def __init__(self, name, file_name, highlighter=PythonSyntaxHighlighter, tab_size=4): self.form = QtGui.QDialog() self.form.setWindowTitle(tr("Edit: {}").format(str(file_name.name))) self.editor = CodeEditor() self.editor.appendPlainText(file_name.read_text()) self.highlighter = highlighter(self.editor.document()) self.error_message = QtGui.QLabel() self.file_name = file_name self.name = name self.tab_size = tab_size layout = QtGui.QVBoxLayout(self.form) layout.addWidget(self.editor) layout.addWidget(self.error_message)
def get_real_path(obj): if obj.File: file_path = obj.File else: file_path = obj.Name + ".py" obj.File = file_path file_path = resolve_path(file_path, App.ActiveDocument.FileName) if not file_path.exists(): try: file_path.write_text( get_template("scripts", "default_script.py.txt")) except BaseException: error_dialog(tr("Script file '{}' cannot be created").format( str(file_path)), raise_exception=True) return file_path
def timeout(self): obj = self.get_object() time_dir = sign(obj.Start, obj.End) self.cur_dir = getattr(self, 'cur_dir', 1) step = obj.Step * time_dir * self.cur_dir time = obj.Time + step after = sign(time, obj.End) != time_dir and obj.End != time before = sign(obj.Start, time) != time_dir and obj.Start != time if after: time = obj.End elif before: time = obj.Start if after or before: if obj.Pendulum: self.cur_dir = -self.cur_dir time = obj.Time - step elif obj.Loop: time = obj.Start else: obj.Enabled = False if time != obj.Time: obj.Time = time log(tr("Timer {} ({}) = {}").format(obj.Label, obj.Name, time)) App.ActiveDocument.recompute()
def retranslateUi(self): self.title = tr("Platform") self.form.label.setText(tr("Additional python paths (one per line):")) self.form.labelSystemPaths.setText(tr("Core python paths:"))
def init_gui_timers(): add_global_action(name="CreateTimer", icon=Icons.timer, action=TimerObject.create, menu=tr("Create Timer"), activation=lambda: True)
def value_changed(self, v): self.object.Time = v self.form.label.setText(tr("Time: {}").format(v)) App.ActiveDocument.recompute()