Newer
Older
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
child = vrmlNode(self, NODE_NORMAL, -1)
child.setRoot(url) # initialized dicts
child.parse(0)
# if self.getExternprotoName():
if self.getExternprotoName():
if not extern_key: # if none is spesified - use the name
extern_key = self.getSpec()
if extern_key:
self.children.remove(child)
child.parent = None
extern_child = child.findSpecRecursive(extern_key)
if extern_child:
self.children.append(extern_child)
extern_child.parent = self
if DEBUG:
print("\tEXTERNPROTO ID found!:", extern_key)
else:
print("\tEXTERNPROTO ID not found!:", extern_key)
# Watch it! - restore lines
lines[:] = lines_old
return new_i
def __parse(self, i, IS_PROTO_DATA=False):
'''
print('parsing at', i, end="")
print(i, self.id, self.lineno)
'''
l = lines[i]
if l == '[':
# An anonymous list
self.id = None
i += 1
else:
words = []
node_type, new_i = is_nodeline(i, words)
if not node_type: # fail for parsing new node.
print("Failed to parse new node")
raise ValueError
if self.node_type == NODE_REFERENCE:
# Only assign the reference and quit
key = words[words.index('USE') + 1]
self.id = (words[0],)
self.reference = self.getDefDict()[key]
return new_i
self.id = tuple(words)
# fill in DEF/USE
key = self.getDefName()
if key != None:
self.getDefDict()[key] = self
key = self.getProtoName()
if not key:
key = self.getExternprotoName()
proto_dict = self.getProtoDict()
if key != None:
proto_dict[key] = self
# Parse the proto nodes fields
self.proto_node = vrmlNode(self, NODE_ARRAY, new_i)
new_i = self.proto_node.parse(new_i)
self.children.remove(self.proto_node)
# print(self.proto_node)
new_i += 1 # skip past the {
else: # If we're a proto instance, add the proto node as our child.
spec = self.getSpec()
try:
self.children.append(proto_dict[spec])
#pass
except:
pass
del spec
del proto_dict, key
i = new_i
# print(self.id)
ok = True
while ok:
if i >= len(lines):
return len(lines) - 1
l = lines[i]
# print('\tDEBUG:', i, self.node_type, l)
if l == '':
i += 1
continue
if l == '}':
if self.node_type != NODE_NORMAL: # also ends proto nodes, we may want a type for these too.
print('wrong node ending, expected an } ' + str(i) + ' ' + str(self.node_type))
if DEBUG:
raise ValueError
### print("returning", i)
return i + 1
if l == ']':
if self.node_type != NODE_ARRAY:
print('wrong node ending, expected a ] ' + str(i) + ' ' + str(self.node_type))
if DEBUG:
raise ValueError
### print("returning", i)
return i + 1
node_type, new_i = is_nodeline(i, [])
if node_type: # check text\n{
child = vrmlNode(self, node_type, i)
i = child.parse(i)
elif l == '[': # some files have these anonymous lists
child = vrmlNode(self, NODE_ARRAY, i)
i = child.parse(i)
elif is_numline(i):
l_split = l.split(',')
values = None
# See if each item is a float?
for num_type in (int, float):
try:
values = [num_type(v) for v in l_split]
break
except:
pass
try:
values = [[num_type(v) for v in segment.split()] for segment in l_split]
break
except:
pass
if values == None: # dont parse
values = l_split
# This should not extend over multiple lines however it is possible
# print(self.array_data)
if values:
self.array_data.extend(values)
i += 1
else:
words = l.split()
if len(words) > 2 and words[1] == 'USE':
vrmlNode(self, NODE_REFERENCE, i)
else:
# print("FIELD", i, l)
#
#words = l.split()
### print('\t\ttag', i)
# this is a tag/
# print(words, i, l)
value = l
# print(i)
# javastrips can exist as values.
quote_count = l.count('"')
if quote_count % 2: # odd number?
# print('MULTILINE')
while 1:
i += 1
l = lines[i]
quote_count = l.count('"')
if quote_count % 2: # odd number?
value += '\n' + l[:l.rfind('"')]
break # assume
else:
value += '\n' + l
value_all = value.split()
def iskey(k):
if k[0] != '"' and k[0].isalpha() and k.upper() not in ('TRUE', 'FALSE'):
return True
return False
def split_fields(value):
'''
key 0.0 otherkey 1,2,3 opt1 opt1 0.0
-> [key 0.0], [otherkey 1,2,3], [opt1 opt1 0.0]
'''
field_list = []
field_context = []
for j in range(len(value)):
if iskey(value[j]):
if field_context:
# this IS a key but the previous value was not a key, ot it was a defined field.
if (not iskey(field_context[-1])) or ((len(field_context) == 3 and field_context[1] == 'IS')):
field_list.append(field_context)
field_context = [value[j]]
else:
# The last item was not a value, multiple keys are needed in some cases.
field_context.append(value[j])
else:
# Is empty, just add this on
field_context.append(value[j])
else:
# Add a value to the list
field_context.append(value[j])
if field_context:
field_list.append(field_context)
return field_list
for value in split_fields(value_all):
# Split
if value[0] == 'field':
# field SFFloat creaseAngle 4
self.proto_field_defs.append(value)
else:
self.fields.append(value)
i += 1
def gzipOpen(path):
try:
import gzip
except:
gzip = None
data = None
if gzip:
try:
data = gzip.open(path, 'r').read()
except:
pass
else:
print('\tNote, gzip module could not be imported, compressed files will fail to load')
if data == None:
try:
data = open(path, 'rU').read()
except:
pass
return data
def vrml_parse(path):
'''
Sets up the root node and returns it so load_web3d() can deal with the blender side of things.
Return root (vrmlNode, '') or (None, 'Error String')
'''
data = gzipOpen(path)
if data == None:
return None, 'Failed to open file: ' + path
# Stripped above
lines[:] = vrmlFormat(data)
lines.insert(0, '{')
lines.insert(0, 'dymmy_node')
lines.append('}')
# Use for testing our parsed output, so we can check on line numbers.
'''
ff = open('/tmp/test.txt', 'w')
ff.writelines([l+'\n' for l in lines])
ff.close()
'''
# Now evaluate it
node_type, new_i = is_nodeline(0, [])
if not node_type:
return None, 'Error: VRML file has no starting Node'
# Trick to make sure we get all root nodes.
lines.insert(0, '{')
lines.insert(0, 'root_node____') # important the name starts with an ascii char
lines.append('}')
root = vrmlNode(None, NODE_NORMAL, -1)
root.setRoot(path) # we need to set the root so we have a namespace and know the path incase of inlineing
# Parse recursively
root.parse(0)
# This prints a load of text
if DEBUG:
print(root)
return root, ''
# ====================== END VRML
# ====================== X3d Support
# Sane as vrml but replace the parser
class x3dNode(vrmlNode):
def __init__(self, parent, node_type, x3dNode):
vrmlNode.__init__(self, parent, node_type, -1)
self.x3dNode = x3dNode
def parse(self, IS_PROTO_DATA=False):
# print(self.x3dNode.tagName)
define = self.x3dNode.getAttributeNode('DEF')
if define:
self.getDefDict()[define.value] = self
else:
use = self.x3dNode.getAttributeNode('USE')
if use:
try:
self.reference = self.getDefDict()[use.value]
self.node_type = NODE_REFERENCE
except:
print('\tWarning: reference', use.value, 'not found')
self.parent.children.remove(self)
return
for x3dChildNode in self.x3dNode.childNodes:
if x3dChildNode.nodeType in (x3dChildNode.TEXT_NODE, x3dChildNode.COMMENT_NODE, x3dChildNode.CDATA_SECTION_NODE):
continue
node_type = NODE_NORMAL
# print(x3dChildNode, dir(x3dChildNode))
if x3dChildNode.getAttributeNode('USE'):
node_type = NODE_REFERENCE
child = x3dNode(self, node_type, x3dChildNode)
child.parse()
# TODO - x3d Inline
def getSpec(self):
return self.x3dNode.tagName # should match vrml spec
def getDefName(self):
data = self.x3dNode.getAttributeNode('DEF')
if data:
data.value # XXX, return??
return None
# Other funcs operate from vrml, but this means we can wrap XML fields, still use nice utility funcs
# getFieldAsArray getFieldAsBool etc
def getFieldName(self, field, ancestry, AS_CHILD=False):
# ancestry and AS_CHILD are ignored, only used for VRML now
self_real = self.getRealNode() # incase we're an instance
field_xml = self.x3dNode.getAttributeNode(field)
if field_xml:
value = field_xml.value
# We may want to edit. for x3d spesific stuff
# Sucks a bit to return the field name in the list but vrml excepts this :/
return value.split()
else:
return None
def x3d_parse(path):
'''
Sets up the root node and returns it so load_web3d() can deal with the blender side of things.
Return root (x3dNode, '') or (None, 'Error String')
'''
try:
import xml.dom.minidom
except:
return None, 'Error, import XML parsing module (xml.dom.minidom) failed, install python'
'''
try: doc = xml.dom.minidom.parse(path)
except: return None, 'Could not parse this X3D file, XML error'
'''
# Could add a try/except here, but a console error is more useful.
data = gzipOpen(path)
if data == None:
return None, 'Failed to open file: ' + path
doc = xml.dom.minidom.parseString(data)
try:
x3dnode = doc.getElementsByTagName('X3D')[0]
except:
return None, 'Not a valid x3d document, cannot import'
root = x3dNode(None, NODE_NORMAL, x3dnode)
root.setRoot(path) # so images and Inline's we load have a relative path
root.parse()
return root, ''
## f = open('/_Cylinder.wrl', 'r')
# f = open('/fe/wrl/Vrml/EGS/TOUCHSN.WRL', 'r')
# vrml_parse('/fe/wrl/Vrml/EGS/TOUCHSN.WRL')
#vrml_parse('/fe/wrl/Vrml/EGS/SCRIPT.WRL')
'''
import os
files = os.popen('find /fe/wrl -iname "*.wrl"').readlines()
files.sort()
tot = len(files)
for i, f in enumerate(files):
#if i < 801:
# continue
f = f.strip()
print(f, i, tot)
vrml_parse(f)
'''
# NO BLENDER CODE ABOVE THIS LINE.
# -----------------------------------------------------------------------------------
import bpy
import image_utils
# import BPyImage
# import BPySys
# reload(BPySys)
# reload(BPyImage)
# import Blender
# from Blender import Texture, Material, Mathutils, Mesh, Types, Window
from mathutils import Vector, Matrix
RAD_TO_DEG = 57.29578
GLOBALS = {'CIRCLE_DETAIL': 16}
def translateRotation(rot):
''' axis, angle '''
return Matrix.Rotation(rot[3], 4, Vector(rot[:3]))
def translateScale(sca):
mat = Matrix() # 4x4 default
mat[0][0] = sca[0]
mat[1][1] = sca[1]
mat[2][2] = sca[2]
return mat
def translateTransform(node, ancestry):
cent = node.getFieldAsFloatTuple('center', None, ancestry) # (0.0, 0.0, 0.0)
rot = node.getFieldAsFloatTuple('rotation', None, ancestry) # (0.0, 0.0, 1.0, 0.0)
sca = node.getFieldAsFloatTuple('scale', None, ancestry) # (1.0, 1.0, 1.0)
scaori = node.getFieldAsFloatTuple('scaleOrientation', None, ancestry) # (0.0, 0.0, 1.0, 0.0)
tx = node.getFieldAsFloatTuple('translation', None, ancestry) # (0.0, 0.0, 0.0)
if cent:
cent_mat = Matrix.Translation(cent)
cent_imat = cent_mat.inverted()
else:
cent_mat = cent_imat = None
if rot:
rot_mat = translateRotation(rot)
else:
rot_mat = None
if sca:
sca_mat = translateScale(sca)
else:
sca_mat = None
if scaori:
scaori_mat = translateRotation(scaori)
scaori_imat = scaori_mat.inverted()
else:
scaori_mat = scaori_imat = None
if tx:
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
else:
tx_mat = None
new_mat = Matrix()
mats = [tx_mat, cent_mat, rot_mat, scaori_mat, sca_mat, scaori_imat, cent_imat]
for mtx in mats:
if mtx:
new_mat = new_mat * mtx
return new_mat
def translateTexTransform(node, ancestry):
cent = node.getFieldAsFloatTuple('center', None, ancestry) # (0.0, 0.0)
rot = node.getFieldAsFloat('rotation', None, ancestry) # 0.0
sca = node.getFieldAsFloatTuple('scale', None, ancestry) # (1.0, 1.0)
tx = node.getFieldAsFloatTuple('translation', None, ancestry) # (0.0, 0.0)
if cent:
# cent is at a corner by default
cent_mat = Matrix.Translation(Vector(cent).to_3d())
else:
cent_mat = cent_imat = None
if rot:
rot_mat = Matrix.Rotation(rot, 4, 'Z') # translateRotation(rot)
else:
rot_mat = None
if sca:
sca_mat = translateScale((sca[0], sca[1], 0.0))
else:
sca_mat = None
if tx:
tx_mat = Matrix.Translation(Vector(tx).to_3d())
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
else:
tx_mat = None
new_mat = Matrix()
# as specified in VRML97 docs
mats = [cent_imat, sca_mat, rot_mat, cent_mat, tx_mat]
for mtx in mats:
if mtx:
new_mat = new_mat * mtx
return new_mat
# 90d X rotation
import math
MATRIX_Z_TO_Y = Matrix.Rotation(math.pi / 2.0, 4, 'X')
def getFinalMatrix(node, mtx, ancestry):
transform_nodes = [node_tx for node_tx in ancestry if node_tx.getSpec() == 'Transform']
if node.getSpec() == 'Transform':
transform_nodes.append(node)
transform_nodes.reverse()
if mtx is None:
mtx = Matrix()
for node_tx in transform_nodes:
mat = translateTransform(node_tx, ancestry)
mtx = mat * mtx
# worldspace matrix
mtx = MATRIX_Z_TO_Y * mtx
return mtx
def importMesh_IndexedFaceSet(geom, bpyima, ancestry):
# print(geom.lineno, geom.id, vrmlNode.DEF_NAMESPACE.keys())
ccw = geom.getFieldAsBool('ccw', True, ancestry)
ifs_colorPerVertex = geom.getFieldAsBool('colorPerVertex', True, ancestry) # per vertex or per face
ifs_normalPerVertex = geom.getFieldAsBool('normalPerVertex', True, ancestry)
# This is odd how point is inside Coordinate
# VRML not x3d
#coord = geom.getChildByName('coord') # 'Coordinate'
coord = geom.getChildBySpec('Coordinate') # works for x3d and vrml
if coord:
ifs_points = coord.getFieldAsArray('point', 3, ancestry)
else:
coord = []
if not coord:
print('\tWarnint: IndexedFaceSet has no points')
return None, ccw
ifs_faces = geom.getFieldAsArray('coordIndex', 0, ancestry)
coords_tex = None
if ifs_faces: # In rare cases this causes problems - no faces but UVs???
# WORKS - VRML ONLY
# coords_tex = geom.getChildByName('texCoord')
coords_tex = geom.getChildBySpec('TextureCoordinate')
if coords_tex:
ifs_texpoints = coords_tex.getFieldAsArray('point', 2, ancestry)
ifs_texfaces = geom.getFieldAsArray('texCoordIndex', 0, ancestry)
if not ifs_texpoints:
# IF we have no coords, then dont bother
coords_tex = None
# WORKS - VRML ONLY
# vcolor = geom.getChildByName('color')
vcolor = geom.getChildBySpec('Color')
vcolor_spot = None # spot color when we dont have an array of colors
if vcolor:
# float to char
ifs_vcol = [(0, 0, 0)] # EEKADOODLE - vertex start at 1
ifs_vcol.extend([col for col in vcolor.getFieldAsArray('color', 3, ancestry)])
ifs_color_index = geom.getFieldAsArray('colorIndex', 0, ancestry)
if not ifs_vcol:
vcolor_spot = vcolor.getFieldAsFloatTuple('color', [], ancestry)
# Convert faces into somthing blender can use
edges = []
# All lists are aligned!
faces = []
faces_uv = [] # if ifs_texfaces is empty then the faces_uv will match faces exactly.
faces_orig_index = [] # for ngons, we need to know our original index
if coords_tex and ifs_texfaces:
do_uvmap = True
else:
do_uvmap = False
# current_face = [0] # pointer anyone
def add_face(face, fuvs, orig_index):
l = len(face)
if l == 3 or l == 4:
faces.append(face)
# faces_orig_index.append(current_face[0])
if do_uvmap:
faces_uv.append(fuvs)
faces_orig_index.append(orig_index)
elif l == 2:
edges.append(face)
elif l > 4:
for i in range(2, len(face)):
faces.append([face[0], face[i - 1], face[i]])
if do_uvmap:
faces_uv.append([fuvs[0], fuvs[i - 1], fuvs[i]])
faces_orig_index.append(orig_index)
else:
# faces with 1 verts? pfft!
# still will affect index ordering
pass
face = []
fuvs = []
orig_index = 0
for i, fi in enumerate(ifs_faces):
# ifs_texfaces and ifs_faces should be aligned
if fi != -1:
# face.append(int(fi)) # in rare cases this is a float
# EEKADOODLE!!!
# Annoyance where faces that have a zero index vert get rotated. This will then mess up UVs and VColors
face.append(int(fi) + 1) # in rare cases this is a float, +1 because of stupid EEKADOODLE :/
if do_uvmap:
if i >= len(ifs_texfaces):
print('\tWarning: UV Texface index out of range')
fuvs.append(ifs_texfaces[0])
else:
fuvs.append(ifs_texfaces[i])
else:
add_face(face, fuvs, orig_index)
face = []
if do_uvmap:
fuvs = []
orig_index += 1
add_face(face, fuvs, orig_index)
del add_face # dont need this func anymore
bpymesh = bpy.data.meshes.new(name="XXX")
# EEKADOODLE
bpymesh.vertices.add(1 + (len(ifs_points)))
bpymesh.vertices.foreach_set("co", [0, 0, 0] + [a for v in ifs_points for a in v]) # XXX25 speed
# print(len(ifs_points), faces, edges, ngons)
try:
bpymesh.faces.add(len(faces))
bpymesh.faces.foreach_set("vertices_raw", [a for f in faces for a in (f + [0] if len(f) == 3 else f)]) # XXX25 speed
except KeyError:
print("one or more vert indices out of range. corrupt file?")
#for f in faces:
# bpymesh.faces.extend(faces, smooth=True)
Campbell Barton
committed
bpymesh.validate()
bpymesh.update()
if len(bpymesh.faces) != len(faces):
print('\tWarning: adding faces did not work! file is invalid, not adding UVs or vcolors')
return bpymesh, ccw
# Apply UVs if we have them
if not do_uvmap:
faces_uv = faces # fallback, we didnt need a uvmap in the first place, fallback to the face/vert mapping.
if coords_tex:
#print(ifs_texpoints)
# print(geom)
uvlay = bpymesh.uv_textures.new()
for i, f in enumerate(uvlay.data):
f.image = bpyima
fuv = faces_uv[i] # uv indices
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
for j, uv in enumerate(f.uv):
# print(fuv, j, len(ifs_texpoints))
try:
f.uv[j] = ifs_texpoints[fuv[j]] # XXX25, speedup
except:
print('\tWarning: UV Index out of range')
f.uv[j] = ifs_texpoints[0] # XXX25, speedup
elif bpyima and len(bpymesh.faces):
# Oh Bugger! - we cant really use blenders ORCO for for texture space since texspace dosnt rotate.
# we have to create VRML's coords as UVs instead.
# VRML docs
'''
If the texCoord field is NULL, a default texture coordinate mapping is calculated using the local
coordinate system bounding box of the shape. The longest dimension of the bounding box defines the S coordinates,
and the next longest defines the T coordinates. If two or all three dimensions of the bounding box are equal,
ties shall be broken by choosing the X, Y, or Z dimension in that order of preference.
The value of the S coordinate ranges from 0 to 1, from one end of the bounding box to the other.
The T coordinate ranges between 0 and the ratio of the second greatest dimension of the bounding box to the greatest dimension.
'''
# Note, S,T == U,V
# U gets longest, V gets second longest
xmin, ymin, zmin = ifs_points[0]
xmax, ymax, zmax = ifs_points[0]
for co in ifs_points:
x, y, z = co
if x < xmin:
xmin = x
if y < ymin:
ymin = y
if z < zmin:
zmin = z
if x > xmax:
xmax = x
if y > ymax:
ymax = y
if z > zmax:
zmax = z
xlen = xmax - xmin
ylen = ymax - ymin
zlen = zmax - zmin
depth_min = xmin, ymin, zmin
depth_list = [xlen, ylen, zlen]
depth_sort = depth_list[:]
depth_sort.sort()
depth_idx = [depth_list.index(val) for val in depth_sort]
axis_u = depth_idx[-1]
axis_v = depth_idx[-2] # second longest
# Hack, swap these !!! TODO - Why swap??? - it seems to work correctly but should not.
# axis_u,axis_v = axis_v,axis_u
min_u = depth_min[axis_u]
min_v = depth_min[axis_v]
depth_u = depth_list[axis_u]
depth_v = depth_list[axis_v]
depth_list[axis_u]
if axis_u == axis_v:
# This should be safe because when 2 axies have the same length, the lower index will be used.
axis_v += 1
uvlay = bpymesh.uv_textures.new()
# HACK !!! - seems to be compatible with Cosmo though.
depth_v = depth_u = max(depth_v, depth_u)
bpymesh_vertices = bpymesh.vertices[:]
bpymesh_faces = bpymesh.faces[:]
for j, f in enumerate(uvlay.data):
f.image = bpyima
fuv = f.uv
f_v = bpymesh_faces[j].vertices[:] # XXX25 speed
for i, v in enumerate(f_v):
co = bpymesh_vertices[v].co
fuv[i] = (co[axis_u] - min_u) / depth_u, (co[axis_v] - min_v) / depth_v
# Add vcote
if vcolor:
# print(ifs_vcol)
collay = bpymesh.vertex_colors.new()
for f_idx, f in enumerate(collay.data):
fv = bpymesh.faces[f_idx].vertices[:]
if len(fv) == 3: # XXX speed
fcol = f.color1, f.color2, f.color3
else:
fcol = f.color1, f.color2, f.color3, f.color4
if ifs_colorPerVertex:
for i, c in enumerate(fcol):
color_index = fv[i] # color index is vert index
if ifs_color_index:
try:
color_index = ifs_color_index[color_index]
except:
print('\tWarning: per vertex color index out of range')
continue
if color_index < len(ifs_vcol):
c.r, c.g, c.b = ifs_vcol[color_index]
else:
#print('\tWarning: per face color index out of range')
pass
else:
if vcolor_spot: # use 1 color, when ifs_vcol is []
for c in fcol:
c.r, c.g, c.b = vcolor_spot
else:
color_index = faces_orig_index[f_idx] # color index is face index
#print(color_index, ifs_color_index)
if ifs_color_index:
if color_index >= len(ifs_color_index):
print('\tWarning: per face color index out of range')
color_index = 0
else:
color_index = ifs_color_index[color_index]
try:
col = ifs_vcol[color_index]
except IndexError:
# TODO, look
col = (1.0, 1.0, 1.0)
for i, c in enumerate(fcol):
c.r, c.g, c.b = col
# XXX25
# bpymesh.vertices.delete([0, ]) # EEKADOODLE
return bpymesh, ccw
def importMesh_IndexedLineSet(geom, ancestry):
# VRML not x3d
#coord = geom.getChildByName('coord') # 'Coordinate'
coord = geom.getChildBySpec('Coordinate') # works for x3d and vrml
if coord:
points = coord.getFieldAsArray('point', 3, ancestry)
else:
points = []
if not points:
print('\tWarning: IndexedLineSet had no points')
return None
ils_lines = geom.getFieldAsArray('coordIndex', 0, ancestry)
lines = []
line = []
for il in ils_lines:
if il == -1:
lines.append(line)
line = []
else:
line.append(int(il))
lines.append(line)
# vcolor = geom.getChildByName('color') # blender dosnt have per vertex color
bpycurve = bpy.data.curves.new('IndexedCurve', 'CURVE')
bpycurve.dimensions = '3D'
for line in lines:
if not line:
continue
co = points[line[0]]
nu = bpycurve.splines.new('POLY')
nu.points.add(len(line))
for il, pt in zip(line, nu.points):
pt.co[0:3] = points[il]
return bpycurve
def importMesh_PointSet(geom, ancestry):
# VRML not x3d
#coord = geom.getChildByName('coord') # 'Coordinate'
coord = geom.getChildBySpec('Coordinate') # works for x3d and vrml
if coord:
points = coord.getFieldAsArray('point', 3, ancestry)
else:
points = []
# vcolor = geom.getChildByName('color') # blender dosnt have per vertex color
bpymesh = bpy.data.meshes.new("XXX")
bpymesh.vertices.add(len(points))
bpymesh.vertices.foreach_set("co", [a for v in points for a in v])
Campbell Barton
committed
# No need to validate
bpymesh.update()
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
return bpymesh
GLOBALS['CIRCLE_DETAIL'] = 12
def bpy_ops_add_object_hack(): # XXX25, evil
scene = bpy.context.scene
obj = scene.objects[0]
scene.objects.unlink(obj)
bpymesh = obj.data
bpy.data.objects.remove(obj)
return bpymesh
def importMesh_Sphere(geom, ancestry):
diameter = geom.getFieldAsFloat('radius', 0.5, ancestry)
# bpymesh = Mesh.Primitives.UVsphere(GLOBALS['CIRCLE_DETAIL'], GLOBALS['CIRCLE_DETAIL'], diameter)
bpy.ops.mesh.primitive_uv_sphere_add(segments=GLOBALS['CIRCLE_DETAIL'],
ring_count=GLOBALS['CIRCLE_DETAIL'],
size=diameter,
view_align=False,
enter_editmode=False,
)
bpymesh = bpy_ops_add_object_hack()
bpymesh.transform(MATRIX_Z_TO_Y)
return bpymesh
def importMesh_Cylinder(geom, ancestry):
# bpymesh = bpy.data.meshes.new()
diameter = geom.getFieldAsFloat('radius', 1.0, ancestry)
height = geom.getFieldAsFloat('height', 2, ancestry)
# bpymesh = Mesh.Primitives.Cylinder(GLOBALS['CIRCLE_DETAIL'], diameter, height)
bpy.ops.mesh.primitive_cylinder_add(vertices=GLOBALS['CIRCLE_DETAIL'],
radius=diameter,
depth=height,
cap_ends=True,
view_align=False,
enter_editmode=False,
)
bpymesh = bpy_ops_add_object_hack()
bpymesh.transform(MATRIX_Z_TO_Y)
# Warning - Rely in the order Blender adds verts
# not nice design but wont change soon.
bottom = geom.getFieldAsBool('bottom', True, ancestry)
side = geom.getFieldAsBool('side', True, ancestry)
top = geom.getFieldAsBool('top', True, ancestry)
if not top: # last vert is top center of tri fan.
# bpymesh.vertices.delete([(GLOBALS['CIRCLE_DETAIL'] + GLOBALS['CIRCLE_DETAIL']) + 1]) # XXX25
pass
if not bottom: # second last vert is bottom of triangle fan
# XXX25
# bpymesh.vertices.delete([GLOBALS['CIRCLE_DETAIL'] + GLOBALS['CIRCLE_DETAIL']])
pass
if not side:
# remove all quads
# XXX25
# bpymesh.faces.delete(1, [f for f in bpymesh.faces if len(f) == 4])
pass
return bpymesh
def importMesh_Cone(geom, ancestry):
# bpymesh = bpy.data.meshes.new()
diameter = geom.getFieldAsFloat('bottomRadius', 1.0, ancestry)
height = geom.getFieldAsFloat('height', 2, ancestry)