Skip to content
Snippets Groups Projects
Commit f37ac2ec authored by Campbell Barton's avatar Campbell Barton
Browse files

removed the class for exporting x3d completely.

parent a5a3c60b
No related branches found
No related tags found
No related merge requests found
...@@ -68,6 +68,7 @@ x3d_names_reserved = {"Anchor", "Appearance", "Arc2D", "ArcClose2D", "AudioClip" ...@@ -68,6 +68,7 @@ x3d_names_reserved = {"Anchor", "Appearance", "Arc2D", "ArcClose2D", "AudioClip"
"TriangleFanSet", "TriangleSet", "TriangleSet2D", "TriangleStripSet", "Viewpoint", "VisibilitySensor", "TriangleFanSet", "TriangleSet", "TriangleSet2D", "TriangleStripSet", "Viewpoint", "VisibilitySensor",
"WorldInfo", "X3D", "XvlShell", "VertexShader", "FragmentShader", "MultiShaderAppearance", "ShaderAppearance"} "WorldInfo", "X3D", "XvlShell", "VertexShader", "FragmentShader", "MultiShaderAppearance", "ShaderAppearance"}
def clamp_color(col): def clamp_color(col):
return tuple([max(min(c, 1.0), 0.0) for c in col]) return tuple([max(min(c, 1.0), 0.0) for c in col])
...@@ -91,40 +92,27 @@ def clean_str(name, prefix='rsvd_'): ...@@ -91,40 +92,27 @@ def clean_str(name, prefix='rsvd_'):
newName = newName.replace(bad, "_") newName = newName.replace(bad, "_")
return newName return newName
namesFog = ("", "LINEAR", "EXPONENTIAL", "")
########################################################## ##########################################################
# Functions for writing output file # Functions for writing output file
########################################################## ##########################################################
class x3d_class: def export(file,
global_matrix,
namesFog = ("", "LINEAR", "EXPONENTIAL", "") scene,
use_apply_modifiers=False,
use_selection=True,
EXPORT_TRI=False,):
def __init__(self, filepath): fw = file.write
#--- public you can change these ---
self.global_matrix = mathutils.Matrix.Rotation(-(math.pi / 2.0), 4, 'X')
#--- class private don't touch ---
self.filepath = filepath
self.file = None
if filepath.lower().endswith('.x3dz'):
try:
import gzip
self.file = gzip.open(filepath, "w")
except:
print("failed to import compression modules, exporting uncompressed")
self.filepath = filepath[:-1] # remove trailing z
if self.file is None:
self.file = open(self.filepath, "w", encoding="utf8", newline="\n")
self.nodeID = 0
########################################################## ##########################################################
# Writing nodes routines # Writing nodes routines
########################################################## ##########################################################
@staticmethod def writeHeader(ident):
def writeHeader(fw, ident):
filepath = fw.__self__.name filepath = fw.__self__.name
#bfile = sys.expandpath( Blender.Get('filepath') ).replace('<', '&lt').replace('>', '&gt') #bfile = sys.expandpath( Blender.Get('filepath') ).replace('<', '&lt').replace('>', '&gt')
bfile = repr(os.path.basename(filepath).replace('<', '&lt').replace('>', '&gt'))[1:-1] # use outfile name bfile = repr(os.path.basename(filepath).replace('<', '&lt').replace('>', '&gt'))[1:-1] # use outfile name
...@@ -143,16 +131,14 @@ class x3d_class: ...@@ -143,16 +131,14 @@ class x3d_class:
ident += "\t" ident += "\t"
return ident return ident
@staticmethod def writeFooter(ident):
def writeFooter(fw, ident):
ident = ident[:-1] ident = ident[:-1]
fw("%s</Scene>\n" % ident) fw("%s</Scene>\n" % ident)
ident = ident[:-1] ident = ident[:-1]
fw("%s</X3D>" % ident) fw("%s</X3D>" % ident)
return ident return ident
@staticmethod def writeViewpoint(ident, ob, mat, scene):
def writeViewpoint(fw, ident, ob, mat, scene):
loc, quat, scale = mat.decompose() loc, quat, scale = mat.decompose()
fw("%s<Viewpoint DEF=\"%s\" " % (ident, clean_str(ob.name))) fw("%s<Viewpoint DEF=\"%s\" " % (ident, clean_str(ob.name)))
fw("description=\"%s\" " % ob.name) fw("description=\"%s\" " % ob.name)
...@@ -162,8 +148,7 @@ class x3d_class: ...@@ -162,8 +148,7 @@ class x3d_class:
fw("fieldOfView=\"%.3g\" " % ob.data.angle) fw("fieldOfView=\"%.3g\" " % ob.data.angle)
fw(" />\n") fw(" />\n")
@staticmethod def writeFog(ident, world):
def writeFog(fw, ident, world):
if world: if world:
mtype = world.mist_settings.falloff mtype = world.mist_settings.falloff
mparam = world.mist_settings mparam = world.mist_settings
...@@ -178,12 +163,10 @@ class x3d_class: ...@@ -178,12 +163,10 @@ class x3d_class:
else: else:
return return
@staticmethod def writeNavigationInfo(ident, scene):
def writeNavigationInfo(fw, ident, scene):
fw('%s<NavigationInfo headlight="false" visibilityLimit="0.0" type=\'"EXAMINE","ANY"\' avatarSize="0.25, 1.75, 0.75" />\n' % ident) fw('%s<NavigationInfo headlight="false" visibilityLimit="0.0" type=\'"EXAMINE","ANY"\' avatarSize="0.25, 1.75, 0.75" />\n' % ident)
@staticmethod def writeSpotLight(ident, ob, mtx, lamp, world):
def writeSpotLight(fw, ident, ob, mtx, lamp, world):
safeName = clean_str(ob.name) safeName = clean_str(ob.name)
if world: if world:
ambi = world.ambient_color ambi = world.ambient_color
...@@ -214,8 +197,7 @@ class x3d_class: ...@@ -214,8 +197,7 @@ class x3d_class:
fw("direction=\"%.4g %.4g %.4g\" " % (dx, dy, dz)) fw("direction=\"%.4g %.4g %.4g\" " % (dx, dy, dz))
fw("location=\"%.4g %.4g %.4g\" />\n" % location) fw("location=\"%.4g %.4g %.4g\" />\n" % location)
@staticmethod def writeDirectionalLight(ident, ob, mtx, lamp, world):
def writeDirectionalLight(fw, ident, ob, mtx, lamp, world):
safeName = clean_str(ob.name) safeName = clean_str(ob.name)
if world: if world:
ambi = world.ambient_color ambi = world.ambient_color
...@@ -233,8 +215,7 @@ class x3d_class: ...@@ -233,8 +215,7 @@ class x3d_class:
fw("intensity=\"%.4g\" " % intensity) fw("intensity=\"%.4g\" " % intensity)
fw("direction=\"%.4g %.4g %.4g\" />\n" % (dx, dy, dz)) fw("direction=\"%.4g %.4g %.4g\" />\n" % (dx, dy, dz))
@staticmethod def writePointLight(ident, ob, mtx, lamp, world):
def writePointLight(fw, ident, ob, mtx, lamp, world):
safeName = clean_str(ob.name) safeName = clean_str(ob.name)
if world: if world:
...@@ -256,26 +237,27 @@ class x3d_class: ...@@ -256,26 +237,27 @@ class x3d_class:
fw("radius=\"%.4g\" " % lamp.distance) fw("radius=\"%.4g\" " % lamp.distance)
fw("location=\"%.4g %.4g %.4g\" />\n" % location) fw("location=\"%.4g %.4g %.4g\" />\n" % location)
def secureName(self, name): def secureName(name):
name = name + str(self.nodeID) name = name + str(secureName.nodeID)
self.nodeID = self.nodeID + 1 secureName.nodeID += 1
if len(name) <= 3: if len(name) <= 3:
newname = "_" + str(self.nodeID) newname = "_" + str(secureName.nodeID)
return "%s" % (newname) return "%s" % (newname)
else: else:
for bad in ('"', '#', "'", ', ', '.', '[', '\\', ']', '{', '}'): for bad in ('"', '#', "'", ', ', '.', '[', '\\', ']', '{', '}'):
name = name.replace(bad, "_") name = name.replace(bad, "_")
if name in x3d_names_reserved: if name in x3d_names_reserved:
newname = name[0:3] + "_" + str(self.nodeID) newname = name[0:3] + "_" + str(secureName.nodeID)
return "%s" % (newname) return "%s" % (newname)
elif name[0].isdigit(): elif name[0].isdigit():
newname = "_" + name + str(self.nodeID) newname = "_" + name + str(secureName.nodeID)
return "%s" % (newname) return "%s" % (newname)
else: else:
newname = name newname = name
return "%s" % (newname) return "%s" % (newname)
secureName.nodeID = 0
def writeIndexedFaceSet(self, fw, ident, ob, mesh, mtx, world, EXPORT_TRI=False): def writeIndexedFaceSet(ident, ob, mesh, mtx, world, EXPORT_TRI=False):
shape_name_x3d = clean_str(ob.name) shape_name_x3d = clean_str(ob.name)
mesh_name_x3d = clean_str(mesh.name) mesh_name_x3d = clean_str(mesh.name)
...@@ -408,7 +390,7 @@ class x3d_class: ...@@ -408,7 +390,7 @@ class x3d_class:
ident += "\t" ident += "\t"
if image: if image:
self.writeImageTexture(fw, ident, image) writeImageTexture(ident, image)
if mesh_materials_use_face_texture[material_index]: if mesh_materials_use_face_texture[material_index]:
if image.use_tiles: if image.use_tiles:
...@@ -438,7 +420,7 @@ class x3d_class: ...@@ -438,7 +420,7 @@ class x3d_class:
fw("/>\n") fw("/>\n")
if material: if material:
self.writeMaterial(fw, ident, material, clean_str(material.name, ""), world) writeMaterial(ident, material, clean_str(material.name, ""), world)
ident = ident[:-1] ident = ident[:-1]
fw("%s</Appearance>\n" % ident) fw("%s</Appearance>\n" % ident)
...@@ -476,7 +458,7 @@ class x3d_class: ...@@ -476,7 +458,7 @@ class x3d_class:
if True: if True:
# "coordIndex" # "coordIndex"
fw('coordIndex="') fw("coordIndex=\"")
if EXPORT_TRI: if EXPORT_TRI:
for i in face_group: for i in face_group:
fv = mesh_faces[i].vertices[:] fv = mesh_faces[i].vertices[:]
...@@ -513,7 +495,6 @@ class x3d_class: ...@@ -513,7 +495,6 @@ class x3d_class:
if is_uv: if is_uv:
fw("%s<TextureCoordinate point=\"" % ident) fw("%s<TextureCoordinate point=\"" % ident)
fw = fw
mesh_faces_uv = mesh.uv_textures.active.data mesh_faces_uv = mesh.uv_textures.active.data
for i in face_group: for i in face_group:
for uv in mesh_faces_uv[i].uv: for uv in mesh_faces_uv[i].uv:
...@@ -554,9 +535,7 @@ class x3d_class: ...@@ -554,9 +535,7 @@ class x3d_class:
ident = ident[:-1] ident = ident[:-1]
fw("%s</Collision>\n" % ident) fw("%s</Collision>\n" % ident)
def writeMaterial(ident, mat, matName, world):
@staticmethod
def writeMaterial(fw, ident, mat, matName, world):
# look up material name, use it if available # look up material name, use it if available
if mat.tag: if mat.tag:
fw("%s<Material USE=\"MA_%s\" />\n" % (ident, matName)) fw("%s<Material USE=\"MA_%s\" />\n" % (ident, matName))
...@@ -589,8 +568,7 @@ class x3d_class: ...@@ -589,8 +568,7 @@ class x3d_class:
fw("shininess=\"%.3g\" " % shininess) fw("shininess=\"%.3g\" " % shininess)
fw("transparency=\"%s\" />\n" % transp) fw("transparency=\"%s\" />\n" % transp)
@staticmethod def writeImageTexture(ident, image):
def writeImageTexture(fw, ident, image):
name = image.name name = image.name
if image.tag: if image.tag:
...@@ -614,8 +592,7 @@ class x3d_class: ...@@ -614,8 +592,7 @@ class x3d_class:
fw("url='%s' />\n" % " ".join(["\"%s\"" % f.replace("\\", "/") for f in images])) fw("url='%s' />\n" % " ".join(["\"%s\"" % f.replace("\\", "/") for f in images]))
def writeBackground(self, ident, world): def writeBackground(ident, world):
fw = self.file.write
if world: if world:
worldname = world.name worldname = world.name
...@@ -628,7 +605,7 @@ class x3d_class: ...@@ -628,7 +605,7 @@ class x3d_class:
sky_triple = clamp_color(world.zenith_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)) mix_triple = clamp_color((grd_triple[i] + sky_triple[i]) / 2.0 for i in range(3))
fw("%s<Background DEF=\"%s\" " % (ident, self.secureName(worldname))) fw("%s<Background DEF=\"%s\" " % (ident, secureName(worldname)))
# No Skytype - just Hor color # No Skytype - just Hor color
if blending == (False, False, False): if blending == (False, False, False):
fw("groundColor=\"%.3g %.3g %.3g\" " % grd_triple) fw("groundColor=\"%.3g %.3g %.3g\" " % grd_triple)
...@@ -684,15 +661,7 @@ class x3d_class: ...@@ -684,15 +661,7 @@ class x3d_class:
########################################################## ##########################################################
# export routine # export routine
########################################################## ##########################################################
def export_main():
def export(self, scene,
use_apply_modifiers=False,
use_selection=True,
EXPORT_TRI=False,
):
fw = self.file.write
world = scene.world world = scene.world
# tag un-exported IDs # tag un-exported IDs
...@@ -700,14 +669,13 @@ class x3d_class: ...@@ -700,14 +669,13 @@ class x3d_class:
bpy.data.materials.tag(False) bpy.data.materials.tag(False)
bpy.data.images.tag(False) bpy.data.images.tag(False)
print("Info: starting X3D export to %r..." % self.filepath) print("Info: starting X3D export to %r..." % file.name)
ident = "" ident = ""
ident = self.writeHeader(fw, ident) ident = writeHeader(ident)
# self.writeScript() writeNavigationInfo(ident, scene)
self.writeNavigationInfo(fw, ident, scene) writeBackground(ident, world)
self.writeBackground(ident, world) writeFog(ident, world)
self.writeFog(fw, ident, world)
ident = "\t\t" ident = "\t\t"
...@@ -726,10 +694,10 @@ class x3d_class: ...@@ -726,10 +694,10 @@ class x3d_class:
for ob, ob_mat in derived: for ob, ob_mat in derived:
objType = ob.type objType = ob.type
objName = ob.name objName = ob.name
ob_mat = self.global_matrix * ob_mat ob_mat = global_matrix * ob_mat
if objType == 'CAMERA': if objType == 'CAMERA':
self.writeViewpoint(fw, ident, ob, ob_mat, scene) writeViewpoint(ident, ob, ob_mat, scene)
elif objType in ('MESH', 'CURVE', 'SURF', 'FONT'): elif objType in ('MESH', 'CURVE', 'SURF', 'FONT'):
if (objType != 'MESH') or (use_apply_modifiers and ob.is_modified(scene, 'PREVIEW')): if (objType != 'MESH') or (use_apply_modifiers and ob.is_modified(scene, 'PREVIEW')):
try: try:
...@@ -740,7 +708,7 @@ class x3d_class: ...@@ -740,7 +708,7 @@ class x3d_class:
me = ob.data me = ob.data
if me is not None: if me is not None:
self.writeIndexedFaceSet(fw, ident, ob, me, ob_mat, world, EXPORT_TRI=EXPORT_TRI) writeIndexedFaceSet(ident, ob, me, ob_mat, world, EXPORT_TRI=EXPORT_TRI)
# free mesh created with create_mesh() # free mesh created with create_mesh()
if me != ob.data: if me != ob.data:
...@@ -750,13 +718,13 @@ class x3d_class: ...@@ -750,13 +718,13 @@ class x3d_class:
data = ob.data data = ob.data
datatype = data.type datatype = data.type
if datatype == 'POINT': if datatype == 'POINT':
self.writePointLight(fw, ident, ob, ob_mat, data, world) writePointLight(ident, ob, ob_mat, data, world)
elif datatype == 'SPOT': elif datatype == 'SPOT':
self.writeSpotLight(fw, ident, ob, ob_mat, data, world) writeSpotLight(ident, ob, ob_mat, data, world)
elif datatype == 'SUN': elif datatype == 'SUN':
self.writeDirectionalLight(fw, ident, ob, ob_mat, data, world) writeDirectionalLight(ident, ob, ob_mat, data, world)
else: else:
self.writeDirectionalLight(fw, ident, ob, ob_mat, data, world) writeDirectionalLight(ident, ob, ob_mat, data, world)
else: else:
#print "Info: Ignoring [%s], object type [%s] not handle yet" % (object.name,object.getType) #print "Info: Ignoring [%s], object type [%s] not handle yet" % (object.name,object.getType)
pass pass
...@@ -764,47 +732,11 @@ class x3d_class: ...@@ -764,47 +732,11 @@ class x3d_class:
if free: if free:
free_derived_objects(ob_main) free_derived_objects(ob_main)
ident = self.writeFooter(fw, ident) ident = writeFooter(ident)
self.cleanup() export_main()
file.close()
########################################################## print("Info: finished X3D export to %r" % file.name)
# Utility methods
##########################################################
def cleanup(self):
self.file.close()
print("Info: finished X3D export to %r" % self.filepath)
def faceToString(self, face):
print("Debug: face.flag=0x%x (bitflags)" % face.flag)
if face.sel:
print("Debug: face.sel=true")
print("Debug: face.mode=0x%x (bitflags)" % face.mode)
if face.mode & Mesh.FaceModes.TWOSIDE:
print("Debug: face.mode twosided")
print("Debug: face.transp=0x%x (enum)" % face.blend_type)
if face.blend_type == Mesh.FaceTranspModes.SOLID:
print("Debug: face.transp.SOLID")
if face.image:
print("Debug: face.image=%s" % face.image.name)
print("Debug: face.materialIndex=%d" % face.materialIndex)
def meshToString(self, mesh):
# print("Debug: mesh.hasVertexUV=%d" % mesh.vertexColors)
print("Debug: mesh.faceUV=%d" % (len(mesh.uv_textures) > 0))
# print("Debug: mesh.faceUV=%d" % mesh.faceUV)
print("Debug: mesh.hasVertexColours=%d" % (len(mesh.vertex_colors) > 0))
# print("Debug: mesh.hasVertexColours=%d" % mesh.hasVertexColours())
print("Debug: mesh.vertices=%d" % len(mesh.vertices))
print("Debug: mesh.faces=%d" % len(mesh.faces))
print("Debug: mesh.materials=%d" % len(mesh.materials))
return s
########################################################## ##########################################################
...@@ -825,17 +757,29 @@ def save(operator, context, filepath="", ...@@ -825,17 +757,29 @@ def save(operator, context, filepath="",
if not filepath.lower().endswith('.x3d'): if not filepath.lower().endswith('.x3d'):
filepath = '.'.join(filepath.split('.')[:-1]) + '.x3d' filepath = '.'.join(filepath.split('.')[:-1]) + '.x3d'
scene = context.scene
world = scene.world
if bpy.ops.object.mode_set.poll(): if bpy.ops.object.mode_set.poll():
bpy.ops.object.mode_set(mode='OBJECT') bpy.ops.object.mode_set(mode='OBJECT')
wrlexport = x3d_class(filepath) file = None
wrlexport.export(scene, if filepath.lower().endswith('.x3dz'):
use_apply_modifiers=use_apply_modifiers, try:
use_selection=use_selection, import gzip
EXPORT_TRI=use_triangulate, file = gzip.open(filepath, "w")
) except:
print("failed to import compression modules, exporting uncompressed")
filepath = filepath[:-1] # remove trailing z
if file is None:
file = open(filepath, "w")
global_matrix = mathutils.Matrix.Rotation(-(math.pi / 2.0), 4, 'X')
export(file,
global_matrix,
context.scene,
use_apply_modifiers=use_apply_modifiers,
use_selection=use_selection,
EXPORT_TRI=use_triangulate,
)
return {'FINISHED'} return {'FINISHED'}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment