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 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 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 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011
|
#!BPY
""" Registration info for Blender menus:
Name: 'VRML 2.0 (.wrl)...'
Blender: 232
Group: 'Export'
Submenu: 'All Objects...' all
Submenu: 'Selected Objects...' selected
Tooltip: 'Export to VRML2 (.wrl) file.'
"""
__author__ = ("Rick Kimball", "Ken Miller", "Steve Matthews")
__url__ = ["blender", "elysiun",
"Author's (Rick) homepage, http://kimballsoftware.com/blender",
"Show your VRML world at vrmlworld.net, http://vrmlworld.net"]
__version__ = "2004/01/19"
__bpydoc__ = """\
This script exports to VRML 2.0 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:<br>
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>
Material colors need work;<br>
Spotlight softness needs work;<br>
Can't get the texture array associated with material * not the UV ones;<br>
Can't set smoothing, crease angle and mesh smoothing * setting not
accessible.
"""
# $Id: wrl2export.py,v 1.4 2004/11/07 16:31:13 ianwill Exp $
#
#------------------------------------------------------------------------
# VRML2 exporter for blender 2.28a or above
#
# Source: http://blender.kimballsoftware.com/
#
# Authors: Rick Kimball with much inspiration
# from the forum at www.elysiun.com
# and irc://irc.freenode.net/blenderchat
# Ken Miller and Steve Matthews (Added Camera Support)
#
# ***** BEGIN GPL LICENSE BLOCK *****
#
# Copyright (C) 2003,2004: Rick Kimball rick@vrmlworld.net
#
# 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., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
#
# ***** END GPL LICENCE BLOCK *****
#
# To use script:
# 1.) load this file in the text window.
# (press SHIFT+F11, Open New via Datablock button)
# 2.) make sure your mouse is over the text edit window and
# run this script. (press ALT+P)
# Or:
# copy to the scripts directory and it will appear in the
# export list. (Needs 2.32 or higher)
#
# Notes:
# a.) output filename is same as current blender file with .wrl extension
# b.) error messages go to the Blender DOS console window
#
# The latest version of this python export script:
# http://blender.kimballsoftware.com/
#
# If you like this script, try using http://vrmlworld.net/
# to show off your VRML world.
#
# 2004-01-19 by Rick Kimball <rick@vrmlworld.net>
# o added sub menus and file selector dialog
#
# 2004-01-17 by Rick Kimball <rick@vrmlworld.net>
# o add meta comments so script will appear in export menu list
#
# 2003-11-01 by Rick Kimball <rick@vrmlworld.net>
# o fixed issues related to Lamp object and 2.28a API.
#
# 2003-07-19 by Rick Kimball <rick@vrmlworld.net>
# o made compatible with new Python API in 2.28
#
# 2003-01-16 by Ken Miller - with math help from Steve Matthews :)
# o Added support for exporting cameras out of Blender
# o Sets the name of the camera as the object name
# o sets the description of the camera as the object name,
# which should be modified to something meaningful
# o sets the position and orientation
#
# 2003-01-19 Rick Kimball <rick@vrmlworld.net>
# o Added Support For PointLight, SpotLight and DirectionalLight using Lamps
# o Creates multi singlesided or doublesided IFS
# o Creates IndexedLineSets if DrawTypes is WIRE instead of Shaded
#
# 2003-02-03 Rick Kimball <rick@vrmlworld.net>
# o attempts to catch exceptions for empty objects
#
# 2003-02-04 Rick Kimball <rick@vrmlworld.net>
# o fixed file overwrite problem when blender filename is all uppercase
#
# 2003-02-08 Rick Kimball <rick@vrmlworld.net>
# o cleanStr() creates valid VRML DEF names even if object.name
# is zero length or uses VRML reserved names or characters
#
#------------------------------------------------------------------------
# Known Issue:
# o doesn't handle multiple materials (don't use material indices)
# o doesn't handle multiple UV textures on a single mesh. (create a mesh for each texture)
# o material colors need work
# o spotlight softness needs work
# o can't get the texture array associated with material * not the UV ones
# o can't set smoothing, crease angle and mesh smoothing * setting not accesible
#
# Still Todo:
#
# - Support for material indexes
# - Automatically Split IFS when multiple UV textures found * warning only now
# - Automatically Split IFS when combination of single vs double sided
# - Automatically Split IFS when face with only 2 vertices is found should be an ILS
# - Export common coordinate map for split IFS
# - Intelligent color array vs color index
# - Support more blender objects: World
# - Figure out how to output Animation
# - Add GUI to control the following:
# o All/Layer/Object output radio button
# o Color per vertex toggle yes/no
# o Complex/Simple VRML output radio button
# o Compressed/Uncompressed output radio button
# o Decimal precision dropdown 1,2,3,4,5,6
# o IFS/Elevation Grid output radio button
# o Normals output toggle yes/no
# o Proto output toggle yes/no
# o Verbose console progress
import Blender
from Blender import NMesh, Lamp
import math
#-- module constants
radD=math.pi/180.0
rad90=90.0*radD # for rotation
rad30=30.0*radD # default crease angle
ARG=''
#------------------------------------------------------------------------
#-- utility functions and classes --
#------------------------------------------------------------------------
def rad2deg(v):
return round(v*180.0/math.pi,4)
def deg2rad(v):
return (v*math.pi)/180.0;
class DrawTypes:
"""Object DrawTypes enum values
BOUNDS - draw only the bounding box of the object
WIRE - draw object as a wire frame
SOLID - draw object with flat shading
SHADED - draw object with OpenGL shading
"""
BOUNDBOX = 1
WIRE = 2
SOLID = 3
SHADED = 4
TEXTURE = 5
#------------------------------------------------------
# the Blender.Object class seems to be missing this...
#------------------------------------------------------
if not hasattr(Blender.Object,'DrawTypes'):
Blender.Object.DrawTypes = DrawTypes()
#------------------------------------------------------------------------
#-- VRML2Export --
#------------------------------------------------------------------------
class VRML2Export:
#------------------------------------------------------------------------
def __init__(self, filename):
#--- public you can change these ---
self.verbose=1 # level of verbosity in console 0-none, 1-some, 2-most
self.cp=3 # decimals for material color values 0.000 - 1.000
self.vp=3 # decimals for vertex coordinate values 0.000 - n.000
self.tp=3 # decimals for texture coordinate values 0.000 - 1.000
self.ambientIntensity=.2
self.defCreaseAngle=rad30
self.smooth=0
#--- class private don't touch ---
self.texNames={} # dictionary of textureNames
self.matNames={} # dictionary of materiaNames
self.indentLevel=0 # keeps track of current indenting
self.filename=filename
self.file = open(filename, "w")
self.bNav=0
self.nNodeID=0
self.VRMLReserved=[ "Anchor","Appearance","Anchor","AudioClip","Background",
"Billboard", "Box", "Collision", "Color", "ColorInterpolator",
"Cone", "Coordinate", "CoordinateInterpolator", "Cylinder",
"CylinderSensor", "DirectionalLight", "ElevationGrid",
"Extrustion", "Fog", "FontStyle", "Group", "ImageTexture",
"IndexedFaceSet", "IndexedLineSet", "Inline", "LOD",
"Material", "MovieTexture", "NavigationInfo", "Normal",
"NormalInterpolator","OrientationInterpolator", "PixelTexture",
"PlaneSensor", "PointLight", "PointSet", "PositionInterpolator",
"ProxmimitySensor", "ScalarInterpolator", "Script", "Shape",
"Sound", "Sphere", "SphereSensor", "SpotLight", "Switch",
"Text", "TextureCoordinate", "TextureTransform", "TimeSensor",
"TouchSensor", "Transform", "Viewpoint", "VisibilitySensor",
"WorldInfo"
]
#------------------------------------------------------------------------
# writeHeader, export file, cleanup
#------------------------------------------------------------------------
def writeHeader(self):
self.file.write("#VRML V2.0 utf8\n")
self.file.write("# modeled using blender3d http://blender.org/$\n")
self.file.write("# exported using wrl2export.py version $Revision: 1.4 $\n")
self.file.write("# get latest exporter at http://kimballsoftware.com/blender/\n\n")
def export(self, scene):
print "Info: starting VRML2 export to " + self.filename + "..."
self.writeHeader()
theObjects = []
if ARG == 'selected':
theObjects = Blender.Object.GetSelected()
else:
theObjects = scene.getChildren()
for object in theObjects:
try:
objType=object.getType()
if objType == "Mesh":
self.writeIndexedFaceSet(object, normals = 0)
elif objType == "Camera":
self.writeCameraInfo(object)
elif objType == "Lamp":
# if there is a lamp then we probably want to turn off the headlight
if self.bNav == 0:
self.writeNavigationInfo()
self.bNav=1
#endif
lamp=Lamp.Get(object.data.getName())
try:
lampType=lamp.getType()
if lampType == Lamp.Types.Lamp:
self.writePointLight(object, lamp)
elif lampType == Lamp.Types.Spot:
self.writeSpotLight(object, lamp)
elif lampType == Lamp.Types.Sun:
self.writeDirectionalLight(object, lamp)
else:
self.writeDirectionalLight(object, lamp)
#endif
except AttributeError:
print "Error: Unable to get type info for %s" % object.name
else:
print "Info: Ignoring [%s], object type [%s] not handle yet" % \
(object.name,object.getType())
#endif
except ValueError:
print "Error: object named %s has problem with accessing an attribute" % object.name
#end try
#endfor
self.cleanup()
def cleanup(self):
self.file.close()
self.texNames={}
self.matNames={}
self.indentLevel=0
print "Info: finished VRML2 export to %s\n" % self.filename
#------------------------------------------------------------------------
# Writes out camera info as a viewpoint
# Handles orientation, position
# Use camera object name to set description
#------------------------------------------------------------------------
def writeCameraInfo(self, object):
if self.verbose > 0:
print "Info: exporting camera named="+object.name
#endif
self.writeIndented("DEF %s Viewpoint {\n" % (self.cleanStr(object.name)), 1)
self.writeIndented("description \"%s\" \n" % (object.name))
# get the camera location, subtract 90 degress from X to orient like VRML does
loc = self.rotatePointForVRML(object.loc)
rot = [object.RotX - 1.57, object.RotY, object.RotZ]
nRot = self.rotatePointForVRML(rot)
# convert to Quaternion and to Angle Axis
Q = self.eulerToQuaternions(nRot[0], nRot[1], nRot[2])
Q1 = self.multiplyQuaternions(Q[0], Q[1])
Qf = self.multiplyQuaternions(Q1, Q[2])
angleAxis = self.quaternionToAngleAxis(Qf)
# write orientation statement
self.writeIndented("orientation %3.2f %3.2f %3.2f %3.2f\n" %
(angleAxis[0], angleAxis[1], -angleAxis[2], angleAxis[3]))
# write position statement
self.writeIndented("position %3.2f %3.2f %3.2f\n" %
(loc[0], loc[1], loc[2]))
self.writeIndented("} # Viewpoint\n", -1)
self.writeIndented("\n")
#------------------------------------------------------------------------
def writeIndexedFaceSet(self, object, normals = 0):
if self.verbose > 0:
print "Info: exporting mesh named=["+object.name+"]"
#endif
imageMap={} # set of used images
sided={} # 'one':cnt , 'two':cnt
vColors={} # 'multi':1
mesh=object.getData()
nIFSCnt=self.countIFSSetsNeeded(mesh, imageMap, sided, vColors)
meshName = self.cleanStr(object.name)
if nIFSCnt > 1:
self.writeIndented("DEF %s%s Group {\n" % ("G_", meshName),1)
self.writeIndented("children [\n",1)
#endif
if self.verbose > 0:
print "Debug: [%s] has %d UV Textures" % (object.name, nIFSCnt)
#endif
if sided.has_key('two') and sided['two'] > 0:
bTwoSided=1
else:
bTwoSided=0
self.writeIndented("DEF %s Shape {\n" % meshName,1)
# show script debugging info
if self.verbose > 1:
self.meshToString(mesh)
print "Debug: mesh.faces["
for face in mesh.faces:
self.faceToString(face)
#endfor
print "Debug: ]"
#endif
maters=mesh.materials
hasImageTexture=0
if len(maters) > 0 or mesh.hasFaceUV():
self.writeIndented("appearance Appearance {\n", 1)
# right now this script can only handle a single material per mesh.
if len(maters) >= 1:
mat=Blender.Material.Get(maters[0].name)
self.writeMaterial(mat, self.cleanStr(maters[0].name,'mat_'))
if len(maters) > 1:
print "Warning: mesh named %s has multiple materials" % meshName
print "Warning: only one material per object handled"
#endif
else:
self.writeIndented("material NULL\n")
#endif
#-- textures
if mesh.hasFaceUV():
for face in mesh.faces:
if (hasImageTexture == 0) and (face.image):
self.writeImageTexture(face.image.name)
hasImageTexture=1 # keep track of face texture
#endif
#endfor
#endif
self.writeIndented("} # Appearance\n", -1)
#endif
#-------------------------------------------------------------------
#--
#-- IndexedFaceSet or IndexedLineSet
#
# check if object is wireframe only
if object.drawType == Blender.Object.DrawTypes.WIRE:
# user selected WIRE=2 on the Drawtype=Wire on (F9) Edit page
ifStyle="IndexedLineSet"
else:
# user selected BOUNDS=1, SOLID=3, SHARED=4, or TEXTURE=5
ifStyle="IndexedFaceSet"
#endif
self.writeIndented("geometry %s {\n" % ifStyle, 1)
if object.drawType != Blender.Object.DrawTypes.WIRE:
if bTwoSided == 1:
self.writeIndented("solid FALSE # two sided\n")
else:
self.writeIndented("solid TRUE # one sided\n")
#endif
#endif
#---
#--- output coordinates
self.writeCoordinates(object, mesh, meshName)
if object.drawType != Blender.Object.DrawTypes.WIRE:
#---
#--- output textureCoordinates if UV texture used
if mesh.hasFaceUV():
if hasImageTexture == 1:
self.writeTextureCoordinates(mesh)
if vColors.has_key('multi'):
self.writeVertexColors(mesh) # experiment
#endif
else:
self.writeFaceColors(mesh)
#endif hasImageTexture
#endif hasFaceUV
# TBD: figure out how to get this properly
if self.smooth:
creaseAngle=self.defCreaseAngle;
self.writeIndented("creaseAngle %s\n" % creaseAngle)
else:
self.writeIndented("creaseAngle 0.0 # in radians\n")
#endif mesh.smooth
#endif WIRE
#--- output vertexColors
if mesh.hasVertexColours() and vColors.has_key('multi'):
self.writeVertexColors(mesh)
#endif
#--- output closing braces
self.writeIndented("} # %s\n" % ifStyle, -1)
self.writeIndented("} # Shape\n", -1)
if nIFSCnt > 1:
self.writeIndented("] # children\n", -1)
self.writeIndented("} # Group\n", -1)
#endif
self.writeIndented("\n")
#------------------------------------------------------------------------
def writeCoordinates(self, object, mesh, meshName):
#-- vertices
self.writeIndented("coord DEF %s%s Coordinate {\n" % ("coord_",meshName), 1)
self.writeIndented("point [\n", 1)
meshVertexList = mesh.verts
# create vertex list and pre rotate -90 degrees X for VRML
mm=object.getMatrix()
for vertex in meshVertexList:
v=self.rotVertex(mm, vertex);
self.writeIndented("%s %s %s,\n" %
(round(v[0],self.vp),
round(v[1],self.vp),
round(v[2],self.vp) ))
#endfor
self.writeIndented("] # point\n", -1)
self.writeIndented("} # Coordinate\n", -1)
self.writeIndented("coordIndex [\n", 1)
coordIndexList=[]
for face in mesh.faces:
cordStr=""
for i in range(len(face)):
indx=meshVertexList.index(face[i])
cordStr = cordStr + "%s, " % indx
#endfor
self.writeIndented(cordStr + "-1,\n")
#endfor
self.writeIndented("] # coordIndex\n", -1)
#------------------------------------------------------------------------
def writeTextureCoordinates(self, mesh):
texCoordList=[]
texIndexList=[]
j=0
for face in mesh.faces:
for i in range(len(face)):
texIndexList.append(j)
texCoordList.append(face.uv[i])
j=j+1
#endfor
texIndexList.append(-1)
#endfor
self.writeIndented("texCoord TextureCoordinate {\n", 1)
self.writeIndented("point [\n", 1)
for i in range(len(texCoordList)):
self.writeIndented("%s %s," %
(round(texCoordList[i][0],self.tp),
round(texCoordList[i][1],self.tp))+"\n")
#endfor
self.writeIndented("] # point\n", -1)
self.writeIndented("} # texCoord\n", -1)
self.writeIndented("texCoordIndex [\n", 1)
texIndxStr=""
for i in range(len(texIndexList)):
texIndxStr = texIndxStr + "%d, " % texIndexList[i]
if texIndexList[i]==-1:
self.writeIndented(texIndxStr + "\n")
texIndxStr=""
#endif
#endfor
self.writeIndented("] # texCoordIndex\n", -1)
#------------------------------------------------------------------------
def writeFaceColors(self, mesh):
self.writeIndented("colorPerVertex FALSE\n")
self.writeIndented("color Color {\n",1)
self.writeIndented("color [\n",1)
for face in mesh.faces:
if face.col:
c=face.col[0]
if self.verbose > 1:
print "Debug: face.col r=%d g=%d b=%d" % (c.r, c.g, c.b)
#endif
aColor = self.rgbToFS(c)
self.writeIndented("%s,\n" % aColor)
#endfor
self.writeIndented("] # color\n",-1)
self.writeIndented("} # Color\n",-1)
#------------------------------------------------------------------------
def writeVertexColors(self, mesh):
self.writeIndented("colorPerVertex TRUE\n")
self.writeIndented("color Color {\n",1)
self.writeIndented("color [\n",1)
for i in range(len(mesh.verts)):
c=self.getVertexColorByIndx(mesh,i)
if self.verbose > 1:
print "Debug: vertex[%d].col r=%d g=%d b=%d" % (i, c.r, c.g, c.b)
#endif
aColor = self.rgbToFS(c)
self.writeIndented("%s,\n" % aColor)
#endfor
self.writeIndented("] # color\n",-1)
self.writeIndented("} # Color\n",-1)
#------------------------------------------------------------------------
def writeMaterial(self, mat, matName):
# look up material name, use it if available
if self.matNames.has_key(matName):
self.writeIndented("material USE %s\n" % matName)
self.matNames[matName]+=1
return;
#endif
self.matNames[matName]=1
ambient = mat.amb
diffuseR, diffuseG, diffuseB = mat.rgbCol[0], mat.rgbCol[1],mat.rgbCol[2]
emisR, emisG, emisB = diffuseR*mat.emit, diffuseG*mat.emit, diffuseB*mat.emit
shininess = mat.hard/255.0
specR = mat.specCol[0]
specG = mat.specCol[1]
specB = mat.specCol[2]
transp = 1-mat.alpha
self.writeIndented("material DEF %s Material {\n" % matName, 1)
self.writeIndented("diffuseColor %s %s %s" %
(round(diffuseR,self.cp), round(diffuseG,self.cp), round(diffuseB,self.cp)) +
"\n")
self.writeIndented("ambientIntensity %s" %
(round(ambient,self.cp))+
"\n")
self.writeIndented("specularColor %s %s %s" %
(round(specR,self.cp), round(specG,self.cp), round(specB,self.cp)) +
"\n" )
self.writeIndented("emissiveColor %s %s %s" %
(round(emisR,self.cp), round(emisG,self.cp), round(emisB,self.cp)) +
"\n" )
self.writeIndented("shininess %s" %
(round(shininess,self.cp)) +
"\n" )
self.writeIndented("transparency %s" %
(round(transp,self.cp)) +
"\n")
self.writeIndented("} # Material\n",-1)
#------------------------------------------------------------------------
def writeImageTexture(self, name):
if self.texNames.has_key(name):
self.writeIndented("texture USE %s\n" % self.cleanStr(name))
self.texNames[name] += 1
return
else:
self.writeIndented("texture DEF %s ImageTexture {\n" % self.cleanStr(name), 1)
self.writeIndented("url \"%s\"\n" % name)
self.writeIndented("} # ImageTexture \n",-1)
self.texNames[name] = 1
#endif
#------------------------------------------------------------------------
def writeSpotLight(self, object, lamp):
safeName = self.cleanStr(object.name)
# compute cutoff and beamwidth
intensity=min(lamp.energy/1.5,1.0) # TBD: figure out the right value
beamWidth=deg2rad(lamp.spotSize)*.5;
cutOffAngle=beamWidth*.99
(dx,dy,dz)=self.computeDirection(object)
# note -dx seems to equal om[3][0]
# note -dz seems to equal om[3][1]
# note dy seems to equal om[3][2]
om = object.getMatrix()
location=self.rotVertex(om, (0,0,0));
radius = lamp.dist*math.cos(beamWidth)
self.writeIndented("DEF %s SpotLight {\n" % safeName,1)
self.writeIndented("radius %s\n" % radius )
self.writeIndented("intensity %s\n" % intensity )
self.writeIndented("beamWidth %s # lamp.spotSize %s\n" % (beamWidth, lamp.spotSize) )
self.writeIndented("cutOffAngle %s # lamp.spotBlend %s\n" % (cutOffAngle, lamp.spotBlend))
self.writeIndented("direction %s %s %s # lamp.RotX=%s RotY=%s RotZ=%s\n" % \
(round(dx,3),round(dy,3),round(dz,3),
round(rad2deg(object.RotX),3),
round(rad2deg(object.RotY),3),
round(rad2deg(object.RotZ),3)))
self.writeIndented("location %s %s %s\n" % (round(location[0],3),
round(location[1],3),
round(location[2],3)))
self.writeIndented("} # SpotLight\n",-1)
# export a cone that matches the spotlight in verbose mode
if self.verbose > 1:
self.writeIndented("#generated visible spotlight cone\n")
self.writeIndented("Transform { # Spotlight Cone\n",1)
self.writeIndented("translation %s %s %s\n" % (round(location[0],3),
round(location[1],3),
round(location[2],3)))
rot = [object.RotX, object.RotY, object.RotZ]
nRot = self.rotatePointForVRML(rot)
# convert to Quaternion and to Angle Axis
Q = self.eulerToQuaternions(nRot[0], nRot[1], nRot[2])
Q1 = self.multiplyQuaternions(Q[0], Q[1])
Qf = self.multiplyQuaternions(Q1, Q[2])
angleAxis = self.quaternionToAngleAxis(Qf)
# write orientation statement
self.writeIndented("rotation %3.2f %3.2f %3.2f %3.2f\n" %
(angleAxis[0], angleAxis[1], -angleAxis[2], angleAxis[3]))
self.writeIndented("children [\n",1)
ch=radius
br=ch*math.sin(beamWidth)
self.writeIndented("Transform {\n",1)
self.writeIndented("translation 0 -%s 0\n" % (ch/2))
self.writeIndented("children ")
self.writeIndented("Collision {\n",1)
self.writeIndented("collide FALSE children Shape {\n",1)
self.writeIndented("geometry Cone { height %s bottomRadius %s }\n" % (ch, br))
self.writeIndented("appearance Appearance{\n",1)
self.writeIndented("material Material { diffuseColor 1 1 1 transparency .8 }\n")
self.writeIndented("} # Appearance\n",-1)
self.writeIndented("} # Shape\n",-1)
self.writeIndented("} # Collision\n",-1)
self.writeIndented("} # Transform visible cone \n",-1)
self.writeIndented("] # Spot children\n",-1)
self.writeIndented("} # SpotLight Cone Transform\n",-1)
#endif debug cone
self.writeIndented("\n")
#------------------------------------------------------------------------
def writeDirectionalLight(self, object, lamp):
safeName = self.cleanStr(object.name)
intensity=min(lamp.energy/1.5, 1.0) # TBD: figure out the right value
(dx,dy,dz)=self.computeDirection(object)
self.writeIndented("DEF %s DirectionalLight {\n" % safeName,1)
self.writeIndented("ambientIntensity %s\n" % self.ambientIntensity )
self.writeIndented("intensity %s\n" % intensity )
self.writeIndented("direction %s %s %s\n" % (round(dx,4),round(dy,4),round(dz,4)))
self.writeIndented("} # DirectionalLight\n",-1)
self.writeIndented("\n")
#------------------------------------------------------------------------
def writePointLight(self, object, lamp):
safeName = self.cleanStr(object.name)
om = object.getMatrix()
location=self.rotVertex(om, (0,0,0));
intensity=min(lamp.energy/1.5,1.0) # TBD: figure out the right value
radius = lamp.dist
self.writeIndented("DEF %s PointLight {\n" % safeName,1)
self.writeIndented("ambientIntensity %s\n" % self.ambientIntensity )
self.writeIndented("intensity %s\n" % intensity )
self.writeIndented("location %s %s %s\n" % (round(location[0],3),
round(location[1],3),
round(location[2],3)))
self.writeIndented("radius %s\n" % radius )
self.writeIndented("} # PointLight\n",-1)
self.writeIndented("\n")
#------------------------------------------------------------------------
def writeNavigationInfo(self):
self.writeIndented("NavigationInfo {\n",1)
self.writeIndented("headlight FALSE\n")
self.writeIndented("avatarSize [0.25, 1.75, 0.75]\n")
self.writeIndented("} # NavigationInfo\n",-1)
self.writeIndented("\n")
#------------------------------------------------------------------------
#--- Utility methods
#------------------------------------------------------------------------
def cleanStr(self, name, prefix='rsvd_'):
"""cleanStr(name,prefix) - try to create a valid VRML DEF name from object name"""
newName=name[:]
if len(newName) == 0:
self.nNodeID+=1
return "%s%d" % (prefix, self.nNodeID)
if newName in self.VRMLReserved:
newName='%s%s' % (prefix,newName)
#endif
if newName[0] in ['0','1','2','3','4','5','6','7','8','9']:
newName='%s%s' % ('_',newName)
#endif
for bad in [' ','"','#',"'",',','.','[','\\',']','{','}']:
newName=newName.replace(bad,'_')
return newName
def countIFSSetsNeeded(self, mesh, imageMap, sided, vColors):
"""
countIFFSetsNeeded() - should look at a blender mesh to determine
how many VRML IndexFaceSets or IndexLineSets are needed. A
new mesh created under the following conditions:
o - split by UV Textures / one per mesh
o - split by face, one sided and two sided
o - split by smooth and flat faces
o - split when faces only have 2 vertices * needs to be an IndexLineSet
"""
imageNameMap={}
faceMap={}
nFaceIndx=0
for face in mesh.faces:
sidename='';
if (face.mode & NMesh.FaceModes.TWOSIDE) == NMesh.FaceModes.TWOSIDE:
sidename='two'
else:
sidename='one'
#endif
if not vColors.has_key('multi'):
for face in mesh.faces:
if face.col:
c=face.col[0]
if c.r != 255 and c.g != 255 and c.b !=255:
vColors['multi']=1
#endif
#endif
#endfor
#endif
if sided.has_key(sidename):
sided[sidename]+=1
else:
sided[sidename]=1
#endif
if face.image:
faceName="%s_%s" % (face.image.name, sidename);
if imageMap.has_key(faceName):
imageMap[faceName].append(face)
else:
imageMap[faceName]=[face.image.name,sidename,face]
#endif
#endif
#endfor
if self.verbose > 0:
for faceName in imageMap.keys():
ifs=imageMap[faceName]
print "Debug: faceName=%s image=%s, solid=%s facecnt=%d" % \
(faceName, ifs[0], ifs[1], len(ifs)-2)
#endif
#endif
return len(imageMap.keys())
def faceToString(self,face):
print "Debug: face.flag=0x%x (bitflags)" % face.flag
if face.flag & NMesh.FaceFlags.SELECT == NMesh.FaceFlags.SELECT:
print "Debug: face.flag.SELECT=true"
#endif
print "Debug: face.mode=0x%x (bitflags)" % face.mode
if (face.mode & NMesh.FaceModes.TWOSIDE) == NMesh.FaceModes.TWOSIDE:
print "Debug: face.mode twosided"
#endif
print "Debug: face.transp=0x%x (enum)" % face.transp
if face.transp == NMesh.FaceTranspModes.SOLID:
print "Debug: face.transp.SOLID"
#
if face.image:
print "Debug: face.image=%s" % face.image.name
#endif
print "Debug: face.materialIndex=%d" % face.materialIndex
def getVertexColorByIndx(self, mesh, indx):
for face in mesh.faces:
j=0
for vertex in face.v:
if vertex.index == indx:
c=face.col[j]
#endif
j=j+1
#endfor
#endfor
return c
def meshToString(self,mesh):
print "Debug: mesh.hasVertexUV=%d" % mesh.hasVertexUV()
print "Debug: mesh.hasFaceUV=%d" % mesh.hasFaceUV()
print "Debug: mesh.hasVertexColours=%d" % mesh.hasVertexColours()
print "Debug: mesh.verts=%d" % len(mesh.verts)
print "Debug: mesh.faces=%d" % len(mesh.faces)
print "Debug: mesh.materials=%d" % len(mesh.materials)
def rgbToFS(self, c):
s="%s %s %s" % (
round(c.r/255.0,self.cp),
round(c.g/255.0,self.cp),
round(c.b/255.0,self.cp))
return s
def computeDirection(self, object):
x,y,z=(0,-1.0,0) # point down
ax,ay,az = (object.RotX,object.RotZ,object.RotY)
# rot X
x1=x
y1=y*math.cos(ax)-z*math.sin(ax)
z1=y*math.sin(ax)+z*math.cos(ax)
# rot Y
x2=x1*math.cos(ay)+z1*math.sin(ay)
y2=y1
z2=z1*math.cos(ay)-x1*math.sin(ay)
# rot Z
x3=x2*math.cos(az)-y2*math.sin(az)
y3=x2*math.sin(az)+y2*math.cos(az)
z3=z2
return [x3,y3,z3]
# swap Y and Z to handle axis difference between Blender and VRML
#------------------------------------------------------------------------
def rotatePointForVRML(self, v):
x = v[0]
y = v[2]
z = -v[1]
vrmlPoint=[x, y, z]
return vrmlPoint
def rotVertex(self, mm, v):
lx,ly,lz=v[0],v[1],v[2]
gx=(mm[0][0]*lx + mm[1][0]*ly + mm[2][0]*lz) + mm[3][0]
gy=((mm[0][2]*lx + mm[1][2]*ly+ mm[2][2]*lz) + mm[3][2])
gz=-((mm[0][1]*lx + mm[1][1]*ly + mm[2][1]*lz) + mm[3][1])
rotatedv=[gx,gy,gz]
return rotatedv
def writeIndented(self, s, inc=0):
if inc < 1:
self.indentLevel = self.indentLevel + inc
#endif
spaces=""
for x in xrange(self.indentLevel):
spaces = spaces + " "
#endfor
self.file.write(spaces + s)
if inc > 0:
self.indentLevel = self.indentLevel + inc
#endif
# Converts a Euler to three new Quaternions
# Angles of Euler are passed in as radians
#------------------------------------------------------------------------
def eulerToQuaternions(self, x, y, z):
Qx = [math.cos(x/2), math.sin(x/2), 0, 0]
Qy = [math.cos(y/2), 0, math.sin(y/2), 0]
Qz = [math.cos(z/2), 0, 0, math.sin(z/2)]
quaternionVec=[Qx,Qy,Qz]
return quaternionVec
# Multiply two Quaternions together to get a new Quaternion
#------------------------------------------------------------------------
def multiplyQuaternions(self, Q1, Q2):
result = [((Q1[0] * Q2[0]) - (Q1[1] * Q2[1]) - (Q1[2] * Q2[2]) - (Q1[3] * Q2[3])),
((Q1[0] * Q2[1]) + (Q1[1] * Q2[0]) + (Q1[2] * Q2[3]) - (Q1[3] * Q2[2])),
((Q1[0] * Q2[2]) + (Q1[2] * Q2[0]) + (Q1[3] * Q2[1]) - (Q1[1] * Q2[3])),
((Q1[0] * Q2[3]) + (Q1[3] * Q2[0]) + (Q1[1] * Q2[2]) - (Q1[2] * Q2[1]))]
return result
# Convert a Quaternion to an Angle Axis (ax, ay, az, angle)
# angle is in radians
#------------------------------------------------------------------------
def quaternionToAngleAxis(self, Qf):
scale = math.pow(Qf[1],2) + math.pow(Qf[2],2) + math.pow(Qf[3],2)
ax = Qf[1]
ay = Qf[2]
az = Qf[3]
if scale > .0001:
ax/=scale
ay/=scale
az/=scale
#endif
angle = 2 * math.acos(Qf[0])
result = [ax, ay, az, angle]
return result
def file_callback(filename):
if filename.find('.wrl', -4) < 0: filename += '.wrl'
wrlexport=VRML2Export(filename)
scene = Blender.Scene.getCurrent()
wrlexport.export(scene)
#enddef
#------------------------------------------------------------------------
# main routine
#------------------------------------------------------------------------
try:
ARG = __script__['arg'] # user selected argument
except:
print "older version"
if Blender.Get('version') < 225:
print "Warning: VRML2 export failed, wrong blender version!"
print " You aren't running blender version 2.25 or greater"
print " download a newer version from http://blender.org/"
else:
if ARG == 'all' or ARG == 'selected':
Blender.Window.FileSelector(file_callback,"Export VRML 2.0")
else:
baseFileName=Blender.Get('filename')
if baseFileName.find('.') != -1:
dots=Blender.Get('filename').split('.')[0:-1]
else:
dots=[baseFileName]
#endif
dots+=["wrl"]
vrmlFile=".".join(dots)
file_callback(vrmlFile)
#endif
#endif
|