@m_adam Hi, this partially works for me.
When I manually run your script for the assets we have already created, the preview images are set and displayed after a restart of Cinema 4D.
However if I insert the code directly into our script, then sometimes one object has the preview image correctly set and displayed after a restart. But most of the time it is the preview image created by Cinema 4D itself, which is almost completely black.
To clarify what we do:
Within a batch process (a script executed within Cinema 4D) we create multiple textured objects and add each of those to a newly created database. The object to be added to the Database have Redshift materials.
So if we create for example 9 object within that batch process then all 9 object most of the time have a black image, sometimes one of those has the correct image.
The database is create this way:
bases = maxon.BaseArray(maxon.AssetRepositoryRef)
assetDb = AssetRepositoryTypes.AssetDatabase
assetDb = maxon.RegistryInterface.FindEntryValue(assetDb._registry, assetDb._ids)
repository = maxon.AssetInterface.CreateRepositoryFromUrl(
rid, assetDb, bases, maxon.Url(self.databasePath), True, False, False, None)
if not repository:
raise RuntimeError("Repository construction failed.")
In a loop we iterate objects and create an Asset from that object, at that part I added the above code.
for objectVaraint in variants:
assetMetadata = maxon.AssetMetaData()
assetCategoryId = maxon.Id(self.categoryId)
# A StoreAssetStruct is a helper data structure for storing assets which bundles up an asset
# category and storage and lookup repository for the operation.
storeAssetStruct = maxon.StoreAssetStruct(assetCategoryId, self.repository, self.repository)
# Use the convenience method AssetCreationInterface.CreateObjectAsset() to create and store an
# object asset in one operation. The instantiation of a FileAsset for the object is hidden
# away, and instead we deal directly with the AssetDescription which is representing the object
# file asset.
assetDescription = maxon.AssetCreationInterface.CreateObjectAsset(
objectVaraint, doc, storeAssetStruct, assetId, assetName, assetVersion, assetMetadata, True)
metadata = assetDescription.GetMetaData()
imagepUrl = maxon.Url("C:/Users/Till Niese/picture.png")
# Stores the Url Meta Data, if there was no preview stored previously (aka the default png file for the given category) then it will be refreshed right away
# Otherwise Cinema 4D need to be restarted
maxon.AssetDescriptionInterface.StoreUrlMetaData = AssetDescriptionInterface.StoreUrlMetaData
assetDescription.StoreUrlMetaData(maxon.ASSETMETADATA.ASSET_PREVIEWIMAGEURL, imagepUrl, maxon.AssetMetaData.KIND.PERSISTENT)
# Turn off automatic preview
metaProperties = metadata.Get(maxon.ASSETMETADATA.MetaProperties)
metaProperties.Set(maxon.ASSET.METAPROPERTIES.BASE.AUTOMATICPREVIEW, False)
assetDescription.StoreMetaData(maxon.ASSETMETADATA.MetaProperties, metaProperties, maxon.AssetMetaData.KIND.PERSISTENT)
Right after the imports I have added this:
# --- AssetDescriptionInterface monkey patching fix for AssetDescriptionInterface.StoreUrlMetaData() --------------------------
@maxon.interface.MAXON_INTERFACE(maxon.consts.MAXON_REFERENCE_COPY_ON_WRITE, "net.maxon.interface.assetdescription")
class AssetDescriptionInterface(maxon.AssetBaseInterface):
@maxon.interface.MAXON_FUNCTION_EXTEND("net.maxon.interface.assetdescription.StoreUrlMetaData")
def StoreUrlMetaData(self, metaId, source, kind):
return self.GetRepository().StoreUrlMetaData(self, metaId, source, kind)
maxon.AssetDescriptionInterface.StoreUrlMetaData = AssetDescriptionInterface.StoreUrlMetaData
@maxon.MAXON_REGISTRY("net.maxon.registry.assetrepositorytypes")
class AssetRepositoryTypes(maxon.Registry):
AssetDatabase = maxon.MAXON_DECLARATION("net.maxon.assets.repositorytype.database")
Kind regards,
Till