Exemplo n.º 1
0
    def __init__(self, application):
        super().__init__()  # Call super to make multiple inheritance work.

        self._scene = Scene()
        self._application = application
        self._is_model_rendering_enabled = True

        self._active_view = None
        self._views = {}

        self._active_tool = None
        self._tool_operation_active = False
        self._tools = {}
        self._camera_tool = None
        self._selection_tool = None
        self._tools_enabled = True

        self._active_stage = None
        self._stages = {}

        self._input_devices = {}

        PluginRegistry.addType("stage", self.addStage)
        PluginRegistry.addType("view", self.addView)
        PluginRegistry.addType("tool", self.addTool)
        PluginRegistry.addType("input_device", self.addInputDevice)
Exemplo n.º 2
0
    def __init__(self, application: "Application") -> None:
        super().__init__()  # Call super to make multiple inheritance work.

        self._scene = Scene()
        self._application = application

        self._active_view = None  # type: Optional[View]
        self._views = {}  # type: Dict[str, View]

        self._active_tool = None  # type: Optional[Tool]
        self._tool_operation_active = False
        self._tools = {}  # type: Dict[str, Tool]
        self._camera_tool = None #type: Optional[Tool]
        self._selection_tool = None #type: Optional[Tool]
        self._tools_enabled = True #type: bool

        self._active_stage = None #type: Optional[Stage]
        self._stages = {} #type: Dict[str, Stage]

        self._input_devices = {} #type: Dict[str, InputDevice]

        PluginRegistry.addType("stage", self.addStage)
        PluginRegistry.addType("view", self.addView)
        PluginRegistry.addType("tool", self.addTool)
        PluginRegistry.addType("input_device", self.addInputDevice)
Exemplo n.º 3
0
def test_findObject():
    scene = Scene()
    node = SceneNode()
    scene.getRoot().addChild(node)

    assert scene.findObject(id(node)) == node
    assert scene.findObject(12) is None
Exemplo n.º 4
0
 def setUp(self):
     # Called before the first testfunction is executed
     self._scene = Scene()
     self._scene_object = SceneNode()
     self._scene_object2 = SceneNode()
     self._scene_object.addChild(self._scene_object2)
     self._scene.getRoot().addChild(self._scene_object)
     temp_matrix = Matrix()
     temp_matrix.setByTranslation(Vector(10,10,10))
     self._scene_object2.setLocalTransformation(deepcopy(temp_matrix))
     temp_matrix.setByScaleFactor(0.5)
     self._scene_object.setLocalTransformation(temp_matrix)
Exemplo n.º 5
0
    def __init__(self, application: "Application") -> None:
        super().__init__()  # Call super to make multiple inheritance work.

        self._scene = Scene()
        self._application = application

        self._active_view = None  # type: Optional[View]
        self._views = {}  # type: Dict[str, View]

        self._active_tool = None  # type: Optional[Tool]
        self._tool_operation_active = False
        self._tools = {}  # type: Dict[str, Tool]
        self._camera_tool = None #type: Optional[Tool]
        self._selection_tool = None #type: Optional[Tool]
        self._tools_enabled = True #type: bool

        self._active_stage = None #type: Optional[Stage]
        self._stages = {} #type: Dict[str, Stage]

        self._input_devices = {} #type: Dict[str, InputDevice]

        PluginRegistry.addType("stage", self.addStage)
        PluginRegistry.addType("view", self.addView)
        PluginRegistry.addType("tool", self.addTool)
        PluginRegistry.addType("input_device", self.addInputDevice)
Exemplo n.º 6
0
def test_switchRoot():
    scene = Scene()
    new_root = SceneNode()
    scene.rootChanged = MagicMock()

    scene.setRoot(new_root)
    assert scene.getRoot() == new_root
    assert scene.rootChanged.emit.call_count == 1

    scene.setRoot(new_root)
    assert scene.rootChanged.emit.call_count == 1
Exemplo n.º 7
0
class SceneTest(unittest.TestCase):
    def setUp(self):
        # Called before the first testfunction is executed
        self._scene = Scene()
        self._scene_object = SceneNode()
        self._scene_object2 = SceneNode()
        self._scene_object.addChild(self._scene_object2)
        self._scene.getRoot().addChild(self._scene_object)
        temp_matrix = Matrix()
        temp_matrix.setByTranslation(Vector(10,10,10))
        self._scene_object2.setLocalTransformation(deepcopy(temp_matrix))
        temp_matrix.setByScaleFactor(0.5)
        self._scene_object.setLocalTransformation(temp_matrix)

    def tearDown(self):
        # Called after the last testfunction was executed
        pass

    def test_checkWorldTransformation(self):
        numpy.testing.assert_array_almost_equal(self._scene_object2.getWorldTransformation().getData(), numpy.array([[0.5,0,0,5],[0,0.5,0,5],[0,0,0.5,5],[0,0,0,1]]))
Exemplo n.º 8
0
 def setUp(self):
     # Called before the first testfunction is executed
     self._scene = Scene()
     self._scene_object = SceneNode()
     self._scene_object2 = SceneNode()
     self._scene_object.addChild(self._scene_object2)
     self._scene.getRoot().addChild(self._scene_object)
     temp_matrix = Matrix()
     temp_matrix.setByTranslation(Vector(10, 10, 10))
     self._scene_object2.setLocalTransformation(deepcopy(temp_matrix))
     temp_matrix.setByScaleFactor(0.5)
     self._scene_object.setLocalTransformation(temp_matrix)
Exemplo n.º 9
0
def test_findObject():
    scene = Scene()
    node = SceneNode()
    scene.getRoot().addChild(node)

    assert scene.findObject(id(node)) == node
    assert scene.findObject(12) is None
Exemplo n.º 10
0
def test_switchRoot():
    scene = Scene()
    new_root = SceneNode()
    scene.rootChanged = MagicMock()

    scene.setRoot(new_root)
    assert scene.getRoot() == new_root
    assert scene.rootChanged.emit.call_count == 1

    scene.setRoot(new_root)
    assert scene.rootChanged.emit.call_count == 1
Exemplo n.º 11
0
def test_ignoreSceneChanges():
    scene = Scene()
    scene.sceneChanged.emit = MagicMock()
    scene.setIgnoreSceneChanges(ignore_scene_changes=True)
    root = scene.getRoot()

    root.addChild(SceneNode())
    assert scene.sceneChanged.emit.call_count == 0

    scene.setIgnoreSceneChanges(ignore_scene_changes=False)
    root.addChild(SceneNode())
    assert scene.sceneChanged.emit.call_count == 2
Exemplo n.º 12
0
class SceneTest(unittest.TestCase):
    def setUp(self):
        # Called before the first testfunction is executed
        self._scene = Scene()
        self._scene_object = SceneNode()
        self._scene_object2 = SceneNode()
        self._scene_object.addChild(self._scene_object2)
        self._scene.getRoot().addChild(self._scene_object)
        temp_matrix = Matrix()
        temp_matrix.setByTranslation(Vector(10, 10, 10))
        self._scene_object2.setLocalTransformation(deepcopy(temp_matrix))
        temp_matrix.setByScaleFactor(0.5)
        self._scene_object.setLocalTransformation(temp_matrix)

    def tearDown(self):
        # Called after the last testfunction was executed
        pass

    def test_checkWorldTransformation(self):
        numpy.testing.assert_array_almost_equal(
            self._scene_object2.getWorldTransformation().getData(),
            numpy.array([[0.5, 0, 0, 5], [0, 0.5, 0, 5], [0, 0, 0.5, 5], [0, 0, 0, 1]]),
        )
Exemplo n.º 13
0
    def __init__(self, application):
        super().__init__()  # Call super to make multiple inheritence work.
        self._active_tool = None
        self._tools = {}

        self._input_devices = {}

        self._active_view = None
        self._views = {}
        self._scene = Scene()
        self._application = application
        self._camera_tool = None
        self._selection_tool = None

        PluginRegistry.addType("view", self.addView)
        PluginRegistry.addType("tool", self.addTool)
        PluginRegistry.addType("input_device", self.addInputDevice)
Exemplo n.º 14
0
def test_ignoreSceneChanges():
    scene = Scene()
    scene.sceneChanged.emit = MagicMock()
    scene.setIgnoreSceneChanges(ignore_scene_changes = True)
    root = scene.getRoot()

    root.addChild(SceneNode())
    assert scene.sceneChanged.emit.call_count == 0

    scene.setIgnoreSceneChanges(ignore_scene_changes=False)
    root.addChild(SceneNode())
    assert scene.sceneChanged.emit.call_count == 2
Exemplo n.º 15
0
class Controller:
    def __init__(self, application):
        super().__init__()  # Call super to make multiple inheritance work.

        self._scene = Scene()
        self._application = application
        self._is_model_rendering_enabled = True

        self._active_view = None
        self._views = {}

        self._active_tool = None
        self._tool_operation_active = False
        self._tools = {}
        self._camera_tool = None
        self._selection_tool = None
        self._tools_enabled = True

        self._active_stage = None
        self._stages = {}

        self._input_devices = {}

        PluginRegistry.addType("stage", self.addStage)
        PluginRegistry.addType("view", self.addView)
        PluginRegistry.addType("tool", self.addTool)
        PluginRegistry.addType("input_device", self.addInputDevice)

    ##  Get the application.
    #   \returns Application \type {Application}
    def getApplication(self):
        return self._application

    ##  Add a view by name if it"s not already added.
    #   \param name \type{string} Unique identifier of view (usually the plugin name)
    #   \param view \type{View} The view to be added
    def addView(self, view: View):
        name = view.getPluginId()
        if name not in self._views:
            self._views[name] = view
            view.setRenderer(self._application.getRenderer())
            self.viewsChanged.emit()
        else:
            Logger.log(
                "w",
                "%s was already added to view list. Unable to add it again.",
                name)

    ##  Request view by name. Returns None if no view is found.
    #   \param name \type{string} Unique identifier of view (usually the plugin name)
    #   \return View \type{View} if name was found, none otherwise.
    def getView(self, name: str) -> Optional[View]:
        try:
            return self._views[name]
        except KeyError:  # No such view
            Logger.log("e", "Unable to find %s in view list", name)
            return None

    ##  Return all views.
    #   \return views \type{dict}
    def getAllViews(self) -> Dict[str, View]:
        return self._views

    ##  Request active view. Returns None if there is no active view
    #   \return view \type{View} if an view is active, None otherwise.
    def getActiveView(self) -> Optional[View]:
        return self._active_view

    ##  Set the currently active view.
    #   \param name \type{string} The name of the view to set as active
    def setActiveView(self, name: str):
        Logger.log("d", "Setting active view to %s", name)
        try:
            if self._active_view:
                self._active_view.event(ViewEvent(Event.ViewDeactivateEvent))

            self._active_view = self._views[name]

            if self._active_view:
                self._active_view.event(ViewEvent(Event.ViewActivateEvent))

            self.activeViewChanged.emit()
        except KeyError:
            Logger.log("e", "No view named %s found", name)
        except Exception as e:
            Logger.logException(
                "e", "An exception occurred while switching views: %s", str(e))

    def enableModelRendering(self):
        self._is_model_rendering_enabled = True

    def disableModelRendering(self):
        self._is_model_rendering_enabled = False

    def isModelRenderingEnabled(self):
        return self._is_model_rendering_enabled

    ##  Emitted when the list of views changes.
    viewsChanged = Signal()

    ##  Emitted when the active view changes.
    activeViewChanged = Signal()

    ##  Add a stage by name if it's not already added.
    #   \param name \type{string} Unique identifier of stage (usually the plugin name)
    #   \param stage \type{Stage} The stage to be added
    def addStage(self, stage: Stage):
        name = stage.getPluginId()
        if name not in self._stages:
            self._stages[name] = stage
            self.stagesChanged.emit()

    ##  Request stage by name. Returns None if no stage is found.
    #   \param name \type{string} Unique identifier of stage (usually the plugin name)
    #   \return Stage \type{Stage} if name was found, none otherwise.
    def getStage(self, name: str) -> Optional[Stage]:
        try:
            return self._stages[name]
        except KeyError:  # No such view
            Logger.log("e", "Unable to find %s in stage list", name)
            return None

    ##  Return all stages.
    #   \return stages \type{dict}
    def getAllStages(self) -> Dict[str, Stage]:
        return self._stages

    ##  Request active stage. Returns None if there is no active stage
    #   \return stage \type{Stage} if an stage is active, None otherwise.
    def getActiveStage(self) -> Optional[Stage]:
        return self._active_stage

    ##  Set the currently active stage.
    #   \param name \type{string} The name of the stage to set as active
    def setActiveStage(self, name: str):
        Logger.log("d", "Setting active stage to %s", name)
        try:
            self._active_stage = self._stages[name]
            self.activeStageChanged.emit()
        except KeyError:
            Logger.log("e", "No stage named %s found", name)
        except Exception as e:
            Logger.logException(
                "e", "An exception occurred while switching stages: %s",
                str(e))

    ##  Emitted when the list of stages changes.
    stagesChanged = Signal()

    ##  Emitted when the active stage changes.
    activeStageChanged = Signal()

    ##  Add an input device (e.g. mouse, keyboard, etc) if it's not already added.
    #   \param device The input device to be added
    def addInputDevice(self, device: InputDevice):
        name = device.getPluginId()
        if name not in self._input_devices:
            self._input_devices[name] = device
            device.event.connect(self.event)
        else:
            Logger.log(
                "w",
                "%s was already added to input device list. Unable to add it again."
                % name)

    ##  Request input device by name. Returns None if no device is found.
    #   \param name \type{string} Unique identifier of input device (usually the plugin name)
    #   \return input \type{InputDevice} device if name was found, none otherwise.
    def getInputDevice(self, name: str) -> Optional[InputDevice]:
        try:
            return self._input_devices[name]
        except KeyError:  # No such device
            Logger.log("e", "Unable to find %s in input devices", name)
            return None

    ##  Remove an input device from the list of input devices.
    #   Does nothing if the input device is not in the list.
    #   \param name \type{string} The name of the device to remove.
    def removeInputDevice(self, name: str):
        if name in self._input_devices:
            self._input_devices[name].event.disconnect(self.event)
            del self._input_devices[name]

    ##  Request tool by name. Returns None if no view is found.
    #   \param name \type{string} Unique identifier of tool (usually the plugin name)
    #   \return tool \type{Tool} if name was found, none otherwise.
    def getTool(self, name: str):
        try:
            return self._tools[name]
        except KeyError:  # No such tool
            Logger.log("e", "Unable to find %s in tools", name)
            return None

    ##  Get all tools
    #   \return tools \type{dict}
    def getAllTools(self):
        return self._tools

    ##  Add a Tool (transform object, translate object) if its not already added.
    #   \param tool \type{Tool} Tool to be added
    def addTool(self, tool):
        name = tool.getPluginId()
        if name not in self._tools:
            self._tools[name] = tool
            tool.operationStarted.connect(self._onToolOperationStarted)
            tool.operationStopped.connect(self._onToolOperationStopped)
            self.toolsChanged.emit()
        else:
            Logger.log(
                "w",
                "%s was already added to tool list. Unable to add it again.",
                name)

    def _onToolOperationStarted(self, tool):
        if not self._tool_operation_active:
            self._tool_operation_active = True
            self.toolOperationStarted.emit(tool)

    def _onToolOperationStopped(self, tool):
        if self._tool_operation_active:
            self._tool_operation_active = False
            self.toolOperationStopped.emit(tool)

    ##  Gets whether a tool is currently in use
    #   \return \type{bool} true if a tool current being used.
    def isToolOperationActive(self) -> bool:
        return self._tool_operation_active

    ##  Request active tool. Returns None if there is no active tool
    #   \return Tool \type{Tool} if an tool is active, None otherwise.
    def getActiveTool(self):
        return self._active_tool

    ##  Set the current active tool.
    #   The tool can be set by name of the tool or directly passing the tool object.
    #   \param tool \type{Tool} or \type{string}
    def setActiveTool(self, tool):
        from UM.Tool import Tool
        if self._active_tool:
            self._active_tool.event(ToolEvent(ToolEvent.ToolDeactivateEvent))

        if isinstance(tool, Tool) or tool is None:
            new_tool = tool
        else:
            new_tool = self.getTool(tool)

        tool_changed = False
        if self._active_tool is not new_tool:
            self._active_tool = new_tool
            tool_changed = True

        if self._active_tool:
            self._active_tool.event(ToolEvent(ToolEvent.ToolActivateEvent))

        from UM.Scene.Selection import Selection  # Imported here to prevent a circular dependency.
        if not self._active_tool and Selection.getCount(
        ) > 0:  # If something is selected, a tool must always be active.
            if "TranslateTool" in self._tools:
                self._active_tool = self._tools[
                    "TranslateTool"]  # Then default to the translation tool.
                self._active_tool.event(ToolEvent(ToolEvent.ToolActivateEvent))
                tool_changed = True
            else:
                Logger.log(
                    "w",
                    "Controller does not have an active tool and could not default to Translate tool."
                )

        if tool_changed:
            self.activeToolChanged.emit()

    ##  Emitted when the list of tools changes.
    toolsChanged = Signal()

    ##  Emitted when a tool changes its enabled state.
    toolEnabledChanged = Signal()

    ##  Emitted when the active tool changes.
    activeToolChanged = Signal()

    ##  Emitted whenever a tool starts a longer operation.
    #
    #   \param tool The tool that started the operation.
    #   \sa Tool::startOperation
    toolOperationStarted = Signal()

    ##  Emitted whenever a tool stops a longer operation.
    #
    #   \param tool The tool that stopped the operation.
    #   \sa Tool::stopOperation
    toolOperationStopped = Signal()

    ##  Get the scene
    #   \return scene \type{Scene}
    def getScene(self) -> Scene:
        return self._scene

    ##  Process an event
    #   \param event \type{Event} event to be handle.
    #   The event is first passed to the camera tool, then active tool and finally selection tool.
    #   If none of these events handle it (when they return something that does not evaluate to true)
    #   a context menu signal is emitted.
    def event(self, event: Event):
        # First, try to perform camera control
        if self._camera_tool and self._camera_tool.event(event):
            return

        if self._tools and event.type == Event.KeyPressEvent:
            from UM.Scene.Selection import Selection  # Imported here to prevent a circular dependency.
            if Selection.hasSelection():
                for key, tool in self._tools.items():
                    if tool.getShortcutKey(
                    ) is not None and event.key == tool.getShortcutKey():
                        self.setActiveTool(tool)

        if self._selection_tool and self._selection_tool.event(event):
            return

        # If we are not doing camera control, pass the event to the active tool.
        if self._active_tool and self._active_tool.event(event):
            return

        if self._active_view:
            self._active_view.event(event)

        if event.type == Event.MouseReleaseEvent and MouseEvent.RightButton in event.buttons:
            self.contextMenuRequested.emit(event.x, event.y)

    contextMenuRequested = Signal()

    ##  Set the tool used for handling camera controls.
    #   Camera tool is the first tool to receive events.
    #   The tool can be set by name of the tool or directly passing the tool object.
    #   \param tool \type{Tool} or \type{string}
    #   \sa setSelectionTool
    #   \sa setActiveTool
    def setCameraTool(self, tool):
        from UM.Tool import Tool
        if isinstance(tool, Tool) or tool is None:
            self._camera_tool = tool
        else:
            self._camera_tool = self.getTool(tool)

    ##  Get the camera tool (if any)
    #   \returns camera tool (or none)
    def getCameraTool(self):
        return self._camera_tool

    ##  Set the tool used for performing selections.
    #   Selection tool receives its events after camera tool and active tool.
    #   The tool can be set by name of the tool or directly passing the tool object.
    #   \param tool \type{Tool} or \type{string}
    #   \sa setCameraTool
    #   \sa setActiveTool
    def setSelectionTool(self, tool):
        from UM.Tool import Tool
        if isinstance(tool, Tool) or tool is None:
            self._selection_tool = tool
        else:
            self._selection_tool = self.getTool(tool)

    def getToolsEnabled(self):
        return self._tools_enabled

    def setToolsEnabled(self, enabled):
        self._tools_enabled = enabled

    # Rotate camera view according defined angle
    def rotateView(self, coordinate="x", angle=0):
        camera = self._scene.getActiveCamera()
        self._camera_tool.setOrigin(Vector(0, 100, 0))
        if coordinate == "home":
            camera.setPosition(Vector(0, 300, 700))
            camera.setPerspective(True)
            camera.lookAt(Vector(0, 100, 100))
            self._camera_tool.rotateCam(0, 0)
        elif coordinate == "3d":
            camera.setPosition(Vector(-180, 250, 1000))

            camera.setPerspective(True)
            camera.lookAt(Vector(0, 100, 100))

            self._camera_tool.rotateCam(0, 0)

        else:
            # for comparison is == used, because might not store them at the same location
            # https://stackoverflow.com/questions/1504717/why-does-comparing-strings-in-python-using-either-or-is-sometimes-produce
            camera.setPosition(Vector(0, 0, 700))
            camera.setPerspective(True)
            camera.lookAt(Vector(0, 100, 0))

            if coordinate == "x":
                self._camera_tool.rotateCam(angle, 0)
            elif coordinate == "y":
                self._camera_tool.rotateCam(0, angle)
Exemplo n.º 16
0
class Controller:
    def __init__(self, application: "Application") -> None:
        super().__init__()  # Call super to make multiple inheritance work.

        self._scene = Scene()
        self._application = application

        self._active_view = None  # type: Optional[View]
        self._views = {}  # type: Dict[str, View]

        self._active_tool = None  # type: Optional[Tool]
        self._tool_operation_active = False
        self._tools = {}  # type: Dict[str, Tool]
        self._camera_tool = None #type: Optional[Tool]
        self._selection_tool = None #type: Optional[Tool]
        self._tools_enabled = True #type: bool

        self._active_stage = None #type: Optional[Stage]
        self._stages = {} #type: Dict[str, Stage]

        self._input_devices = {} #type: Dict[str, InputDevice]

        PluginRegistry.addType("stage", self.addStage)
        PluginRegistry.addType("view", self.addView)
        PluginRegistry.addType("tool", self.addTool)
        PluginRegistry.addType("input_device", self.addInputDevice)

    ##  Add a view by name if it"s not already added.
    #   \param name \type{string} Unique identifier of view (usually the plugin name)
    #   \param view \type{View} The view to be added
    def addView(self, view: View) -> None:
        name = view.getPluginId()
        if name not in self._views:
            self._views[name] = view
            view.setRenderer(self._application.getRenderer())
            self.viewsChanged.emit()
        else:
            Logger.log("w", "%s was already added to view list. Unable to add it again.", name)

    ##  Request view by name. Returns None if no view is found.
    #   \param name \type{string} Unique identifier of view (usually the plugin name)
    #   \return View \type{View} if name was found, none otherwise.
    def getView(self, name: str) -> Optional[View]:
        try:
            return self._views[name]
        except KeyError:  # No such view
            Logger.log("e", "Unable to find %s in view list", name)
            return None

    ##  Return all views.
    #   \return views \type{dict}
    def getAllViews(self) -> Dict[str, View]:
        return self._views

    ##  Request active view. Returns None if there is no active view
    #   \return view \type{View} if an view is active, None otherwise.
    def getActiveView(self) -> Optional[View]:
        return self._active_view

    ##  Set the currently active view.
    #   \param name \type{string} The name of the view to set as active
    def setActiveView(self, name: str) -> None:
        Logger.log("d", "Setting active view to %s", name)
        try:
            if self._active_view:
                self._active_view.event(ViewEvent(Event.ViewDeactivateEvent))

            self._active_view = self._views[name]

            if self._active_view:
                self._active_view.event(ViewEvent(Event.ViewActivateEvent))

            self.activeViewChanged.emit()
        except KeyError:
            Logger.log("e", "No view named %s found", name)
        except Exception as e:
            Logger.logException("e", "An exception occurred while switching views: %s", str(e))

    ##  Emitted when the list of views changes.
    viewsChanged = Signal()

    ##  Emitted when the active view changes.
    activeViewChanged = Signal()

    ##  Add a stage by name if it's not already added.
    #   \param name \type{string} Unique identifier of stage (usually the plugin name)
    #   \param stage \type{Stage} The stage to be added
    def addStage(self, stage: Stage) -> None:
        name = stage.getPluginId()
        if name not in self._stages:
            self._stages[name] = stage
            self.stagesChanged.emit()

    ##  Request stage by name. Returns None if no stage is found.
    #   \param name \type{string} Unique identifier of stage (usually the plugin name)
    #   \return Stage \type{Stage} if name was found, none otherwise.
    def getStage(self, name: str) -> Optional[Stage]:
        try:
            return self._stages[name]
        except KeyError:  # No such view
            Logger.log("e", "Unable to find %s in stage list", name)
            return None

    ##  Return all stages.
    #   \return stages \type{dict}
    def getAllStages(self) -> Dict[str, Stage]:
        return self._stages

    ##  Request active stage. Returns None if there is no active stage
    #   \return stage \type{Stage} if an stage is active, None otherwise.
    def getActiveStage(self) -> Optional[Stage]:
        return self._active_stage

    ##  Set the currently active stage.
    #   \param name \type{string} The name of the stage to set as active
    def setActiveStage(self, name: str) -> None:
        try:
            # Don't actually change the stage if it is the current selected one.
            if self._active_stage != self._stages[name]:
                previous_stage = self._active_stage
                Logger.log("d", "Setting active stage to %s", name)
                self._active_stage = self._stages[name]

                # If there is no error switching stages, then finish first the previous stage (if it exists) and start the new stage
                if previous_stage is not None:
                    previous_stage.onStageDeselected()
                self._active_stage.onStageSelected()
                self.activeStageChanged.emit()
        except KeyError:
            Logger.log("e", "No stage named %s found", name)
        except Exception as e:
            Logger.logException("e", "An exception occurred while switching stages: %s", str(e))

    ##  Emitted when the list of stages changes.
    stagesChanged = Signal()

    ##  Emitted when the active stage changes.
    activeStageChanged = Signal()

    ##  Add an input device (e.g. mouse, keyboard, etc) if it's not already added.
    #   \param device The input device to be added
    def addInputDevice(self, device: InputDevice) -> None:
        name = device.getPluginId()
        if name not in self._input_devices:
            self._input_devices[name] = device
            device.event.connect(self.event)
        else:
            Logger.log("w", "%s was already added to input device list. Unable to add it again." % name)

    ##  Request input device by name. Returns None if no device is found.
    #   \param name \type{string} Unique identifier of input device (usually the plugin name)
    #   \return input \type{InputDevice} device if name was found, none otherwise.
    def getInputDevice(self, name: str) -> Optional[InputDevice]:
        try:
            return self._input_devices[name]
        except KeyError:  # No such device
            Logger.log("e", "Unable to find %s in input devices", name)
            return None

    ##  Remove an input device from the list of input devices.
    #   Does nothing if the input device is not in the list.
    #   \param name \type{string} The name of the device to remove.
    def removeInputDevice(self, name: str) -> None:
        if name in self._input_devices:
            self._input_devices[name].event.disconnect(self.event)
            del self._input_devices[name]

    ##  Request tool by name. Returns None if no view is found.
    #   \param name \type{string} Unique identifier of tool (usually the plugin name)
    #   \return tool \type{Tool} if name was found, None otherwise.
    def getTool(self, name: str) -> Optional["Tool"]:
        try:
            return self._tools[name]
        except KeyError:  # No such tool
            Logger.log("e", "Unable to find %s in tools", name)
            return None

    ##  Get all tools
    #   \return tools \type{dict}
    def getAllTools(self) -> Dict[str, "Tool"]:
        return self._tools

    ##  Add a Tool (transform object, translate object) if its not already added.
    #   \param tool \type{Tool} Tool to be added
    def addTool(self, tool: "Tool") -> None:
        name = tool.getPluginId()
        if name not in self._tools:
            self._tools[name] = tool
            tool.operationStarted.connect(self._onToolOperationStarted)
            tool.operationStopped.connect(self._onToolOperationStopped)
            self.toolsChanged.emit()
        else:
            Logger.log("w", "%s was already added to tool list. Unable to add it again.", name)

    def _onToolOperationStarted(self, tool: "Tool") -> None:
        if not self._tool_operation_active:
            self._tool_operation_active = True
            self.toolOperationStarted.emit(tool)

    def _onToolOperationStopped(self, tool: "Tool") -> None:
        if self._tool_operation_active:
            self._tool_operation_active = False
            self.toolOperationStopped.emit(tool)

    ##  Gets whether a tool is currently in use
    #   \return \type{bool} true if a tool current being used.
    def isToolOperationActive(self) -> bool:
        return self._tool_operation_active

    ##  Request active tool. Returns None if there is no active tool
    #   \return Tool if a tool is active, None otherwise.
    def getActiveTool(self) -> Optional["Tool"]:
        return self._active_tool

    ##  Set the current active tool.
    #   The tool can be set by name of the tool or directly passing the tool object.
    #   \param tool A tool object or the name of a tool.
    def setActiveTool(self, tool: Optional[Union["Tool", str]]):
        from UM.Tool import Tool
        if self._active_tool:
            self._active_tool.event(ToolEvent(ToolEvent.ToolDeactivateEvent))

        if isinstance(tool, Tool) or tool is None:
            new_tool = cast(Optional[Tool], tool)
        else:
            new_tool = self.getTool(tool)

        tool_changed = False
        if self._active_tool is not new_tool:
            self._active_tool = new_tool
            tool_changed = True

        if self._active_tool:
            self._active_tool.event(ToolEvent(ToolEvent.ToolActivateEvent))

        from UM.Scene.Selection import Selection  # Imported here to prevent a circular dependency.
        if not self._active_tool and Selection.getCount() > 0:  # If something is selected, a tool must always be active.
            if "TranslateTool" in self._tools:
                self._active_tool = self._tools["TranslateTool"]  # Then default to the translation tool.
                self._active_tool.event(ToolEvent(ToolEvent.ToolActivateEvent))
                tool_changed = True
            else:
                Logger.log("w", "Controller does not have an active tool and could not default to Translate tool.")

        if tool_changed:
            self.activeToolChanged.emit()

    ##  Emitted when the list of tools changes.
    toolsChanged = Signal()

    ##  Emitted when a tool changes its enabled state.
    toolEnabledChanged = Signal()

    ##  Emitted when the active tool changes.
    activeToolChanged = Signal()

    ##  Emitted whenever a tool starts a longer operation.
    #
    #   \param tool The tool that started the operation.
    #   \sa Tool::startOperation
    toolOperationStarted = Signal()

    ##  Emitted whenever a tool stops a longer operation.
    #
    #   \param tool The tool that stopped the operation.
    #   \sa Tool::stopOperation
    toolOperationStopped = Signal()

    ##  Get the scene
    #   \return scene \type{Scene}
    def getScene(self) -> Scene:
        return self._scene

    ##  Process an event
    #   \param event \type{Event} event to be handle.
    #   The event is first passed to the selection tool, then the active tool and finally the camera tool.
    #   If none of these events handle it (when they return something that does not evaluate to true)
    #   a context menu signal is emitted.
    def event(self, event: Event):
        if self._selection_tool and self._selection_tool.event(event):
            return

        if self._active_tool and self._active_tool.event(event):
            return

        if self._camera_tool and self._camera_tool.event(event):
            return

        if self._tools and event.type == Event.KeyPressEvent:
            event = cast(KeyEvent, event)
            from UM.Scene.Selection import Selection  # Imported here to prevent a circular dependency.
            if Selection.hasSelection():
                for key, tool in self._tools.items():
                    if tool.getShortcutKey() is not None and event.key == tool.getShortcutKey():
                        self.setActiveTool(tool)

        if self._active_view:
            self._active_view.event(event)

        if event.type == Event.MouseReleaseEvent:
            event = cast(MouseEvent, event)
            if MouseEvent.RightButton in event.buttons:
                self.contextMenuRequested.emit(event.x, event.y)

    contextMenuRequested = Signal()

    ##  Set the tool used for handling camera controls.
    #   Camera tool is the first tool to receive events.
    #   The tool can be set by name of the tool or directly passing the tool object.
    #   \param tool \type{Tool} or \type{string}
    #   \sa setSelectionTool
    #   \sa setActiveTool
    def setCameraTool(self, tool: Union["Tool", str]):
        from UM.Tool import Tool
        if isinstance(tool, Tool) or tool is None:
            self._camera_tool = cast(Optional[Tool], tool)
        else:
            self._camera_tool = self.getTool(tool)

    ##  Get the camera tool (if any)
    #   \returns camera tool (or none)
    def getCameraTool(self) -> Optional["Tool"]:
        return self._camera_tool

    ##  Set the tool used for performing selections.
    #   Selection tool receives its events after camera tool and active tool.
    #   The tool can be set by name of the tool or directly passing the tool object.
    #   \param tool \type{Tool} or \type{string}
    #   \sa setCameraTool
    #   \sa setActiveTool
    def setSelectionTool(self, tool: Union[str, "Tool"]):
        from UM.Tool import Tool
        if isinstance(tool, Tool) or tool is None:
            self._selection_tool = cast(Optional[Tool], tool)
        else:
            self._selection_tool = self.getTool(tool)

    def getToolsEnabled(self) -> bool:
        return self._tools_enabled

    def setToolsEnabled(self, enabled: bool) -> None:
        self._tools_enabled = enabled

    # Rotate camera view according defined angle
    def rotateView(self, coordinate: str = "x", angle: int = 0) -> None:
        camera = self._scene.getActiveCamera()
        if not camera:
            return
        self._camera_tool.setOrigin(Vector(0, 100, 0)) #type: ignore
        if coordinate == "home":
            camera.setPosition(Vector(0, 0, 700))
            camera.setPerspective(True)
            camera.lookAt(Vector(0, 100, 100))
            self._camera_tool.rotateCam(0, 0) #type: ignore
        elif coordinate == "3d":
            camera.setPosition(Vector(-750, 600, 700))
            camera.setPerspective(True)
            camera.lookAt(Vector(0, 100, 100))
            self._camera_tool.rotateCam(0, 0) #type: ignore

        else:
            # for comparison is == used, because might not store them at the same location
            # https://stackoverflow.com/questions/1504717/why-does-comparing-strings-in-python-using-either-or-is-sometimes-produce
            camera.setPosition(Vector(0, 0, 700))
            camera.setPerspective(True)
            camera.lookAt(Vector(0, 100, 0))

            if coordinate == "x":
                self._camera_tool.rotateCam(angle, 0) #type: ignore
            elif coordinate == "y":
                self._camera_tool.rotateCam(0, angle) #type: ignore
Exemplo n.º 17
0
class Controller:
    def __init__(self, application: "Application") -> None:
        super().__init__()  # Call super to make multiple inheritance work.

        self._scene = Scene()
        self._application = application

        self._active_view = None  # type: Optional[View]
        self._views = {}  # type: Dict[str, View]

        self._active_tool = None  # type: Optional[Tool]
        self._tool_operation_active = False
        self._tools = {}  # type: Dict[str, Tool]
        self._camera_tool = None #type: Optional[Tool]
        self._selection_tool = None #type: Optional[Tool]
        self._tools_enabled = True #type: bool

        self._active_stage = None #type: Optional[Stage]
        self._stages = {} #type: Dict[str, Stage]

        self._input_devices = {} #type: Dict[str, InputDevice]

        PluginRegistry.addType("stage", self.addStage)
        PluginRegistry.addType("view", self.addView)
        PluginRegistry.addType("tool", self.addTool)
        PluginRegistry.addType("input_device", self.addInputDevice)

    ##  Add a view by name if it"s not already added.
    #   \param name \type{string} Unique identifier of view (usually the plugin name)
    #   \param view \type{View} The view to be added
    def addView(self, view: View) -> None:
        name = view.getPluginId()
        if name not in self._views:
            self._views[name] = view
            view.setRenderer(self._application.getRenderer())
            self.viewsChanged.emit()
        else:
            Logger.log("w", "%s was already added to view list. Unable to add it again.", name)

    ##  Request view by name. Returns None if no view is found.
    #   \param name \type{string} Unique identifier of view (usually the plugin name)
    #   \return View \type{View} if name was found, none otherwise.
    def getView(self, name: str) -> Optional[View]:
        try:
            return self._views[name]
        except KeyError:  # No such view
            Logger.log("e", "Unable to find %s in view list", name)
            return None

    ##  Return all views.
    #   \return views \type{dict}
    def getAllViews(self) -> Dict[str, View]:
        return self._views

    ##  Request active view. Returns None if there is no active view
    #   \return view \type{View} if an view is active, None otherwise.
    def getActiveView(self) -> Optional[View]:
        return self._active_view

    ##  Set the currently active view.
    #   \param name \type{string} The name of the view to set as active
    def setActiveView(self, name: str) -> None:
        Logger.log("d", "Setting active view to %s", name)
        try:
            if self._active_view:
                self._active_view.event(ViewEvent(Event.ViewDeactivateEvent))

            self._active_view = self._views[name]

            if self._active_view:
                self._active_view.event(ViewEvent(Event.ViewActivateEvent))

            self.activeViewChanged.emit()
        except KeyError:
            Logger.log("e", "No view named %s found", name)
        except Exception as e:
            Logger.logException("e", "An exception occurred while switching views: %s", str(e))

    ##  Emitted when the list of views changes.
    viewsChanged = Signal()

    ##  Emitted when the active view changes.
    activeViewChanged = Signal()

    ##  Add a stage by name if it's not already added.
    #   \param name \type{string} Unique identifier of stage (usually the plugin name)
    #   \param stage \type{Stage} The stage to be added
    def addStage(self, stage: Stage) -> None:
        name = stage.getPluginId()
        if name not in self._stages:
            self._stages[name] = stage
            self.stagesChanged.emit()

    ##  Request stage by name. Returns None if no stage is found.
    #   \param name \type{string} Unique identifier of stage (usually the plugin name)
    #   \return Stage \type{Stage} if name was found, none otherwise.
    def getStage(self, name: str) -> Optional[Stage]:
        try:
            return self._stages[name]
        except KeyError:  # No such view
            Logger.log("e", "Unable to find %s in stage list", name)
            return None

    ##  Return all stages.
    #   \return stages \type{dict}
    def getAllStages(self) -> Dict[str, Stage]:
        return self._stages

    ##  Request active stage. Returns None if there is no active stage
    #   \return stage \type{Stage} if an stage is active, None otherwise.
    def getActiveStage(self) -> Optional[Stage]:
        return self._active_stage

    ##  Set the currently active stage.
    #   \param name \type{string} The name of the stage to set as active
    def setActiveStage(self, name: str) -> None:
        try:
            # Don't actually change the stage if it is the current selected one.
            if self._active_stage != self._stages[name]:
                previous_stage = self._active_stage
                Logger.log("d", "Setting active stage to %s", name)
                self._active_stage = self._stages[name]

                # If there is no error switching stages, then finish first the previous stage (if it exists) and start the new stage
                if previous_stage is not None:
                    previous_stage.onStageDeselected()
                self._active_stage.onStageSelected()
                self.activeStageChanged.emit()
        except KeyError:
            Logger.log("e", "No stage named %s found", name)
        except Exception as e:
            Logger.logException("e", "An exception occurred while switching stages: %s", str(e))

    ##  Emitted when the list of stages changes.
    stagesChanged = Signal()

    ##  Emitted when the active stage changes.
    activeStageChanged = Signal()

    ##  Add an input device (e.g. mouse, keyboard, etc) if it's not already added.
    #   \param device The input device to be added
    def addInputDevice(self, device: InputDevice) -> None:
        name = device.getPluginId()
        if name not in self._input_devices:
            self._input_devices[name] = device
            device.event.connect(self.event)
        else:
            Logger.log("w", "%s was already added to input device list. Unable to add it again." % name)

    ##  Request input device by name. Returns None if no device is found.
    #   \param name \type{string} Unique identifier of input device (usually the plugin name)
    #   \return input \type{InputDevice} device if name was found, none otherwise.
    def getInputDevice(self, name: str) -> Optional[InputDevice]:
        try:
            return self._input_devices[name]
        except KeyError:  # No such device
            Logger.log("e", "Unable to find %s in input devices", name)
            return None

    ##  Remove an input device from the list of input devices.
    #   Does nothing if the input device is not in the list.
    #   \param name \type{string} The name of the device to remove.
    def removeInputDevice(self, name: str) -> None:
        if name in self._input_devices:
            self._input_devices[name].event.disconnect(self.event)
            del self._input_devices[name]

    ##  Request tool by name. Returns None if no view is found.
    #   \param name \type{string} Unique identifier of tool (usually the plugin name)
    #   \return tool \type{Tool} if name was found, None otherwise.
    def getTool(self, name: str) -> Optional["Tool"]:
        try:
            return self._tools[name]
        except KeyError:  # No such tool
            Logger.log("e", "Unable to find %s in tools", name)
            return None

    ##  Get all tools
    #   \return tools \type{dict}
    def getAllTools(self) -> Dict[str, "Tool"]:
        return self._tools

    ##  Add a Tool (transform object, translate object) if its not already added.
    #   \param tool \type{Tool} Tool to be added
    def addTool(self, tool: "Tool") -> None:
        name = tool.getPluginId()
        if name not in self._tools:
            self._tools[name] = tool
            tool.operationStarted.connect(self._onToolOperationStarted)
            tool.operationStopped.connect(self._onToolOperationStopped)
            self.toolsChanged.emit()
        else:
            Logger.log("w", "%s was already added to tool list. Unable to add it again.", name)

    def _onToolOperationStarted(self, tool: "Tool") -> None:
        if not self._tool_operation_active:
            self._tool_operation_active = True
            self.toolOperationStarted.emit(tool)

    def _onToolOperationStopped(self, tool: "Tool") -> None:
        if self._tool_operation_active:
            self._tool_operation_active = False
            self.toolOperationStopped.emit(tool)

    ##  Gets whether a tool is currently in use
    #   \return \type{bool} true if a tool current being used.
    def isToolOperationActive(self) -> bool:
        return self._tool_operation_active

    ##  Request active tool. Returns None if there is no active tool
    #   \return Tool if a tool is active, None otherwise.
    def getActiveTool(self) -> Optional["Tool"]:
        return self._active_tool

    ##  Set the current active tool.
    #   The tool can be set by name of the tool or directly passing the tool object.
    #   \param tool A tool object or the name of a tool.
    def setActiveTool(self, tool: Optional[Union["Tool", str]]):
        from UM.Tool import Tool
        if self._active_tool:
            self._active_tool.event(ToolEvent(ToolEvent.ToolDeactivateEvent))

        if isinstance(tool, Tool) or tool is None:
            new_tool = cast(Optional[Tool], tool)
        else:
            new_tool = self.getTool(tool)

        tool_changed = False
        if self._active_tool is not new_tool:
            self._active_tool = new_tool
            tool_changed = True

        if self._active_tool:
            self._active_tool.event(ToolEvent(ToolEvent.ToolActivateEvent))

        from UM.Scene.Selection import Selection  # Imported here to prevent a circular dependency.
        if not self._active_tool and Selection.getCount() > 0:  # If something is selected, a tool must always be active.
            if "TranslateTool" in self._tools:
                self._active_tool = self._tools["TranslateTool"]  # Then default to the translation tool.
                self._active_tool.event(ToolEvent(ToolEvent.ToolActivateEvent))
                tool_changed = True
            else:
                Logger.log("w", "Controller does not have an active tool and could not default to Translate tool.")

        if tool_changed:
            self.activeToolChanged.emit()

    ##  Emitted when the list of tools changes.
    toolsChanged = Signal()

    ##  Emitted when a tool changes its enabled state.
    toolEnabledChanged = Signal()

    ##  Emitted when the active tool changes.
    activeToolChanged = Signal()

    ##  Emitted whenever a tool starts a longer operation.
    #
    #   \param tool The tool that started the operation.
    #   \sa Tool::startOperation
    toolOperationStarted = Signal()

    ##  Emitted whenever a tool stops a longer operation.
    #
    #   \param tool The tool that stopped the operation.
    #   \sa Tool::stopOperation
    toolOperationStopped = Signal()

    ##  Get the scene
    #   \return scene \type{Scene}
    def getScene(self) -> Scene:
        return self._scene

    ##  Process an event
    #   \param event \type{Event} event to be handle.
    #   The event is first passed to the selection tool, then the active tool and finally the camera tool.
    #   If none of these events handle it (when they return something that does not evaluate to true)
    #   a context menu signal is emitted.
    def event(self, event: Event):
        if self._selection_tool and self._selection_tool.event(event):
            return

        if self._active_tool and self._active_tool.event(event):
            return

        if self._camera_tool and self._camera_tool.event(event):
            return

        if self._tools and event.type == Event.KeyPressEvent:
            event = cast(KeyEvent, event)
            from UM.Scene.Selection import Selection  # Imported here to prevent a circular dependency.
            if Selection.hasSelection():
                for key, tool in self._tools.items():
                    if tool.getShortcutKey() is not None and event.key == tool.getShortcutKey():
                        self.setActiveTool(tool)

        if self._active_view:
            self._active_view.event(event)

        if event.type == Event.MouseReleaseEvent:
            event = cast(MouseEvent, event)
            if MouseEvent.RightButton in event.buttons:
                self.contextMenuRequested.emit(event.x, event.y)

    contextMenuRequested = Signal()

    ##  Set the tool used for handling camera controls.
    #   Camera tool is the first tool to receive events.
    #   The tool can be set by name of the tool or directly passing the tool object.
    #   \param tool \type{Tool} or \type{string}
    #   \sa setSelectionTool
    #   \sa setActiveTool
    def setCameraTool(self, tool: Union["Tool", str]):
        from UM.Tool import Tool
        if isinstance(tool, Tool) or tool is None:
            self._camera_tool = cast(Optional[Tool], tool)
        else:
            self._camera_tool = self.getTool(tool)

    ##  Get the camera tool (if any)
    #   \returns camera tool (or none)
    def getCameraTool(self) -> Optional["Tool"]:
        return self._camera_tool

    ##  Set the tool used for performing selections.
    #   Selection tool receives its events after camera tool and active tool.
    #   The tool can be set by name of the tool or directly passing the tool object.
    #   \param tool \type{Tool} or \type{string}
    #   \sa setCameraTool
    #   \sa setActiveTool
    def setSelectionTool(self, tool: Union[str, "Tool"]):
        from UM.Tool import Tool
        if isinstance(tool, Tool) or tool is None:
            self._selection_tool = cast(Optional[Tool], tool)
        else:
            self._selection_tool = self.getTool(tool)

    def getToolsEnabled(self) -> bool:
        return self._tools_enabled

    def setToolsEnabled(self, enabled: bool) -> None:
        self._tools_enabled = enabled

    def deleteAllNodesWithMeshData(self, only_selectable:bool = True) -> None:
        Logger.log("i", "Clearing scene")
        if not self.getToolsEnabled():
            return

        nodes = []
        for node in DepthFirstIterator(self.getScene().getRoot()):
            if not node.getMeshData() and not node.callDecoration("isGroup"):
                continue  # Node that doesnt have a mesh and is not a group.
            if only_selectable and not node.isSelectable():
                continue  # Only remove nodes that are selectable.
            if node.getParent() and cast(SceneNode, node.getParent()).callDecoration("isGroup"):
                continue  # Grouped nodes don't need resetting as their parent (the group) is resetted)
            nodes.append(node)
        if nodes:
            from UM.Operations.GroupedOperation import GroupedOperation
            op = GroupedOperation()

            for node in nodes:
                from UM.Operations.RemoveSceneNodeOperation import RemoveSceneNodeOperation
                op.addOperation(RemoveSceneNodeOperation(node))

                # Reset the print information
                self.getScene().sceneChanged.emit(node)

            op.push()
            from UM.Scene.Selection import Selection
            Selection.clear()

    # Rotate camera view according defined angle
    def setCameraRotation(self, coordinate: str = "x", angle: int = 0) -> None:
        camera = self._scene.getActiveCamera()
        if not camera:
            return
        camera.setZoomFactor(camera.getDefaultZoomFactor())
        self._camera_tool.setOrigin(Vector(0, 100, 0))  # type: ignore
        if coordinate == "home":
            camera.setPosition(Vector(0, 100, 700))
            camera.lookAt(Vector(0, 100, 0))
            self._camera_tool.rotateCamera(0, 0)  # type: ignore
        elif coordinate == "3d":
            camera.setPosition(Vector(-750, 600, 700))
            camera.lookAt(Vector(0, 100, 100))
            self._camera_tool.rotateCamera(0, 0)  # type: ignore
        else:
            # for comparison is == used, because might not store them at the same location
            # https://stackoverflow.com/questions/1504717/why-does-comparing-strings-in-python-using-either-or-is-sometimes-produce

            if coordinate == "x":
                camera.setPosition(Vector(0, 100, 700))
                camera.lookAt(Vector(0, 100, 0))
                self._camera_tool.rotateCamera(angle, 0)  # type: ignore
            elif coordinate == "y":
                if angle == 90:
                    # Prepare the camera for top view, so no rotation has to be applied after setting the top view.
                    camera.setPosition(Vector(0, 100, 100))
                    camera.lookAt(Vector(0, 100, 0))
                    self._camera_tool.rotateCamera(90, 0)  # type: ignore
                    # Actually set the top view.
                    camera.setPosition(Vector(0, 800, 1))
                    camera.lookAt(Vector(0, 100, 1))
                    self._camera_tool.rotateCamera(0, 0)  # type: ignore
                else:
                    camera.setPosition(Vector(0, 100, 700))
                    camera.lookAt(Vector(0, 100, 0))
                    self._camera_tool.rotateCamera(0, angle)  # type: ignore
Exemplo n.º 18
0
def test_cameras():
    scene = Scene()
    camera_1 = Camera("camera_one")
    camera_2 = Camera("camera_two")
    scene.getRoot().addChild(camera_1)

    assert scene.findCamera("camera_one") == camera_1
    assert scene.findCamera("camera_nope") is None
    assert scene.findCamera("camera_two") is None
    scene.getRoot().addChild(camera_2)
    assert scene.findCamera("camera_two") == camera_2

    all_cameras = scene.getAllCameras()
    assert camera_1 in all_cameras
    assert camera_2 in all_cameras

    scene.setActiveCamera("camera_one")
    assert scene.getActiveCamera() == camera_1
    scene.setActiveCamera(
        "camera_one")  # Ensure that setting it again doesn't break things.
Exemplo n.º 19
0
def test_cameras():
    scene = Scene()
    camera_1 = Camera("camera_one")
    camera_2 = Camera("camera_two")
    scene.getRoot().addChild(camera_1)

    assert scene.findCamera("camera_one") == camera_1
    assert scene.findCamera("camera_nope") is None
    assert scene.findCamera("camera_two") is None
    scene.getRoot().addChild(camera_2)
    assert scene.findCamera("camera_two") == camera_2

    all_cameras = scene.getAllCameras()
    assert camera_1 in all_cameras
    assert camera_2 in all_cameras

    scene.setActiveCamera("camera_one")
    assert scene.getActiveCamera() == camera_1
    scene.setActiveCamera("camera_one")  # Ensure that setting it again doesn't break things.
Exemplo n.º 20
0
class Controller:
    """Glue class that holds the scene, (active) view(s), (active) tool(s) and possible user inputs.

    The different types of views / tools / inputs are defined by plugins.
    :sa View
    :sa Tool
    :sa Scene
    """
    def __init__(self, application: "Application") -> None:
        super().__init__()  # Call super to make multiple inheritance work.

        self._scene = Scene()
        self._application = application

        self._active_view = None  # type: Optional[View]
        self._views = {}  # type: Dict[str, View]

        self._active_tool = None  # type: Optional[Tool]
        self._fallback_tool = "TranslateTool"  # type: str
        self._tool_operation_active = False
        self._tools = {}  # type: Dict[str, Tool]
        self._camera_tool = None  #type: Optional[Tool]
        self._selection_tool = None  #type: Optional[Tool]
        self._tools_enabled = True  #type: bool

        self._active_stage = None  #type: Optional[Stage]
        self._stages = {}  #type: Dict[str, Stage]

        self._input_devices = {}  #type: Dict[str, InputDevice]

        PluginRegistry.addType("stage", self.addStage)
        PluginRegistry.addType("view", self.addView)
        PluginRegistry.addType("tool", self.addTool)
        PluginRegistry.addType("input_device", self.addInputDevice)

    def addView(self, view: View) -> None:
        """Add a view by name if it"s not already added.

        :param view: The view to be added
        """

        name = view.getId()
        if name not in self._views:
            self._views[name] = view
            view.setRenderer(self._application.getRenderer())
            self.viewsChanged.emit()
        else:
            Logger.log(
                "w",
                "%s was already added to view list. Unable to add it again.",
                name)

    def getView(self, name: str) -> Optional[View]:
        """Request view by name. Returns None if no view is found.

        :return: View  if name was found, none otherwise.
        """

        try:
            return self._views[name]
        except KeyError:  # No such view
            Logger.log("e", "Unable to find %s in view list", name)
            return None

    def getAllViews(self) -> Dict[str, View]:
        """Return all views.

        :return: views
        """

        return self._views

    def getActiveView(self) -> Optional[View]:
        """Request active view. Returns None if there is no active view

        :return: view if an view is active, None otherwise.
        """

        return self._active_view

    def setActiveView(self, name: str) -> None:
        """Set the currently active view.

        :param name:  The name of the view to set as active
        """

        Logger.log("d", "Setting active view to %s", name)
        try:
            if self._active_view:
                self._active_view.event(ViewEvent(Event.ViewDeactivateEvent))

            self._active_view = self._views[name]

            if self._active_view:
                self._active_view.event(ViewEvent(Event.ViewActivateEvent))

            self.activeViewChanged.emit()
        except KeyError:
            Logger.log("e", "No view named %s found", name)
        except Exception as e:
            Logger.logException(
                "e", "An exception occurred while switching views: %s", str(e))

    viewsChanged = Signal()
    """Emitted when the list of views changes."""

    activeViewChanged = Signal()
    """Emitted when the active view changes."""

    def addStage(self, stage: Stage) -> None:
        """Add a stage if it's not already added.

        :param stage: The stage to be added
        """

        name = stage.getId()
        if name not in self._stages:
            self._stages[name] = stage
            self.stagesChanged.emit()

    def getStage(self, name: str) -> Optional[Stage]:
        """Request stage by name. Returns None if no stage is found.

        :param name: Unique identifier of stage (usually the plugin name)
        :return: Stage if name was found, None otherwise.
        """

        try:
            return self._stages[name]
        except KeyError:  # No such view
            Logger.log("e", "Unable to find %s in stage list", name)
            return None

    def getAllStages(self) -> Dict[str, Stage]:
        """Return all stages.

        :return: stages
        """

        return self._stages

    def getActiveStage(self) -> Optional[Stage]:
        """Request active stage. Returns None if there is no active stage

        :return: stage if an stage is active, None otherwise.
        """

        return self._active_stage

    def setActiveStage(self, name: str) -> None:
        """Set the currently active stage.

        :param name: The name of the stage to set as active
        """

        try:
            # Don't actually change the stage if it is the current selected one.
            if self._active_stage != self._stages[name]:
                previous_stage = self._active_stage
                Logger.log("d", "Setting active stage to %s", name)
                self._active_stage = self._stages[name]

                # If there is no error switching stages, then finish first the previous stage (if it exists) and start the new stage
                if previous_stage is not None:
                    previous_stage.onStageDeselected()
                self._active_stage.onStageSelected()
                self.activeStageChanged.emit()
        except KeyError:
            Logger.log("e", "No stage named %s found", name)
        except Exception as e:
            Logger.logException(
                "e", "An exception occurred while switching stages: %s",
                str(e))

    stagesChanged = Signal()
    """Emitted when the list of stages changes."""

    activeStageChanged = Signal()
    """Emitted when the active stage changes."""

    def addInputDevice(self, device: InputDevice) -> None:
        """Add an input device (e.g. mouse, keyboard, etc) if it's not already added.

        :param device: The input device to be added
        """

        name = device.getId()
        if name not in self._input_devices:
            self._input_devices[name] = device
            device.event.connect(self.event)
        else:
            Logger.log(
                "w",
                "%s was already added to input device list. Unable to add it again."
                % name)

    def getInputDevice(self, name: str) -> Optional[InputDevice]:
        """Request input device by name. Returns None if no device is found.

        :param name: Unique identifier of input device (usually the plugin name)
        :return: input device if name was found, none otherwise.
        """

        try:
            return self._input_devices[name]
        except KeyError:  # No such device
            Logger.log("e", "Unable to find %s in input devices", name)
            return None

    def removeInputDevice(self, name: str) -> None:
        """Remove an input device from the list of input devices.

        Does nothing if the input device is not in the list.
        :param name: The name of the device to remove.
        """

        if name in self._input_devices:
            self._input_devices[name].event.disconnect(self.event)
            del self._input_devices[name]

    def getFallbackTool(self) -> str:
        """Request the current fallbacl tool.

        :return: Id of the fallback tool
        """

        return self._fallback_tool

    def setFallbackTool(self, tool: str) -> None:
        """Set the current active tool. The tool must be set by name.

        :param tool: The tools name which shall be used as fallback
        """

        if self._fallback_tool is not tool:
            self._fallback_tool = tool

    def getTool(self, name: str) -> Optional["Tool"]:
        """Request tool by name. Returns None if no tool is found.

        :param name: Unique identifier of tool (usually the plugin name)
        :return: tool if name was found, None otherwise.
        """

        try:
            return self._tools[name]
        except KeyError:  # No such tool
            Logger.log("e", "Unable to find %s in tools", name)
            return None

    def getAllTools(self) -> Dict[str, "Tool"]:
        """Get all tools

        :return: tools
        """

        return self._tools

    def addTool(self, tool: "Tool") -> None:
        """Add a Tool (transform object, translate object) if its not already added.

        :param tool: Tool to be added
        """

        name = tool.getId()
        if name not in self._tools:
            self._tools[name] = tool
            tool.operationStarted.connect(self._onToolOperationStarted)
            tool.operationStopped.connect(self._onToolOperationStopped)
            self.toolsChanged.emit()
        else:
            Logger.log(
                "w",
                "%s was already added to tool list. Unable to add it again.",
                name)

    def _onToolOperationStarted(self, tool: "Tool") -> None:
        if not self._tool_operation_active:
            self._tool_operation_active = True
            self.toolOperationStarted.emit(tool)

    def _onToolOperationStopped(self, tool: "Tool") -> None:
        if self._tool_operation_active:
            self._tool_operation_active = False
            self.toolOperationStopped.emit(tool)

    def isToolOperationActive(self) -> bool:
        """Gets whether a tool is currently in use

        :return: true if a tool current being used.
        """

        return self._tool_operation_active

    def getActiveTool(self) -> Optional["Tool"]:
        """Request active tool. Returns None if there is no active tool

        :return: Tool if a tool is active, None otherwise.
        """

        return self._active_tool

    def setActiveTool(self, tool: Optional[Union["Tool", str]]):
        """Set the current active tool.

        The tool can be set by name of the tool or directly passing the tool object.
        :param tool: A tool object or the name of a tool.
        """

        from UM.Tool import Tool
        if self._active_tool:
            self._active_tool.event(ToolEvent(ToolEvent.ToolDeactivateEvent))

        if isinstance(tool, Tool) or tool is None:
            new_tool = cast(Optional[Tool], tool)
        else:
            new_tool = self.getTool(tool)

        tool_changed = False
        if self._active_tool is not new_tool:
            self._active_tool = new_tool
            tool_changed = True

        if self._active_tool:
            self._active_tool.event(ToolEvent(ToolEvent.ToolActivateEvent))

        from UM.Scene.Selection import Selection  # Imported here to prevent a circular dependency.
        if not self._active_tool and Selection.getCount(
        ) > 0:  # If something is selected, a tool must always be active.
            if self._fallback_tool in self._tools:
                self._active_tool = self._tools[
                    self.
                    _fallback_tool]  # Then default to the translation tool.
                self._active_tool.event(ToolEvent(ToolEvent.ToolActivateEvent))
                tool_changed = True
            else:
                Logger.log(
                    "w",
                    "Controller does not have an active tool and could not default to the tool, called \"{}\"."
                    .format(self._fallback_tool))

        if tool_changed:
            Selection.setFaceSelectMode(False)
            Selection.clearFace()
            self.activeToolChanged.emit()

    toolsChanged = Signal()
    """Emitted when the list of tools changes."""

    toolEnabledChanged = Signal()
    """Emitted when a tool changes its enabled state."""

    activeToolChanged = Signal()
    """Emitted when the active tool changes."""

    toolOperationStarted = Signal()
    """Emitted whenever a tool starts a longer operation.

    :param tool: The tool that started the operation.
    :sa Tool::startOperation
    """

    toolOperationStopped = Signal()
    """Emitted whenever a tool stops a longer operation.

    :param tool: The tool that stopped the operation.
    :sa Tool::stopOperation
    """

    def getScene(self) -> Scene:
        """Get the scene

        :return: scene
        """

        return self._scene

    def event(self, event: Event):
        """Process an event

        The event is first passed to the selection tool, then the active tool and finally the camera tool.
        If none of these events handle it (when they return something that does not evaluate to true)
        a context menu signal is emitted.

        :param event: event to be handle.
        """

        if self._selection_tool and self._selection_tool.event(event):
            return

        if self._active_tool and self._active_tool.event(event):
            return

        if self._camera_tool and self._camera_tool.event(event):
            return

        if self._tools and event.type == Event.KeyPressEvent:
            event = cast(KeyEvent, event)
            from UM.Scene.Selection import Selection  # Imported here to prevent a circular dependency.
            if Selection.hasSelection():
                for key, tool in self._tools.items():
                    if tool.getShortcutKey(
                    ) is not None and event.key == tool.getShortcutKey():
                        self.setActiveTool(tool)

        if self._active_view:
            self._active_view.event(event)

        if event.type == Event.MouseReleaseEvent:
            event = cast(MouseEvent, event)
            if MouseEvent.RightButton in event.buttons:
                self.contextMenuRequested.emit(event.x, event.y)

    contextMenuRequested = Signal()

    def setCameraTool(self, tool: Union["Tool", str]):
        """Set the tool used for handling camera controls.

        Camera tool is the first tool to receive events.
        The tool can be set by name of the tool or directly passing the tool object.
        :param tool:
        :sa setSelectionTool
        :sa setActiveTool
        """

        from UM.Tool import Tool
        if isinstance(tool, Tool) or tool is None:
            self._camera_tool = cast(Optional[Tool], tool)
        else:
            self._camera_tool = self.getTool(tool)

    def getCameraTool(self) -> Optional["Tool"]:
        """Get the camera tool (if any)

        :returns: camera tool (or none)
        """

        return self._camera_tool

    def setSelectionTool(self, tool: Union[str, "Tool"]):
        """Set the tool used for performing selections.

        Selection tool receives its events after camera tool and active tool.
        The tool can be set by name of the tool or directly passing the tool object.
        :param tool:
        :sa setCameraTool
        :sa setActiveTool
        """

        from UM.Tool import Tool
        if isinstance(tool, Tool) or tool is None:
            self._selection_tool = cast(Optional[Tool], tool)
        else:
            self._selection_tool = self.getTool(tool)

    def getToolsEnabled(self) -> bool:
        return self._tools_enabled

    def setToolsEnabled(self, enabled: bool) -> None:
        self._tools_enabled = enabled

    def deleteAllNodesWithMeshData(self, only_selectable: bool = True) -> None:
        Logger.log("i", "Clearing scene")
        if not self.getToolsEnabled():
            return

        nodes = []
        for node in DepthFirstIterator(self.getScene().getRoot()):
            if not node.isEnabled():
                continue
            if not node.getMeshData() and not node.callDecoration("isGroup"):
                continue  # Node that doesnt have a mesh and is not a group.
            if only_selectable and not node.isSelectable():
                continue  # Only remove nodes that are selectable.
            if node.getParent() and cast(
                    SceneNode, node.getParent()).callDecoration("isGroup"):
                continue  # Grouped nodes don't need resetting as their parent (the group) is resetted)
            nodes.append(node)
        if nodes:
            from UM.Operations.GroupedOperation import GroupedOperation
            op = GroupedOperation()

            for node in nodes:
                from UM.Operations.RemoveSceneNodeOperation import RemoveSceneNodeOperation
                op.addOperation(RemoveSceneNodeOperation(node))

                # Reset the print information
                self.getScene().sceneChanged.emit(node)

            op.push()
            from UM.Scene.Selection import Selection
            Selection.clear()

    # Rotate camera view according defined angle
    def setCameraRotation(self, coordinate: str = "x", angle: int = 0) -> None:
        camera = self._scene.getActiveCamera()
        if not camera:
            return
        camera.setZoomFactor(camera.getDefaultZoomFactor())
        self._camera_tool.setOrigin(Vector(0, 100, 0))  # type: ignore
        self.setCameraOrigin(coordinate)
        if coordinate == "home":
            camera.setPosition(Vector(0, 100, 700))
            self._camera_tool.rotateCamera(0, 0)  # type: ignore
        elif coordinate == "3d":
            camera.setPosition(Vector(-750, 600, 700))
            self._camera_tool.rotateCamera(0, 0)  # type: ignore
        else:
            # for comparison is == used, because might not store them at the same location
            # https://stackoverflow.com/questions/1504717/why-does-comparing-strings-in-python-using-either-or-is-sometimes-produce

            if coordinate == "x":
                camera.setPosition(Vector(0, 100, 700))
                self._camera_tool.rotateCamera(angle, 0)  # type: ignore
            elif coordinate == "y":
                if angle % 360 == 90:
                    # Prepare the camera for top view, so no rotation has to be applied after setting the top view.
                    camera.setPosition(Vector(0, 100, 100))
                    self._camera_tool.rotateCamera(90, 0)  # type: ignore
                    # Actually set the top view.
                    camera.setPosition(Vector(0, 800, 1))
                    self.setCameraOrigin("z")
                    camera.lookAt(Vector(0, 100, 1))
                    self._camera_tool.rotateCamera(0, 0)  # type: ignore
                elif angle % 360 == 270:
                    camera.setPosition(Vector(0, -100, 100))
                    self._camera_tool.rotateCamera(90, 0)  # type: ignore
                    camera.setPosition(Vector(0, -800, 1))
                    self.setCameraOrigin("z")
                    camera.lookAt(Vector(0, 100, 1))
                    self._camera_tool.rotateCamera(0, 0)  # type: ignore
                else:
                    camera.setPosition(Vector(0, 100, 700))
                    self._camera_tool.rotateCamera(0, angle)  # type: ignore

    # Position camera view according to defined position
    def setCameraPosition(self,
                          x_position: int = 0,
                          y_position: int = 0,
                          z_position: int = 0) -> None:
        camera = self._scene.getActiveCamera()
        if not camera:
            return
        camera.setPosition(Vector(x_position, y_position, z_position))

    # Indicate position where the camera should point at
    def setLookAtPosition(self,
                          x_look_at_position: int = 0,
                          y_look_at_position: int = 0,
                          z_look_at_position: int = 0) -> None:
        camera = self._scene.getActiveCamera()
        if not camera:
            return
        camera.lookAt(
            Vector(x_look_at_position, y_look_at_position, z_look_at_position))

    # Sets the zoom factor of the camera
    def setCameraZoomFactor(self, camera_zoom_factor: float = 0) -> None:
        camera = self._scene.getActiveCamera()
        if not camera:
            return
        camera.setZoomFactor(camera_zoom_factor)

    def setCameraOrigin(self, coordinate: str = "home"):
        """Changes the origin of the camera, i.e. where it looks at.

        :param coordinate: One of the following options:
        - "home": The centre of the build plate.
        - "3d": The centre of the build volume.
        - "x", "y" and "z": Also the centre of the build plate. These are just
        aliases for the setCameraRotation function.
        """

        camera = self._scene.getActiveCamera()
        if not camera:
            return
        coordinates = {
            "home": Vector(0, 100, 0),
            "3d": Vector(0, 100, 100),
            "x": Vector(0, 100, 0),
            "y": Vector(0, 100, 0),
            "z": Vector(0, 100, 1)
        }
        camera.lookAt(coordinates[coordinate])