NAP
pythonscriptcomponent.h
1 /* This Source Code Form is subject to the terms of the Mozilla Public
2  * License, v. 2.0. If a copy of the MPL was not distributed with this
3  * file, You can obtain one at https://mozilla.org/MPL/2.0/. */
4 
5 #pragma once
6 
7 // Pybind includes
8 #include <pybind11/pybind11.h>
9 #include <pybind11/stl.h>
10 
11 // Nap includes
12 #include <rtti/rtti.h>
13 #include <nap/signalslot.h>
14 #include <componentptr.h>
15 #include <nap/logger.h>
16 #include <instanceproperty.h>
17 #include <rtti/object.h>
18 #include <nap/resourceptr.h>
19 #include <entityptr.h>
20 #include <pythonscriptservice.h>
21 #include <pythonscript.h>
22 
23 
24 namespace nap
25 {
26 
27  // Forward declarations
28  class PythonScriptComponent;
29  class PythonScriptComponentInstance;
30 
31 
40  class NAPAPI PythonScriptComponent : public Component
41  {
42  RTTI_ENABLE(Component)
44 
46 
47  public:
48  bool init(utility::ErrorState& errorState) override;
49 
50  ResourcePtr<PythonScript> mPythonScript = nullptr;
51  std::string mClassName;
52  std::vector<std::string> mDependencies;
53 
54  // Inherited from Component
55  virtual void getDependentComponents(std::vector<rtti::TypeInfo>& components) const override;
56 
57  private:
58  pybind11::module mModule;
59  pybind11::object mPythonClass;
60  };
61 
62 
72  {
73  RTTI_ENABLE(ComponentInstance)
74  public:
77 
78  // Inherited from ComponentInstance
79  virtual void update(double deltaTime) override;
80  bool init(utility::ErrorState& errorState) override;
81 
92  template <typename ReturnType, typename ...Args>
93  bool get(const std::string& identifier, utility::ErrorState& errorState, ReturnType& returnValue, Args&&... args);
94 
104  template <typename ...Args>
105  bool call(const std::string& identifier, utility::ErrorState& errorState, Args&&... args);
106 
107  private:
108  PythonScriptComponent* mResource = nullptr;
109  pybind11::object mInstance;
110  bool mInitialized = false;
111  };
112 
113 
115  // Template Definitions
117 
118  template <typename ReturnType, typename ...Args>
119  bool PythonScriptComponentInstance::get(const std::string& identifier, utility::ErrorState& errorState, ReturnType& returnValue, Args&&... args)
120  {
121  try
122  {
123  returnValue = mInstance.attr(identifier.c_str())(args...).template cast<ReturnType>();
124  }
125  catch (const pybind11::error_already_set& err)
126  {
127  errorState.fail("Runtime python error while executing %s: %s", mResource->mPythonScript->mPath.c_str(), err.what());
128  return false;
129  }
130  return true;
131  }
132 
133 
134  template <typename ...Args>
135  bool PythonScriptComponentInstance::call(const std::string& identifier, utility::ErrorState& errorState, Args&&... args)
136  {
137  try
138  {
139  mInstance.attr(identifier.c_str())(std::forward<Args>(args)...);
140  }
141  catch (const pybind11::error_already_set& err)
142  {
143  errorState.fail("Runtime python error while executing %s: %s", mResource->mPythonScript->mPath.c_str(), err.what());
144  return false;
145  }
146  return true;
147  }
148 }
nap::rtti::ObjectPtr
Definition: objectptr.h:154
nap::utility::ErrorState
Definition: errorstate.h:19
nap::PythonScriptComponentInstance
Definition: pythonscriptcomponent.h:71
nap::PythonScriptComponent
Definition: pythonscriptcomponent.h:40
nap::utility::ErrorState::fail
void fail(T &&errorMessage)
Definition: errorstate.h:73
nap::EntityInstance
Definition: entity.h:34
nap::PythonScript
Definition: pythonscript.h:27
nap::PythonScriptComponentInstance::get
bool get(const std::string &identifier, utility::ErrorState &errorState, ReturnType &returnValue, Args &&... args)
Definition: pythonscriptcomponent.h:119
nap::ComponentInstance
Definition: component.h:43
nap::Component
Definition: component.h:151
nap
Definition: templateapp.h:17
nap::PythonScriptComponentInstance::call
bool call(const std::string &identifier, utility::ErrorState &errorState, Args &&... args)
Definition: pythonscriptcomponent.h:135
nap::rtti::TypeInfo
rttr::type TypeInfo
Definition: typeinfo.h:140