diff --git a/Scripts/Python/plasma/Plasma.py b/Scripts/Python/plasma/Plasma.py index df37bed5bc..3a6b555c24 100644 --- a/Scripts/Python/plasma/Plasma.py +++ b/Scripts/Python/plasma/Plasma.py @@ -5261,6 +5261,24 @@ def saveAsJPEG(self,filename,quality=75): """Saves this image to disk as a JPEG file""" pass +class ptImageLibMod: + """Plasma ImageLibraryModifier class""" + def __init__(self,imlkey): + """None""" + pass + + def getImage(imageName): + """Returns the named image, if present""" + pass + + def getImages(): + """Returns a tuple of ptImages""" + pass + + def getNames(): + """Returns a tuple of the image names""" + pass + class ptInputInterface: """Plasma input interface class""" def __init__(self): diff --git a/Scripts/Python/stupStartUp.py b/Scripts/Python/stupStartUp.py index 29a1a9da9d..98990cf620 100644 --- a/Scripts/Python/stupStartUp.py +++ b/Scripts/Python/stupStartUp.py @@ -114,8 +114,14 @@ def OnServerInitComplete(self): ########################### def randomizeBackground(self): - if ilmList := BGObj.sceneobject.getImageLibMods(): - ilm = ptImageLibMod(ilmList[0]) - bgChoice = random.choice(ilm.getNames()) - if newImage := ilm.getImage(bgChoice): - BGLayer.layer.texture = newImage + if not BGObj.sceneobject or not BGLayer.layer: + PtDebugPrint("stupStartUp: Missing ptAttribs for randomized backgrounds. Leaving default...") + return + + # Get the first available ImageLibModifier on our Background SceneObject + if ilm := BGObj.sceneobject.getImageLibMods()[0]: + # Choose a random image from the available images + bgChoice = random.choice(ilm.getImages()) + # Update the Background Layer's texture to our chosen image + BGLayer.layer.texture = bgChoice + PtDebugPrint("stupStartUp: Background has been randomized.") diff --git a/Sources/Plasma/FeatureLib/pfPython/pyImageLibMod.cpp b/Sources/Plasma/FeatureLib/pfPython/pyImageLibMod.cpp index 3b7fb47900..3c0746f429 100644 --- a/Sources/Plasma/FeatureLib/pfPython/pyImageLibMod.cpp +++ b/Sources/Plasma/FeatureLib/pfPython/pyImageLibMod.cpp @@ -45,8 +45,10 @@ You can contact Cyan Worlds, Inc. by email legal@cyan.com #include "hsResMgr.h" #include "pnKeyedObject/plUoid.h" +#include "pyImage.h" #include "pyImageLibMod.h" + void pyImageLibMod::setKey(pyKey& ilmKey) // only for python glue, do NOT call { if (fModifier && fModifierKey) @@ -56,17 +58,34 @@ void pyImageLibMod::setKey(pyKey& ilmKey) // only for python glue, do NOT call fModifierKey = ilmKey.getKey(); } -plBitmap* pyImageLibMod::GetImage(const ST::string& name) const +pyImage* pyImageLibMod::GetImage(const ST::string& name) const { plBitmap* image; + if (fModifier) image = fModifier->GetImage(name); else image = plImageLibMod::ConvertNoRef(fModifierKey->ObjectIsLoaded())->GetImage(name); - return image; + + return pyImage::ConvertFrom(pyImage::New(dynamic_cast(image))); +} + +const std::vector pyImageLibMod::GetImages() const +{ + std::vector imageList; + plImageLibMod* mod; + + if (fModifier) + mod = fModifier; + else + mod = plImageLibMod::ConvertNoRef(fModifierKey->ObjectIsLoaded()); + + for (const auto& image : mod->GetImages()) + imageList.push_back(pyImage::ConvertFrom(pyImage::New(dynamic_cast(image)))); + return imageList; } -std::vector pyImageLibMod::GetImageNames() const +const std::vector pyImageLibMod::GetImageNames() const { std::vector nameList; plImageLibMod* mod; diff --git a/Sources/Plasma/FeatureLib/pfPython/pyImageLibMod.h b/Sources/Plasma/FeatureLib/pfPython/pyImageLibMod.h index 82c9a7fd98..71f0d1dcda 100644 --- a/Sources/Plasma/FeatureLib/pfPython/pyImageLibMod.h +++ b/Sources/Plasma/FeatureLib/pfPython/pyImageLibMod.h @@ -55,6 +55,8 @@ You can contact Cyan Worlds, Inc. by email legal@cyan.com #include "pnKeyedObject/plKey.h" #include "plModifier/plImageLibMod.h" +class pyImage; + class pyImageLibMod { protected: @@ -114,8 +116,9 @@ class pyImageLibMod plKey GetKey() const { return fModifier ? fModifier->GetKey() : fModifierKey; } // for python access - plBitmap* GetImage (const ST::string& name) const; - std::vector GetImageNames() const; + pyImage* GetImage (const ST::string& name) const; + const std::vector GetImageNames() const; + const std::vector GetImages() const; }; #endif // pyImageLibMod_h diff --git a/Sources/Plasma/FeatureLib/pfPython/pyImageLibModGlue.cpp b/Sources/Plasma/FeatureLib/pfPython/pyImageLibModGlue.cpp index 1a6943d050..635409124f 100644 --- a/Sources/Plasma/FeatureLib/pfPython/pyImageLibModGlue.cpp +++ b/Sources/Plasma/FeatureLib/pfPython/pyImageLibModGlue.cpp @@ -113,13 +113,22 @@ PYTHON_METHOD_DEFINITION(ptImageLibMod, getImage, args) PYTHON_RETURN_ERROR; } - plMipmap* image = plMipmap::ConvertNoRef(self->fThis->GetImage(name)); + pyImage* image = self->fThis->GetImage(name); if (image) - return pyImage::New(image); + return pyImage::New(image->GetKey()); PYTHON_RETURN_NONE; } +PYTHON_METHOD_DEFINITION_NOARGS(ptImageLibMod, getImages) +{ + const std::vector imageList = self->fThis->GetImages(); + PyObject* retVal = PyTuple_New(imageList.size()); + for (size_t curKey = 0; curKey < imageList.size(); curKey++) + PyTuple_SET_ITEM(retVal, curKey, pyImage::New(imageList[curKey]->GetKey())); + return retVal; +} + PYTHON_METHOD_DEFINITION_NOARGS(ptImageLibMod, getNames) { std::vector nameList = self->fThis->GetImageNames(); @@ -131,6 +140,7 @@ PYTHON_METHOD_DEFINITION_NOARGS(ptImageLibMod, getNames) PYTHON_START_METHODS_TABLE(ptImageLibMod) PYTHON_METHOD(ptImageLibMod, getImage, "Params: name\nReturns the ptImage with the specified name"), + PYTHON_METHOD_NOARGS(ptImageLibMod, getImages, "Returns a tuple of the library's ptImages"), PYTHON_METHOD_NOARGS(ptImageLibMod, getNames, "Returns the list of image names"), PYTHON_END_METHODS_TABLE; diff --git a/Sources/Plasma/FeatureLib/pfPython/pySceneObject.cpp b/Sources/Plasma/FeatureLib/pfPython/pySceneObject.cpp index cd2189d321..859c33b148 100644 --- a/Sources/Plasma/FeatureLib/pfPython/pySceneObject.cpp +++ b/Sources/Plasma/FeatureLib/pfPython/pySceneObject.cpp @@ -49,6 +49,7 @@ You can contact Cyan Worlds, Inc. by email legal@cyan.com #include "pyKey.h" #include "pyMatrix44.h" +#include "pyImageLibMod.h" #include "pySceneObject.h" #include "cyAvatar.h" @@ -838,7 +839,7 @@ std::vector pySceneObject::GetImageLibMods() { const plImageLibMod* ilm = plImageLibMod::ConvertNoRef(obj->GetModifierByType(plImageLibMod::Index())); if (ilm) - pyPL.push_back(pyKey::New(ilm->GetKey())); + pyPL.push_back(pyImageLibMod::New(ilm->GetKey())); } } return pyPL; diff --git a/Sources/Plasma/FeatureLib/pfPython/pySceneObjectGlue.cpp b/Sources/Plasma/FeatureLib/pfPython/pySceneObjectGlue.cpp index 6270bc4c09..6ee4371401 100644 --- a/Sources/Plasma/FeatureLib/pfPython/pySceneObjectGlue.cpp +++ b/Sources/Plasma/FeatureLib/pfPython/pySceneObjectGlue.cpp @@ -184,9 +184,9 @@ PYTHON_METHOD_DEFINITION_NOARGS(ptSceneobject, getPythonMods) PYTHON_METHOD_DEFINITION_NOARGS(ptSceneobject, getImageLibMods) { std::vector vecList = self->fThis->GetImageLibMods(); - PyObject* retVal = PyList_New(vecList.size()); + PyObject* retVal = PyTuple_New(vecList.size()); for (int curKey = 0; curKey < vecList.size(); curKey++) - PyList_SetItem(retVal, curKey, vecList[curKey]); // steals the vecList ref + PyTuple_SetItem(retVal, curKey, vecList[curKey]); return retVal; } diff --git a/Sources/Plasma/PubUtilLib/plModifier/plImageLibMod.cpp b/Sources/Plasma/PubUtilLib/plModifier/plImageLibMod.cpp index cbe0d028a4..0b71ea08da 100644 --- a/Sources/Plasma/PubUtilLib/plModifier/plImageLibMod.cpp +++ b/Sources/Plasma/PubUtilLib/plModifier/plImageLibMod.cpp @@ -49,13 +49,6 @@ You can contact Cyan Worlds, Inc. by email legal@cyan.com #include "hsStream.h" #include "hsResMgr.h" -plImageLibMod::plImageLibMod() -{ -} - -plImageLibMod::~plImageLibMod() -{ -} bool plImageLibMod::MsgReceive(plMessage* msg) { @@ -106,6 +99,14 @@ plBitmap* plImageLibMod::GetImage(const ST::string& imageName) const return nullptr; } +const std::vector plImageLibMod::GetImages() const +{ + std::vector images; + for (auto image : fImages) + images.emplace_back(image); + return images; +} + const std::vector plImageLibMod::GetImageNames() const { std::vector names; diff --git a/Sources/Plasma/PubUtilLib/plModifier/plImageLibMod.h b/Sources/Plasma/PubUtilLib/plModifier/plImageLibMod.h index a6f84d4d57..eaede4c586 100644 --- a/Sources/Plasma/PubUtilLib/plModifier/plImageLibMod.h +++ b/Sources/Plasma/PubUtilLib/plModifier/plImageLibMod.h @@ -58,8 +58,8 @@ class plImageLibMod : public plSingleModifier bool IEval(double secs, float del, uint32_t dirty) override { return false; } public: - plImageLibMod(); - virtual ~plImageLibMod(); + plImageLibMod() {}; + virtual ~plImageLibMod() {}; CLASSNAME_REGISTER( plImageLibMod ); GETINTERFACE_ANY( plImageLibMod, plSingleModifier ); @@ -76,6 +76,7 @@ class plImageLibMod : public plSingleModifier size_t GetNumImages() const { return fImages.size(); } plBitmap* GetImage(const ST::string&) const; + const std::vector GetImages() const; const std::vector GetImageNames() const; };