Newer
Older
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
# ##### BEGIN GPL LICENSE BLOCK #####
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software Foundation,
# Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
#
# ##### END GPL LICENSE BLOCK #####
# <pep8 compliant>
# Contributors: bart:neeneenee*de, http://www.neeneenee.de/vrml, Campbell Barton
"""
This script exports to X3D format.
Usage:
Run this script from "File->Export" menu. A pop-up will ask whether you
want to export only selected or all relevant objects.
Known issues:
Doesn't handle multiple materials (don't use material indices);<br>
Doesn't handle multiple UV textures on a single mesh (create a mesh for each texture);<br>
Can't get the texture array associated with material * not the UV ones;
"""
import math
import os
import bpy
import mathutils
Campbell Barton
committed
from bpy_extras.io_utils import create_derived_objects, free_derived_objects
x3d_names_reserved = {'Anchor', 'Appearance', 'Arc2D', 'ArcClose2D', 'AudioClip', 'Background', 'Billboard',
'BooleanFilter', 'BooleanSequencer', 'BooleanToggle', 'BooleanTrigger', 'Box', 'Circle2D',
'Collision', 'Color', 'ColorInterpolator', 'ColorRGBA', 'component', 'Cone', 'connect',
'Contour2D', 'ContourPolyline2D', 'Coordinate', 'CoordinateDouble', 'CoordinateInterpolator',
'CoordinateInterpolator2D', 'Cylinder', 'CylinderSensor', 'DirectionalLight', 'Disk2D',
'ElevationGrid', 'EspduTransform', 'EXPORT', 'ExternProtoDeclare', 'Extrusion', 'field',
'fieldValue', 'FillProperties', 'Fog', 'FontStyle', 'GeoCoordinate', 'GeoElevationGrid',
'GeoLocationLocation', 'GeoLOD', 'GeoMetadata', 'GeoOrigin', 'GeoPositionInterpolator',
'GeoTouchSensor', 'GeoViewpoint', 'Group', 'HAnimDisplacer', 'HAnimHumanoid', 'HAnimJoint',
'HAnimSegment', 'HAnimSite', 'head', 'ImageTexture', 'IMPORT', 'IndexedFaceSet',
'IndexedLineSet', 'IndexedTriangleFanSet', 'IndexedTriangleSet', 'IndexedTriangleStripSet',
'Inline', 'IntegerSequencer', 'IntegerTrigger', 'IS', 'KeySensor', 'LineProperties', 'LineSet',
'LoadSensor', 'LOD', 'Material', 'meta', 'MetadataDouble', 'MetadataFloat', 'MetadataInteger',
'MetadataSet', 'MetadataString', 'MovieTexture', 'MultiTexture', 'MultiTextureCoordinate',
'MultiTextureTransform', 'NavigationInfo', 'Normal', 'NormalInterpolator', 'NurbsCurve',
'NurbsCurve2D', 'NurbsOrientationInterpolator', 'NurbsPatchSurface',
'NurbsPositionInterpolator', 'NurbsSet', 'NurbsSurfaceInterpolator', 'NurbsSweptSurface',
'NurbsSwungSurface', 'NurbsTextureCoordinate', 'NurbsTrimmedSurface', 'OrientationInterpolator',
'PixelTexture', 'PlaneSensor', 'PointLight', 'PointSet', 'Polyline2D', 'Polypoint2D',
'PositionInterpolator', 'PositionInterpolator2D', 'ProtoBody', 'ProtoDeclare', 'ProtoInstance',
'ProtoInterface', 'ProximitySensor', 'ReceiverPdu', 'Rectangle2D', 'ROUTE', 'ScalarInterpolator',
'Scene', 'Script', 'Shape', 'SignalPdu', 'Sound', 'Sphere', 'SphereSensor', 'SpotLight', 'StaticGroup',
'StringSensor', 'Switch', 'Text', 'TextureBackground', 'TextureCoordinate', 'TextureCoordinateGenerator',
'TextureTransform', 'TimeSensor', 'TimeTrigger', 'TouchSensor', 'Transform', 'TransmitterPdu',
'TriangleFanSet', 'TriangleSet', 'TriangleSet2D', 'TriangleStripSet', 'Viewpoint', 'VisibilitySensor',
'WorldInfo', 'X3D', 'XvlShell', 'VertexShader', 'FragmentShader', 'MultiShaderAppearance', 'ShaderAppearance'}
# h3d defines
H3D_TOP_LEVEL = 'TOP_LEVEL_TI'
H3D_CAMERA_FOLLOW = 'CAMERA_FOLLOW_TRANSFORM'
H3D_VIEW_MATRIX = 'view_matrix'
def clamp_color(col):
return tuple([max(min(c, 1.0), 0.0) for c in col])
Campbell Barton
committed
def matrix_direction_neg_z(matrix):
return (matrix.to_3x3() * mathutils.Vector((0.0, 0.0, -1.0))).normalized()[:]
Campbell Barton
committed
def prefix_quoted_str(value, prefix):
return value[0] + prefix + value[1:]
def suffix_quoted_str(value, suffix):
return value[:-1] + suffix + value[-1:]
Campbell Barton
committed
def clean_def(txt):
Campbell Barton
committed
# see report [#28256]
Campbell Barton
committed
if not txt:
txt = "None"
Campbell Barton
committed
# no digit start
Campbell Barton
committed
if txt[0] in "1234567890+-":
txt = "_" + txt
return txt.translate({ # control characters 0x0-0x1f
Campbell Barton
committed
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
0x01: "_",
0x02: "_",
0x03: "_",
0x04: "_",
0x05: "_",
0x06: "_",
0x07: "_",
0x08: "_",
0x09: "_",
0x0a: "_",
0x0b: "_",
0x0c: "_",
0x0d: "_",
0x0e: "_",
0x0f: "_",
0x10: "_",
0x11: "_",
0x12: "_",
0x13: "_",
0x14: "_",
0x15: "_",
0x16: "_",
0x17: "_",
0x18: "_",
0x19: "_",
0x1a: "_",
0x1b: "_",
0x1c: "_",
0x1d: "_",
0x1e: "_",
0x1f: "_",
0x7f: "_", # 127
0x20: "_", # space
0x22: "_", # "
0x27: "_", # '
0x23: "_", # #
0x2c: "_", # ,
0x2e: "_", # .
0x5b: "_", # [
0x5d: "_", # ]
0x5c: "_", # \
0x7b: "_", # {
0x7d: "_", # }
Campbell Barton
committed
})
def build_hierarchy(objects):
"""
objects_set = set(objects)
par_lookup = {}
def test_parent(parent):
while (parent is not None) and (parent not in objects_set):
parent = parent.parent
return parent
par_lookup.setdefault(test_parent(obj.parent), []).append((obj, []))
for parent, children in par_lookup.items():
for obj, subchildren in children:
subchildren[:] = par_lookup.get(obj, [])
# -----------------------------------------------------------------------------
# H3D Functions
# -----------------------------------------------------------------------------
def h3d_shader_glsl_frag_patch(filepath, scene, global_vars, frag_uniform_var_map):
h3d_file = open(filepath, 'r', encoding='utf-8')
last_transform = None
if l.startswith("void main(void)"):
lines.append("\n")
lines.append("// h3d custom vars begin\n")
for v in global_vars:
lines.append("%s\n" % v)
lines.append("// h3d custom vars end\n")
lines.append("\n")
elif l.lstrip().startswith("lamp_visibility_other("):
w = l.split(', ')
last_transform = w[1] + "_transform" # XXX - HACK!!!
w[1] = '(view_matrix * %s_transform * vec4(%s.x, %s.y, %s.z, 1.0)).xyz' % (w[1], w[1], w[1], w[1])
l = ", ".join(w)
elif l.lstrip().startswith("lamp_visibility_sun_hemi("):
w = l.split(', ')
w[0] = w[0][len("lamp_visibility_sun_hemi(") + 1:]
if not h3d_is_object_view(scene, frag_uniform_var_map[w[0]]):
w[0] = '(mat3(normalize(view_matrix[0].xyz), normalize(view_matrix[1].xyz), normalize(view_matrix[2].xyz)) * -%s)' % w[0]
else:
w[0] = ('(mat3(normalize((view_matrix*%s)[0].xyz), normalize((view_matrix*%s)[1].xyz), normalize((view_matrix*%s)[2].xyz)) * -%s)' %
Loading
Loading full blame...