diff --git a/install/scripts/test.py b/install/scripts/test.py index 4b04cff8ac..6fd4ce848f 100644 --- a/install/scripts/test.py +++ b/install/scripts/test.py @@ -265,3 +265,45 @@ def visit(self, selectionset): print('') +soundshader = GlobalSoundManager.getSoundShader('tdm_ai_lady_alertdown_to_idle') + +if not soundshader.isNull(): + print('Name of this sound shader: ' + soundshader.getName()) + + radii = soundshader.getRadii() + + print('Minimum radius in meters: ' + str(radii.getMin(1))) + print('Maximum radius in meters: ' + str(radii.getMax(1))) + + fileList = soundshader.getSoundFileList() + for i in range(0, len(fileList)): + print(' Sound file used by this shader: ' + fileList[i]) + + if (len(fileList) > 0): + GlobalSoundManager.playSound(fileList[0]) + +# Test SelectionGroup interface +group = GlobalSelectionGroupManager.createSelectionGroup() + +print('Created group with ID: ' + str(group.getId())) + +# Test traversing the current selection +class GroupAdder(SelectionVisitor) : + def visit(self, node): + group.addNode(node) + +visitor = Walker() +GlobalSelectionSystem.foreachSelected(visitor) + +print('The group contains now ' + str(group.size()) + ' items') + +# Deselect the group +GlobalSelectionGroupManager.setGroupSelected(group.getId(), 0) + +# List nodes in this group +class SelectionGroupWalker(SelectionGroupVisitor) : + def visit(self, node): + print('Group Member: ' + node.getNodeType()) + +gropWalker = SelectionGroupWalker(); +group.foreachNode(gropWalker) \ No newline at end of file diff --git a/plugins/script/Makefile.am b/plugins/script/Makefile.am index 0d20430917..ac6e2e757d 100644 --- a/plugins/script/Makefile.am +++ b/plugins/script/Makefile.am @@ -37,5 +37,6 @@ script_la_SOURCES = ScriptingSystem.cpp \ interfaces/ShaderSystemInterface.cpp \ interfaces/SkinInterface.cpp \ interfaces/SelectionSetInterface.cpp \ + interfaces/SelectionGroupInterface.cpp \ interfaces/SoundInterface.cpp \ interfaces/GameInterface.cpp diff --git a/plugins/script/ScriptingSystem.cpp b/plugins/script/ScriptingSystem.cpp index 1463943e6c..aa3fccf200 100644 --- a/plugins/script/ScriptingSystem.cpp +++ b/plugins/script/ScriptingSystem.cpp @@ -33,6 +33,7 @@ #include "interfaces/SoundInterface.h" #include "interfaces/DialogInterface.h" #include "interfaces/SelectionSetInterface.h" +#include "interfaces/SelectionGroupInterface.h" #include "ScriptWindow.h" #include "SceneNodeBuffer.h" @@ -519,6 +520,7 @@ void ScriptingSystem::initialiseModule(const ApplicationContext& ctx) addInterface("SoundManager", std::make_shared()); addInterface("DialogInterface", std::make_shared()); addInterface("SelectionSetInterface", std::make_shared()); + addInterface("SelectionGroupInterface", std::make_shared()); GlobalCommandSystem().addCommand( "RunScript", diff --git a/plugins/script/interfaces/SelectionGroupInterface.cpp b/plugins/script/interfaces/SelectionGroupInterface.cpp new file mode 100644 index 0000000000..da97f5a2a4 --- /dev/null +++ b/plugins/script/interfaces/SelectionGroupInterface.cpp @@ -0,0 +1,141 @@ +#include "SelectionGroupInterface.h" + +#include + +namespace script +{ + +ScriptSelectionGroup::ScriptSelectionGroup(const selection::ISelectionGroupPtr& group) : + _group(group) +{} + +std::size_t ScriptSelectionGroup::getId() +{ + return _group ? _group->getId() : 0; +} + +const std::string& ScriptSelectionGroup::getName() +{ + return _group ? _group->getName() : _emptyStr; +} + +void ScriptSelectionGroup::setName(const std::string& name) +{ + if (_group) + { + _group->setName(name); + } +} + +void ScriptSelectionGroup::addNode(const ScriptSceneNode& node) +{ + if (_group) + { + _group->addNode(node); + } +} + +void ScriptSelectionGroup::removeNode(const scene::INodePtr& node) +{ + if (_group) + { + _group->removeNode(node); + } +} + +std::size_t ScriptSelectionGroup::size() +{ + return _group ? _group->size() : 0; +} + +void ScriptSelectionGroup::setSelected(int selected) +{ + if (_group) + { + _group->setSelected(static_cast(selected)); + } +} + +void ScriptSelectionGroup::foreachNode(SelectionGroupVisitor& visitor) +{ + if (_group) + { + _group->foreachNode([&](const scene::INodePtr& node) + { + visitor.visit(node); + }); + } +} + +std::string ScriptSelectionGroup::_emptyStr; + +// ----------------------------------- + +ScriptSelectionGroup SelectionGroupInterface::createSelectionGroup() +{ + return ScriptSelectionGroup(GlobalSelectionGroupManager().createSelectionGroup()); +} + +ScriptSelectionGroup SelectionGroupInterface::getSelectionGroup(std::size_t id) +{ + return ScriptSelectionGroup(GlobalSelectionGroupManager().getSelectionGroup(id)); +} + +ScriptSelectionGroup SelectionGroupInterface::findOrCreateSelectionGroup(std::size_t id) +{ + return ScriptSelectionGroup(GlobalSelectionGroupManager().findOrCreateSelectionGroup(id)); +} + +void SelectionGroupInterface::setGroupSelected(std::size_t id, int selected) +{ + GlobalSelectionGroupManager().setGroupSelected(id, static_cast(selected)); +} + +void SelectionGroupInterface::deleteAllSelectionGroups() +{ + GlobalSelectionGroupManager().deleteAllSelectionGroups(); +} + +void SelectionGroupInterface::deleteSelectionGroup(std::size_t id) +{ + GlobalSelectionGroupManager().deleteSelectionGroup(id); +} + +// IScriptInterface implementation +void SelectionGroupInterface::registerInterface(py::module& scope, py::dict& globals) +{ + // Expose the SelectionGroupVisitor interface + py::class_ visitor(scope, "SelectionGroupVisitor"); + + visitor.def(py::init<>()); + visitor.def("visit", &SelectionGroupVisitor::visit); + + // Add SelectionGroup declaration + py::class_ selectionGroup(scope, "SelectionGroup"); + + selectionGroup.def(py::init()); + selectionGroup.def("getId", &ScriptSelectionGroup::getId); + selectionGroup.def("getName", &ScriptSelectionGroup::getName, py::return_value_policy::reference); + selectionGroup.def("setName", &ScriptSelectionGroup::setName); + selectionGroup.def("addNode", &ScriptSelectionGroup::addNode); + selectionGroup.def("removeNode", &ScriptSelectionGroup::removeNode); + selectionGroup.def("size", &ScriptSelectionGroup::size); + selectionGroup.def("setSelected", &ScriptSelectionGroup::setSelected); + selectionGroup.def("foreachNode", &ScriptSelectionGroup::foreachNode); + + // Add the module declaration to the given python namespace + py::class_ selectionGroupManager(scope, "SelectionGroupManager"); + + selectionGroupManager.def("createSelectionGroup", &SelectionGroupInterface::createSelectionGroup); + selectionGroupManager.def("getSelectionGroup", &SelectionGroupInterface::getSelectionGroup); + selectionGroupManager.def("findOrCreateSelectionGroup", &SelectionGroupInterface::findOrCreateSelectionGroup); + selectionGroupManager.def("setGroupSelected", &SelectionGroupInterface::setGroupSelected); + selectionGroupManager.def("deleteAllSelectionGroups", &SelectionGroupInterface::deleteAllSelectionGroups); + selectionGroupManager.def("deleteSelectionGroup", &SelectionGroupInterface::deleteSelectionGroup); + + // Now point the Python variable "GlobalSelectionGroupManager" to this instance + globals["GlobalSelectionGroupManager"] = this; +} + + +} diff --git a/plugins/script/interfaces/SelectionGroupInterface.h b/plugins/script/interfaces/SelectionGroupInterface.h new file mode 100644 index 0000000000..4df3ab8c81 --- /dev/null +++ b/plugins/script/interfaces/SelectionGroupInterface.h @@ -0,0 +1,73 @@ +#pragma once + +#include + +#include "iscript.h" +#include "iselectiongroup.h" + +#include "SceneGraphInterface.h" + +namespace script +{ + +// ========== SelectionGroup Handling ========== + +class SelectionGroupVisitor +{ +public: + virtual ~SelectionGroupVisitor() {} + virtual void visit(const scene::INodePtr& node) = 0; +}; + +// Wrap around the ISelectionSetManager::Visitor interface +class SelectionGroupVisitorWrapper : + public SelectionGroupVisitor +{ +public: + void visit(const scene::INodePtr& node) override + { + // Wrap this method to python + PYBIND11_OVERLOAD_PURE( + void, /* Return type */ + SelectionGroupVisitor, /* Parent class */ + visit, /* Name of function in C++ (must match Python name) */ + ScriptSceneNode(node) /* Argument(s) */ + ); + } +}; + +class ScriptSelectionGroup +{ +private: + selection::ISelectionGroupPtr _group; + + static std::string _emptyStr; +public: + ScriptSelectionGroup(const selection::ISelectionGroupPtr& group); + + std::size_t getId(); + const std::string& getName(); + void setName(const std::string& name); + void addNode(const ScriptSceneNode& node); + void removeNode(const scene::INodePtr& node); + std::size_t size(); + void setSelected(int selected); + void foreachNode(SelectionGroupVisitor& visitor); +}; + +class SelectionGroupInterface : + public IScriptInterface +{ +public: + ScriptSelectionGroup createSelectionGroup(); + ScriptSelectionGroup getSelectionGroup(std::size_t id); + ScriptSelectionGroup findOrCreateSelectionGroup(std::size_t id); + void setGroupSelected(std::size_t id, int selected); + void deleteAllSelectionGroups(); + void deleteSelectionGroup(std::size_t id); + + // IScriptInterface implementation + void registerInterface(py::module& scope, py::dict& globals) override; +}; + +} // namespace script diff --git a/tools/msvc/script.vcxproj b/tools/msvc/script.vcxproj index 782675389d..bc1aa61bdb 100644 --- a/tools/msvc/script.vcxproj +++ b/tools/msvc/script.vcxproj @@ -319,6 +319,7 @@ + @@ -349,6 +350,7 @@ + Create Create diff --git a/tools/msvc/script.vcxproj.filters b/tools/msvc/script.vcxproj.filters index 61551b2947..429ef81c8e 100644 --- a/tools/msvc/script.vcxproj.filters +++ b/tools/msvc/script.vcxproj.filters @@ -91,6 +91,9 @@ src + + src\interfaces + @@ -171,5 +174,8 @@ src\interfaces + + src\interfaces + \ No newline at end of file