Skip to content

Commit

Permalink
Merge pull request #7 from TombstoneTumbleweedArt/experimental
Browse files Browse the repository at this point in the history
Experimental
  • Loading branch information
TombstoneTumbleweedArt authored Apr 3, 2022
2 parents e8c9055 + e3af0e0 commit 1b3323f
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 77 deletions.
105 changes: 29 additions & 76 deletions Blender_Files/import_ply.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,9 @@


# ######### CHANGELOG ########
#
# v2.1 - Refactored the Brad Patch to theoretically accept any sort of weird ply file by only
# extracting the named color data (rgb[a]) from colindices.
#
# v2.0 - Reintegrated the original importer and added Verts/Colors as load option. Now correctly loads:
#
Expand Down Expand Up @@ -63,6 +66,7 @@ def __init__(self, name, count):
self.count = count
self.properties = []


def load(self, format, stream):
if format == b'ascii':
stream = stream.readline().split()
Expand All @@ -87,6 +91,7 @@ def __init__(self, name, list_type, numeric_type):
self.list_type = list_type
self.numeric_type = numeric_type


def read_format(self, format, count, num_type, stream):
import struct

Expand All @@ -107,6 +112,7 @@ def read_format(self, format, count, num_type, stream):
else:
mapper = int
ans = [mapper(x) for x in stream[:count]]
# pop the buffer stack
stream[:count] = []
return ans
else:
Expand Down Expand Up @@ -149,7 +155,7 @@ def load(self, format, stream):
for i in self.specs
}


# 28 March 2022 - this function reads and parses the ply header
def read(self, filepath):
import re

Expand Down Expand Up @@ -294,18 +300,20 @@ def _plyf_header_line_iterator(plyf):
print("Invalid header ('end_header' line not found!)")
return invalid_ply

obj = obj_spec.load(format_specs[format], plyf)


# 1 April 2022 - Moved these two conditions here
# If user attempts to load point cloud as mesh, flip the bit
# ISSUE - Feb 20, 2022
# Case 1 - Only verts in file
if len(obj) < 2:
if len(obj_spec.specs) < 2:
self.use_verts = True

# Case 2 - 'element face 0' in file (JWF, we see you!)
elif len(obj[b'face']) == 0:
elif (obj_spec.specs[1].count == 0):
self.use_verts = True


obj = obj_spec.load(format_specs[format], plyf)

return obj_spec, obj, texture


Expand All @@ -320,14 +328,14 @@ def load_ply_mesh(self, filepath, ply_name):
return

# If attempting to load a point cloud file as mesh, import as verts instead and bail out
# ISSUE - Feb 20, 2022
if self.use_verts == True:
mesh = load_ply_verts(self, filepath, ply_name)
else:

uvindices = colindices = None
colmultiply = None

# MP Comment - below comments are left from stock importer
# TODO import normals
# noindices = None

Expand Down Expand Up @@ -500,7 +508,6 @@ def add_face(vertices, indices, uvindices, colindices):
def load_ply_verts(self, filepath, ply_name):
import bpy

# ISSUE - Feb 20, 2022
obj_spec, obj, texture = read(self, filepath)

if obj is None:
Expand Down Expand Up @@ -533,39 +540,8 @@ def load_ply_verts(self, filepath, ply_name):

mesh_uvs = []
mesh_colors = []

verts = obj[b'vertex']

################## ITS ALL IN THE verts OBJECT
# [0] = x pos
# [1] = y pos
# [2] = z pos
# [3] = x norm * If present
# [4] = y norm
# [5] = z norm
# [6] = r color * Will start at [3] if no normals found
# [7] = g color
# [8] = b color
# [9] = a color

# If len(verts[0]) is greater than 7, we have normals
vertlength = len(verts[0])


# BRAD PATCH v2.0: JWF occasionally spits out len(9) files but so does BTracer2, albeit for different reasons. This creates an index clash here as we need both
# unique solutions for len(9)

# Fix: If verts[3] is a float, normals = true
# by implication, anything other is a JWF

if vertlength > 7:
# Bug Fix, 9 Mar 2022, changed <10 to <=10
if vertlength <= 10:
if isinstance(verts[0][3], float):
normals = True
else:
jwf = True


# Copy the positions
mesh = bpy.data.meshes.new(name=ply_name)
mesh.vertices.add(len(obj[b'vertex']))
Expand All @@ -581,43 +557,22 @@ def load_ply_verts(self, filepath, ply_name):


# COLOR
# If colors are found, create a new Attribute 'Col' to hold them (NOT the Vertex_Color block!)
# TODO: Make this more Pythonic
if colindices:
# If colors are found, create a new Attribute 'Col' to hold them (NOT the Vertex_Color block!)
bpy.context.active_object.data.attributes.new(name="Col", type='FLOAT_COLOR', domain='POINT')
newcolor = bpy.context.active_object.data
# If there are no normals, the color data will start at [3], otherwise [6]
for i, col in enumerate(verts):
if normals == False:
if len(colindices) == 3:
newcolor.attributes['Col'].data[i].color[0] = (verts[i][3]) / 255.0
newcolor.attributes['Col'].data[i].color[1] = (verts[i][4]) / 255.0
newcolor.attributes['Col'].data[i].color[2] = (verts[i][5]) / 255.0
else:
newcolor.attributes['Col'].data[i].color[0] = (verts[i][3]) / 255.0
newcolor.attributes['Col'].data[i].color[1] = (verts[i][4]) / 255.0
newcolor.attributes['Col'].data[i].color[2] = (verts[i][5]) / 255.0
newcolor.attributes['Col'].data[i].color[3] = (verts[i][6]) / 255.0
elif normals == True:
if len(colindices) == 3:
newcolor.attributes['Col'].data[i].color[0] = (verts[i][6]) / 255.0
newcolor.attributes['Col'].data[i].color[1] = (verts[i][7]) / 255.0
newcolor.attributes['Col'].data[i].color[2] = (verts[i][8]) / 255.0
else:
newcolor.attributes['Col'].data[i].color[0] = (verts[i][6]) / 255.0
newcolor.attributes['Col'].data[i].color[1] = (verts[i][7]) / 255.0
newcolor.attributes['Col'].data[i].color[2] = (verts[i][8]) / 255.0
newcolor.attributes['Col'].data[i].color[3] = (verts[i][9]) / 255.0
elif jwf == True:
if len(colindices) == 3:
newcolor.attributes['Col'].data[i].color[0] = (verts[i][3]) / 255.0
newcolor.attributes['Col'].data[i].color[1] = (verts[i][4]) / 255.0
newcolor.attributes['Col'].data[i].color[2] = (verts[i][5]) / 255.0
else:
newcolor.attributes['Col'].data[i].color[0] = (verts[i][3]) / 255.0
newcolor.attributes['Col'].data[i].color[1] = (verts[i][4]) / 255.0
newcolor.attributes['Col'].data[i].color[2] = (verts[i][5]) / 255.0
newcolor.attributes['Col'].data[i].color[3] = (verts[i][6]) / 255.0

if (len(colindices) <= 3):
newcolor.attributes['Col'].data[i].color[0] = (verts[i][colindices[0]]) / 255.0
newcolor.attributes['Col'].data[i].color[1] = (verts[i][colindices[1]]) / 255.0
newcolor.attributes['Col'].data[i].color[2] = (verts[i][colindices[2]]) / 255.0
else:
newcolor.attributes['Col'].data[i].color[0] = (verts[i][colindices[0]]) / 255.0
newcolor.attributes['Col'].data[i].color[1] = (verts[i][colindices[1]]) / 255.0
newcolor.attributes['Col'].data[i].color[2] = (verts[i][colindices[2]]) / 255.0
newcolor.attributes['Col'].data[i].color[3] = (verts[i][colindices[3]]) / 255.0

mesh.update()
mesh.validate()

Expand All @@ -634,9 +589,7 @@ def load_ply(self, filepath):
t = time.time()
ply_name = bpy.path.display_name_from_filepath(filepath)

# If the user clicks Ply as Verts, use that loader. Otherwise proceed as normal
print(self.use_verts)

# If the user clicks Ply as Verts, use that loader. Otherwise proceed as normal
if self.use_verts:
mesh = load_ply_verts(self, filepath, ply_name)
else:
Expand Down
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# import-ply-as-verts v2.0 &nbsp; &nbsp; &nbsp; &nbsp; [![Generic badge](https://img.shields.io/badge/Release-2.0-<COLOR>.svg)](https://shields.io/) &nbsp; &nbsp; &nbsp; &nbsp; ![Logo_Blender-Dark](https://user-images.githubusercontent.com/24717972/154959144-bd55fdc0-2ab9-43e4-8747-33c7465a9c8f.svg)
# import-ply-as-verts v2.1 &nbsp; &nbsp; &nbsp; &nbsp; [![Generic badge](https://img.shields.io/badge/Release-2.1-<COLOR>.svg)](https://shields.io/) &nbsp; &nbsp; &nbsp; &nbsp; ![Logo_Blender-Dark](https://user-images.githubusercontent.com/24717972/154959144-bd55fdc0-2ab9-43e4-8747-33c7465a9c8f.svg)
## Blender 3.0 * / 3.1 Alpha (and later) New PLY Importer
<ul>
<li> * Vertex colored mesh only in 3.0 (see <strong>Compatibility</strong>).
Expand All @@ -7,6 +7,7 @@
<li>Retains the functionality of the original codebase.</li>
</ul>

<strong>3 April 2022 - Version 2.1 Release</strong>: fixed several compatibility issues with various odd types of Ply.



Expand Down

0 comments on commit 1b3323f

Please sign in to comment.