Skip to content

Commit 42cf448

Browse files
committed
Added compatibility for split vertex normals with objs
1 parent e99f151 commit 42cf448

File tree

3 files changed

+43
-110
lines changed

3 files changed

+43
-110
lines changed

bseq/importer.py

Lines changed: 35 additions & 106 deletions
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ def apply_transformation(meshio_mesh, obj, depsgraph):
8181
# evaluate the rigid body transformations (only relevant for .bin format)
8282
rigid_body_transformation = mathutils.Matrix.Identity(4)
8383
if meshio_mesh is not None:
84-
if meshio_mesh.field_data.get("transformation_matrix") is not None:
84+
if "transformation_matrix" in meshio_mesh.field_data:
8585
rigid_body_transformation = meshio_mesh.field_data["transformation_matrix"]
8686

8787
# multiply everything together (with custom transform matrix)
@@ -142,11 +142,10 @@ def update_mesh(meshio_mesh, mesh):
142142
mesh.validate()
143143

144144
# copy attributes
145-
attributes = mesh.attributes
146145
for k, v in meshio_mesh.point_data.items():
147146
k = "bseq_" + k
148147
attribute = None
149-
if k not in attributes:
148+
if k not in mesh.attributes:
150149
if len(v.shape) == 1:
151150
# one dimensional attribute
152151
attribute = mesh.attributes.new(k, "FLOAT", "POINT")
@@ -165,7 +164,7 @@ def update_mesh(meshio_mesh, mesh):
165164
show_message_box('more than 2 dimensional tensor, ignored')
166165
continue
167166
else:
168-
attribute = attributes[k]
167+
attribute = mesh.attributes[k]
169168
name_string = None
170169
if attribute.data_type == "FLOAT":
171170
name_string = "value"
@@ -174,10 +173,36 @@ def update_mesh(meshio_mesh, mesh):
174173

175174
attribute.data.foreach_set(name_string, v.ravel())
176175

177-
# set as split norm
178-
if mesh.BSEQ.split_norm_att_name and mesh.BSEQ.split_norm_att_name == k:
179-
mesh.use_auto_smooth = True
180-
mesh.normals_split_custom_set_from_vertices(v)
176+
# # set as split norm
177+
# if mesh.BSEQ.split_norm_att_name and mesh.BSEQ.split_norm_att_name == k:
178+
# mesh.use_auto_smooth = True
179+
# mesh.normals_split_custom_set_from_vertices(v)
180+
181+
# I want to set normals if the scene property use_imported_normals is true and the normals are either in point_data["obj:vn"] or field_data["obj:vn"]
182+
if bpy.context.scene.BSEQ.use_imported_normals:
183+
print("use_imported_normals")
184+
# print all the keys in point_data, field_data, cell_data
185+
print("point_data", meshio_mesh.point_data.keys())
186+
print("field_data", meshio_mesh.field_data.keys())
187+
print("cell_data", meshio_mesh.cell_data.keys())
188+
189+
mesh.use_auto_smooth = True
190+
191+
192+
if "obj:vn" in meshio_mesh.point_data and len(meshio_mesh.point_data["obj:vn"]) == len(mesh.vertices):
193+
print("obj:vn in point_data", len(mesh.loops))
194+
# vert_norms = [tuple(x) for x in meshio_mesh.point_data["obj:vn"]]
195+
196+
mesh.normals_split_custom_set_from_vertices(meshio_mesh.point_data["obj:vn"])
197+
198+
for i in range(len(mesh.vertices)):
199+
print(mesh.vertices[i].normal)
200+
elif "obj:vn" in meshio_mesh.field_data and "obj:vn_face_idx" in meshio_mesh.cell_data:
201+
print("obj:vn in field_data")
202+
indices = meshio_mesh.cell_data["obj:vn_face_idx"][0]
203+
indices = [item for sublist in indices for item in sublist]
204+
vert_norms = [meshio_mesh.field_data["obj:vn"][i - 1] for i in indices]
205+
mesh.normals_split_custom_set(vert_norms)
181206

182207
# function to create a single meshio object
183208
def create_meshio_obj(filepath):
@@ -188,27 +213,7 @@ def create_meshio_obj(filepath):
188213
show_message_box("Error when reading: " + filepath + ",\n" + traceback.format_exc(),
189214
"Meshio Loading Error" + str(e),
190215
icon="ERROR")
191-
192-
# if filepath.endswith(".obj"):
193-
# # Save all current objects
194-
# objs = set(bpy.context.scene.objects)
195-
# # Reload the object
196-
# bpy.ops.import_scene.obj(filepath=filepath)
197-
# # Substract all previous items from the current items and print their names
198-
# imported_objs = set(bpy.context.scene.objects) - objs
199-
200-
# # Check if the imported object worked correctly
201-
# if len(imported_objs) == 1:
202-
# obj = imported_objs.pop()
203-
# else:
204-
# show_message_box("Error when reading: " + filepath + ",\n" + traceback.format_exc(),
205-
# "obj. Loading Error in create_meshio_obj",
206-
# icon="ERROR")
207-
# return
208-
209-
# obj.name = os.path.basename(filepath)
210-
# return
211-
216+
return
212217
# create the object
213218
name = os.path.basename(filepath)
214219
mesh = bpy.data.meshes.new(name)
@@ -224,38 +229,6 @@ def create_obj(fileseq, root_path, transform_matrix=Matrix([[1, 0, 0, 0], [0, 1,
224229
current_frame = bpy.context.scene.frame_current
225230
filepath = fileseq[current_frame % len(fileseq)]
226231

227-
#.obj sequences have to be handled differently
228-
# is_obj_seq = filepath.endswith(".obj")
229-
# if is_obj_seq and bpy.context.scene.BSEQ.use_blender_obj_import:
230-
231-
# # Save all current objects
232-
# objs = set(bpy.context.scene.objects)
233-
# # Reload the object
234-
# bpy.ops.import_scene.obj(filepath=filepath)
235-
# # Substract all previous items from the current items and print their names
236-
# imported_objs = set(bpy.context.scene.objects) - objs
237-
238-
# # Check if the imported object worked correctly
239-
# if len(imported_objs) == 1:
240-
# tmp_obj = imported_objs.pop()
241-
# else:
242-
# show_message_box("Error when reading: " + filepath + ",\n" + traceback.format_exc(),
243-
# "obj. Loading Error in create_obj",
244-
# icon="ERROR")
245-
# return
246-
247-
# name = fileseq.basename() + "@" + fileseq.extension()
248-
249-
# # Create object with empty mesh
250-
# object = bpy.data.objects.new(name, bpy.data.meshes.new(name))
251-
# object.data = tmp_obj.data
252-
253-
# # Delete tmp_obj with data
254-
# bpy.data.objects.remove(tmp_obj, do_unlink=True)
255-
256-
# enabled = True
257-
258-
# else:
259232
meshio_mesh = None
260233
enabled = True
261234
try:
@@ -284,13 +257,12 @@ def create_obj(fileseq, root_path, transform_matrix=Matrix([[1, 0, 0, 0], [0, 1,
284257
object.matrix_world = transform_matrix
285258
driver = object.driver_add("BSEQ.frame")
286259
driver.driver.expression = 'frame'
287-
if enabled: # and not is_obj_seq:
260+
if enabled:
288261
update_mesh(meshio_mesh, object.data)
289262
bpy.context.collection.objects.link(object)
290263
bpy.ops.object.select_all(action="DESELECT")
291264
bpy.context.view_layer.objects.active = object
292265

293-
294266
def update_obj(scene, depsgraph=None):
295267

296268
for obj in bpy.data.objects:
@@ -320,49 +292,6 @@ def update_obj(scene, depsgraph=None):
320292
# in case the blender file was created on windows system, but opened in linux system
321293
pattern = bpy.path.native_pathsep(pattern)
322294
fs = fileseq.FileSequence(pattern)
323-
324-
# if pattern.endswith(".obj") and scene.BSEQ.use_blender_obj_import:
325-
# filepath = fs[current_frame % len(fs)]
326-
327-
# # Save all current objects
328-
# objs = set(scene.objects)
329-
330-
# # Reload the object
331-
# bpy.ops.import_scene.obj(filepath=filepath)
332-
333-
# # Substract all previous items from the current items and print their names
334-
# imported_objs = set(scene.objects) - objs
335-
336-
# # Check if the imported object worked correctly
337-
# if len(imported_objs) == 1:
338-
# new_tmp_obj = imported_objs.pop()
339-
# else:
340-
# show_message_box("Error when reading: " + filepath + ",\n" + traceback.format_exc(),
341-
# "obj. Loading Error in update_obj",
342-
# icon="ERROR")
343-
# continue
344-
345-
# # Copy the data except for material
346-
# if obj.data.materials:
347-
# # assign to 1st material slot
348-
# new_tmp_obj.data.materials[0] = obj.data.materials[0]
349-
# else:
350-
# # no slots
351-
# new_tmp_obj.data.materials.append(obj.data.materials[0])
352-
353-
# obj.data = new_tmp_obj.data
354-
355-
# # Delete the temporary object with the data
356-
# bpy.data.objects.remove(new_tmp_obj, do_unlink=True)
357-
358-
# # purge old meshes
359-
# bpy.ops.outliner.orphans_purge(do_recursive=True)
360-
361-
# apply_transformation(meshio_mesh, obj, depsgraph)
362-
363-
# end_time = time.perf_counter()
364-
# obj.BSEQ.last_benchmark = (end_time - start_time) * 1000
365-
# continue
366295

367296
if obj.BSEQ.use_advance and obj.BSEQ.script_name:
368297
script = bpy.data.texts[obj.BSEQ.script_name]

bseq/messenger.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,10 @@ def selected_callback():
66
# seems like that this is not necessary
77
# if not bpy.context.view_layer.objects.active:
88
# return
9-
9+
10+
if not bpy.context.active_object:
11+
return
12+
1013
name = bpy.context.active_object.name
1114
idx = bpy.data.objects.find(name)
1215
if idx >= 0:

bseq/panels.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,18 +22,19 @@ def filter_items(self, context, data, property):
2222

2323
def draw_item(self, context, layout, data, item, icon, active_data, active_propname):
2424
if item:
25-
split = layout.split(factor=0.5)
25+
split = layout.split(factor=0.4)
2626
col1 = split.column()
2727
col2 = split.column()
2828
split2 = col2.split(factor=0.25)
2929
col2 = split2.column()
3030
col3 = split2.column()
31-
split3 = col3.split(factor=0.33)
31+
split3 = col3.split(factor=0.5)
3232
col3 = split3.column()
3333
col4 = split3.column()
34-
col4.alignment = 'CENTER'
34+
col4.alignment = 'EXPAND'
3535
start_frame = item.BSEQ.start_end_frame[0]
3636
end_frame = item.BSEQ.start_end_frame[1]
37+
3738
col1.prop(item, "name", text='', emboss=False)
3839
if item.BSEQ.enabled:
3940
col2.prop(item.BSEQ, "enabled", text="", icon="PLAY")

0 commit comments

Comments
 (0)