Python Forum
Rotate with just one line
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Rotate with just one line
#1
I'm not completely new to Python, but I'm faced with a difficult problem for me.
Previously I wrote parsers and file converters.
Now I need to work with SVG files and I can’t find a solution.
I wrote this code using the Qt5 framework.
# python3 animated_svg.py

import sys
from PyQt5.QtWidgets import QApplication, QMainWindow, QGraphicsScene, QGraphicsView
from PyQt5.QtCore import Qt, QTimer
from PyQt5.QtSvg import QGraphicsSvgItem

class MainWindow(QMainWindow):
    def __init__(self):
        super().__init__()

        # Create scene and view
        self.scene = QGraphicsScene()
        self.view = QGraphicsView(self.scene)
        self.setCentralWidget(self.view)

        # Set screen size
        self.view.setFixedSize(700, 700)

        # Load SVG file with a line
        self.svg_item = QGraphicsSvgItem("two_lines.svg")
        self.scene.addItem(self.svg_item)

        # Find the center of the line
        line_center = self.svg_item.boundingRect().center()

        # Set the rotation origin point to the center of the line
        self.svg_item.setTransformOriginPoint(line_center)

        # Create initial parameters for line rotation
        self.angle = 0  # Initial rotation angle
        self.direction = 1  # Rotation direction: 1 - right, -1 - left

        # Create a timer for animation
        self.timer = QTimer(self)
        self.timer.timeout.connect(self.rotateLine)
        self.timer.start(50)  # Interval in milliseconds

    def rotateLine(self):
        # Increase or decrease the rotation angle
        self.angle += self.direction * 1  # Change the angle by 1 degree

        # Check if we have reached the extreme angle values
        if self.angle >= 45:
            self.angle = 45
            self.direction = -1  # Change direction to left
        elif self.angle <= -45:
            self.angle = -45
            self.direction = 1  # Change direction to right

        # Rotate the line
        self.svg_item.setRotation(self.angle)

if __name__ == "__main__":
    app = QApplication(sys.argv)
    mainWindow = MainWindow()
    mainWindow.show()
    sys.exit(app.exec_())
added two_lines.svg as a separate file
its text is like this:
Output:
<svg xmlns="http://www.w3.org/2000/svg"> <g id="horizontal_line"> <line x1="25" y1="100" x2="425" y2="100" stroke="black" stroke-width="2" /> </g> <g id="vertical_line"> <line x1="225" y1="50" x2="225" y2="150" stroke="red" stroke-width="2" /> </g> </svg>
After running the code, I just see the rotation of two lines, although I planned to rotate only one line with id="horizontal_line"
[Image: work-img.png]
I'll ask the community for help on how to do this.
Perhaps you need to change the text of the SVG file itself and also change the code of the main file.
I'd be grateful for any hints.
Reply
#2
You cannot do this in qt. Look for a package that lets you manipulate structured vector graphics.
Reply
#3
(May-26-2024, 09:52 PM)deanhystad Wrote: You cannot do this in qt. Look for a package that lets you manipulate structured vector graphics.

Can you please give more details or an example.
I don't understand python very well and I doubt that I will understand it thoroughly.
If it doesn’t work, I’ll most likely just abandon the project.
Reply
#4
See what you find when you google python svg. Since svg files are xml, maybe xml packages are a better option. There are serveral Python xml packages. The standard python distribution comes with xml.dom, xml.sax and xml.etree. I've heard good things about untangle, but it is getting old. Similar to untangle is xmltodict. Both of these read xml and create a python object to access the information.

Why do you want to rotate one of the lines? Structured vector graphics are not really meant to be used this way. They are meant to be do exactly what you are seeing, creating a graphic object that can be scaled or rotated about multiple axes. All the parts of the graphic affected by any translation or rotation. If you want two lines to act independently, you should have two svg files, or just draw lines. Even if you have success parsing your svg file, you still need to extract the line you want to rotate, rotate the line, place it back in the svg file, and load the file.

What is your end goal?
Reply
#5
(May-26-2024, 09:52 PM)deanhystad Wrote: What is your end goal?

I have a more complex SVG file, which is what I tried to animate.
But it didn’t work out in Python, and it didn’t quite work out in HTML+CSS.
As a result, so far we have been able to animate a little using styles in the SVG file itself, but this is not a technological solution.
Below is the svg file
https://drive.google.com/file/d/1z6J5VhO...flrjP/view
And work video

The hardest part is finding the center of rotation.
Now the rotation is around the number 5 located on the right.
And this
https://drive.google.com/file/d/16pbkgfm...sp=sharing
Before this, there were solutions on QT5, where they worked with SVG graphics and everything was fine.
As I already wrote, animation inside the SVG file itself is not technologically advanced and probably almost no one uses it.
It’s more interesting to take the original SVG file, parse it with some program, in C++ or Python, and animate it.
I understand this should work.
Reply
#6
Wow. Bravo. I've never seen anything like it. This is also ASP.net Microsoft. On the def and statement, you might need several definitions representing that 1 degree change. So you might write it out as possible solutions which graph to use. Otherwise it would rotate the whole graph. Bravo..again. Thanks for your knowledge in Python.

This may not work if you run this through a simulator. Sequence of files will be off base. Flight simulator. Probably not that accurate also. I thought this was a good idea. There always making flight simulators on the internet. Interesting I already knew Java and fortran programming language was part of them. Now I know Python. So, I can't wait. But lots more to learn. Learning on the IDE somewhat or Online compiler programiz. <class nonetype> This tells me it works. But it goes. Interesting learning, but it's a go.
Programs are like instructions or rules. Learning it gets us closer to a solution. Desired outcome. Computer talk.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  how to rotate lines in tkinter canvas helpmewithpython 1 3,584 Oct-06-2020, 06:56 PM
Last Post: deanhystad

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020