Plugin Cafe Homepage
Forum Home Forum Home > Plugin Cafe > PYTHON Development
  New Posts New Posts
  FAQ FAQ  Forum Search

Scaling 2d planes to their texture..

Author
Message
  Topic Search Topic Search
mikeb View Drop Down
Member
Member
Avatar

Joined: 2014 Sep 24
Location: Amsterdam
Online Status: Offline
Posts: 9
Direct Link To This Post Topic: Scaling 2d planes to their texture..
    Posted: 2014 Sep 24 at 6:33am
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..
Back to Top
monkeytack View Drop Down
Member
Member


Joined: 2014 Apr 14
Location: Germany
Online Status: Offline
Posts: 283
Direct Link To This Post Posted: 2014 Sep 24 at 7:23am
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()



Edited by monkeytack - 2014 Sep 25 at 1:03am
Back to Top
mikeb View Drop Down
Member
Member
Avatar

Joined: 2014 Sep 24
Location: Amsterdam
Online Status: Offline
Posts: 9
Direct Link To This Post Posted: 2014 Sep 24 at 11:26am
i'll take a look at that tonight mate cheers..
Back to Top
mikeb View Drop Down
Member
Member
Avatar

Joined: 2014 Sep 24
Location: Amsterdam
Online Status: Offline
Posts: 9
Direct Link To This Post Posted: 2014 Sep 25 at 1:09am
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%20test%20code.zip
Back to Top
mikeb View Drop Down
Member
Member
Avatar

Joined: 2014 Sep 24
Location: Amsterdam
Online Status: Offline
Posts: 9
Direct Link To This Post Posted: 2014 Sep 25 at 1:21am
Just saw you edit fella.
Back to Top
monkeytack View Drop Down
Member
Member


Joined: 2014 Apr 14
Location: Germany
Online Status: Offline
Posts: 283
Direct Link To This Post Posted: 2014 Sep 25 at 7:51am
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()


Back to Top
mikeb View Drop Down
Member
Member
Avatar

Joined: 2014 Sep 24
Location: Amsterdam
Online Status: Offline
Posts: 9
Direct Link To This Post Posted: 2014 Sep 25 at 5:31pm
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?
Back to Top
monkeytack View Drop Down
Member
Member


Joined: 2014 Apr 14
Location: Germany
Online Status: Offline
Posts: 283
Direct Link To This Post Posted: 2014 Sep 25 at 5:46pm
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;)



Back to Top
mikeb View Drop Down
Member
Member
Avatar

Joined: 2014 Sep 24
Location: Amsterdam
Online Status: Offline
Posts: 9
Direct Link To This Post Posted: 2014 Sep 28 at 9:51am
I can't get this working, i'm going to try again tonight!?
Back to Top
monkeytack View Drop Down
Member
Member


Joined: 2014 Apr 14
Location: Germany
Online Status: Offline
Posts: 283
Direct Link To This Post Posted: 2014 Sep 28 at 10:54am
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
Back to Top
mikeb View Drop Down
Member
Member
Avatar

Joined: 2014 Sep 24
Location: Amsterdam
Online Status: Offline
Posts: 9
Direct Link To This Post Posted: 2014 Sep 28 at 4:34pm
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%20test%20code%203.zip

thanks again for your help..
Back to Top
mikeb View Drop Down
Member
Member
Avatar

Joined: 2014 Sep 24
Location: Amsterdam
Online Status: Offline
Posts: 9
Direct Link To This Post Posted: 2014 Sep 28 at 4:39pm
Back to Top
monkeytack View Drop Down
Member
Member


Joined: 2014 Apr 14
Location: Germany
Online Status: Offline
Posts: 283
Direct Link To This Post Posted: 2014 Sep 28 at 5:35pm
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


Back to Top
mikeb View Drop Down
Member
Member
Avatar

Joined: 2014 Sep 24
Location: Amsterdam
Online Status: Offline
Posts: 9
Direct Link To This Post Posted: 2014 Sep 28 at 5:56pm
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..
Back to Top
monkeytack View Drop Down
Member
Member


Joined: 2014 Apr 14
Location: Germany
Online Status: Offline
Posts: 283
Direct Link To This Post Posted: 2014 Sep 29 at 6:53am
you´re welcome.

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

cheers
Martin
Back to Top

Forum Jump Forum Permissions View Drop Down

Bulletin Board Software by Web Wiz Forums® version 9.61 [Free Express Edition]
Copyright ©2001-2009 Web Wiz

This page was generated in 0.109 seconds.