SOLVED How to know baking is end with BakeTextureTag?

Hello, I am a newbie in c4d python programming.
And have tried to bake BakeTextureTag by script.

Like below..

if __name__ == '__main__':
    tags = doc.GetActiveTags()

    for i, tag in enumerate(tags):
        c4d.CallButton(tag, c4d.BAKETEXTURE_BAKE)

The problem is how to know when the baking is done??

I'd like to bake one by one. Because I baked several things at the same time, my computer goes down 😞

I've seen some examples of threads. It's too hard, and it's just showing me the uvw tag bake not BakeTexture Tag.

Please give me some advice.

Thanks.

Hi, @velbie first of all welcome to the plugincafe community 🙂
For your next topics and also this one please try to use our QA feature.

Regarding your issue, unfortunately, there is no way to catch the result or wait until the CallButton action ended.
However the Python SDK offer the c4d.utils.BakeTexture function which need to be initialized via c4d.utils.InitBakeTexture.
The BakeTexture function is an asynchronous function meaning when it returns, the baking process is done, moreover, both methods let you define baking setting.

Finally, we have the texture_baker example which uses the baking functions.

Hope it answers your question, if not feel free to ask again.
Cheers,
Maxime.

Thank you so much @m_adam !
I try to texture_baker example just now you mentioned! Adding some options with BaseContainer()

        bakeData[c4d.BAKE_TEX_AMBIENT_OCCLUSION] = True
        bakeData[c4d.BAKE_TEX_AO_VERTEX_MAP] = True

It's working but I want to make a vertex map tag into the selected object like the below image.

It's automatically generated when I click the bake button in the Bake texture tag(blue) with my hand, but I don't know how to do it with python code.

This is code that simplifies and adds options.
Finally, I got the bitmap so Can I use a bitmap to make the Bake texture tag into the object?

import c4d

class TextureBakerThread(c4d.threading.C4DThread):

    def __init__(self, doc, textags, texuvws, destuvws):
        self.doc = doc
        self.textags = textags
        self.texuvws = texuvws
        self.destuvws = destuvws

        self.bakeDoc = None
        self.bakeData = None
        self.bakeBmp = c4d.bitmaps.MultipassBitmap(512, 512, c4d.COLORMODE_RGB)
        self.bakeError = c4d.BAKE_TEX_ERR_NONE

    def Begin(self):

        # Defines baking setting
        bakeData = c4d.BaseContainer()
        bakeData[c4d.BAKE_TEX_WIDTH] = 512
        bakeData[c4d.BAKE_TEX_HEIGHT] = 512
        bakeData[c4d.BAKE_TEX_PIXELBORDER] = 1
        bakeData[c4d.BAKE_TEX_CONTINUE_UV] = False
        bakeData[c4d.BAKE_TEX_SUPERSAMPLING] = 0
        bakeData[c4d.BAKE_TEX_FILL_COLOR] = c4d.Vector(1)
        bakeData[c4d.BAKE_TEX_USE_BUMP] = False
        bakeData[c4d.BAKE_TEX_USE_CAMERA_VECTOR] = False
        bakeData[c4d.BAKE_TEX_AUTO_SIZE] = False
        bakeData[c4d.BAKE_TEX_NO_GI] = False
        bakeData[c4d.BAKE_TEX_GENERATE_UNDO] = False
        bakeData[c4d.BAKE_TEX_PREVIEW] = False
        bakeData[c4d.BAKE_TEX_COLOR] = True
        bakeData[c4d.BAKE_TEX_UV_LEFT] = 0.0
        bakeData[c4d.BAKE_TEX_UV_RIGHT] = 1.0
        bakeData[c4d.BAKE_TEX_UV_TOP] = 0.0
        bakeData[c4d.BAKE_TEX_UV_BOTTOM] = 1.0
        bakeData[c4d.BAKE_TEX_AMBIENT_OCCLUSION] = True # HERE I ADD
        bakeData[c4d.BAKE_TEX_AO_VERTEX_MAP] = True # HERE I ADD
        # bakeData[c4d.BAKE_TEX_OPTIMAL_MAPPING] = c4d.BAKE_TEX_OPTIMAL_MAPPING_CUBIC

        self.bakeData = bakeData

        # Initializes bake process
        bakeInfo = c4d.utils.InitBakeTexture(self.doc, self.textags, self.texuvws, self.destuvws, self.bakeData, self.Get())
        self.bakeDoc = bakeInfo[0]
        self.bakeError = bakeInfo[1]

        if self.bakeError != c4d.BAKE_TEX_ERR_NONE or self.bakeDoc is None:
            return False

        # Starts bake thread
        self.Start(c4d.THREADMODE_ASYNC, c4d.THREADPRIORITY_BELOW)
        return True

    def BakeTextureHook(self, info):
        # Texture Baker hook, currently not used
        # print info
        pass

    def Main(self):
        # Bake Texture Thread Main routine
        self.bakeError = c4d.utils.BakeTexture(self.bakeDoc, self.bakeData, self.bakeBmp, self.Get(), self.BakeTextureHook)


if __name__ == '__main__':
    obj = doc.GetActiveObject()
    uvwTag = obj.GetTag(c4d.Tuvw)
    tags = obj.GetTags()
    textags, texuvws, destuvws = [], [], []
    for tag in tags:
        if tag.CheckType(c4d.Ttexture):
            textags.append(tag)
            texuvws.append(uvwTag)
            destuvws.append(uvwTag)

    textureBakerThread = TextureBakerThread(doc, textags, texuvws, destuvws)
    print textureBakerThread
    print textureBakerThread.Begin()
    bmp = textureBakerThread.bakeBmp
    if bmp is None:
        raise RuntimeError("Failed to retrieve the baked bitmap.")

    # Displays the bitmap
    c4d.bitmaps.ShowBitmap(bmp)

    # Removes the reference to the C4D Thread, so the memory used is free
    textureBakerThread = None

Thank you for your time @m_adam !

Hi @velbie unfrotually this is not possible to do it due to a bug in the Python API. I've opened a new bug report.

I will try to find a workaround within the next days (but I'm pretty doubtfully to come with a solution this week)
Cheers,
Maxime.

Thank you for letting me know @m_adam

Hi Unfortunately I didn't found any workaround. I saw you deleted the topic but I restored it since there is still valuable information for the next readers, moreover, I will update the post once the bug will be fixed.

Cheers,
Maxime