Problem with baking ambient occlusion



  • On 02/02/2017 at 06:24, xxxxxxxx wrote:

    User Information:
    Cinema 4D Version:   R18 
    Platform:   Windows  ;   
    Language(s) :       PYTHON  ;

    ---------
    Hi,
    i am trying to bake ambient occlusion maps using python in cinema 4d R18.
    I do get a file written to disk and it looks ok, but it contains the diffuse color and shading of the surface instead of the ambient occlusion.

    Here is the code:

    bakeSettings = c4d.BaseContainer()
     
      bakeSettings.SetInt32(c4d.BAKE_TEX_WIDTH, int(options.option_width))
      bakeSettings.SetInt32(c4d.BAKE_TEX_HEIGHT, int(options.option_height))
      bakeSettings.SetInt32(c4d.BAKE_TEX_PIXELBORDER, int(options.option_pixelborder))
      bakeSettings.SetInt32(c4d.BAKE_TEX_SUPERSAMPLING, int(options.option_ss))
      bakeSettings.SetVector(c4d.BAKE_TEX_FILL_COLOR, options.option_bg_color['color'])
      bakeSettings.SetBool(c4d.BAKE_TEX_AMBIENT_OCCLUSION, True)

    bmp = c4d.bitmaps.BaseBitmap()
     
      bmp.Init(int(options.option_width), int(options.option_height))

    ret = c4d.utils.InitBakeTexture(doc, textureTag, uvwTags[0], uvwTags[0], bakeSettings)
     
      c4d.utils.BakeTexture(ret[0], bakeSettings, bmp, None, progressCallback)
     
      bmp.Save(filename, c4d.FILTER_PNG)

    Thanks,
    Per



  • On 03/02/2017 at 06:02, xxxxxxxx wrote:

    Hi Per,

    welcome to the Plugin Café forums 🙂

    Unfortunately the Python documentation is a bit inaccurate here (one could also say it's wrong).
    You need to pass a MultipassBitmap in order to make work.

    Edit: I moved this post to the Python sub-forum.



  • On 06/02/2017 at 01:04, xxxxxxxx wrote:

    Hi again,

    I changed the bitmap creation, the code looks like this now:

    bakeSettings = c4d.BaseContainer()
     
      bakeSettings.SetInt32(c4d.BAKE_TEX_WIDTH, int(options.option_width))
      bakeSettings.SetInt32(c4d.BAKE_TEX_HEIGHT, int(options.option_height))
      bakeSettings.SetInt32(c4d.BAKE_TEX_PIXELBORDER, int(options.option_pixelborder))
      bakeSettings.SetInt32(c4d.BAKE_TEX_SUPERSAMPLING, int(options.option_ss))
      bakeSettings.SetVector(c4d.BAKE_TEX_FILL_COLOR, options.option_bg_color['color'])
      bakeSettings.SetBool(c4d.BAKE_TEX_AMBIENT_OCCLUSION, True)

    bmp = c4d.bitmaps.MultipassBitmap(int(options.option_width), int(options.option_height), c4d.COLORMODE_ARGB)

    ret = c4d.utils.InitBakeTexture(doc, textureTag, uvwTags[0], uvwTags[0], bakeSettings)
     
      c4d.utils.BakeTexture(ret[0], bakeSettings, bmp, None, progressCallback)
     
      bmp.Save(filename, c4d.FILTER_PNG)

    But it does not work, now I get an image that is filled with the background color only.

    Any ideas?

    Thanks,

    Per



  • On 06/02/2017 at 04:35, xxxxxxxx wrote:

    Hm? Except that I do not use a progress hook and that I fill the BaseContainer with constant values, I use pretty much your code and it seems to work fine for me. Perhaps you can reduce your code to a complete example, I can throw into the Script Manager in order to reproduce your issue?



  • On 07/02/2017 at 01:43, xxxxxxxx wrote:

    Hi again,

    here is the full script:

      
    import c4d  
    import os  
    from c4d import gui  
       
    LBL_INFO1 = 1000  
    LBL_INFO2 = 1001  
    LBL_INFO3 = 1003  
    LBL_INFO4 = 1004  
    LBL_INFO5 = 1005  
    LBL_INFO6 = 1006  
      
    TXT_WIDTH = 10001  
    TXT_HEIGHT = 10002  
    TXT_SS = 10003  
    TXT_FILEPATH = 10004  
    TXT_PIXELBORDER = 10005  
    COLOR_FIELD_ID = 10006  
      
    GROUP_OPTIONS = 20000  
    BTN_OK = 20001  
    BTN_CANCEL = 20002  
       
    class OptionsDialog(gui.GeDialog) :  
      def CreateLayout(self) :  
      self.SetTitle('Bake objects')  
      
      self.AddStaticText(LBL_INFO1, c4d.BFH_LEFT, name='Width:')   
      self.AddEditText(TXT_WIDTH, c4d.BFH_SCALEFIT)  
      self.SetString(TXT_WIDTH, '1024')    
      
      self.AddStaticText(LBL_INFO2, c4d.BFH_LEFT, name='Height:')   
      self.AddEditText(TXT_HEIGHT, c4d.BFH_SCALEFIT)  
      self.SetString(TXT_HEIGHT, '1024')    
      
      self.AddStaticText(LBL_INFO3, c4d.BFH_LEFT, name='Supersampling:')   
      self.AddEditText(TXT_SS, c4d.BFH_SCALEFIT)  
      self.SetString(TXT_SS, '0')    
      
      self.AddStaticText(LBL_INFO5, c4d.BFH_LEFT, name='Pixel border:')   
      self.AddEditText(TXT_PIXELBORDER, c4d.BFH_SCALEFIT)  
      self.SetString(TXT_PIXELBORDER, '2')    
      
      self.AddStaticText(LBL_INFO6, c4d.BFH_LEFT, name='Background color:')   
      self.AddColorField(COLOR_FIELD_ID, c4d.BFH_SCALEFIT)  
      self.SetColorField(COLOR_FIELD_ID, c4d.Vector(1), 1, 1, 1)  
      
      self.AddStaticText(LBL_INFO4, c4d.BFH_LEFT, name='File output path:')   
      self.AddEditText(TXT_FILEPATH, c4d.BFH_SCALEFIT)  
      self.SetString(TXT_FILEPATH, 'c:\\temp')    
        
      self.GroupBegin(GROUP_OPTIONS, c4d.BFH_CENTER, 2, 1)  
      self.AddButton(BTN_OK, c4d.BFH_SCALE, name='OK')  
      self.AddButton(BTN_CANCEL, c4d.BFH_SCALE, name='Cancel')  
      self.GroupEnd()  
      self.ok = False  
      return True  
       
      def Command(self, id, msg) :  
      if id==BTN_CANCEL:  
        self.Close()  
      elif id==BTN_OK:  
        self.ok = True  
        self.option_width = self.GetString(TXT_WIDTH)  
        self.option_height = self.GetString(TXT_HEIGHT)  
        self.option_ss = self.GetString(TXT_SS)  
        self.option_pixelborder = self.GetString(TXT_PIXELBORDER)  
        self.option_filepath = self.GetString(TXT_FILEPATH)  
        self.option_bg_color = self.GetColorField(COLOR_FIELD_ID)  
        self.Close()  
      return True  
       
      
    progressStatus=dict()  
    progressStatus[c4d.BAKE_STATE_NONE]="None"  
    progressStatus[c4d.BAKE_STATE_PREPARE]="Prepare"  
    progressStatus[c4d.BAKE_STATE_GI_PREPASS]="GI prepass"  
    progressStatus[c4d.BAKE_STATE_FILL_IMAGE]="Fill image"  
    progressStatus[c4d.BAKE_STATE_COMPLETE]="Complete"  
    progressStatus[c4d.BAKE_STATE_INITIALIZE]="Initialize"  
    progressStatus[c4d.BAKE_STATE_RESIZENOTIFY]="Resize notify"  
        
    def progressCallback(status) :      
      print("state:" + progressStatus[status['state']] + "   progress:"+str(100*status['r'])+"%")  
      
    def doBake(doc, obj, options) :    
      
      # find texture and uv tags  
      textureTag = None  
      uvwTags = []  
      
      for tag in obj.GetTags() :  
      if type(tag) == c4d.TextureTag and textureTag == None:  
        textureTag = tag  
      
      if type(tag) == c4d.UVWTag:  
        uvwTags.append(tag)  
      
      bakeSettings = c4d.BaseContainer()  
      bakeSettings.SetInt32(c4d.BAKE_TEX_WIDTH,int(options.option_width))  
      bakeSettings.SetInt32(c4d.BAKE_TEX_HEIGHT,int(options.option_height))  
      bakeSettings.SetInt32(c4d.BAKE_TEX_PIXELBORDER, int(options.option_pixelborder))  
      bakeSettings.SetInt32(c4d.BAKE_TEX_SUPERSAMPLING, int(options.option_ss))  
      bakeSettings.SetVector(c4d.BAKE_TEX_FILL_COLOR, options.option_bg_color['color'])  
      bakeSettings.SetBool(c4d.BAKE_TEX_AMBIENT_OCCLUSION, True)  
      
      bmp = c4d.bitmaps.MultipassBitmap(int(options.option_width), int(options.option_height), c4d.COLORMODE_ARGB)  
      
      ret = c4d.utils.InitBakeTexture(doc, textureTag, uvwTags[0], uvwTags[len(uvwTags)-1], bakeSettings)  
        
      ret2 = c4d.utils.BakeTexture(ret[0], bakeSettings, bmp, None, progressCallback)   
        
      filename = os.path.join(options.option_filepath,obj.GetName()+".png")  
      
      ret3 = bmp.Save(filename, c4d.FILTER_PNG)  
      
      print("wrote: "+filename)  
      
        
    def main() :  
      # Get the selected objects, including children.  
      selection = doc.GetActiveObjects(c4d.GETACTIVEOBJECTFLAGS_CHILDREN)  
       
      if len(selection) <= 0:  
      gui.MessageDialog('Must select objects!')  
      return  
       
      dlg = OptionsDialog()  
      dlg.Open(c4d.DLG_TYPE_MODAL, defaultw=300, defaulth=50)  
        
      if not dlg.ok:  
      return  
       
        
      for i in range(0,len(selection)) :  
      sel = selection[i]  
     _     _doBake(doc,sel,dlg)  
     _     _  
    if __name__=='__main__':  
      main()  
    
    

    Edit: Added code tags, see BBcodes below edit field



  • On 08/02/2017 at 02:34, xxxxxxxx wrote:

    Sorry,
    there was an error in the code in the last post.

    i have updated it so it should run. But the baked texture still looks wrong.

    Cheers,

    Per

    __



  • On 08/02/2017 at 06:00, xxxxxxxx wrote:

    Hm, maybe we have different expectations, what the result should look like. Even with your code I get the result I expect here.
    Just to make sure, the UVs are layed out correctly?

    Or maybe there's something special in your scene, which triggers an issue. Can you please provide me the scene and a description, what you expect to get as an result? You can email it to sdk_support.



  • On 08/02/2017 at 06:44, xxxxxxxx wrote:

    Hi Andreas.

    It was the geometry that was wrong, I was confused about what uv set was actually being used when calling InitBakeTexture. The geometry had multiple uv sets and the one that was actually used when baking was broken.

    It works fine now, thanks a lot for the help.

    Cheers,

    Per


Log in to reply