# Copyright 2009 Autodesk, Inc.  All rights reserved.
# Use of this software is subject to the terms of the Autodesk license agreement 
# provided at the time of installation or download, or which otherwise accompanies
# this software in either electronic or hard copy form.
#
# Script description:
# Shows new function to find objects with name pattern
#
# Topic: FBFindObjectsByName, FBFindObjectsByNamespace
#


from pyfbsdk import *

import random

# constant
SEARCH_INCLUDE_NAMESPACE = True
SEARCH_ALL_OBJECTS = False

def CreateDatas():
    """ This function creates dummy datas with namespace"""

    cubenames = ["ns:Cube", "ns:Cube is a troubled cube", "ns:ns1:My Cube of Doom", "ns:this is a ns:ns1:Cube", "Cube ends with be"]

    for n in cubenames:
        c = FBModelCube("dummy")
        c.LongName = n
        c.Visible = True
        c.Show = True
        c.Translation = FBVector3d(random.random()*50, random.random()*50, random.random()*50)

    t = FBTexture("texture")
    t.LongName = "my text ns:texture of doom"

    t = FBTexture("texture")
    t.LongName = "my text ns:texture doesn't end with be"

    m = FBMaterial("material")
    m.LongName = "special ns:material"


def FindWithWildcard(pattern, byName):
    """ This function finds an object (not just a model)
        with a particular pattern in its name.        
    """

    cl = FBComponentList()
    if byName:
        # First param is a pattern
        # second param is a boolean saying if the search must be made in complete name (including namespace)
        # third param is a boolean. If True: we search for models only, if False, we search for all objects
        # including textures, materials, etc...
        FBFindObjectsByName( pattern, cl, SEARCH_INCLUDE_NAMESPACE, SEARCH_ALL_OBJECTS )
    else:
        # This function search objects by using their namespace only.
        FBFindObjectsByNamespace( pattern, cl )

    if len(cl) > 0:
        print len(cl), " objects found matching pattern", pattern
        for o in cl:
            print "    ", o.LongName
    else:
        print "No object found matching pattern", pattern

CreateDatas()

# Pattern description:
# Currently our patterns only support *
# you can have as many * in you pattern
# the * is good for as much character as you like.

BY_NAME = True
# Find all objects whose name begins with "ns"
FindWithWildcard( "ns*", BY_NAME )

# Find all objects whose name ends with "be"
FindWithWildcard( "*be", BY_NAME )

# Find all objects whose name contains "Cube"
FindWithWildcard( "*Cube*", BY_NAME )

# Find all objects whose name contains token: "Cube" and "be" in this order
FindWithWildcard( "*Cube*be*", BY_NAME )

BY_NAMESPACE = False

# find all objects whose namespace begins with "ns"
FindWithWildcard( "ns*", BY_NAMESPACE )

# find all objects whose namespace ends with "ns"
FindWithWildcard( "*ns", BY_NAMESPACE )

# Find all objects whose name contains tokens: "n", "ns" and "s1" in this order
FindWithWildcard( "*n*ns*s1", BY_NAMESPACE )