BasicOperations/FindObjectsWithWildcard.py

BasicOperations/FindObjectsWithWildcard.py
1 # Copyright 2009 Autodesk, Inc. All rights reserved.
2 # Use of this software is subject to the terms of the Autodesk license agreement
3 # provided at the time of installation or download, or which otherwise accompanies
4 # this software in either electronic or hard copy form.
5 #
6 # Script description:
7 # Shows new function to find objects with name pattern
8 #
9 # Topic: FBFindObjectsByName
10 #
11 
12 
13 from pyfbsdk import *
14 
15 import random
16 
17 # constant
18 SEARCH_MODELS_ONLY = False
19 
20 def CreateDatas():
21  """ This function creates dummy datas with namespace"""
22 
23  FBApplication().FileNew()
24 
25  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"]
26 
27  for n in cubenames:
28  c = FBModelCube("dummy")
29  c.LongName = n
30  c.Visible = True
31  c.Show = True
32  c.Translation = FBVector3d(random.random()*50, random.random()*50, random.random()*50)
33 
34  t = FBTexture("texture")
35  t.LongName = "my text ns:texture of doom"
36 
37  t = FBTexture("texture")
38  t.LongName = "my text ns:texture doesn't end with be"
39 
40  m = FBMaterial("material")
41  m.LongName = "special ns:material"
42 
43 
44 def FindWithWildcard(pattern, byShortName):
45  """ This function finds an object (not just a model)
46  with a particular pattern in its name.
47  """
48 
49  cl = FBComponentList()
50  if byShortName:
51  # First param is a pattern
52  # second param is a boolean saying if the search must be made in LongName (including namespace)
53  # third param is a boolean. If True: we search for models only, if False, we search for all objects
54  # including textures, materials, etc...
55  FBFindObjectsByName( pattern, cl, False, SEARCH_MODELS_ONLY )
56  else:
57  # This function search objects by including their namespace part (called LabelName, or LongName)
58  FBFindObjectsByName( pattern, cl, True, SEARCH_MODELS_ONLY )
59 
60  if byShortName:
61  print len(cl), "objects found matching ShortName pattern: \"" + pattern + "\""
62  else:
63  print len(cl), "objects found matching LongName pattern: \"" + pattern + "\""
64 
65  for o in cl:
66  print " ", o.FullName
67 
68 CreateDatas()
69 
70 # Pattern description:
71 # Currently our patterns only support *
72 # you can have as many * in you pattern
73 # the * is good for as much character as you like.
74 
75 BY_SHORT_NAME = True
76 # Find all objects whose name arbitrarily, match for all
77 FindWithWildcard( "*", BY_SHORT_NAME )
78 
79 # Find all objects whose short name begins with "ns"
80 FindWithWildcard( "ns*", BY_SHORT_NAME )
81 
82 # Find all objects whose short name ends with "be"
83 FindWithWildcard( "*be", BY_SHORT_NAME )
84 
85 # Find all objects whose short name contains "Cube"
86 FindWithWildcard( "*Cube*", BY_SHORT_NAME )
87 
88 # Find all objects whose short name contains token: "Cube" and "be" in this order
89 FindWithWildcard( "*Cube*be*", BY_SHORT_NAME )
90 
91 BY_SHORT_NAME = False
92 
93 # find all objects whose long name begins with "ns"
94 FindWithWildcard( "ns*", BY_SHORT_NAME )
95 
96 # find all objects whose long name ends with "ns"
97 FindWithWildcard( "*ns", BY_SHORT_NAME )
98 
99 # Find all objects whose long name contains tokens: "n", "ns" and "s1" in this order
100 FindWithWildcard( "*n*ns*s1", BY_SHORT_NAME )