Files
o3de/Gems/EditorPythonBindings/Code/Source/PythonReflectionComponent.cpp
T
lumberyard-employee-dm f648cb1fd8 Update the FileIO Aliases (#4186)
* Update the FileIOAlias naming to make the cache, project root and engine
root paths more clear

The alias of `@root@`, `@assets@`, and `@projectplatformcache@` has been
collapsed to `@projectproductassets@`

The alias of `@devroot@` and `@engroot@` has been collapsed to
`@engroot@`

The alias of `@devassets@` and `@projectroot@` has been collapsed to
`@projectroot@`

Signed-off-by: lumberyard-employee-dm <56135373+lumberyard-employee-dm@users.noreply.github.com>

* Updated use of devassets and devroot properties in python

Those properties now use projectroot and engroot

Signed-off-by: lumberyard-employee-dm <56135373+lumberyard-employee-dm@users.noreply.github.com>

* Updating the alias @engroot@ alias path comment in each platform specific LocalFileIO_*.cpp file

Signed-off-by: lumberyard-employee-dm <56135373+lumberyard-employee-dm@users.noreply.github.com>

* Removed hardcoded size of 9 for the product asset alias.

The ResolvePath function now just appends the @projectproductassets@
alias with the input path

Signed-off-by: lumberyard-employee-dm <56135373+lumberyard-employee-dm@users.noreply.github.com>

* Remove duplicate @projectproductassets@ check in ProcessFileTreeRequest

Signed-off-by: lumberyard-employee-dm <56135373+lumberyard-employee-dm@users.noreply.github.com>

* Fix for typos in Hydra python test

Signed-off-by: lumberyard-employee-dm <56135373+lumberyard-employee-dm@users.noreply.github.com>

* Updated LocalFileIO::Copy call on Windows to use the Unicode aware CopyFileW API

Signed-off-by: lumberyard-employee-dm <56135373+lumberyard-employee-dm@users.noreply.github.com>

* Updated the AWSMetreicsGemAllocatorFixture to properly suppress asset
cache write errors for Test file creation.

Signed-off-by: lumberyard-employee-dm <56135373+lumberyard-employee-dm@users.noreply.github.com>

* Removed unneeded call to set the @projectproductasstes@ alias at the bottom of the AssetSeedManagerTest SetUp

Signed-off-by: lumberyard-employee-dm <56135373+lumberyard-employee-dm@users.noreply.github.com>

* Added a deprecated alias map to the FileIO System

When a deprecated alias is accessed, the FileIO System logs an AZ_Error and indicates the alias that should be used

Signed-off-by: lumberyard-employee-dm <56135373+lumberyard-employee-dm@users.noreply.github.com>

* Updated python test scripts to use the projectroot binding

Retrieving the AutomatedTesting project path based on "<devroot>/AutomatedTesting" has been removed.

Signed-off-by: lumberyard-employee-dm <56135373+lumberyard-employee-dm@users.noreply.github.com>

* Updated references to devroot and devgame within the codebase

The GetAbsoluteDevGameFolderPath functions has been replaced with direct call to AZ::Utils::GetProjectPath
The GetAbsoluteDevRootFolderPath functions has been replaced with direct calls to AZ::Utils::GetEnginePath

Signed-off-by: lumberyard-employee-dm <56135373+lumberyard-employee-dm@users.noreply.github.com>

* Updated <engroot>/AutomatedTesting references to projectroot


Signed-off-by: lumberyard-employee-dm <56135373+lumberyard-employee-dm@users.noreply.github.com>

* Replaced references that assumes the project path is <engroot>/AutomatedTesting with <projectroot> in the AutomatedTesting python test

Signed-off-by: lumberyard-employee-dm <56135373+lumberyard-employee-dm@users.noreply.github.com>

* Correct casing in emfxworkspace file


Signed-off-by: lumberyard-employee-dm <56135373+lumberyard-employee-dm@users.noreply.github.com>

* Removed newly added AppendPathParts function
Removed the Path constructors which accepts a PathIterable instance

The PathIterable isn't safe to return to a user of the Path class as it might be referencing temporary paths supplied via PathView arguments

Signed-off-by: lumberyard-employee-dm <56135373+lumberyard-employee-dm@users.noreply.github.com>

* Fixed unused parameter warning

Signed-off-by: lumberyard-employee-dm <56135373+lumberyard-employee-dm@users.noreply.github.com>

* Undid change to the LexicallyProximate function to set the path result to the base path.

It needs to return the *this path if the pathResult is empty

Signed-off-by: lumberyard-employee-dm <56135373+lumberyard-employee-dm@users.noreply.github.com>

* Moved the LocalFileIO ConvertToAbsolutePath implementations to AZ::Utils

Fixed the ConvertToAbsolutePath implementation for Unix platforms to use a buffer that is size PATH_MAX(4096 on all our supported Unix platforms).
Because the buffer before was AZ::IO::MaxPathLength which as a size of 1024, this was resulting in the Unix `realpath` function smashing the stack when it converts a path to an absolute path that is longer than 1024 characters

Signed-off-by: lumberyard-employee-dm <56135373+lumberyard-employee-dm@users.noreply.github.com>

* Updated the EditorCore.Tests to attach the AZ Environment to the EditorCore shared library that is statically loaded on launch.

Signed-off-by: lumberyard-employee-dm <56135373+lumberyard-employee-dm@users.noreply.github.com>

* Fixed for DeprecatedAlaisesKeyVisitor Visit function causing the non string_view overloads being hidden causing a hidden base class function warning

Signed-off-by: lumberyard-employee-dm <56135373+lumberyard-employee-dm@users.noreply.github.com>

* Changed the AWSMetricsGemMock to use a temporary for writing test assets

Signed-off-by: lumberyard-employee-dm <56135373+lumberyard-employee-dm@users.noreply.github.com>

* Updated the LocalFileIO::ResolvePath function to use HasRootPath to determine if a path starts with a root path such as "D:", "D:/" or "/"

IsAbsolute was not the corect check as the path of "D:" is a relative
path.
To be absolute according to the Windows the path must have a root
directory. i.e "D:/" or "D:\"

Signed-off-by: lumberyard-employee-dm <56135373+lumberyard-employee-dm@users.noreply.github.com>

* Removed absolute path comment from LocalFile_UnixLike.cpp and LocalFile_Android.cpp FindFiles implementations
Updated the ConvertToAlias to supply an AZ::IO::FixedMaxPath

Signed-off-by: lumberyard-employee-dm <56135373+lumberyard-employee-dm@users.noreply.github.com>

* Replaced usage of the @projectproductassets@ alias with @engroot@ when referring to the LmbrCentral source folder in the CopyDependencyBuilderTest and the SeedBuilderTests

Signed-off-by: lumberyard-employee-dm <56135373+lumberyard-employee-dm@users.noreply.github.com>

* Updated the ScriptCanvas Upgrade Tool to output backed up files to the
Project User Path instead of the engine root

Signed-off-by: lumberyard-employee-dm <56135373+lumberyard-employee-dm@users.noreply.github.com>

* Fixed whitespacing issues in Application.cpp

Signed-off-by: lumberyard-employee-dm <56135373+lumberyard-employee-dm@users.noreply.github.com>

* Remove unnecessary creation of a FixedMaxPath in the UpgradeTool.cpp

Signed-off-by: lumberyard-employee-dm <56135373+lumberyard-employee-dm@users.noreply.github.com>

* Modified testSeedFolder variable in the SeedBuilderTests to use the
@engroot@ alias instead of @projectproductassets@/.. alias when
referring to the LmbrCentral Gem source path

Signed-off-by: lumberyard-employee-dm <56135373+lumberyard-employee-dm@users.noreply.github.com>

* Updated references to the Project Asset Cache in the PythonTests.

Those tests no longer use the logic `azlmbr.paths.projectroot / "Cache" / "pc"` to retrieve a path to the cache root but instead the `azlmbr.paths.projectproductassets` constant

Signed-off-by: lumberyard-employee-dm <56135373+lumberyard-employee-dm@users.noreply.github.com>

* Fixed the FileIO Deprecated Alias test on Windows

Signed-off-by: lumberyard-employee-dm <56135373+lumberyard-employee-dm@users.noreply.github.com>

* Removing @projectsourceassets@ alias, as it is only used once.

Updated the PhysX EditorSystemComponent.cpp to query the ProjectPath
form the SettingsRegistry.

Signed-off-by: lumberyard-employee-dm <56135373+lumberyard-employee-dm@users.noreply.github.com>

* Replaced @projectproductassets@ alias with @products@

Signed-off-by: lumberyard-employee-dm <56135373+lumberyard-employee-dm@users.noreply.github.com>

* Rollback changes to the PhysX EditorSystemComponent.cpp

The changes to use the ProjectPath from the SettingsRegistry has been implemented in PR #4497

Signed-off-by: lumberyard-employee-dm <56135373+lumberyard-employee-dm@users.noreply.github.com>
2021-10-05 15:12:53 -05:00

383 lines
16 KiB
C++

/*
* Copyright (c) Contributors to the Open 3D Engine Project.
* For complete copyright and license terms please see the LICENSE at the root of this distribution.
*
* SPDX-License-Identifier: Apache-2.0 OR MIT
*
*/
#include <PythonReflectionComponent.h>
#include <AzFramework/StringFunc/StringFunc.h>
#include <Source/PythonCommon.h>
#include <Source/PythonUtility.h>
#include <Source/PythonTypeCasters.h>
#include <Source/PythonProxyBus.h>
#include <Source/PythonProxyObject.h>
#include <Source/PythonSymbolsBus.h>
#include <pybind11/embed.h>
#include <AzCore/PlatformDef.h>
#include <AzCore/RTTI/AttributeReader.h>
#include <AzCore/RTTI/BehaviorContext.h>
#include <AzCore/std/smart_ptr/make_shared.h>
#include <AzCore/Serialization/EditContextConstants.inl>
#include <AzCore/Serialization/SerializeContext.h>
#include <AzCore/PlatformDef.h>
#include <AzCore/IO/SystemFile.h>
#include <AzCore/IO/SystemFile.h>
#include <AzFramework/IO/LocalFileIO.h>
namespace EditorPythonBindings
{
namespace Internal
{
static constexpr const char* s_azlmbr = "azlmbr";
static constexpr const char* s_default = "default";
static constexpr const char* s_globals = "globals";
// a structure for pybind11 to bind to hold constants, properties, and enums from the Behavior Context
struct StaticPropertyHolder final
{
AZ_CLASS_ALLOCATOR(StaticPropertyHolder, AZ::SystemAllocator, 0);
StaticPropertyHolder() = default;
~StaticPropertyHolder() = default;
bool AddToScope(pybind11::module scope)
{
m_behaviorContext = nullptr;
AZ::ComponentApplicationBus::BroadcastResult(m_behaviorContext, &AZ::ComponentApplicationRequests::GetBehaviorContext);
AZ_Error("python", m_behaviorContext, "Behavior context not available");
if (m_behaviorContext == nullptr)
{
return false;
}
m_fullName = PyModule_GetName(scope.ptr());
pybind11::setattr(scope, "__getattr__", pybind11::cpp_function([this](const char* attribute)
{
return this->GetPropertyValue(attribute);
}));
pybind11::setattr(scope, "__setattr__", pybind11::cpp_function([this](const char* attribute, pybind11::object value)
{
return this->SetPropertyValue(attribute, value);
}));
return true;
}
void AddProperty(AZStd::string_view name, AZ::BehaviorProperty* behaviorProperty)
{
AZStd::string baseName(name);
Scope::FetchScriptName(behaviorProperty->m_attributes, baseName);
AZ::Crc32 namedKey(baseName);
if (m_properties.find(namedKey) == m_properties.end())
{
m_properties[namedKey] = behaviorProperty;
}
else
{
AZ_Warning("python", false, "Skipping duplicate property named %s\n", baseName.c_str());
}
}
protected:
void SetPropertyValue(const char* attributeName, pybind11::object value)
{
auto behaviorPropertyIter = m_properties.find(AZ::Crc32(attributeName));
if (behaviorPropertyIter != m_properties.end())
{
AZ::BehaviorProperty* property = behaviorPropertyIter->second;
AZ_Error("python", property->m_setter, "%s is not a writable property in %s.", attributeName, m_fullName.c_str());
if (property->m_setter)
{
EditorPythonBindings::Call::StaticMethod(property->m_setter, pybind11::args(pybind11::make_tuple(value)));
}
}
}
pybind11::object GetPropertyValue(const char* attributeName)
{
AZ::Crc32 crcAttributeName(attributeName);
auto behaviorPropertyIter = m_properties.find(crcAttributeName);
if (behaviorPropertyIter != m_properties.end())
{
AZ::BehaviorProperty* property = behaviorPropertyIter->second;
AZ_Error("python", property->m_getter, "%s is not a readable property in %s.", attributeName, m_fullName.c_str());
if (property->m_getter)
{
return EditorPythonBindings::Call::StaticMethod(property->m_getter, pybind11::args());
}
}
return pybind11::cast<pybind11::none>(Py_None);
}
AZ::BehaviorContext* m_behaviorContext = nullptr;
AZStd::unordered_map<AZ::Crc32, AZ::BehaviorProperty*> m_properties;
AZStd::string m_fullName;
};
using StaticPropertyHolderPointer = AZStd::unique_ptr<StaticPropertyHolder>;
using StaticPropertyHolderMapEntry = AZStd::pair<pybind11::module, StaticPropertyHolderPointer>;
struct StaticPropertyHolderMap final
: public AZStd::unordered_map<AZStd::string, StaticPropertyHolderMapEntry>
{
Module::PackageMapType m_packageMap;
void AddToScope()
{
for (auto&& element : *this)
{
StaticPropertyHolderMapEntry& entry = element.second;
entry.second->AddToScope(entry.first);
}
}
void AddProperty(pybind11::module scope, const AZStd::string& propertyName, AZ::BehaviorProperty* behaviorProperty)
{
AZStd::string scopeName = PyModule_GetName(scope.ptr());
auto&& iter = find(scopeName);
if (iter == end())
{
StaticPropertyHolder* holder = aznew StaticPropertyHolder();
insert(AZStd::make_pair(scopeName, StaticPropertyHolderMapEntry{ scope, holder }));
holder->AddProperty(propertyName, behaviorProperty);
}
else
{
StaticPropertyHolderMapEntry& entry = iter->second;
entry.second->AddProperty(propertyName, behaviorProperty);
}
PythonSymbolEventBus::QueueBroadcast(&PythonSymbolEventBus::Events::LogGlobalProperty, scopeName, propertyName, behaviorProperty);
}
pybind11::module DetermineScope(pybind11::module scope, const AZStd::string& fullName)
{
return Module::DeterminePackageModule(m_packageMap, fullName, scope, scope, false);
}
};
AZStd::string PyResolvePath(AZStd::string_view path)
{
char pyPath[AZ_MAX_PATH_LEN];
AZ::IO::FileIOBase::GetInstance()->ResolvePath(path.data(), pyPath, AZ_MAX_PATH_LEN);
return { pyPath };
}
void RegisterAliasIfExists(pybind11::module pathsModule, AZStd::string_view alias, AZStd::string_view attribute)
{
const char* aliasPath = AZ::IO::FileIOBase::GetInstance()->GetAlias(alias.data());
if (aliasPath)
{
pathsModule.attr(attribute.data()) = aliasPath;
}
else
{
pathsModule.attr(attribute.data()) = "";
}
}
void RegisterPaths(pybind11::module parentModule)
{
pybind11::module pathsModule = parentModule.def_submodule("paths");
pathsModule.def("resolve_path", [](const char* path)
{
return PyResolvePath(path);
});
pathsModule.def("ensure_alias", [](const char* alias, const char* path)
{
const char* aliasPath = AZ::IO::FileIOBase::GetInstance()->GetAlias(alias);
if (aliasPath == nullptr)
{
AZ::IO::FileIOBase::GetInstance()->SetAlias(alias, path);
}
});
RegisterAliasIfExists(pathsModule, "@engroot@", "engroot");
RegisterAliasIfExists(pathsModule, "@products@", "products");
RegisterAliasIfExists(pathsModule, "@projectroot@", "projectroot");
RegisterAliasIfExists(pathsModule, "@log@", "log");
const char* executableFolder = nullptr;
AZ::ComponentApplicationBus::BroadcastResult(executableFolder, &AZ::ComponentApplicationBus::Events::GetExecutableFolder);
if (executableFolder)
{
pathsModule.attr("executableFolder") = executableFolder;
}
}
}
//////////////////////////////////////////////////////////////////////////
// PythonReflectionComponent
void PythonReflectionComponent::Reflect(AZ::ReflectContext* context)
{
if (auto&& serialize = azrtti_cast<AZ::SerializeContext*>(context))
{
serialize->Class<PythonReflectionComponent, AZ::Component>()
->Version(1)
->Attribute(AZ::Edit::Attributes::SystemComponentTags, AZStd::vector<AZ::Crc32>{AZ_CRC_CE("AssetBuilder")})
;
}
}
void PythonReflectionComponent::GetProvidedServices(AZ::ComponentDescriptor::DependencyArrayType& provided)
{
provided.push_back(PythonReflectionService);
}
void PythonReflectionComponent::GetIncompatibleServices(AZ::ComponentDescriptor::DependencyArrayType& incompatible)
{
incompatible.push_back(PythonReflectionService);
}
void PythonReflectionComponent::GetRequiredServices(AZ::ComponentDescriptor::DependencyArrayType& required)
{
required.push_back(PythonEmbeddedService);
}
void PythonReflectionComponent::Activate()
{
EditorPythonBindings::EditorPythonBindingsNotificationBus::Handler::BusConnect();
}
void PythonReflectionComponent::Deactivate()
{
OnPreFinalize();
}
void PythonReflectionComponent::ExportGlobalsFromBehaviorContext(pybind11::module parentModule)
{
AZ::BehaviorContext* behaviorContext = nullptr;
AZ::ComponentApplicationBus::BroadcastResult(behaviorContext, &AZ::ComponentApplicationRequests::GetBehaviorContext);
AZ_Error("Editor", behaviorContext, "Behavior context not available");
if (!behaviorContext)
{
return;
}
// when a global method does not have a Module attribute put into the 'azlmbr.globals' module
auto globalsModule = parentModule.def_submodule(Internal::s_globals);
Module::PackageMapType modulePackageMap;
// add global methods flagged for Automation as Python global functions
for (const auto& methodEntry : behaviorContext->m_methods)
{
const AZStd::string& methodName = methodEntry.first;
AZ::BehaviorMethod* behaviorMethod = methodEntry.second;
if (Scope::IsBehaviorFlaggedForEditor(behaviorMethod->m_attributes))
{
pybind11::module targetModule;
auto moduleNameResult = Module::GetName(behaviorMethod->m_attributes);
if(moduleNameResult)
{
targetModule = Module::DeterminePackageModule(modulePackageMap, *moduleNameResult, parentModule, globalsModule, false);
}
else
{
targetModule = globalsModule;
}
if (behaviorMethod->HasResult())
{
targetModule.def(methodName.c_str(), [behaviorMethod](pybind11::args args)
{
return Call::StaticMethod(behaviorMethod, args);
});
}
else
{
targetModule.def(methodName.c_str(), [behaviorMethod](pybind11::args args)
{
Call::StaticMethod(behaviorMethod, args);
});
}
// log global method symbol
AZStd::string subModuleName = pybind11::cast<AZStd::string>(targetModule.attr("__name__"));
PythonSymbolEventBus::QueueBroadcast(&PythonSymbolEventBus::Events::LogGlobalMethod, subModuleName, methodName, behaviorMethod);
}
}
// add global properties flagged for Automation as Python static class properties
m_staticPropertyHolderMap = AZStd::make_shared<Internal::StaticPropertyHolderMap>();
struct GlobalPropertyHolder {};
pybind11::class_<GlobalPropertyHolder> staticPropertyHolder(globalsModule, "property");
for (const auto& propertyEntry : behaviorContext->m_properties)
{
const AZStd::string& propertyName = propertyEntry.first;
AZ::BehaviorProperty* behaviorProperty = propertyEntry.second;
if (Scope::IsBehaviorFlaggedForEditor(behaviorProperty->m_attributes))
{
auto propertyScopeName = Module::GetName(behaviorProperty->m_attributes);
if (propertyScopeName)
{
pybind11::module scope = m_staticPropertyHolderMap->DetermineScope(parentModule, *propertyScopeName);
m_staticPropertyHolderMap->AddProperty(scope, propertyName, behaviorProperty);
}
// log global property symbol
AZStd::string subModuleName = pybind11::cast<AZStd::string>(globalsModule.attr("__name__"));
PythonSymbolEventBus::QueueBroadcast(&PythonSymbolEventBus::Events::LogGlobalProperty, subModuleName, propertyName, behaviorProperty);
if (behaviorProperty->m_getter && behaviorProperty->m_setter)
{
staticPropertyHolder.def_property_static(
propertyName.c_str(),
[behaviorProperty](pybind11::object) { return Call::StaticMethod(behaviorProperty->m_getter, {}); },
[behaviorProperty](pybind11::object, pybind11::args args) { return Call::StaticMethod(behaviorProperty->m_setter, args); }
);
}
else if (behaviorProperty->m_getter)
{
staticPropertyHolder.def_property_static(
propertyName.c_str(),
[behaviorProperty](pybind11::object) { return Call::StaticMethod(behaviorProperty->m_getter, {}); },
pybind11::cpp_function()
);
}
else if (behaviorProperty->m_setter)
{
AZ_Warning("python", false, "Global property %s only has a m_setter; write only properties not supported", propertyName.c_str());
}
else
{
AZ_Error("python", false, "Global property %s has neither a m_getter or m_setter", propertyName.c_str());
}
}
}
m_staticPropertyHolderMap->AddToScope();
}
void PythonReflectionComponent::OnPreFinalize()
{
m_staticPropertyHolderMap.reset();
EditorPythonBindings::EditorPythonBindingsNotificationBus::Handler::BusDisconnect();
}
void PythonReflectionComponent::OnImportModule(PyObject* module)
{
pybind11::module parentModule = pybind11::cast<pybind11::module>(module);
std::string pythonModuleName = pybind11::cast<std::string>(parentModule.attr("__name__"));
if (AzFramework::StringFunc::Equal(pythonModuleName.c_str(), Internal::s_azlmbr))
{
// declare the default module to capture behavior that did not define a "Module" attribute
pybind11::module defaultModule = parentModule.def_submodule(Internal::s_default);
ExportGlobalsFromBehaviorContext(parentModule);
PythonProxyObjectManagement::CreateSubmodule(parentModule, defaultModule);
PythonProxyBusManagement::CreateSubmodule(parentModule);
Internal::RegisterPaths(parentModule);
PythonSymbolEventBus::QueueBroadcast(&PythonSymbolEventBus::Events::Finalize);
}
}
}