f7b2e23cb50abfd34bfe1537738a9ccd863d1a0b
[OpenColorIO-Configs.git] / aces_1.0.0 / python / aces_ocio / util.py
1 #!/usr/bin/env python
2 # -*- coding: utf-8 -*-
3
4 import os
5 import re
6
7 import PyOpenColorIO as OCIO
8
9 __author__ = 'ACES Developers'
10 __copyright__ = 'Copyright (C) 2014 - 2015 - ACES Developers'
11 __license__ = ''
12 __maintainer__ = 'ACES Developers'
13 __email__ = 'aces@oscars.org'
14 __status__ = 'Production'
15
16 __all__ = ['ColorSpace',
17            'mat44FromMat33',
18            'filter_words',
19            'files_walker']
20
21 #
22 # Utility classes and functions
23 #
24
25 class ColorSpace:
26     """
27     A container for data needed to define an OCIO 'Color Space'
28     """
29
30     def __init__(self,
31                  name,
32                  description=None,
33                  bitDepth=OCIO.Constants.BIT_DEPTH_F32,
34                  equalityGroup=None,
35                  family=None,
36                  isData=False,
37                  toReferenceTransforms=[],
38                  fromReferenceTransforms=[],
39                  allocationType=OCIO.Constants.ALLOCATION_UNIFORM,
40                  allocationVars=[0.0, 1.0]):
41         """
42         Object description.
43
44         Parameters
45         ----------
46         parameter : type
47             Parameter description.
48
49         Returns
50         -------
51         type
52              Return value description.
53         """
54
55         self.name = name
56         self.bitDepth = bitDepth
57         self.description = description
58         self.equalityGroup = equalityGroup
59         self.family = family
60         self.isData = isData
61         self.toReferenceTransforms = toReferenceTransforms
62         self.fromReferenceTransforms = fromReferenceTransforms
63         self.allocationType = allocationType
64         self.allocationVars = allocationVars
65
66
67 def mat44FromMat33(mat33):
68     """
69     Creates a 4x4 matrix from given 3x3 matrix.
70
71     Parameters
72     ----------
73     parameter : type
74         Parameter description.
75
76     Returns
77     -------
78     type
79          Return value description.
80     """
81
82     return [mat33[0], mat33[1], mat33[2], 0.0,
83             mat33[3], mat33[4], mat33[5], 0.0,
84             mat33[6], mat33[7], mat33[8], 0.0,
85             0, 0, 0, 1.0]
86
87
88 def filter_words(words, filters_in=None, filters_out=None, flags=0):
89     """
90     Object description.
91
92     Parameters
93     ----------
94     parameter : type
95         Parameter description.
96
97     Returns
98     -------
99     type
100          Return value description.
101     """
102
103     filtered_words = []
104     for word in words:
105         if filters_in:
106             filter_matched = False
107             for filter in filters_in:
108                 if re.search(filter, word, flags):
109                     filter_matched = True
110                     break
111             if not filter_matched:
112                 continue
113
114         if filters_out:
115             filter_matched = False
116             for filter in filters_out:
117                 if re.search(filter, word, flags):
118                     filter_matched = True
119                     break
120             if filter_matched:
121                 continue
122         filtered_words.append(word)
123     return filtered_words
124
125
126 def files_walker(directory, filters_in=None, filters_out=None, flags=0):
127     """
128     Object description.
129
130     Parameters
131     ----------
132     parameter : type
133         Parameter description.
134
135     Returns
136     -------
137     type
138          Return value description.
139     """
140
141     for parent_directory, directories, files in os.walk(directory,
142                                                         topdown=False,
143                                                         followlinks=True):
144         for file in files:
145             path = os.path.join(parent_directory, file)
146             if os.path.isfile(path):
147                 if not filter_words((path,), filters_in, filters_out, flags):
148                     continue
149
150                 yield path