Advertisement
nux95

Cinema 4D - Texture Import Script 1.0

Sep 21st, 2012
1,866
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.65 KB | None | 0 0
  1. # coding: UTF-8
  2. #
  3. # Copyright (C) 2012, Niklas Rosenstein
  4.  
  5. import os
  6. import sys
  7. import glob
  8. import c4d
  9.  
  10. # Default information.
  11. DEFAULT_GLOB = '*.tif; *.tiff'
  12.  
  13. def main():
  14.     # Obtain the path to a directory used to obtain the filenames.
  15.     dirname = c4d.storage.LoadDialog(c4d.FILESELECTTYPE_ANYTHING,
  16.         "Select Directory", c4d.FILESELECT_DIRECTORY)
  17.  
  18.     # Check if the user has canceled the dialog.
  19.     if not dirname:
  20.         return False
  21.  
  22.     # The directory-path must end with a slash, so we will append it if it
  23.     # is not there.
  24.     if not dirname.endswith(os.sep):
  25.         dirname += os.sep
  26.  
  27.     # Obtain the glob-sequence that is used to match the filenames.
  28.     globseq = c4d.gui.InputDialog("GLOB Sequence", DEFAULT_GLOB)
  29.  
  30.     # The input is not valid if not character was entered (which is equal to
  31.     # pressing the Cancel button).
  32.     if not globseq:
  33.         return False
  34.  
  35.     # Obtain a list of glob-sequences from the user-input (seperated by
  36.     # semi-colons).
  37.     globseq = filter(lambda x: x, map(str.strip, globseq.split(';')))
  38.     if not globseq:
  39.         return False
  40.  
  41.     # Obtain a list of all file-names.
  42.     files = []
  43.     for g in globseq:
  44.         g = dirname + g
  45.         files.extend(glob.glob(g))
  46.  
  47.     # Create a material + plane for all files in the list. We pass *op* as root
  48.     # object for the planes, which is the current selected object. The order
  49.     # of iteration is reversed because the items are inserted from top-to-top
  50.     # by the `process_file` function, and that way, they would appear the other
  51.     # way round in the Object Manager.
  52.     for f in reversed(files):
  53.         print process_file(f, op)
  54.  
  55.     # Tell Cinema 4D to update the interface after the script was run.
  56.     c4d.EventAdd()
  57.     return True
  58.  
  59. def process_file(f, root=None):
  60.     """ Processes a file and creates a material and a plane for it. If *root*
  61.        is passed, it must be a `c4d.BaseObject` that will be used to insert
  62.        the planes. It returns a tuple of the created material and the
  63.        plane object. """
  64.     if not os.path.exists(f):
  65.         return None
  66.  
  67.     # Obtain the base-name of the file (which is relative to its
  68.     # parent-directory).
  69.     fbase = os.path.basename(f)
  70.  
  71.     # Create a material and fill it with information (its name, etc.)
  72.     mat = c4d.BaseMaterial(c4d.Mmaterial)
  73.     mat.SetName(fbase)
  74.     mat[c4d.MATERIAL_USE_ALPHA] = True
  75.  
  76.     # Create the shaders. Note that we cannot insert the shader twice, that is
  77.     # why we need to create a clone on the second assignment.
  78.     shader = c4d.BaseShader(c4d.Xbitmap)
  79.     shader[c4d.BITMAPSHADER_FILENAME] = f
  80.     mat[c4d.MATERIAL_COLOR_SHADER] = shader
  81.     mat.InsertShader(shader)
  82.     shader = shader.GetClone()
  83.     mat[c4d.MATERIAL_ALPHA_SHADER] = shader
  84.     mat.InsertShader(shader)
  85.  
  86.     # Load the image to read its dimensions.
  87.     bmp = c4d.bitmaps.BaseBitmap()
  88.     bmp.InitWith(f)
  89.     width, height = bmp.GetSize()
  90.  
  91.     # Create a plane-object, resize it and assign the material to it.
  92.     plane = c4d.BaseObject(c4d.Oplane)
  93.     plane.SetName(fbase)
  94.     plane[c4d.PRIM_PLANE_WIDTH] = width
  95.     plane[c4d.PRIM_PLANE_HEIGHT] = height
  96.     tex = plane.MakeTag(c4d.Ttexture)
  97.     tex[c4d.TEXTURETAG_MATERIAL] = mat
  98.     tex[c4d.TEXTURETAG_PROJECTION] = c4d.TEXTURETAG_PROJECTION_UVW
  99.  
  100.     # Insert the material and the plane into the document.
  101.     doc.InsertObject(plane, root)
  102.     doc.AddUndo(c4d.UNDOTYPE_NEW, plane)
  103.     doc.InsertMaterial(mat)
  104.     doc.AddUndo(c4d.UNDOTYPE_NEW, mat)
  105.  
  106.     # Return the references to the material and plane-object.
  107.     return (mat, plane)
  108.  
  109. doc.StartUndo()
  110. main()
  111. doc.EndUndo()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement