[X3D-Ecosystem] XSLT—X3D XML to Blender Python

John Carlson yottzumm at gmail.com
Wed Jul 10 09:26:37 PDT 2024


Could this rough draft be extended? Sorry about formatting.  Rotations look
a bit buggy.

<?xml version="1.0" encoding="UTF-8"?> <xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> <xsl:output method="text"
indent="no"/> <xsl:template match="/"> <xsl:text>import bpy import math #
Clear existing mesh objects bpy.ops.object.select_all(action='DESELECT')
bpy.ops.object.select_by_type(type='MESH') bpy.ops.object.delete()
</xsl:text> <xsl:apply-templates/> </xsl:template> <xsl:template
match="Scene"> <xsl:apply-templates/> </xsl:template> <xsl:template
match="Transform"> <xsl:text> # Create a new mesh mesh =
bpy.data.meshes.new(name="X3DMesh") obj = bpy.data.objects.new("X3DObject",
mesh) # Link object to scene bpy.context.scene.collection.objects.link(obj)
# Get transformation data translation = [</xsl:text> <xsl:value-of
select="@translation"/> <xsl:text>] rotation = [</xsl:text> <xsl:value-of
select="@rotation"/> <xsl:text>] scale = [</xsl:text> <xsl:value-of
select="@scale"/> <xsl:text>] # Apply transformation obj.location =
translation obj.rotation_euler = (rotation[3] * rotation[0], rotation[3] *
rotation[1], rotation[3] * rotation[2]) obj.scale = scale </xsl:text>
<xsl:apply-templates/> </xsl:template> <xsl:template match="Shape">
<xsl:apply-templates/> </xsl:template> <xsl:template
match="IndexedFaceSet"> <xsl:text> # Create mesh from coordinates and face
indices verts = [ </xsl:text> <xsl:for-each select="Coordinate/point">
<xsl:text> (</xsl:text> <xsl:value-of select="@point"/> <xsl:text>),
</xsl:text> </xsl:for-each> <xsl:text>] faces = [ </xsl:text>
<xsl:call-template name="split-faces"> <xsl:with-param name="text"
select="@coordIndex"/> </xsl:call-template> <xsl:text>]
mesh.from_pydata(verts, [], faces) mesh.update() </xsl:text>
</xsl:template> <xsl:template name="split-faces"> <xsl:param name="text"/>
<xsl:choose> <xsl:when test="contains($text, '-1')"> <xsl:text>
(</xsl:text> <xsl:value-of select="substring-before($text, '-1')"/>
<xsl:text>), </xsl:text> <xsl:call-template name="split-faces">
<xsl:with-param name="text" select="substring-after($text, '-1')"/>
</xsl:call-template> </xsl:when> <xsl:otherwise> <xsl:if
test="string-length($text) > 0"> <xsl:text> (</xsl:text> <xsl:value-of
select="$text"/> <xsl:text>), </xsl:text> </xsl:if> </xsl:otherwise>
</xsl:choose> </xsl:template> </xsl:stylesheet>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://web3d.org/pipermail/x3d-ecosystem_web3d.org/attachments/20240710/2fd1d70b/attachment.html>


More information about the X3D-Ecosystem mailing list