In the material editor there is a nifty little feature to Save Texture that allows you to extract an embedded texture file from the .vpb file. But I would like to do this via script instead on all materials in my scene, is that possible?
In the Node Editor I can see the path for the textures but that path is usually a local path that I don't have access to, so I can't copy the texture from there. I've also tried exporting the scene as an .fbx file, but that only extracts some of the texture and that is a problem.
So is there a way to extract all the textures (diffuse, normal, specular, etc.) via script? Is this, or will it be, possible with the new material API in VRED 2023 or can it be done in previous versions (e.g. 2021.3) of VRED as well?
Solved! Go to Solution.
Solved by markus_keller. Go to Solution.
Without looking into it, I´m pretty sure it is possible with the API in 2023. But the "Save texture" in the material does also work for multiple selected materials now, asking for a folder and saving the image name and format comes that is shown in the path. So, not sure how often you must repeat this but selecting all materials and saving the diffuse-, glossy-, roughness- etc. textures for your selection ...the GUI way... is probably faster than writing a script.
It is far to many materials and textures to do it manually, it has to be done with script.
Looking into the documentation for the vrMaterialService in VRED 2023 I can't find any function for saving or extracting textures?
How is it done?
Here is a script that writes all texture in the scene to "c:\vred-snapshots":
import os
def saveImage(image, newPath, doNotOverwriteFiles):
path = image.getAbsolutePath()
newFileName = newPath + os.path.basename(path)
# making sure that existing images are not
if doNotOverwriteFiles:
i = 0
while os.path.exists(newFileName):
i = i + 1
newFileName = newPath + str(i) + "_" + os.path.basename(path)
# save image
vrImageService.saveImage(image, newFileName)
def collectImages():
images = []
# collect all materials
mats = vrMaterialService.getAllMaterials()
handledImages = set()
for mat in mats:
# get all materials from texture
textures = mat.getTextures()
for texture in textures:
image = texture.getImage()
imageId = image.getObjectId()
# only collect each image once
if image.isValid() and not imageId in handledImages:
images.append(image)
handledImages.add(imageId)
return images
# giving the second parameter True or False, to save
doNotOverwriteFiles = True
for image in collectImages():
saveImage(image, "c:/vred-snapshots/", doNotOverwriteFiles)
Or as a one-liner if you want to save a specific texture (diffuse for example):
mat.getDiffuseTexture().getImage().toQImage().save("e:\\test.png")
I just gave this a go and it looks like it is working nicly - thank you! 🙂
Can't find what you're looking for? Ask the community or share your knowledge.