PythonUtility.cpp 34 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721
  1. /*
  2. * Copyright (c) Contributors to the Open 3D Engine Project.
  3. * For complete copyright and license terms please see the LICENSE at the root of this distribution.
  4. *
  5. * SPDX-License-Identifier: Apache-2.0 OR MIT
  6. *
  7. */
  8. #include <Source/PythonUtility.h>
  9. #include <Source/PythonProxyObject.h>
  10. #include <Source/PythonTypeCasters.h>
  11. #include <Source/PythonMarshalComponent.h>
  12. #include <AzCore/RTTI/BehaviorContext.h>
  13. #include <AzCore/RTTI/TypeInfo.h>
  14. #include <AzCore/Serialization/SerializeContext.h>
  15. #include <AzFramework/StringFunc/StringFunc.h>
  16. #include <EditorPythonBindings/CustomTypeBindingBus.h>
  17. #include <pybind11/embed.h>
  18. namespace EditorPythonBindings
  19. {
  20. namespace Module
  21. {
  22. pybind11::module DeterminePackageModule(PackageMapType& modulePackageMap, AZStd::string_view moduleName, pybind11::module parentModule, pybind11::module fallbackModule, [[maybe_unused]] bool alertUsingFallback)
  23. {
  24. if (moduleName.empty() || !moduleName[0])
  25. {
  26. AZ_Warning("python", !alertUsingFallback, "Could not determine missing or empty module; using fallback module");
  27. return fallbackModule;
  28. }
  29. else if (parentModule.is_none())
  30. {
  31. AZ_Warning("python", !alertUsingFallback, "Could not determine using None parent module; using fallback module");
  32. return fallbackModule;
  33. }
  34. AZStd::string parentModuleName(PyModule_GetName(parentModule.ptr()));
  35. modulePackageMap[parentModuleName] = parentModule;
  36. pybind11::module currentModule = parentModule;
  37. AZStd::string fullModuleName(parentModuleName);
  38. fullModuleName.append(".");
  39. fullModuleName.append(moduleName);
  40. AZStd::vector<AZStd::string> moduleParts;
  41. AzFramework::StringFunc::Tokenize(fullModuleName.c_str(), moduleParts, ".", false, false);
  42. for (int modulePartsIndex = 0; modulePartsIndex < moduleParts.size(); ++modulePartsIndex)
  43. {
  44. AZStd::string currentModulePath;
  45. AzFramework::StringFunc::Join(currentModulePath, moduleParts.begin(), moduleParts.begin() + modulePartsIndex + 1, ".");
  46. auto itPackageEntry = modulePackageMap.find(currentModulePath.c_str());
  47. if (itPackageEntry != modulePackageMap.end())
  48. {
  49. currentModule = itPackageEntry->second;
  50. }
  51. else
  52. {
  53. PyObject* newModule = PyImport_AddModule(currentModulePath.c_str());
  54. if (!newModule)
  55. {
  56. AZ_Warning("python", false, "Could not add module named %s; using fallback module", currentModulePath.c_str());
  57. return fallbackModule;
  58. }
  59. else
  60. {
  61. auto newSubModule = pybind11::reinterpret_borrow<pybind11::module>(newModule);
  62. modulePackageMap[currentModulePath] = newSubModule;
  63. const char* subModuleName = moduleParts[modulePartsIndex].c_str();
  64. currentModule.attr(subModuleName) = newSubModule;
  65. currentModule = newSubModule;
  66. }
  67. }
  68. }
  69. return currentModule;
  70. }
  71. }
  72. namespace Internal
  73. {
  74. void LogSerializeTypeInfo(const AZ::TypeId& typeId)
  75. {
  76. AZStd::string info = AZStd::string::format("Serialize class info for typeId %s (", typeId.ToString<AZStd::string>().c_str());
  77. AZ::SerializeContext* serializeContext{ nullptr };
  78. AZ::ComponentApplicationBus::BroadcastResult(serializeContext, &AZ::ComponentApplicationRequests::GetSerializeContext);
  79. if (serializeContext)
  80. {
  81. auto&& classInfo = serializeContext->FindClassData(typeId);
  82. if (classInfo)
  83. {
  84. info = AZStd::string::format("name:%s version:%d isContainer:%s",
  85. classInfo->m_name, classInfo->m_version, classInfo->m_container ? "true" : "false");
  86. }
  87. auto&& genericClassInfo = serializeContext->FindGenericClassInfo(typeId);
  88. if (genericClassInfo)
  89. {
  90. info += " generic:true";
  91. info += AZStd::string::format(" specialized typeId: %s",
  92. genericClassInfo->GetSpecializedTypeId().ToString<AZStd::string>().c_str());
  93. info += AZStd::string::format(" generic typeId: %s",
  94. genericClassInfo->GetGenericTypeId().ToString<AZStd::string>().c_str());
  95. size_t numTemplatedArguments = genericClassInfo->GetNumTemplatedArguments();
  96. info += AZStd::string::format(" template arguments %zu", genericClassInfo->GetNumTemplatedArguments());
  97. for (size_t index = 0; index < numTemplatedArguments; ++index)
  98. {
  99. info += AZStd::string::format(" [%zu] template type: %s",
  100. index,
  101. genericClassInfo->GetTemplatedTypeId(index).ToString<AZStd::string>().c_str());
  102. }
  103. }
  104. }
  105. info += ")";
  106. AZ_Warning("python", false, "Serialize generic class info %s", info.c_str());
  107. }
  108. AZStd::optional<AZ::TypeId> IsEnumClass(const AZ::BehaviorParameter& behaviorParameter)
  109. {
  110. if (behaviorParameter.m_azRtti)
  111. {
  112. // If the underlying type of the supplied type is different, then T is an enum
  113. AZ::TypeId underlyingTypeId = AZ::Internal::GetUnderlyingTypeId(*behaviorParameter.m_azRtti);
  114. if (underlyingTypeId != behaviorParameter.m_typeId)
  115. {
  116. return AZStd::make_optional(underlyingTypeId);
  117. }
  118. }
  119. return AZStd::nullopt;
  120. }
  121. template <typename T>
  122. bool ConvertPythonFromEnumClass(const AZ::TypeId& underlyingTypeId, AZ::BehaviorArgument& behaviorValue, AZ::s64& outboundPythonValue)
  123. {
  124. if (underlyingTypeId == AZ::AzTypeInfo<T>::Uuid())
  125. {
  126. outboundPythonValue = aznumeric_cast<AZ::s64>(*behaviorValue.GetAsUnsafe<T>());
  127. return true;
  128. }
  129. return false;
  130. }
  131. AZStd::optional<pybind11::object> ConvertFromEnumClass(AZ::BehaviorArgument& behaviorValue)
  132. {
  133. if (!behaviorValue.m_azRtti)
  134. {
  135. return AZStd::nullopt;
  136. }
  137. AZ::TypeId underlyingTypeId = AZ::Internal::GetUnderlyingTypeId(*behaviorValue.m_azRtti);
  138. if (underlyingTypeId != behaviorValue.m_typeId)
  139. {
  140. AZ::s64 outboundPythonValue = 0;
  141. bool converted =
  142. ConvertPythonFromEnumClass<AZ::u8>(underlyingTypeId, behaviorValue, outboundPythonValue) ||
  143. ConvertPythonFromEnumClass<AZ::u16>(underlyingTypeId, behaviorValue, outboundPythonValue) ||
  144. ConvertPythonFromEnumClass<AZ::u32>(underlyingTypeId, behaviorValue, outboundPythonValue) ||
  145. ConvertPythonFromEnumClass<AZ::u64>(underlyingTypeId, behaviorValue, outboundPythonValue) ||
  146. ConvertPythonFromEnumClass<AZ::s8>(underlyingTypeId, behaviorValue, outboundPythonValue) ||
  147. ConvertPythonFromEnumClass<AZ::s16>(underlyingTypeId, behaviorValue, outboundPythonValue) ||
  148. ConvertPythonFromEnumClass<AZ::s32>(underlyingTypeId, behaviorValue, outboundPythonValue) ||
  149. ConvertPythonFromEnumClass<AZ::s64>(underlyingTypeId, behaviorValue, outboundPythonValue);
  150. AZ_Error("python", converted, "Enumeration backed by a non-numeric integer type.");
  151. return converted ? AZStd::make_optional(pybind11::cast<AZ::s64>(AZStd::move(outboundPythonValue))) : AZStd::nullopt;
  152. }
  153. return AZStd::nullopt;
  154. }
  155. template <typename T>
  156. bool ConvertBehaviorParameterEnum(pybind11::object obj, const AZ::TypeId& underlyingTypeId, AZ::BehaviorArgument& parameter)
  157. {
  158. if (underlyingTypeId == AZ::AzTypeInfo<T>::Uuid())
  159. {
  160. void* value = parameter.m_tempData.allocate(sizeof(T), AZStd::alignment_of<T>::value, 0);
  161. *reinterpret_cast<T*>(value) = pybind11::cast<T>(obj);
  162. if (parameter.m_traits & AZ::BehaviorParameter::TR_POINTER)
  163. {
  164. *reinterpret_cast<void**>(parameter.m_value) = reinterpret_cast<T*>(&value);
  165. }
  166. else
  167. {
  168. parameter.m_value = value;
  169. }
  170. return true;
  171. }
  172. return false;
  173. }
  174. bool ConvertEnumClassFromPython(pybind11::object obj, const AZ::BehaviorParameter& behaviorArgument, AZ::BehaviorArgument& parameter)
  175. {
  176. if (behaviorArgument.m_azRtti)
  177. {
  178. // If the underlying type of the supplied type is different, then T is an enum
  179. const AZ::TypeId underlyingTypeId = AZ::Internal::GetUnderlyingTypeId(*behaviorArgument.m_azRtti);
  180. if (underlyingTypeId != behaviorArgument.m_typeId)
  181. {
  182. parameter.m_name = behaviorArgument.m_name;
  183. parameter.m_azRtti = behaviorArgument.m_azRtti;
  184. parameter.m_traits = behaviorArgument.m_traits;
  185. parameter.m_typeId = behaviorArgument.m_typeId;
  186. bool handled =
  187. ConvertBehaviorParameterEnum<AZ::u8>(obj, underlyingTypeId, parameter) ||
  188. ConvertBehaviorParameterEnum<AZ::u16>(obj, underlyingTypeId, parameter) ||
  189. ConvertBehaviorParameterEnum<AZ::u32>(obj, underlyingTypeId, parameter) ||
  190. ConvertBehaviorParameterEnum<AZ::u64>(obj, underlyingTypeId, parameter) ||
  191. ConvertBehaviorParameterEnum<AZ::s8>(obj, underlyingTypeId, parameter) ||
  192. ConvertBehaviorParameterEnum<AZ::s16>(obj, underlyingTypeId, parameter) ||
  193. ConvertBehaviorParameterEnum<AZ::s32>(obj, underlyingTypeId, parameter) ||
  194. ConvertBehaviorParameterEnum<AZ::s64>(obj, underlyingTypeId, parameter) ;
  195. AZ_Error("python", handled, "Enumeration backed by a non-numeric integer type.");
  196. return handled;
  197. }
  198. }
  199. return false;
  200. }
  201. // type checks
  202. bool IsPrimitiveType(const AZ::TypeId& typeId)
  203. {
  204. return (typeId == AZ::AzTypeInfo<bool>::Uuid() ||
  205. typeId == AZ::AzTypeInfo<char>::Uuid() ||
  206. typeId == AZ::AzTypeInfo<float>::Uuid() ||
  207. typeId == AZ::AzTypeInfo<double>::Uuid() ||
  208. typeId == AZ::AzTypeInfo<AZ::s8>::Uuid() ||
  209. typeId == AZ::AzTypeInfo<AZ::u8>::Uuid() ||
  210. typeId == AZ::AzTypeInfo<AZ::s16>::Uuid() ||
  211. typeId == AZ::AzTypeInfo<AZ::u16>::Uuid() ||
  212. typeId == AZ::AzTypeInfo<AZ::s32>::Uuid() ||
  213. typeId == AZ::AzTypeInfo<AZ::u32>::Uuid() ||
  214. typeId == AZ::AzTypeInfo<AZ::s64>::Uuid() ||
  215. typeId == AZ::AzTypeInfo<AZ::u64>::Uuid() );
  216. }
  217. bool IsPointerType(const AZ::u32 traits)
  218. {
  219. return (((traits & AZ::BehaviorParameter::TR_POINTER) == AZ::BehaviorParameter::TR_POINTER) ||
  220. ((traits & AZ::BehaviorParameter::TR_REFERENCE) == AZ::BehaviorParameter::TR_REFERENCE));
  221. }
  222. // allocation patterns
  223. void StoreVariableCustomTypeDeleter(
  224. CustomTypeBindingNotifications::ValueHandle handle,
  225. AZ::TypeId typeId,
  226. Convert::StackVariableAllocator& stackVariableAllocator)
  227. {
  228. auto deallocateValue = [typeId = std::move(typeId), handle]() mutable
  229. {
  230. CustomTypeBindingNotificationBus::Event(
  231. typeId,
  232. &CustomTypeBindingNotificationBus::Events::CleanUpValue,
  233. handle);
  234. };
  235. stackVariableAllocator.StoreVariableDeleter(deallocateValue);
  236. }
  237. bool AllocateBehaviorObjectByClass(const AZ::BehaviorClass* behaviorClass, AZ::BehaviorObject& behaviorObject)
  238. {
  239. if (behaviorClass)
  240. {
  241. if (behaviorClass->m_defaultConstructor)
  242. {
  243. AZ::BehaviorObject newBehaviorObject = behaviorClass->Create();
  244. behaviorObject.m_typeId = newBehaviorObject.m_typeId;
  245. behaviorObject.m_address = newBehaviorObject.m_address;
  246. return true;
  247. }
  248. else
  249. {
  250. AZ_Warning("python", behaviorClass->m_defaultConstructor, "Missing default constructor for AZ::BehaviorClass for typeId:%s", behaviorClass->m_name.c_str());
  251. }
  252. }
  253. return false;
  254. }
  255. bool AllocateBehaviorValueParameter(const AZ::BehaviorMethod* behaviorMethod, AZ::BehaviorArgument& result, Convert::StackVariableAllocator& stackVariableAllocator)
  256. {
  257. if (const AZ::BehaviorParameter* resultType = behaviorMethod->GetResult())
  258. {
  259. result.Set(*resultType);
  260. if (auto underlyingTypeId = Internal::IsEnumClass(result); underlyingTypeId)
  261. {
  262. result.m_typeId = underlyingTypeId.value();
  263. }
  264. if (resultType->m_traits & AZ::BehaviorParameter::TR_POINTER)
  265. {
  266. result.m_value = result.m_tempData.allocate(sizeof(intptr_t), alignof(intptr_t));
  267. return true;
  268. }
  269. if (resultType->m_traits & AZ::BehaviorParameter::TR_REFERENCE)
  270. {
  271. return true;
  272. }
  273. if (IsPrimitiveType(resultType->m_typeId))
  274. {
  275. result.m_value = result.m_tempData.allocate(sizeof(intptr_t), alignof(intptr_t));
  276. return true;
  277. }
  278. AZ::BehaviorContext* behaviorContext(nullptr);
  279. AZ::ComponentApplicationBus::BroadcastResult(behaviorContext, &AZ::ComponentApplicationRequests::GetBehaviorContext);
  280. if (!behaviorContext)
  281. {
  282. AZ_Assert(false, "A behavior context is required!");
  283. return false;
  284. }
  285. const AZ::BehaviorClass* behaviorClass = AZ::BehaviorContextHelper::GetClass(behaviorContext, resultType->m_typeId);
  286. if (behaviorClass)
  287. {
  288. AZ::BehaviorObject behaviorObject;
  289. if (AllocateBehaviorObjectByClass(behaviorClass, behaviorObject))
  290. {
  291. result.m_value = behaviorObject.m_address;
  292. result.m_typeId = resultType->m_typeId;
  293. return true;
  294. }
  295. }
  296. else
  297. {
  298. CustomTypeBindingNotifications::AllocationHandle allocationHandleResult;
  299. CustomTypeBindingNotificationBus::EventResult(
  300. allocationHandleResult,
  301. result.m_typeId,
  302. &CustomTypeBindingNotificationBus::Events::AllocateDefault);
  303. if (allocationHandleResult)
  304. {
  305. CustomTypeBindingNotifications::ValueHandle handle = allocationHandleResult.value().first;
  306. const AZ::BehaviorObject& behaviorObject = allocationHandleResult.value().second;
  307. StoreVariableCustomTypeDeleter(handle, behaviorObject.m_typeId, stackVariableAllocator);
  308. result.m_value = behaviorObject.m_address;
  309. result.m_typeId = behaviorObject.m_typeId;
  310. return true;
  311. }
  312. // So far no allocation scheme has been found for this typeId, but the SerializeContext might have more information
  313. // so this code tries to pull out more type information about the typeId so that the user can get more human readable
  314. // information than a UUID
  315. LogSerializeTypeInfo(resultType->m_typeId);
  316. AZ_Error("python", behaviorClass, "A behavior class is missing for %s!",
  317. resultType->m_typeId.ToString<AZStd::string>().c_str());
  318. }
  319. }
  320. return false;
  321. }
  322. void DeallocateBehaviorValueParameter(AZ::BehaviorArgument& valueParameter)
  323. {
  324. if (IsPointerType(valueParameter.m_traits) || IsPrimitiveType(valueParameter.m_typeId))
  325. {
  326. // no constructor was used
  327. return;
  328. }
  329. AZ::BehaviorContext* behaviorContext(nullptr);
  330. AZ::ComponentApplicationBus::BroadcastResult(behaviorContext, &AZ::ComponentApplicationRequests::GetBehaviorContext);
  331. if (!behaviorContext)
  332. {
  333. AZ_Assert(false, "A behavior context is required!");
  334. }
  335. const AZ::BehaviorClass* behaviorClass = AZ::BehaviorContextHelper::GetClass(behaviorContext, valueParameter.m_typeId);
  336. if (behaviorClass)
  337. {
  338. AZ::BehaviorObject behaviorObject;
  339. behaviorObject.m_address = valueParameter.m_value;
  340. behaviorObject.m_typeId = valueParameter.m_typeId;
  341. behaviorClass->Destroy(behaviorObject);
  342. }
  343. }
  344. }
  345. namespace Convert
  346. {
  347. // StackVariableAllocator
  348. StackVariableAllocator::~StackVariableAllocator()
  349. {
  350. for (auto& cleanUp : m_cleanUpItems)
  351. {
  352. cleanUp();
  353. }
  354. }
  355. void StackVariableAllocator::StoreVariableDeleter(VariableDeleter&& deleter)
  356. {
  357. m_cleanUpItems.emplace_back(deleter);
  358. }
  359. // from Python to BehaviorArgument
  360. bool PythonProxyObjectToBehaviorValueParameter(const AZ::BehaviorParameter& behaviorArgument, pybind11::object pyObj, AZ::BehaviorArgument& parameter)
  361. {
  362. auto behaviorObject = pybind11::cast<EditorPythonBindings::PythonProxyObject*>(pyObj)->GetBehaviorObject();
  363. if (behaviorObject)
  364. {
  365. const AZ::BehaviorClass* behaviorClass = AZ::BehaviorContextHelper::GetClass(behaviorObject.value()->m_typeId);
  366. if (!behaviorClass)
  367. {
  368. AZ_Warning("python", false, "Missing BehaviorClass for typeId %s", behaviorObject.value()->m_typeId.ToString<AZStd::string>().c_str());
  369. return false;
  370. }
  371. if (behaviorClass->m_azRtti)
  372. {
  373. // is exact type or can be down casted?
  374. if (!behaviorClass->m_azRtti->IsTypeOf(behaviorArgument.m_typeId))
  375. {
  376. return false;
  377. }
  378. }
  379. else if (behaviorObject.value()->m_typeId != behaviorArgument.m_typeId)
  380. {
  381. // type mismatch detected
  382. return false;
  383. }
  384. if ((behaviorArgument.m_traits & AZ::BehaviorParameter::TR_POINTER) == AZ::BehaviorParameter::TR_POINTER)
  385. {
  386. parameter.m_value = &behaviorObject.value()->m_address;
  387. }
  388. else
  389. {
  390. parameter.m_value = behaviorObject.value()->m_address;
  391. }
  392. parameter.m_typeId = behaviorClass->m_typeId;
  393. parameter.m_azRtti = behaviorClass->m_azRtti;
  394. parameter.m_traits = behaviorArgument.m_traits;
  395. parameter.m_name = behaviorArgument.m_name;
  396. return true;
  397. }
  398. return false;
  399. }
  400. bool CustomPythonToBehavior(
  401. const AZ::BehaviorParameter& behaviorArgument,
  402. pybind11::object pyObj,
  403. AZ::BehaviorArgument& outBehavior,
  404. StackVariableAllocator& stackVariableAllocator)
  405. {
  406. AZStd::optional<CustomTypeBindingNotifications::ValueHandle> handle;
  407. CustomTypeBindingNotificationBus::EventResult(
  408. handle,
  409. behaviorArgument.m_typeId,
  410. &CustomTypeBindingNotificationBus::Events::PythonToBehavior,
  411. pyObj.ptr(),
  412. static_cast<AZ::BehaviorParameter::Traits>(behaviorArgument.m_traits),
  413. outBehavior);
  414. if (handle)
  415. {
  416. Internal::StoreVariableCustomTypeDeleter(handle.value(), behaviorArgument.m_typeId, stackVariableAllocator);
  417. outBehavior.m_typeId = behaviorArgument.m_typeId;
  418. outBehavior.m_traits = behaviorArgument.m_traits;
  419. outBehavior.m_name = behaviorArgument.m_name;
  420. outBehavior.m_azRtti = behaviorArgument.m_azRtti;
  421. return true;
  422. }
  423. return false;
  424. }
  425. bool PythonToBehaviorValueParameter(const AZ::BehaviorParameter& behaviorArgument, pybind11::object pyObj, AZ::BehaviorArgument& parameter, Convert::StackVariableAllocator& stackVariableAllocator)
  426. {
  427. AZStd::optional<PythonMarshalTypeRequests::BehaviorValueResult> result;
  428. PythonMarshalTypeRequests::BehaviorTraits traits = static_cast<PythonMarshalTypeRequests::BehaviorTraits>(behaviorArgument.m_traits);
  429. PythonMarshalTypeRequestBus::EventResult(result, behaviorArgument.m_typeId, &PythonMarshalTypeRequestBus::Events::PythonToBehaviorValueParameter, traits, pyObj, parameter);
  430. if (result && result.value().first)
  431. {
  432. auto deleter = AZStd::move(result.value().second);
  433. if (deleter)
  434. {
  435. stackVariableAllocator.StoreVariableDeleter(AZStd::move(deleter));
  436. }
  437. parameter.m_typeId = behaviorArgument.m_typeId;
  438. parameter.m_traits = behaviorArgument.m_traits;
  439. parameter.m_name = behaviorArgument.m_name;
  440. parameter.m_azRtti = behaviorArgument.m_azRtti;
  441. return true;
  442. }
  443. else if (auto underlyingTypeId = Internal::IsEnumClass(behaviorArgument); underlyingTypeId)
  444. {
  445. AZ::BehaviorParameter tempArg;
  446. tempArg.m_azRtti = behaviorArgument.m_azRtti;
  447. tempArg.m_traits = behaviorArgument.m_traits;
  448. tempArg.m_name = behaviorArgument.m_name;
  449. tempArg.m_typeId = underlyingTypeId.value();
  450. if (PythonToBehaviorValueParameter(tempArg, pyObj, parameter, stackVariableAllocator))
  451. {
  452. parameter.m_typeId = behaviorArgument.m_typeId;
  453. return true;
  454. }
  455. }
  456. else if (pybind11::isinstance<EditorPythonBindings::PythonProxyObject>(pyObj))
  457. {
  458. return PythonProxyObjectToBehaviorValueParameter(behaviorArgument, pyObj, parameter);
  459. }
  460. else if (CustomPythonToBehavior(behaviorArgument, pyObj, parameter, stackVariableAllocator))
  461. {
  462. return true;
  463. }
  464. return false;
  465. }
  466. // from BehaviorArgument to Python
  467. AZStd::optional<pybind11::object> CustomBehaviorToPython(AZ::BehaviorArgument& behaviorValue, Convert::StackVariableAllocator& stackVariableAllocator)
  468. {
  469. AZStd::optional<CustomTypeBindingNotifications::ValueHandle> handle;
  470. PyObject* outPyObj = nullptr;
  471. CustomTypeBindingNotificationBus::EventResult(
  472. handle,
  473. behaviorValue.m_typeId,
  474. &CustomTypeBindingNotificationBus::Events::BehaviorToPython,
  475. behaviorValue,
  476. outPyObj);
  477. if (outPyObj != nullptr && handle)
  478. {
  479. Internal::StoreVariableCustomTypeDeleter(handle.value(), behaviorValue.m_typeId, stackVariableAllocator);
  480. return { pybind11::reinterpret_borrow<pybind11::object>(outPyObj) };
  481. }
  482. return AZStd::nullopt;
  483. }
  484. pybind11::object BehaviorValueParameterToPython(AZ::BehaviorArgument& behaviorValue, Convert::StackVariableAllocator& stackVariableAllocator)
  485. {
  486. auto pyValue = Internal::ConvertFromEnumClass(behaviorValue);
  487. if (pyValue.has_value())
  488. {
  489. return pyValue.value();
  490. }
  491. AZStd::optional<PythonMarshalTypeRequests::PythonValueResult> result;
  492. PythonMarshalTypeRequestBus::EventResult(result, behaviorValue.m_typeId, &PythonMarshalTypeRequestBus::Events::BehaviorValueParameterToPython, behaviorValue);
  493. if (result.has_value())
  494. {
  495. auto deleter = AZStd::move(result.value().second);
  496. if (deleter)
  497. {
  498. stackVariableAllocator.StoreVariableDeleter(AZStd::move(deleter));
  499. }
  500. return result.value().first;
  501. }
  502. else if (auto customResult = CustomBehaviorToPython(behaviorValue, stackVariableAllocator); customResult)
  503. {
  504. return customResult.value();
  505. }
  506. else if (behaviorValue.m_typeId != AZ::Uuid::CreateNull() && behaviorValue.GetValueAddress())
  507. {
  508. return PythonProxyObjectManagement::CreatePythonProxyObject(behaviorValue.m_typeId, behaviorValue.GetValueAddress());
  509. }
  510. AZ_Warning("python", false, "Cannot convert type %s",
  511. behaviorValue.m_name ? behaviorValue.m_name : behaviorValue.m_typeId.ToString<AZStd::string>().c_str());
  512. return pybind11::cast<pybind11::none>(Py_None);
  513. }
  514. AZStd::string GetPythonTypeName(pybind11::object pyObj)
  515. {
  516. if (pybind11::isinstance<PythonProxyObject>(pyObj))
  517. {
  518. return pybind11::cast<PythonProxyObject*>(pyObj)->GetWrappedTypeName();
  519. }
  520. return pybind11::cast<AZStd::string>(pybind11::str(pyObj.get_type()));
  521. }
  522. }
  523. namespace Call
  524. {
  525. constexpr size_t MaxBehaviorMethodArguments = 32;
  526. using BehaviorMethodArgumentArray = AZStd::array<AZ::BehaviorArgument, MaxBehaviorMethodArguments>;
  527. pybind11::object InvokeBehaviorMethodWithResult(AZ::BehaviorMethod* behaviorMethod, pybind11::args pythonInputArgs, AZ::BehaviorObject self, AZ::BehaviorArgument& result)
  528. {
  529. if (behaviorMethod->GetNumArguments() > MaxBehaviorMethodArguments || pythonInputArgs.size() > MaxBehaviorMethodArguments)
  530. {
  531. AZ_Error("python", false, "Too many arguments for class method; set:%zu max:%zu", behaviorMethod->GetMinNumberOfArguments(), MaxBehaviorMethodArguments);
  532. return pybind11::cast<pybind11::none>(Py_None);
  533. }
  534. Convert::StackVariableAllocator stackVariableAllocator;
  535. BehaviorMethodArgumentArray parameters;
  536. int parameterCount = 0;
  537. if (self.IsValid())
  538. {
  539. // record the "this" pointer's metadata like its RTTI so that it can be
  540. // down casted to a parent class type if needed to invoke a parent method
  541. AZ::BehaviorArgument theThisPointer;
  542. if (const AZ::BehaviorParameter* thisInfo = behaviorMethod->GetArgument(0))
  543. {
  544. // avoiding the "Special handling for the generic object holder." since it assumes
  545. // the BehaviorObject.m_value is a pointer; the reference version is already dereferenced
  546. if ((thisInfo->m_traits & AZ::BehaviorParameter::TR_POINTER) == AZ::BehaviorParameter::TR_POINTER)
  547. {
  548. theThisPointer.m_value = &self.m_address;
  549. }
  550. else
  551. {
  552. theThisPointer.m_value = self.m_address;
  553. }
  554. theThisPointer.Set(*thisInfo);
  555. parameters[0].Set(theThisPointer);
  556. ++parameterCount;
  557. }
  558. else
  559. {
  560. AZ_Warning("python", false, "Missing self info index 0 in class method %s", behaviorMethod->m_name.c_str());
  561. return pybind11::cast<pybind11::none>(Py_None);
  562. }
  563. }
  564. // prepare the parameters for the BehaviorMethod
  565. for (auto pythonArg : pythonInputArgs)
  566. {
  567. if (parameterCount < behaviorMethod->GetNumArguments())
  568. {
  569. auto currentPythonArg = pybind11::cast<pybind11::object>(pythonArg);
  570. const AZ::BehaviorParameter* behaviorArgument = behaviorMethod->GetArgument(parameterCount);
  571. if (!behaviorArgument)
  572. {
  573. AZ_Warning("python", false, "Missing argument at index %d in class method %s", parameterCount, behaviorMethod->m_name.c_str());
  574. return pybind11::cast<pybind11::none>(Py_None);
  575. }
  576. if (!Convert::PythonToBehaviorValueParameter(*behaviorArgument, currentPythonArg, parameters[parameterCount], stackVariableAllocator))
  577. {
  578. AZ_Warning("python", false, "BehaviorMethod %s: Parameter at [%d] index expects (%s:%s) for method but got type (%s)",
  579. behaviorMethod->m_name.c_str(), parameterCount,
  580. behaviorArgument->m_name, behaviorArgument->m_typeId.ToString<AZStd::string>().c_str(),
  581. Convert::GetPythonTypeName(currentPythonArg).c_str());
  582. return pybind11::cast<pybind11::none>(Py_None);
  583. }
  584. ++parameterCount;
  585. }
  586. }
  587. // did the Python script send the right amount of arguments?
  588. const auto totalPythonArgs = pythonInputArgs.size() + (self.IsValid() ? 1 : 0); // +1 for the 'this' coming in from a marshaled Python/BehaviorObject
  589. if (totalPythonArgs < behaviorMethod->GetMinNumberOfArguments())
  590. {
  591. AZ_Warning("python", false, "Method %s requires at least %zu parameters got %zu", behaviorMethod->m_name.c_str(), behaviorMethod->GetMinNumberOfArguments(), totalPythonArgs);
  592. return pybind11::cast<pybind11::none>(Py_None);
  593. }
  594. else if (totalPythonArgs > behaviorMethod->GetNumArguments())
  595. {
  596. AZ_Warning("python", false, "Method %s requires %zu parameters but it got more (%zu) - excess parameters will not be used.", behaviorMethod->m_name.c_str(), behaviorMethod->GetMinNumberOfArguments(), totalPythonArgs);
  597. }
  598. if (behaviorMethod->HasResult())
  599. {
  600. if (Internal::AllocateBehaviorValueParameter(behaviorMethod, result, stackVariableAllocator))
  601. {
  602. if (behaviorMethod->Call(parameters.begin(), static_cast<unsigned int>(totalPythonArgs), &result))
  603. {
  604. result.m_azRtti = behaviorMethod->GetResult()->m_azRtti;
  605. result.m_typeId = behaviorMethod->GetResult()->m_typeId;
  606. result.m_traits = behaviorMethod->GetResult()->m_traits;
  607. return Convert::BehaviorValueParameterToPython(result, stackVariableAllocator);
  608. }
  609. else
  610. {
  611. AZ_Warning("python", false, "Failed to call class method %s", behaviorMethod->m_name.c_str());
  612. }
  613. }
  614. else
  615. {
  616. AZ_Warning("python", false, "Failed to allocate return value for method %s", behaviorMethod->m_name.c_str());
  617. }
  618. }
  619. else if (!behaviorMethod->Call(parameters.begin(), static_cast<unsigned int>(totalPythonArgs)))
  620. {
  621. AZ_Warning("python", false, "Failed to invoke class method %s", behaviorMethod->m_name.c_str());
  622. }
  623. return pybind11::cast<pybind11::none>(Py_None);
  624. }
  625. pybind11::object InvokeBehaviorMethod(AZ::BehaviorMethod* behaviorMethod, pybind11::args pythonInputArgs, AZ::BehaviorObject self)
  626. {
  627. AZ::BehaviorArgument result;
  628. result.m_value = nullptr;
  629. pybind11::object pythonOutput = InvokeBehaviorMethodWithResult(behaviorMethod, pythonInputArgs, self, result);
  630. if (result.m_value)
  631. {
  632. Internal::DeallocateBehaviorValueParameter(result);
  633. }
  634. return pythonOutput;
  635. }
  636. pybind11::object StaticMethod(AZ::BehaviorMethod* behaviorMethod, pybind11::args pythonInputArgs)
  637. {
  638. return InvokeBehaviorMethod(behaviorMethod, pythonInputArgs, {});
  639. }
  640. pybind11::object ClassMethod(AZ::BehaviorMethod* behaviorMethod, AZ::BehaviorObject self, pybind11::args pythonInputArgs)
  641. {
  642. if (behaviorMethod->GetNumArguments() == 0)
  643. {
  644. AZ_Error("python", false, "A member level function should require at least one argument");
  645. }
  646. else if (!self.IsValid())
  647. {
  648. AZ_Error("python", false, "Method %s requires at valid self object to invoke", behaviorMethod->m_name.c_str());
  649. }
  650. else
  651. {
  652. return InvokeBehaviorMethod(behaviorMethod, pythonInputArgs, self);
  653. }
  654. return pybind11::cast<pybind11::none>(Py_None);
  655. }
  656. }
  657. }