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'}
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 (mathutils.Vector((0.0, 0.0, -1.0)) * matrix.to_3x3()).normalized()[:]
Campbell Barton
committed
def prefix_quoted_str(value, prefix):
return value[0] + prefix + value[1:]
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, [])
return par_lookup[None]
# -----------------------------------------------------------------------------
# H3D Functions
# -----------------------------------------------------------------------------
def h3d_shader_glsl_frag_patch(filepath):
h3d_file = open(filepath, 'r')
lines = []
for l in h3d_file:
l = l.replace("uniform mat4 unfinvviewmat;", "")
l = l.replace("unfinvviewmat", "gl_ModelViewMatrixInverse")
'''
l = l.replace("varying vec3 varposition;", "")
l = l.replace("varposition", "gl_Vertex") # not needed int H3D
'''
#l = l.replace("varying vec3 varnormal;", "")
#l = l.replace("varnormal", "gl_Normal") # view normal
#l = l.replace("varnormal", "normalize(-(gl_ModelViewMatrix * gl_Vertex).xyz)") # view normal
# l = l.replace("varnormal", "gl_NormalMatrix * gl_Normal") # view normal
lines.append(l)
h3d_file.close()
h3d_file = open(filepath, 'w')
h3d_file.writelines(lines)
h3d_file.close()
# -----------------------------------------------------------------------------
# Functions for writing output file
# -----------------------------------------------------------------------------
def export(file,
global_matrix,
scene,
use_apply_modifiers=False,
use_selection=True,
use_triangulate=False,
use_normals=False,
use_hierarchy=True,
Campbell Barton
committed
# -------------------------------------------------------------------------
Campbell Barton
committed
# Global Setup
# -------------------------------------------------------------------------
Campbell Barton
committed
from bpy_extras.io_utils import unique_name
from xml.sax.saxutils import quoteattr
uuid_cache_object = {} # object
Campbell Barton
committed
uuid_cache_view = {} # object, different namespace
uuid_cache_mesh = {} # mesh
uuid_cache_material = {} # material
uuid_cache_image = {} # image
uuid_cache_world = {} # world
# store files to copy
copy_set = set()
base_src = os.path.dirname(bpy.data.filepath)
base_dst = os.path.dirname(file.name)
filename_strip = os.path.splitext(os.path.basename(file.name))[0]
gpu_shader_cache = {}
if use_h3d:
import gpu
gpu_shader_dummy_mat = bpy.data.materials.new('X3D_DYMMY_MAT')
gpu_shader_cache[None] = gpu.export_shader(scene, gpu_shader_dummy_mat)
Campbell Barton
committed
# -------------------------------------------------------------------------
# File Writing Functions
# -------------------------------------------------------------------------
def writeHeader(ident):
Campbell Barton
committed
filepath_quoted = quoteattr(os.path.basename(file.name))
blender_ver_quoted = quoteattr('Blender %s' % bpy.app.version_string)
fw('%s<?xml version="1.0" encoding="UTF-8"?>\n' % ident)
fw('%s<X3D profile="H3DAPI" version="1.4">\n' % ident)
fw('%s<!DOCTYPE X3D PUBLIC "ISO//Web3D//DTD X3D 3.0//EN" "http://www.web3d.org/specifications/x3d-3.0.dtd">\n' % ident)
fw('%s<X3D version="3.0" profile="Immersive" xmlns:xsd="http://www.w3.org/2001/XMLSchema-instance" xsd:noNamespaceSchemaLocation="http://www.web3d.org/specifications/x3d-3.0.xsd">\n' % ident)
ident += '\t'
fw('%s<head>\n' % ident)
ident += '\t'
Campbell Barton
committed
fw('%s<meta name="filename" content=%s />\n' % (ident, filepath_quoted))
fw('%s<meta name="generator" content=%s />\n' % (ident, blender_ver_quoted))
# this info was never updated, so blender version should be enough
# fw('%s<meta name="translator" content="X3D exporter v1.55 (2006/01/17)" />\n' % ident)
Campbell Barton
committed
ident = ident[:-1]
Loading
Loading full blame...