[ui] introduce Scene3DHelper object

Expose missing QEntity methods and helper functions to QML via this Python class.
This commit is contained in:
Yann Lanthony 2018-11-23 21:36:43 +01:00
parent f6365c5607
commit 2f50587904
5 changed files with 54 additions and 1 deletions

View file

@ -9,6 +9,7 @@ import meshroom
from meshroom.core import nodesDesc
from meshroom.ui import components
from meshroom.ui.components.filepath import FilepathHelper
from meshroom.ui.components.scene3D import Scene3DHelper
from meshroom.ui.palette import PaletteManager
from meshroom.ui.reconstruction import Reconstruction
from meshroom.ui.utils import QmlInstantEngine
@ -51,6 +52,8 @@ class MeshroomApp(QApplication):
self.engine.rootContext().setContextProperty("_PaletteManager", pm)
fpHelper = FilepathHelper(parent=self)
self.engine.rootContext().setContextProperty("Filepath", fpHelper)
scene3DHelper = Scene3DHelper(parent=self)
self.engine.rootContext().setContextProperty("Scene3DHelper", scene3DHelper)
self.engine.rootContext().setContextProperty("MeshroomApp", self)
# Request any potential computation to stop on exit
self.aboutToQuit.connect(r.stopExecution)

View file

@ -3,7 +3,8 @@ def registerTypes():
from PySide2.QtQml import qmlRegisterType
from meshroom.ui.components.edge import EdgeMouseArea
from meshroom.ui.components.filepath import FilepathHelper
from meshroom.ui.components.scene3D import Scene3DHelper
qmlRegisterType(EdgeMouseArea, "GraphEditor", 1, 0, "EdgeMouseArea")
qmlRegisterType(FilepathHelper, "Meshroom.Helpers", 1, 0, "FilepathHelper") # TODO: uncreatable
qmlRegisterType(Scene3DHelper, "Meshroom.Helpers", 1, 0, "Scene3DHelper") # TODO: uncreatable

View file

@ -0,0 +1,42 @@
from PySide2.QtCore import QObject, Slot
from PySide2.Qt3DCore import Qt3DCore
from PySide2.Qt3DRender import Qt3DRender
class Scene3DHelper(QObject):
@Slot(Qt3DCore.QEntity, str, result="QVariantList")
def findChildrenByProperty(self, entity, propertyName):
""" Recursively get all children of an entity that have a property named 'propertyName'. """
children = []
for child in entity.childNodes():
try:
if child.metaObject().indexOfProperty(propertyName) != -1:
children.append(child)
except RuntimeError:
continue
children += self.findChildrenByProperty(child, propertyName)
return children
@Slot(Qt3DCore.QEntity, Qt3DCore.QComponent)
def addComponent(self, entity, component):
""" Adds a component to an entity. """
entity.addComponent(component)
@Slot(Qt3DCore.QEntity, Qt3DCore.QComponent)
def removeComponent(self, entity, component):
""" Removes a component from an entity. """
entity.removeComponent(component)
@Slot(Qt3DCore.QEntity, result=int)
def vertexCount(self, entity):
""" Return vertex count based on children QGeometryRenderer 'vertexCount'."""
return sum([renderer.vertexCount() for renderer in entity.findChildren(Qt3DRender.QGeometryRenderer)])
@Slot(Qt3DCore.QEntity, result=int)
def faceCount(self, entity):
""" Returns face count based on children QGeometry buffers size."""
count = 0
for geo in entity.findChildren(Qt3DRender.QGeometry):
count += sum([attr.count() for attr in geo.attributes() if attr.name() == "vertexPosition"])
return count / 3

View file

@ -0,0 +1,6 @@
pragma Singleton
import Meshroom.Helpers 1.0
Scene3DHelper {
}

View file

@ -4,3 +4,4 @@ SortFilterDelegateModel 1.0 SortFilterDelegateModel.qml
Request 1.0 request.js
# causes random crash at application exit
# singleton Filepath 1.0 Filepath.qml
# singleton Scene3DHelper 1.0 Scene3DHelper.qml