Maxon Developers Maxon Developers
    • Documentation
      • Cinema 4D Python API
      • Cinema 4D C++ API
      • Cineware API
      • ZBrush GoZ API
      • Code Examples on Github
    • Forum
    • Downloads
    • Support
      • Support Procedures
      • Registered Developer Program
      • Plugin IDs
      • Contact Us
    • Categories
      • Overview
      • News & Information
      • Cinema 4D SDK Support
      • Cineware SDK Support
      • ZBrush 4D SDK Support
      • Bugs
      • General Talk
    • Unread
    • Recent
    • Tags
    • Users
    • Login

    How to know baking is end with BakeTextureTag?

    Cinema 4D SDK
    python r19
    2
    7
    1.2k
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • V
      velbie
      last edited by velbie

      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.

      1 Reply Last reply Reply Quote 0
      • M
        m_adam
        last edited by

        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.

        MAXON SDK Specialist

        Development Blog, MAXON Registered Developer

        V 1 Reply Last reply Reply Quote 1
        • V
          velbie @m_adam
          last edited by velbie

          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 !

          1 Reply Last reply Reply Quote 0
          • M
            m_adam
            last edited by

            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.

            MAXON SDK Specialist

            Development Blog, MAXON Registered Developer

            1 Reply Last reply Reply Quote 0
            • V
              velbie
              last edited by

              Thank you for letting me know @m_adam

              1 Reply Last reply Reply Quote 0
              • M
                m_adam
                last edited by

                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

                MAXON SDK Specialist

                Development Blog, MAXON Registered Developer

                1 Reply Last reply Reply Quote 0
                • M
                  m_adam
                  last edited by m_adam

                  Hi @velbie with the latest update of Cinema 4D (R24 SP1), the BAKE_TEX_AO_VERTEXMAPS issue is fixed.

                  Cheers,
                  Maxime.

                  MAXON SDK Specialist

                  Development Blog, MAXON Registered Developer

                  1 Reply Last reply Reply Quote 0
                  • First post
                    Last post