Scaling 2d planes to their texture..



  • On 24/09/2014 at 06:33, xxxxxxxx wrote:

    A while back Lennart (tca studios), wrote a smart bit of python script to make a plane the same dimensions as whatever texture you drop on it. Truly fantastic in many ways.

    But there is on drawback that i haven't worked out. The material paths for the texture must be absolute. While not a huge drawback it would be a future proof script if it could just use local file names.

    So can this be done or is this just the way python looks at files?

    The link to the thread (with the script) can be found below.

    http://forums.cgsociety.org/showthread.php?f=47&t=1081362&page=1&pp=15

    If anyone has any ideas that would be great! If not i hope you still enjoy the brilliance & generosity of TCAstudios..



  • On 24/09/2014 at 07:23, xxxxxxxx wrote:

    Hello,

    ~~if you grab the bitmap within a init and a free Render call it works, like:
    cheers
    Martin
    ~~

    Edit:
    This doesn´t work either.
    Sorry mate!
    I´ll have a lock at it  tonight, if someone smarter isn´t faster.
    Martin

      
    import c4d  
    from c4d import gui,bitmaps  
    from c4d.modules.render import  InitRenderStruct  
      
    #adjust plane to bitmap script  
      
      
    def main() :  
        
      if op==None:return  
      #if it is not a plane primitiv return  
      if op.GetType()!=5168:  
          return  
      
      if op.GetTag(c4d.Ttexture)== None:  
          dialog=gui.MessageDialog("Please assign a material to the object",c4d.GEMB_OK)  
          if dialog== 1:  
              return  
      else:  
          TexTag=op.GetTag(c4d.Ttexture)     
             
      Mat= TexTag.GetMaterial()  
      Shader=Mat[c4d.MATERIAL_COLOR_SHADER]  
      
      
      
      #init the render struct and get the bitmap  
      if Shader.InitRender(InitRenderStruct())==c4d.INITRENDERRESULT_OK:  
          bitmap= Shader.GetBitmap()  
          Shader.FreeRender()  
          if bitmap != None:  
              bmsize     = bitmap.GetSize()  
                
              op[c4d.PRIM_PLANE_WIDTH]=bmsize[0]  
              op[c4d.PRIM_PLANE_HEIGHT]=bmsize[1]  
                
               
              op.Message(c4d.MSG_UPDATE)  
              #render the Bitmap if you like  
              #bitmaps.ShowBitmap(bitmap)  
        
      c4d.EventAdd()  
    


  • On 24/09/2014 at 11:26, xxxxxxxx wrote:

    i'll take a look at that tonight mate cheers..



  • On 25/09/2014 at 01:09, xxxxxxxx wrote:

    I added the code to a python tag and Cinema said the code ran without problem, but nothing happened to the dimensions of the plane. Did i miss something obvious?

    here is the C4d file..

    http://www.doghouseanimations.com/resizing test code.zip



  • On 25/09/2014 at 01:21, xxxxxxxx wrote:

    Just saw you edit fella.



  • On 25/09/2014 at 07:51, xxxxxxxx wrote:

    at the end!

    There are several places where your bitmap can be fund, according to the state of your document (saved or not)or your choice while loading the picture(make a copy or not).
    This script should work in any case.
    Let me know if not.
    cheers
    Martin

      
    import c4d,os  
    from c4d import gui,bitmaps,storage  
    from c4d.modules.render import  InitRenderStruct  
      
      
      
    def main() :  
        
      if op==None:return  
      #not a plane primitive  
      if op.GetType()!=5168:return  
        
        
      doc = c4d.documents.GetActiveDocument()  
      
      if op.GetTag(c4d.Ttexture)== None:  
          dialog=gui.MessageDialog("Please assign a material to the object",c4d.GEMB_OK)  
          if dialog== 1:  
              return  
      else:  
          TexTag=op.GetTag(c4d.Ttexture)     
             
      Mat= TexTag.GetMaterial()  
      Shader=Mat[c4d.MATERIAL_COLOR_SHADER]  
      if Shader.GetType() != c4d.Xbitmap:  
          dialog=gui.MessageDialog("Please insert a picture",c4d.GEMB_OK)  
          if dialog== 1:  
              return  
            
      bitmapPath = Shader[c4d.BITMAPSHADER_FILENAME]  
        
      #if bitmapPath is´nt absolute  
      if not os.path.dirname(bitmapPath) :  
          #the document has´nt been saved already ->picture in user library  
          if not doc.GetDocumentPath() :  
              abspath= c4d.storage.GeGetStartupWritePath()+"/"+"tex"+"/"+bitmapPath  
          #the picture should be inside the asset´s texture folder      
          else:  
              abspath= doc.GetDocumentPath()+"/"+"tex"+"/"+bitmapPath  
                
      else:  
          abspath = bitmapPath  
            
      print abspath   
           
      bmp = bitmaps.BaseBitmap()      
      if bmp.InitWith(abspath)[0]== c4d.IMAGERESULT_OK:  
          if bmp != None:  
              bmpsize     = bmp.GetSize()  
                
              op[c4d.PRIM_PLANE_WIDTH]=bmpsize[0]  
              op[c4d.PRIM_PLANE_HEIGHT]=bmpsize[1]  
                
               
              op.Message(c4d.MSG_UPDATE)  
              #render the Bitmap if you like  
              #bitmaps.ShowBitmap(bmp)  
        
      c4d.EventAdd()   
      
    


  • On 25/09/2014 at 17:31, xxxxxxxx wrote:

    Thanks for sticking in here with me.

    No joy. Maybe i'm doing something wrong. I dropped this code in a python tag on a plane object with a texture attached. Should that have worked?!

    Am i doing something wrong?



  • On 25/09/2014 at 17:46, xxxxxxxx wrote:

    Yes, if you use a Python tag, than op is the Python tag and not the assigned object.
    You have to use obj= op.GetObject() instead.
    Have fun!
    Martin

    like:

      
    import c4d,os  
    from c4d import gui,bitmaps,storage  
      
      
      
      
    def main() :  
      obj= op.GetObject()  
      print obj  
      if obj==None:return  
      #not a plane primitive  
      if obj.GetType()!=5168:return  
        
        
      doc = c4d.documents.GetActiveDocument()  
      
      if obj.GetTag(c4d.Ttexture)== None:  
          dialog=gui.MessageDialog("Please assign a material to the object",c4d.GEMB_OK)  
          if dialog== 1:  
              return  
      else:  
          TexTag=obj.GetTag(c4d.Ttexture)     
             
      Mat= TexTag.GetMaterial()  
      Shader=Mat[c4d.MATERIAL_COLOR_SHADER]  
      print Shader  
      if Shader.GetType() != c4d.Xbitmap:  
          dialog=gui.MessageDialog("Please insert a picture",c4d.GEMB_OK)  
          if dialog== 1:  
              return  
            
      bitmapPath = Shader[c4d.BITMAPSHADER_FILENAME]  
        
      #if bitmapPath is´nt absolute  
      if not os.path.dirname(bitmapPath) :  
          #the document has´nt been saved already ->picture in user library  
          if not doc.GetDocumentPath() :  
              abspath= c4d.storage.GeGetStartupWritePath()+"/"+"tex"+"/"+bitmapPath  
          #the picture should be inside the asset´s texture folder      
          else:  
              abspath= doc.GetDocumentPath()+"/"+"tex"+"/"+bitmapPath  
                
      else:  
          abspath = bitmapPath  
            
      print abspath   
           
      bmp = bitmaps.BaseBitmap()      
      if bmp.InitWith(abspath)[0]== c4d.IMAGERESULT_OK:  
          if bmp != None:  
              bmpsize     = bmp.GetSize()  
                
              obj[c4d.PRIM_PLANE_WIDTH]=bmpsize[0]  
              obj[c4d.PRIM_PLANE_HEIGHT]=bmpsize[1]  
                
               
              obj.Message(c4d.MSG_UPDATE)  
              #render the Bitmap if you like  
              #bitmaps.ShowBitmap(bmp)  
      
        
    if __name__=='__main__':  
      main()  
    

    p.s.
    Don´t use EventAdd() than;)



  • On 28/09/2014 at 09:51, xxxxxxxx wrote:

    I can't get this working, i'm going to try again tonight!?



  • On 28/09/2014 at 10:54, xxxxxxxx wrote:

    Hi,

    be sure to use a plane primitive!!

    At the beginning of the script we exclude any other objects even a converted plane.

    like

        #not a plane primitive  
      if obj.GetType()!=5168:return
    

    because at the end of the script we want to assign the bitmap size to the primitiv values.

                obj[c4d.PRIM_PLANE_WIDTH]=bmpsize[0]  
              obj[c4d.PRIM_PLANE_HEIGHT]=bmpsize[1]
    

    If this doesn´t work, post a scene file.

    cheers
    Martin



  • On 28/09/2014 at 16:34, xxxxxxxx wrote:

    I've tried a couple of ways of applying the code but i can't get it working!?

    I have been using a plane and i have tired many ways of applying the texture, still no joy.

    http://www.doghouseanimations.com/resizing test code 3.zip

    thanks again for your help..



  • On 28/09/2014 at 16:39, xxxxxxxx wrote:

    sorry here is the hyperlink..

    http://www.doghouseanimations.com/resizing test code 3.zip



  • On 28/09/2014 at 17:35, xxxxxxxx wrote:

    ok,

    you did not use a regular project structure.
    The Bitmap is only inside the project folder, but not in a separate "tex" folder like cinema4d usually set it up by default.

    just create a new folder inside your project folder, name it tex, drag the picture into it and open the scene. works fine!

    or

    open the scene and safe it again including the asset. works fine!

    or

    There is a Texture Manager inside cinema4d which shows you every path of every texture in your scene
    Adapt the script to your special needs, if you like
    Please try to understand those lines by searching the sdk.:

    abspath= c4d.storage.GeGetStartupWritePath()+"/"+"tex"+"/"+bitmapPath
    

    and especially

    abspath= doc.GetDocumentPath()+"/"+"tex"+"/"+bitmapPath
    

    I implemented a print statement for the abspath already, that you can read it in the console window.

    cheers
    Martin



  • On 28/09/2014 at 17:56, xxxxxxxx wrote:

    That rocks, you are truly a superstar. I can't believe i missed that!

    If your ever in Amsterdam there is a beer with your name on it..



  • On 29/09/2014 at 06:53, xxxxxxxx wrote:

    you´re welcome.

    I still just stumble through the code.
    But I´ll take you up on the beer.

    cheers
    Martin


Log in to reply