Skip to content

Commit

Permalink
#5585: Add a few methods to the MaterialManager interface to copy/cre…
Browse files Browse the repository at this point in the history
…ate/rename/remove materials
  • Loading branch information
codereader committed Apr 11, 2021
1 parent 111f74e commit 8f5456c
Show file tree
Hide file tree
Showing 3 changed files with 98 additions and 21 deletions.
28 changes: 28 additions & 0 deletions install/scripts/materialtest.py
@@ -0,0 +1,28 @@
# Some interface tests

print("bc_rat can be modified" if GlobalMaterialManager.materialCanBeModified('bc_rat') else "bc_rat cannot be modified")

bcRatCopy = GlobalMaterialManager.copyMaterial('bc_rat', 'bc_rat_copy')

print("Got a copy of bc_rat named {0}".format(bcRatCopy.getName()))
print("The copy can be modified" if GlobalMaterialManager.materialCanBeModified(bcRatCopy.getName()) else "The copy cannot be modified?")

print("Removing the copy again...")
GlobalMaterialManager.removeMaterial(bcRatCopy.getName())

print('Create an empty material named textures/python/blah...')
newMaterial = GlobalMaterialManager.createEmptyMaterial('textures/python/blah')

print("The new material can be modified" if GlobalMaterialManager.materialCanBeModified(newMaterial.getName()) else "The new material cannot be modified?")

renameResult = GlobalMaterialManager.renameMaterial(newMaterial.getName(), newMaterial.getName() + '_renamed')
print("The rename operation was successful" if renameResult else "The rename operation failed")

# Do something with the new material
print('The existing material reference now has the name {0}'.format(newMaterial.getName()))

print("Removing {0} again...".format(newMaterial.getName()))
GlobalMaterialManager.removeMaterial(newMaterial.getName())

print('--- Done ---')

74 changes: 58 additions & 16 deletions plugins/script/interfaces/ShaderSystemInterface.cpp
Expand Up @@ -35,30 +35,65 @@ void ShaderSystemInterface::foreachMaterial(MaterialVisitor& visitor)
std::bind(&ShaderNameToShaderWrapper::visit, &adaptor, std::placeholders::_1));
}

ScriptShader ShaderSystemInterface::getMaterial(const std::string& name)
ScriptMaterial ShaderSystemInterface::getMaterial(const std::string& name)
{
return ScriptShader(GlobalMaterialManager().getMaterial(name));
return ScriptMaterial(GlobalMaterialManager().getMaterial(name));
}

bool ShaderSystemInterface::materialExists(const std::string& name)
{
return GlobalMaterialManager().materialExists(name);
}

bool ShaderSystemInterface::materialCanBeModified(const std::string& name)
{
return GlobalMaterialManager().materialCanBeModified(name);
}

ScriptMaterial ShaderSystemInterface::createEmptyMaterial(const std::string& name)
{
return ScriptMaterial(GlobalMaterialManager().createEmptyMaterial(name));
}

ScriptMaterial ShaderSystemInterface::copyMaterial(const std::string& nameOfOriginal, const std::string& nameOfCopy)
{
return ScriptMaterial(GlobalMaterialManager().copyMaterial(nameOfOriginal, nameOfCopy));
}

bool ShaderSystemInterface::renameMaterial(const std::string& oldName, const std::string& newName)
{
return GlobalMaterialManager().renameMaterial(oldName, newName);
}

void ShaderSystemInterface::removeMaterial(const std::string& name)
{
GlobalMaterialManager().removeMaterial(name);
}

void ShaderSystemInterface::saveMaterial(const std::string& name)
{
GlobalMaterialManager().saveMaterial(name);
}

// IScriptInterface implementation
void ShaderSystemInterface::registerInterface(py::module& scope, py::dict& globals)
{
// Add the declaration for a Shader object
py::class_<ScriptShader> shader(scope, "Material");
// Add the declaration for a Material object
py::class_<ScriptMaterial> material(scope, "Material");

// Add the old name as alias
scope.add_object("Shader", shader);

shader.def(py::init<const MaterialPtr&>());
shader.def("getName", &ScriptShader::getName);
shader.def("getShaderFileName", &ScriptShader::getShaderFileName);
shader.def("getDescription", &ScriptShader::getDescription);
shader.def("getDefinition", &ScriptShader::getDefinition);
shader.def("isVisible", &ScriptShader::isVisible);
shader.def("isAmbientLight", &ScriptShader::isAmbientLight);
shader.def("isBlendLight", &ScriptShader::isBlendLight);
shader.def("isFogLight", &ScriptShader::isFogLight);
shader.def("isNull", &ScriptShader::isNull);
scope.add_object("Shader", material);

material.def(py::init<const MaterialPtr&>());
material.def("getName", &ScriptMaterial::getName);
material.def("getShaderFileName", &ScriptMaterial::getShaderFileName);
material.def("getDescription", &ScriptMaterial::getDescription);
material.def("getDefinition", &ScriptMaterial::getDefinition);
material.def("isVisible", &ScriptMaterial::isVisible);
material.def("isAmbientLight", &ScriptMaterial::isAmbientLight);
material.def("isBlendLight", &ScriptMaterial::isBlendLight);
material.def("isFogLight", &ScriptMaterial::isFogLight);
material.def("isNull", &ScriptMaterial::isNull);

// Expose the MaterialVisitor interface

Expand All @@ -71,6 +106,13 @@ void ShaderSystemInterface::registerInterface(py::module& scope, py::dict& globa

materialManager.def("foreachMaterial", &ShaderSystemInterface::foreachMaterial);
materialManager.def("getMaterial", &ShaderSystemInterface::getMaterial);
materialManager.def("materialExists", &ShaderSystemInterface::materialExists);
materialManager.def("materialCanBeModified", &ShaderSystemInterface::materialCanBeModified);
materialManager.def("createEmptyMaterial", &ShaderSystemInterface::createEmptyMaterial);
materialManager.def("copyMaterial", &ShaderSystemInterface::copyMaterial);
materialManager.def("renameMaterial", &ShaderSystemInterface::renameMaterial);
materialManager.def("removeMaterial", &ShaderSystemInterface::removeMaterial);
materialManager.def("saveMaterial", &ShaderSystemInterface::saveMaterial);

scope.add_object("ShaderVisitor", visitor); // old compatibility name
materialManager.def("foreachShader", &ShaderSystemInterface::foreachMaterial); // old compatibility name
Expand Down
17 changes: 12 additions & 5 deletions plugins/script/interfaces/ShaderSystemInterface.h
Expand Up @@ -10,14 +10,14 @@ namespace script
{

/**
* This class represents a single Shader as seen by the Python script.
* This class represents a single Material as seen by the Python script.
*/
class ScriptShader
class ScriptMaterial
{
// The contained shader (can be NULL)
MaterialPtr _shader;
public:
ScriptShader(const MaterialPtr& shader) :
ScriptMaterial(const MaterialPtr& shader) :
_shader(shader)
{}

Expand Down Expand Up @@ -81,7 +81,7 @@ class MaterialVisitorWrapper :
void, /* Return type */
MaterialVisitor, /* Parent class */
visit, /* Name of function in C++ (must match Python name) */
ScriptShader(shader) /* Argument(s) */
ScriptMaterial(shader) /* Argument(s) */
);
}
};
Expand All @@ -94,7 +94,14 @@ class ShaderSystemInterface :
{
public:
void foreachMaterial(MaterialVisitor& visitor);
ScriptShader getMaterial(const std::string& name);
ScriptMaterial getMaterial(const std::string& name);
bool materialExists(const std::string& name);
bool materialCanBeModified(const std::string& name);
ScriptMaterial createEmptyMaterial(const std::string& name);
ScriptMaterial copyMaterial(const std::string& nameOfOriginal, const std::string& nameOfCopy);
bool renameMaterial(const std::string& oldName, const std::string& newName);
void removeMaterial(const std::string& name);
void saveMaterial(const std::string& name);

// IScriptInterface implementation
void registerInterface(py::module& scope, py::dict& globals) override;
Expand Down

0 comments on commit 8f5456c

Please sign in to comment.