Преобразование представления Qt3DWindow в изображениеPython

Программы на Python
Ответить
Anonymous
 Преобразование представления Qt3DWindow в изображение

Сообщение Anonymous »

У меня есть Qt3DWindow внутри контейнера QWidget с добавленной к нему QSphere (а также материалом, освещением и камерой). Все работает хорошо, когда я запускаю его и вижу сферу. Теперь я хочу «захватить»/«рендерить» это представление и сохранить его как изображение. Я провел целый день, пытаясь понять, как решить эту, казалось бы, очень простую задачу, но до сих пор ничего не получилось.
Я пробовал QRenderCapture(), Grab() и QOffscreenSurface(), как рекомендуют некоторые распространенные Инструменты ИИ, безрезультатно.
Вот рабочий код окна и средства просмотра.
import sys
from PyQt6.QtWidgets import QApplication, QHBoxLayout, QWidget, QPushButton,
from PyQt6.QtWidgets import QMainWindow, QWidget
from PyQt6.QtGui import QIcon, QPixmap, QPainter, QImage, QMatrix4x4, QQuaternion, QVector3D, QColor, QGuiApplication, QPageLayout
from PyQt6.QtCore import QTimer
from PyQt6.Qt3DCore import QEntity, QTransform
from PyQt6 import Qt3DRender
from PyQt6.Qt3DExtras import QForwardRenderer, QPhongMaterial, Qt3DWindow, QOrbitCameraController, QDiffuseSpecularMaterial, QTextureMaterial, QNormalDiffuseMapMaterial, QDiffuseMapMaterial
from PyQt6.Qt3DExtras import QSphereMesh
from PyQt6.Qt3DRender import QRenderCapture, QRenderCaptureReply
from PyQt6.QtCore import QTimer

class MainWindow(QMainWindow):
def __init__(self):
super(MainWindow, self).__init__()
self.view = Qt3DWindow()
self.container = QWidget.createWindowContainer(self.view)
self.setCentralWidget(self.container)

# Create & set layouts
main_layout = QHBoxLayout()
main_layout.addWidget(self.container)

main_widget = QWidget()
main_widget.setLayout(main_layout)
self.setCentralWidget(main_widget)

# Root entity
self.rootEntity = QEntity()

# Load mesh
sphere_mesh = QSphereMesh()
sphere_mesh.setRadius(20)

mesh_entity = QEntity(self.rootEntity)
mesh_entity.addComponent(sphere_mesh)

# Load/create textures
diffuse_material = QPhongMaterial(self.rootEntity)
diffuse_material.setDiffuse(QColor.fromRgbF(0.1, 0.9, 0.7, 0.6))
mesh_entity.addComponent(diffuse_material)

# Add material components to entity
mesh_entity.addComponent(diffuse_material)

# Camera
self.camera = self.view.camera()
self.camera.lens().setPerspectiveProjection(60.0, 16.0 / 9.0, 1, 1000.0)
self.camera.setPosition(QVector3D(0.0, 0, 120.0))
self.camera.setViewCenter(QVector3D(0.0, 0.0, 0.0))

# camera controls
self.camController = QOrbitCameraController(self.rootEntity)
self.camController.setLinearSpeed(200.0)
self.camController.setLookSpeed(280.0)
self.camController.setCamera(self.camera)

# Light
light_entity = QEntity(self.rootEntity)
light = Qt3DRender.QPointLight(light_entity)
light.setConstantAttenuation(0)
light_entity.addComponent(light)
light_transform = QTransform()
light_transform.setTranslation(QVector3D(350, 100, 200))
light_entity.addComponent(light_transform)

self.view.setRootEntity(self.rootEntity)

self.capture_btn = QPushButton('Capture', self)
self.capture_btn.clicked.connect(self.capture_image)
main_layout.addWidget(self.capture_btn)

def capture_image(self):
pass

if __name__ == '__main__':
app = QApplication(sys.argv)
window = MainWindow()
window.setGeometry(100, 100, 800, 600)
window.setWindowTitle("3D Object Viewer")
window.show()
sys.exit(app.exec())


Подробнее здесь: https://stackoverflow.com/questions/791 ... o-an-image
Ответить

Быстрый ответ

Изменение регистра текста: 
Смайлики
:) :( :oops: :roll: :wink: :muza: :clever: :sorry: :angel: :read: *x)
Ещё смайлики…
   
К этому ответу прикреплено по крайней мере одно вложение.

Если вы не хотите добавлять вложения, оставьте поля пустыми.

Максимально разрешённый размер вложения: 15 МБ.

Вернуться в «Python»