image.py 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. import os
  2. import base64
  3. from .. import constants, logger
  4. from . import base_classes, io, api
  5. class Image(base_classes.BaseNode):
  6. """Class the wraps an image node. This is the node that
  7. represent that actual file on disk.
  8. """
  9. def __init__(self, node, parent):
  10. logger.debug("Image().__init__(%s)", node)
  11. base_classes.BaseNode.__init__(self, node, parent, constants.IMAGE)
  12. if(self.scene.options.get(constants.EMBED_TEXTURES, False)):
  13. texturefile = open(api.image.file_path(self.node),"rb")
  14. extension = os.path.splitext(api.image.file_path(self.node))[1][1:].strip().lower()
  15. if(extension == 'jpg') :
  16. extension = 'jpeg'
  17. self[constants.URL] = "data:image/" + extension + ";base64," + base64.b64encode(texturefile.read()).decode("utf-8")
  18. texturefile.close();
  19. else:
  20. texture_folder = self.scene.options.get(constants.TEXTURE_FOLDER, "")
  21. self[constants.URL] = os.path.join(texture_folder, api.image.file_name(self.node))
  22. @property
  23. def destination(self):
  24. """
  25. :return: full destination path (when copied)
  26. """
  27. dirname = os.path.dirname(self.scene.filepath)
  28. return os.path.join(dirname, self[constants.URL])
  29. @property
  30. def filepath(self):
  31. """
  32. :return: source file path
  33. """
  34. return api.image.file_path(self.node)
  35. def copy_texture(self, func=io.copy):
  36. """Copy the texture.
  37. self.filepath > self.destination
  38. :param func: Optional function override (Default value = io.copy)
  39. arguments are (<source>, <destination>)
  40. :return: path the texture was copied to
  41. """
  42. logger.debug("Image().copy_texture()")
  43. func(self.filepath, self.destination)
  44. return self.destination