Skip to content

Commit

Permalink
#5629: Introduce a way for Python scripts to hit every selected face
Browse files Browse the repository at this point in the history
  • Loading branch information
codereader committed Jun 8, 2021
1 parent 7948dad commit 2dc9f1f
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 0 deletions.
8 changes: 8 additions & 0 deletions install/scripts/test.py
Expand Up @@ -91,6 +91,14 @@ def visit(self, node):
visitor = Walker()
GlobalSelectionSystem.foreachSelected(visitor)

# Visit every selected face
class FaceVisitor(dr.SelectedFaceVisitor) :
def visitFace(self, face):
print(face.getShader())

visitor = FaceVisitor()
GlobalSelectionSystem.foreachSelectedFace(visitor)

print('Map name is ' + GlobalMap.getMapName())

print(GlobalMap.getEditMode())
Expand Down
14 changes: 14 additions & 0 deletions plugins/script/interfaces/SelectionInterface.cpp
Expand Up @@ -18,6 +18,14 @@ void SelectionInterface::foreachSelectedComponent(const SelectionSystem::Visitor
GlobalSelectionSystem().foreachSelectedComponent(visitor);
}

void SelectionInterface::foreachSelectedFace(SelectedFaceVisitor& visitor)
{
GlobalSelectionSystem().foreachFace([&](IFace& face)
{
visitor.visitFace(face);
});
}

void SelectionInterface::setSelectedAll(int selected)
{
GlobalSelectionSystem().setSelectedAll(static_cast<bool>(selected));
Expand Down Expand Up @@ -55,12 +63,18 @@ void SelectionInterface::registerInterface(py::module& scope, py::dict& globals)
visitor.def(py::init<>());
visitor.def("visit", &SelectionSystem::Visitor::visit);

// Expose the SelectionFaceVisitor interface
py::class_<SelectedFaceVisitor, SelectedFaceVisitorWrapper> faceVisitor(scope, "SelectedFaceVisitor");
faceVisitor.def(py::init<>());
faceVisitor.def("visitFace", &SelectedFaceVisitor::visitFace);

// Add the module declaration to the given python namespace
py::class_<SelectionInterface> selSys(scope, "SelectionSystem");

selSys.def("getSelectionInfo", &SelectionInterface::getSelectionInfo, py::return_value_policy::reference);
selSys.def("foreachSelected", &SelectionInterface::foreachSelected);
selSys.def("foreachSelectedComponent", &SelectionInterface::foreachSelectedComponent);
selSys.def("foreachSelectedFace", &SelectionInterface::foreachSelectedFace);
selSys.def("setSelectedAll", &SelectionInterface::setSelectedAll);
selSys.def("setSelectedAllComponents", &SelectionInterface::setSelectedAllComponents);
selSys.def("ultimateSelected", &SelectionInterface::ultimateSelected);
Expand Down
27 changes: 27 additions & 0 deletions plugins/script/interfaces/SelectionInterface.h
Expand Up @@ -8,6 +8,7 @@
#include <map>

#include "SceneGraphInterface.h"
#include "BrushInterface.h"

namespace script
{
Expand All @@ -31,6 +32,31 @@ class SelectionVisitorWrapper :
}
};

// Special interface only used by Python scripts to visit selected faces
class SelectedFaceVisitor
{
public:
virtual ~SelectedFaceVisitor() {}

virtual void visitFace(IFace& face) = 0;
};

class SelectedFaceVisitorWrapper :
public SelectedFaceVisitor
{
public:
void visitFace(IFace& face) override
{
// Wrap this method to python
PYBIND11_OVERLOAD_PURE(
void, /* Return type */
SelectedFaceVisitor, /* Parent class */
visitFace, /* Name of function in C++ (must match Python name) */
ScriptFace(face) /* Argument(s) */
);
}
};

class SelectionInterface :
public IScriptInterface
{
Expand All @@ -40,6 +66,7 @@ class SelectionInterface :

void foreachSelected(const SelectionSystem::Visitor& visitor);
void foreachSelectedComponent(const SelectionSystem::Visitor& visitor);
void foreachSelectedFace(SelectedFaceVisitor& visitor);

void setSelectedAll(int selected);
void setSelectedAllComponents(int selected);
Expand Down

0 comments on commit 2dc9f1f

Please sign in to comment.