如何解决 “Error 0x80070057: 参数不正确” 在 PySide6 + Qt3D 中?

huangapple go评论173阅读模式
英文:

How to resolve "Error 0x80070057: The parameter is incorrect" in PySide6 + Qt3D?

问题

  1. 当我尝试运行下面的代码时,它只是尝试绘制一条单独的线,我收到以下错误:

Qt3D.Renderer.RHI.Backend: 使用DirectX后端初始化RHI
无法创建输入布局:错误0x80070057:参数无效。
Qt3D.Renderer.RHI.Backend: 无法构建图形管线:创建失败

  1. 我能够无问题地运行[简单的Qt 3D示例][1],并且我可以修改它以从`.stl`文件加载对象。我还查看了一些在下面类似的方式中使用QGeometryRendererGitHub存储库,但我尝试的所有变体都导致相同的错误。
  2. 平台:Win 10 x64Python 3.11.4PySide6 6.4.2
  3. [1]: https://doc.qt.io/qtforpython-6/examples/example_3d_simple3d.html
  4. 这是一个能复现问题的最小示例:
  5. ```python
  6. # 以下代码保持不变
  1. <details>
  2. <summary>英文:</summary>
  3. When I try to run the code below, which just attempts to draw a single line, I get the following error:

Qt3D.Renderer.RHI.Backend: Initializing RHI with DirectX backend
Failed to create input layout: Error 0x80070057: The parameter is incorrect.
Qt3D.Renderer.RHI.Backend: Failed to build graphics pipeline: Creation Failed

  1. I am able to run the [Simple Qt 3D Example][1] with no issues, and I can modify it load objects from `.stl` files. I&#39;ve also looked through a handful of github repositories that use QGeometryRenderer in a similar manner as below, but all the variations I&#39;ve tried result in the same error.
  2. Platform: Win 10 x64, Python 3.11.4, PySide6 6.4.2.
  3. [1]: https://doc.qt.io/qtforpython-6/examples/example_3d_simple3d.html
  4. Here&#39;s a minimal example that reproduces the issue:
  5. ```python
  6. import struct
  7. import sys
  8. from PySide6.QtCore import (QByteArray)
  9. from PySide6.QtGui import (QGuiApplication, QVector3D)
  10. from PySide6.Qt3DCore import (Qt3DCore)
  11. from PySide6.Qt3DExtras import (Qt3DExtras)
  12. from PySide6.Qt3DRender import (Qt3DRender)
  13. class Line(Qt3DCore.QEntity):
  14. def __init__(self, parent=None,
  15. start=QVector3D(0.0, 0.0, 0.0),
  16. end = QVector3D(10.0, 0.0, 0.0)):
  17. super().__init__(parent)
  18. self.start = start
  19. self.end = end
  20. self.geometry = Qt3DCore.QGeometry(self)
  21. # Create a vertex buffer to hold the vertex data
  22. points = QByteArray()
  23. points.append(struct.pack(&#39;f&#39;, start.x()))
  24. points.append(struct.pack(&#39;f&#39;, start.y()))
  25. points.append(struct.pack(&#39;f&#39;, start.z()))
  26. points.append(struct.pack(&#39;f&#39;, end.x()))
  27. points.append(struct.pack(&#39;f&#39;, end.y()))
  28. points.append(struct.pack(&#39;f&#39;, end.z()))
  29. self.vertexBuffer = Qt3DCore.QBuffer(self.geometry)
  30. self.vertexBuffer.setData(points)
  31. # Create an attribute to hold the vertex data
  32. attribute = Qt3DCore.QAttribute(self.geometry)
  33. attribute.setName(Qt3DCore.QAttribute.defaultPositionAttributeName())
  34. attribute.setVertexBaseType(Qt3DCore.QAttribute.VertexBaseType.Float)
  35. attribute.setVertexSize(3)
  36. attribute.setAttributeType(Qt3DCore.QAttribute.AttributeType.VertexAttribute)
  37. attribute.setBuffer(self.vertexBuffer)
  38. attribute.setByteOffset(0)
  39. attribute.setByteStride(3 * 4)
  40. attribute.setCount(2)
  41. self.geometry.addAttribute(attribute)
  42. # Create a renderer to render the line
  43. self.renderer = Qt3DRender.QGeometryRenderer()
  44. self.renderer.setPrimitiveType(Qt3DRender.QGeometryRenderer.Lines)
  45. self.renderer.setGeometry(self.geometry)
  46. self.addComponent(self.renderer)
  47. class Window(Qt3DExtras.Qt3DWindow):
  48. def __init__(self):
  49. super().__init__()
  50. self.camera().lens().setPerspectiveProjection(45, 16 / 9, 0.1, 1000)
  51. self.camera().setPosition(QVector3D(0, 0, 10))
  52. self.camera().setViewCenter(QVector3D(0, 0, 0))
  53. self.rootEntity = Qt3DCore.QEntity()
  54. self.material = Qt3DExtras.QPhongMaterial(self.rootEntity)
  55. self.line = Line(self.rootEntity)
  56. self.line.addComponent(self.material)
  57. self.camController = Qt3DExtras.QOrbitCameraController(self.rootEntity)
  58. self.camController.setLinearSpeed(50)
  59. self.camController.setLookSpeed(180)
  60. self.camController.setCamera(self.camera())
  61. self.setRootEntity(self.rootEntity)
  62. if __name__ == &#39;__main__&#39;:
  63. app = QGuiApplication(sys.argv)
  64. view = Window()
  65. view.show()
  66. sys.exit(app.exec())

答案1

得分: 0

根据relent95的评论建议,设置QT3D_RENDERER环境变量完全解决了这个问题。

在脚本顶部,我添加了:

  1. os.environ['QT3D_RENDERER'] = 'opengl'

然后脚本就可以无错误地运行了。我在这里找到了一些与此环境变量相关的文档:https://doc.qt.io/qtforpython-6/overviews/qt3drender-porting-to-rhi.html

英文:

As suggested in the comment by relent95 above, setting the QT3D_RENDERER environment variable solved this issue entirely for me.

At the top of the script, I added:

  1. os.environ[&#39;QT3D_RENDERER&#39;] = &#39;opengl&#39;

The script then ran with no errors. I found some documentation related to this environment variable here: https://doc.qt.io/qtforpython-6/overviews/qt3drender-porting-to-rhi.html

huangapple
  • 本文由 发表于 2023年6月15日 03:26:43
  • 转载请务必保留本文链接:https://go.coder-hub.com/76476952.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定