BasicOperations/FindObjectsWithWildcard.py
 
 
 
BasicOperations/FindObjectsWithWildcard.py
# 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
#


from pyfbsdk import *

import random

# constant
SEARCH_MODELS_ONLY = False

def CreateDatas():
    """ This function creates dummy datas with namespace"""
    
    FBApplication().FileNew()
    
    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, byShortName):
    """ This function finds an object (not just a model)
        with a particular pattern in its name.        
    """
    
    cl = FBComponentList()    
    if byShortName:
        # First param is a pattern
        # second param is a boolean saying if the search must be made in LongName (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, False, SEARCH_MODELS_ONLY )
    else:
        # This function search objects by including their namespace part (called LabelName, or LongName)
        FBFindObjectsByName( pattern, cl, True, SEARCH_MODELS_ONLY )
        
    if byShortName:
        print len(cl), "objects found matching ShortName pattern", pattern
    else:
        print len(cl), "objects found matching LongName pattern", pattern
                                
    for o in cl:
        print "    ", o.FullName
        
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_SHORT_NAME = True
# Find all objects whose name arbitrarily, match for all
FindWithWildcard( "*",          BY_SHORT_NAME )

# Find all objects whose short name begins with "ns"
FindWithWildcard( "ns*",        BY_SHORT_NAME )

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

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

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

BY_SHORT_NAME = False

# find all objects whose long name begins with "ns"
FindWithWildcard( "ns*",        BY_SHORT_NAME )

# find all objects whose long name ends with "ns"
FindWithWildcard( "*ns",        BY_SHORT_NAME )

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