Newer
Older
filepath = image.filepath
filepath_full = bpy.path.abspath(filepath)
# collect image paths, can load multiple
Campbell Barton
committed
# [relative, name-only, absolute]
images = []
if bpy.path.is_subdir(filepath_full, dirname):
images.append(os.path.relpath(filepath_full, dirname))
images.append(os.path.basename(filepath_full))
Campbell Barton
committed
images.append(filepath_full)
images = [f.replace('\\', '/') for f in images]
images = [f for i, f in enumerate(images) if f not in images[:i]]
fw(ident_step + "url='%s' " % ' '.join(['"%s"' % f for f in images]))
Campbell Barton
committed
def writeBackground(ident, world):
Campbell Barton
committed
if world is None:
return
Campbell Barton
committed
# note, not re-used
world_id = unique_name(world, 'WO_' + world.name, uuid_cache_world, clean_func=quoteattr)
blending = world.use_sky_blend, world.use_sky_paper, world.use_sky_real
grd_triple = clamp_color(world.horizon_color)
sky_triple = clamp_color(world.zenith_color)
mix_triple = clamp_color((grd_triple[i] + sky_triple[i]) / 2.0 for i in range(3))
ident_step = ident + (' ' * (-len(ident) + \
fw('%s<Background ' % ident)))
Campbell Barton
committed
fw('DEF=%s\n' % world_id)
# No Skytype - just Hor color
if blending == (False, False, False):
fw(ident_step + 'groundColor="%.3g %.3g %.3g"\n' % grd_triple)
fw(ident_step + 'skyColor="%.3g %.3g %.3g"\n' % grd_triple)
# Blend Gradient
elif blending == (True, False, False):
fw(ident_step + 'groundColor="%.3g %.3g %.3g, %.3g %.3g %.3g"\n' % (grd_triple + mix_triple))
fw(ident_step + 'groundAngle="1.57, 1.57"\n')
fw(ident_step + 'skyColor="%.3g %.3g %.3g, %.3g %.3g %.3g"\n' % (sky_triple + mix_triple))
fw(ident_step + 'skyAngle="1.57, 1.57"\n')
# Blend+Real Gradient Inverse
elif blending == (True, False, True):
fw(ident_step + 'groundColor="%.3g %.3g %.3g, %.3g %.3g %.3g"\n' % (sky_triple + grd_triple))
fw(ident_step + 'groundAngle="1.57"\n')
fw(ident_step + 'skyColor="%.3g %.3g %.3g, %.3g %.3g %.3g, %.3g %.3g %.3g"\n' % (sky_triple + grd_triple + sky_triple))
fw(ident_step + 'skyAngle="1.57, 3.14159"\n')
# Paper - just Zen Color
elif blending == (False, False, True):
fw(ident_step + 'groundColor="%.3g %.3g %.3g"\n' % sky_triple)
fw(ident_step + 'skyColor="%.3g %.3g %.3g"\n' % sky_triple)
# Blend+Real+Paper - komplex gradient
elif blending == (True, True, True):
fw(ident_step + 'groundColor="%.3g %.3g %.3g, %.3g %.3g %.3g"\n' % (sky_triple + grd_triple))
fw(ident_step + 'groundAngle="1.57, 1.57"\n')
fw(ident_step + 'skyColor="%.3g %.3g %.3g, %.3g %.3g %.3g"\n' % (sky_triple + grd_triple))
fw(ident_step + 'skyAngle="1.57, 1.57"\n')
# Any Other two colors
else:
fw(ident_step + 'groundColor="%.3g %.3g %.3g"\n' % grd_triple)
fw(ident_step + 'skyColor="%.3g %.3g %.3g"\n' % sky_triple)
if tex.type == 'IMAGE' and tex.image:
namemat = tex.name
pic = tex.image
basename = os.path.basename(bpy.path.abspath(pic.filepath))
fw(ident_step + 'bottomUrl="%s"\n' % basename)
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
# -------------------------------------------------------------------------
# Export Object Hierarchy (recursively called)
# -------------------------------------------------------------------------
def export_object(ident, obj_main_parent, obj_main, obj_children):
world = scene.world
free, derived = create_derived_objects(scene, obj_main)
if derived is None:
return
if use_hierarchy:
obj_main_matrix_world = obj_main.matrix_world
if obj_main_parent:
obj_main_matrix = obj_main_parent.matrix_world.inverted() * obj_main_matrix_world
else:
obj_main_matrix = obj_main_matrix_world
obj_main_matrix_world_invert = obj_main_matrix_world.inverted()
obj_main_id = unique_name(obj_main, obj_main.name, uuid_cache_object, clean_func=quoteattr)
ident = writeTransform_begin(ident, obj_main_matrix if obj_main_parent else global_matrix * obj_main_matrix, obj_main_id)
for obj, obj_matrix in derived:
obj_type = obj.type
if use_hierarchy:
# make transform node relative
obj_matrix = obj_main_matrix_world_invert * obj_matrix
if obj_type == 'CAMERA':
writeViewpoint(ident, obj, obj_matrix, scene)
elif obj_type in ('MESH', 'CURVE', 'SURF', 'FONT'):
if (obj_type != 'MESH') or (use_apply_modifiers and obj.is_modified(scene, 'PREVIEW')):
try:
me = obj.to_mesh(scene, use_apply_modifiers, 'PREVIEW')
except:
me = None
else:
me = obj.data
if me is not None:
writeIndexedFaceSet(ident, obj, me, obj_matrix, world)
# free mesh created with create_mesh()
if me != obj.data:
bpy.data.meshes.remove(me)
elif obj_type == 'LAMP':
data = obj.data
datatype = data.type
if datatype == 'POINT':
writePointLight(ident, obj, obj_matrix, data, world)
elif datatype == 'SPOT':
writeSpotLight(ident, obj, obj_matrix, data, world)
elif datatype == 'SUN':
writeDirectionalLight(ident, obj, obj_matrix, data, world)
else:
writeDirectionalLight(ident, obj, obj_matrix, data, world)
else:
#print "Info: Ignoring [%s], object type [%s] not handle yet" % (object.name,object.getType)
pass
if free:
free_derived_objects(obj_main)
# ---------------------------------------------------------------------
# write out children recursively
# ---------------------------------------------------------------------
for obj_child, obj_child_children in obj_children:
export_object(ident, obj_main, obj_child, obj_child_children)
if use_hierarchy:
ident = writeTransform_end(ident)
# -------------------------------------------------------------------------
# Main Export Function
# -------------------------------------------------------------------------
# tag un-exported IDs
bpy.data.meshes.tag(False)
bpy.data.materials.tag(False)
bpy.data.images.tag(False)
print('Info: starting X3D export to %r...' % file.name)
ident = ''
ident = writeHeader(ident)
Campbell Barton
committed
writeNavigationInfo(ident, scene)
writeBackground(ident, world)
writeFog(ident, world)
Campbell Barton
committed
Campbell Barton
committed
if use_selection:
objects = [obj for obj in scene.objects if obj.is_visible(scene) and o.select]
Campbell Barton
committed
else:
objects = [obj for obj in scene.objects if obj.is_visible(scene)]
Campbell Barton
committed
if use_hierarchy:
objects_hierarchy = build_hierarchy(objects)
else:
objects_hierarchy = ((obj, []) for obj in objects)
for obj_main, obj_main_children in objects_hierarchy:
export_object(ident, None, obj_main, obj_main_children)
ident = writeFooter(ident)
# -------------------------------------------------------------------------
# global cleanup
# -------------------------------------------------------------------------
if use_h3d:
bpy.data.materials.remove(gpu_shader_dummy_mat)
print('Info: finished X3D export to %r' % file.name)
##########################################################
# Callbacks, needed before Main
##########################################################
def save(operator, context, filepath="",
use_selection=True,
use_apply_modifiers=False,
use_triangulate=False,
use_hierarchy=True,
global_matrix=None,
):
Campbell Barton
committed
bpy.path.ensure_ext(filepath, '.x3dz' if use_compress else '.x3d')
if bpy.ops.object.mode_set.poll():
bpy.ops.object.mode_set(mode='OBJECT')
try:
import gzip
file = gzip.open(filepath, 'w')
print('failed to import compression modules, exporting uncompressed')
filepath = filepath[:-1] # remove trailing z
if file is None:
if global_matrix is None:
global_matrix = mathutils.Matrix()
export(file,
global_matrix,
context.scene,
use_apply_modifiers=use_apply_modifiers,
use_selection=use_selection,
use_triangulate=use_triangulate,
use_normals=use_normals,
use_hierarchy=use_hierarchy,
return {'FINISHED'}