Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # coding: UTF-8
- #
- # Copyright (C) 2012, Niklas Rosenstein
- import os
- import sys
- import glob
- import c4d
- # Default information.
- DEFAULT_GLOB = '*.tif; *.tiff'
- def main():
- # Obtain the path to a directory used to obtain the filenames.
- dirname = c4d.storage.LoadDialog(c4d.FILESELECTTYPE_ANYTHING,
- "Select Directory", c4d.FILESELECT_DIRECTORY)
- # Check if the user has canceled the dialog.
- if not dirname:
- return False
- # The directory-path must end with a slash, so we will append it if it
- # is not there.
- if not dirname.endswith(os.sep):
- dirname += os.sep
- # Obtain the glob-sequence that is used to match the filenames.
- globseq = c4d.gui.InputDialog("GLOB Sequence", DEFAULT_GLOB)
- # The input is not valid if not character was entered (which is equal to
- # pressing the Cancel button).
- if not globseq:
- return False
- # Obtain a list of glob-sequences from the user-input (seperated by
- # semi-colons).
- globseq = filter(lambda x: x, map(str.strip, globseq.split(';')))
- if not globseq:
- return False
- # Obtain a list of all file-names.
- files = []
- for g in globseq:
- g = dirname + g
- files.extend(glob.glob(g))
- # Create a material + plane for all files in the list. We pass *op* as root
- # object for the planes, which is the current selected object. The order
- # of iteration is reversed because the items are inserted from top-to-top
- # by the `process_file` function, and that way, they would appear the other
- # way round in the Object Manager.
- for f in reversed(files):
- print process_file(f, op)
- # Tell Cinema 4D to update the interface after the script was run.
- c4d.EventAdd()
- return True
- def process_file(f, root=None):
- """ Processes a file and creates a material and a plane for it. If *root*
- is passed, it must be a `c4d.BaseObject` that will be used to insert
- the planes. It returns a tuple of the created material and the
- plane object. """
- if not os.path.exists(f):
- return None
- # Obtain the base-name of the file (which is relative to its
- # parent-directory).
- fbase = os.path.basename(f)
- # Create a material and fill it with information (its name, etc.)
- mat = c4d.BaseMaterial(c4d.Mmaterial)
- mat.SetName(fbase)
- mat[c4d.MATERIAL_USE_ALPHA] = True
- # Create the shaders. Note that we cannot insert the shader twice, that is
- # why we need to create a clone on the second assignment.
- shader = c4d.BaseShader(c4d.Xbitmap)
- shader[c4d.BITMAPSHADER_FILENAME] = f
- mat[c4d.MATERIAL_COLOR_SHADER] = shader
- mat.InsertShader(shader)
- shader = shader.GetClone()
- mat[c4d.MATERIAL_ALPHA_SHADER] = shader
- mat.InsertShader(shader)
- # Load the image to read its dimensions.
- bmp = c4d.bitmaps.BaseBitmap()
- bmp.InitWith(f)
- width, height = bmp.GetSize()
- # Create a plane-object, resize it and assign the material to it.
- plane = c4d.BaseObject(c4d.Oplane)
- plane.SetName(fbase)
- plane[c4d.PRIM_PLANE_WIDTH] = width
- plane[c4d.PRIM_PLANE_HEIGHT] = height
- tex = plane.MakeTag(c4d.Ttexture)
- tex[c4d.TEXTURETAG_MATERIAL] = mat
- tex[c4d.TEXTURETAG_PROJECTION] = c4d.TEXTURETAG_PROJECTION_UVW
- # Insert the material and the plane into the document.
- doc.InsertObject(plane, root)
- doc.AddUndo(c4d.UNDOTYPE_NEW, plane)
- doc.InsertMaterial(mat)
- doc.AddUndo(c4d.UNDOTYPE_NEW, mat)
- # Return the references to the material and plane-object.
- return (mat, plane)
- doc.StartUndo()
- main()
- doc.EndUndo()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement