Skip to content

Instantly share code, notes, and snippets.

@NiklasRosenstein
Last active November 14, 2017 19:06
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save NiklasRosenstein/fa9c9f72fa819dc001e4 to your computer and use it in GitHub Desktop.
Save NiklasRosenstein/fa9c9f72fa819dc001e4 to your computer and use it in GitHub Desktop.
This function can be used to copy a texture for the Cinema 4D content browser.
def copy_preset_texture(source, dest):
''' Copies a bitmap file from the Cinema 4D preset library to the
file specified by *dest*. The suffix of *source* is used to determine
the output image format.
Arguments:
source: The source texture path that begins with `preset://`
dest: The filename to save the texture to.
Raises
IOError: If *source* does not exist or could not be loaded.
ValueError: If the file suffix of *source* does not match
one of the supported image suffixes.
'''
lower = source.lower()
if lower.endswith('.tif') or lower.endswith('.tiff'):
format = c4d.FILTER_TIF
elif lower.endswith('.tga'):
format = c4d.FILTER_TGA
elif lower.endswith('.bmp'):
format = c4d.FILTER_BMP
elif lower.endswith('.iff'):
format = c4d.FILTER_IFF
elif lower.endswith('.jpg'):
format = c4d.FILTER_JPG
elif lower.endswith('.pict'):
format = c4d.FILTER_PICT
elif lower.endswith('.psd'):
format = c4d.FILTER_PSD
elif lower.endswith('.rla'):
format = c4d.FILTER_RLA
elif lower.endswith('.rpf'):
format = c4d.FILTER_RPF
elif lower.endswith('.b3d'):
format = c4d.FILTER_B3D
# elif lower.endswith('.'):
# format = c4d.FILTER_TIF_B3D
elif lower.endswith('.psb'):
format = c4d.FILTER_PSB
elif lower.endswith('.avi'):
format = c4d.FILTER_AVI
elif lower.endswith('.mov'):
format = c4d.FILTER_MOVIE
#elif lower.endswith('.'):
# format = c4d.FILTER_QTVRSAVER_PANORAMA
#elif lower.endswith('.'):
# format = c4d.FILTER_QTVRSAVER_OBJECT
elif lower.endswith('.hdr'):
format = c4d.FILTER_HDR
#elif lower.endswith('.'):
# format = c4d.FILTER_EXR_LOAD
elif lower.endswith('.exr'):
format = c4d.FILTER_EXR
elif lower.endswith('.png'):
format = c4d.FILTER_PNG
elif lower.endswith('.ies'):
format = c4d.FILTER_IES
elif lower.endswith('.dpx'):
format = c4d.FILTER_DPX
else:
raise ValueError('can not determine image format of <source>', source)
bmp = c4d.bitmaps.BaseBitmap()
result = bmp.InitWith(source)[0]
if result != c4d.IMAGERESULT_OK:
raise IOError('<source> image could not be loaded', source)
flags = c4d.SAVEBIT_ALPHA | c4d.SAVEBIT_MULTILAYER
result = bmp.Save(dest, format, None, flags)
if result == c4d.IMAGERESULT_FILEERROR:
raise IOError('could not save to image', dest)
elif result != c4d.IMAGERESULT_OK:
raise IOError('unknown error while saving', dest)
@gr4ph0s
Copy link

gr4ph0s commented Jan 25, 2017

A quick fix for the R17 (I don't really know when BaseBitmap.Save)

result = bmp.Save(dest, format, savebits=flags)

@NiklasRosenstein
Copy link
Author

Seems like this parameter was already added in R14! I'll update the code.

@NiklasRosenstein
Copy link
Author

Also fixed IMAGERESULT_FILEERROR spelling error.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment