/
dupli.py
202 lines (155 loc) · 7.76 KB
/
dupli.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
#
# This source file is part of appleseed.
# Visit https://appleseedhq.net/ for additional information and resources.
#
# This software is released under the MIT license.
#
# Copyright (c) 2018 The appleseedhq Organization
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
# THE SOFTWARE.
#
import appleseed as asr
from .materials import MaterialTranslator
from .mesh import MeshTranslator
from .object import InstanceTranslator, ObjectKey
from .translator import Translator, ProjectExportMode
from ..logger import get_logger
logger = get_logger()
class DupliListBuilder(object):
'''
Helper class to make sure that the dupli list is cleared,
even in the presence of exceptions.
'''
def __init__(self, obj, scene, settings):
self.__obj = obj
self.__scene = scene
self.__settings = settings
def __enter__(self):
self.__obj.dupli_list_create(self.__scene, self.__settings)
def __exit__(self, type, value, traceback):
self.__obj.dupli_list_clear()
class DupliTranslator(Translator):
#
# Constructor.
#
def __init__(self, dupli, export_mode, asset_handler):
super(DupliTranslator, self).__init__(dupli, asset_handler)
self.__export_mode = export_mode
self.__settings = 'VIEWPORT' if self.__export_mode == ProjectExportMode.INTERACTIVE_RENDER else 'RENDER'
self.__object_translators = []
self.__material_translators = {}
self.__datablock_to_translator = {}
#
# Properties.
#
@property
def bl_dupli(self):
return self._bl_obj
#
# Entity translation.
#
def create_entities(self, scene):
logger.debug("Creating dupli assembly for dupli: %s" % self.appleseed_name)
self.__ass = asr.Assembly(self.appleseed_name, {})
self.__create_dupli_instances(self._bl_obj, scene, self.__settings)
def flush_entities(self, assembly):
logger.debug("Flushing dupli entities for dupli: %s" % self.bl_dupli.name)
for translator in self.__object_translators:
translator.flush_entities(self.__ass)
for translator in self.__material_translators:
self.__material_translators[translator].flush_entities(self.__ass)
ass_name = self._insert_entity_with_unique_name(
assembly.assemblies(),
self.__ass,
self.__ass.get_name())
self.__ass = assembly.assemblies().get_by_name(ass_name)
self.__ass_inst = asr.AssemblyInstance(ass_name + "_inst", {}, ass_name)
ass_inst_name = self._insert_entity_with_unique_name(
assembly.assembly_instances(),
self.__ass_inst,
self.__ass_inst.get_name())
def set_transform_key(self, scene, time, key_times):
self.__set_instance_transforms(self.bl_dupli, scene, time)
def set_deform_key(self, scene, time, key_times):
self.__set_instance_transforms(self.bl_dupli, scene, time)
def update(self, obj, scene):
if obj.is_updated_data:
logger.debug("Updating dupli instances")
# Recreate all the instances.
self.__clear_dupli_instances()
self.__create_dupli_instances(obj, scene, self.__settings)
current_frame = scene.frame_current + scene.frame_subframe
self.__set_instance_transforms(obj, scene, current_frame)
for translator in self.__object_translators:
translator.flush_entities(self.__ass)
else:
logger.debug("Updating dupli xforms")
# Update the transform of all the instances.
self.__update_instance_transforms(obj, scene, 0.0)
#
# Internal methods.
#
def __create_dupli_instances(self, obj, scene, settings):
with DupliListBuilder(obj, scene, settings):
for dupli in obj.dupli_list:
# Create translators for dupli instances.
if dupli.object.type == 'MESH':
obj_key = ObjectKey (dupli.object)
mesh_key = ObjectKey(dupli.object.data)
is_modified = dupli.object.is_modified(scene, 'RENDER')
if is_modified == False and mesh_key in self.__datablock_to_translator:
logger.debug("Creating instance translator for object %s, master obj: %s", obj_key, mesh_key)
master_translator = self.__datablock_to_translator[mesh_key]
self.__object_translators.append(InstanceTranslator(dupli.object, master_translator, self.asset_handler))
master_translator.add_instance()
else:
logger.debug("Creating mesh translator for object %s", obj_key)
translator = MeshTranslator(dupli.object,
self.__export_mode,
self.asset_handler)
self.__object_translators.append(translator)
for slot in dupli.object.material_slots:
mat = slot.material
mat_key = ObjectKey(mat)
if mat_key not in self.__material_translators:
logger.debug("Creating material translator for material %s", mat_key)
mat_translator = MaterialTranslator(mat, self.asset_handler)
self.__material_translators[mat_key] = mat_translator
mat_translator.create_entities(scene)
if not is_modified:
logger.debug("Saving translator for object %s in instance map", obj_key)
self.__datablock_to_translator[mesh_key] = translator
else:
logger.debug("Skipping unsupported dupli source object of type %s", dupli.object.type)
# Create dupli instances entities.
for translator in self.__object_translators:
translator.create_entities(scene)
current_frame = scene.frame_current + scene.frame_subframe
for translator in self.__object_translators:
translator.set_deform_key(scene, current_frame, {current_frame})
def __set_instance_transforms(self, obj, scene, time):
with DupliListBuilder(obj, scene, self.__settings):
for i, mesh in enumerate(self.__object_translators):
mesh.set_transform(time, obj.dupli_list[i].matrix)
def __update_instance_transforms(self, obj, scene, time):
with DupliListBuilder(obj, scene, self.__settings):
for i, mesh in enumerate(self.__object_translators):
mesh.update_transform(time, obj.dupli_list[i].matrix)
def __clear_dupli_instances(self):
self.__ass.clear()