Skip to content

Commit 8602980

Browse files
committed
Fixed obj. loading duplicate bug
1 parent f5f6b4f commit 8602980

File tree

4 files changed

+114
-23
lines changed

4 files changed

+114
-23
lines changed

bseq/importer.py

Lines changed: 67 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -191,8 +191,23 @@ def create_meshio_obj(filepath):
191191

192192
# Do I need this for multi-loading?
193193
if filepath.endswith(".obj"):
194+
# Save all current objects
195+
objs = set(bpy.context.scene.objects)
196+
# Reload the object
194197
bpy.ops.import_scene.obj(filepath=filepath)
195-
obj = bpy.context.selected_objects[0]
198+
# Substract all previous items from the current items and print their names
199+
imported_objs = set(bpy.context.scene.objects) - objs
200+
print(", ".join(o.name for o in imported_objs))
201+
202+
# Check if the imported object worked correctly
203+
if len(imported_objs) == 1:
204+
obj = imported_objs.pop()
205+
else:
206+
show_message_box("Error when reading: " + filepath + ",\n" + traceback.format_exc(),
207+
"obj. Loading Error",
208+
icon="ERROR")
209+
return
210+
196211
obj.name = os.path.basename(filepath)
197212
return
198213
# create the object
@@ -213,16 +228,34 @@ def create_obj(fileseq, root_path, transform_matrix=Matrix([[1, 0, 0, 0], [0, 1,
213228
#.obj sequences have to be handled differently
214229
is_obj_seq = filepath.endswith(".obj")
215230
if is_obj_seq and bpy.context.scene.BSEQ.use_blender_obj_import:
231+
232+
# Save all current objects
233+
objs = set(bpy.context.scene.objects)
234+
# Reload the object
216235
bpy.ops.import_scene.obj(filepath=filepath)
217-
enabled = True
236+
# Substract all previous items from the current items and print their names
237+
imported_objs = set(bpy.context.scene.objects) - objs
238+
print(", ".join(o.name for o in imported_objs))
218239

219-
tmp_obj = bpy.context.selected_objects[-1]
240+
# Check if the imported object worked correctly
241+
if len(imported_objs) == 1:
242+
tmp_obj = imported_objs.pop()
243+
else:
244+
show_message_box("Error when reading: " + filepath + ",\n" + traceback.format_exc(),
245+
"obj. Loading Error",
246+
icon="ERROR")
247+
return
220248

221249
name = fileseq.basename() + "@" + fileseq.extension()
222-
object = bpy.data.objects.new(name, tmp_obj.data)
223250

224-
tmp_obj.select_set(True)
225-
bpy.ops.object.delete()
251+
# Create object with empty mesh
252+
object = bpy.data.objects.new(name, bpy.data.meshes.new(name))
253+
object.data = tmp_obj.data
254+
255+
# Delete tmp_obj with data
256+
bpy.data.objects.remove(tmp_obj, do_unlink=True)
257+
258+
enabled = True
226259

227260
else:
228261
meshio_mesh = None
@@ -294,13 +327,37 @@ def update_obj(scene, depsgraph=None):
294327
if pattern.endswith(".obj") and scene.BSEQ.use_blender_obj_import:
295328
filepath = fs[current_frame % len(fs)]
296329

330+
print("File path update: " + filepath)
331+
332+
# Save all current objects
333+
objs = set(scene.objects)
297334
# Reload the object
298335
bpy.ops.import_scene.obj(filepath=filepath)
299-
tmp_obj = bpy.context.selected_objects[-1]
336+
# Substract all previous items from the current items and print their names
337+
imported_objs = set(scene.objects) - objs
338+
print(", ".join(o.name for o in imported_objs))
339+
340+
# Check if the imported object worked correctly
341+
if len(imported_objs) == 1:
342+
new_tmp_obj = imported_objs.pop()
343+
else:
344+
show_message_box("Error when reading: " + filepath + ",\n" + traceback.format_exc(),
345+
"obj. Loading Error",
346+
icon="ERROR")
347+
continue
348+
349+
# Copy the data except for material
350+
if obj.data.materials:
351+
# assign to 1st material slot
352+
new_tmp_obj.data.materials[0] = obj.data.materials[0]
353+
else:
354+
# no slots
355+
new_tmp_obj.data.materials.append(obj.data.materials[0])
356+
357+
obj.data = new_tmp_obj.data
300358

301-
obj.data = tmp_obj.data
302-
tmp_obj.select_set(True)
303-
bpy.ops.object.delete()
359+
# Delete the temporary object with the data
360+
bpy.data.objects.remove(new_tmp_obj, do_unlink=True)
304361

305362
apply_transformation(meshio_mesh, obj, depsgraph)
306363

bseq/operators.py

Lines changed: 30 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -346,13 +346,33 @@ class WM_OT_batchSequences(bpy.types.Operator, ImportHelper):
346346
bl_label = "Import Sequences"
347347
bl_options = {'PRESET', 'UNDO'}
348348

349-
# filter_glob: bpy.types.StringProperty(
350-
# default="*.txt",
351-
# options={'HIDDEN'},
352-
# maxlen=255, # Max internal buffer length, longer would be clamped.
353-
# )
349+
def update_filter_glob(self, context):
350+
bpy.ops.wm.seq_import_batch('INVOKE_DEFAULT')
351+
352+
filter_string: bpy.props.StringProperty(
353+
default="*.obj",
354+
options={'HIDDEN'},
355+
update=update_filter_glob,
356+
)
357+
358+
filename_ext=''
359+
filter_glob: bpy.props.StringProperty(
360+
default='*.obj',
361+
options={'HIDDEN', 'LIBRARY_EDITABLE'},
362+
)
354363

355364
files: bpy.props.CollectionProperty(type=bpy.types.PropertyGroup)
365+
366+
def invoke(self, context, event):
367+
scene = context.scene
368+
if scene.BSEQ.filter_string:
369+
self.filter_glob = scene.BSEQ.filter_string
370+
else:
371+
self.filter_glob = "*"
372+
373+
context.window_manager.fileselect_add(self)
374+
375+
return {'RUNNING_MODAL'}
356376

357377
def execute(self, context):
358378
scene = context.scene
@@ -363,6 +383,7 @@ def execute(self, context):
363383
show_message_box("When using relative path, please save file before using it", icon="ERROR")
364384
return {"CANCELLED"}
365385

386+
self.filter_glob = '*'
366387

367388
folder = Path(self.filepath)
368389
used_seqs = set()
@@ -379,7 +400,7 @@ def execute(self, context):
379400
create_obj(matching_seqs[0], importer_prop.root_path, transform_matrix=transform_matrix)
380401
used_seqs.add(matching_seqs[0])
381402
return {'FINISHED'}
382-
403+
383404
def draw(self, context):
384405
pass
385406

@@ -403,11 +424,10 @@ def draw(self, context):
403424
layout.use_property_split = True
404425
layout.use_property_decorate = False # No animation.
405426

406-
sfile = context.space_data
407-
operator = sfile.active_operator
427+
# sfile = context.space_data
428+
# operator = sfile.active_operator
408429

409-
layout.prop(operator, 'type')
410-
layout.prop(operator, 'use_setting')
430+
layout.prop(importer_prop, 'filter_string')
411431

412432
layout.alignment = 'LEFT'
413433
layout.prop(importer_prop, "relative", text="Relative Path")

bseq/panels.py

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,19 +22,27 @@ 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.7)
25+
split = layout.split(factor=0.5)
2626
col1 = split.column()
2727
col2 = split.column()
28-
split2 = col2.split(factor=0.5)
28+
split2 = col2.split(factor=0.25)
2929
col2 = split2.column()
3030
col3 = split2.column()
31+
split3 = col3.split(factor=0.33)
32+
col3 = split3.column()
33+
col4 = split3.column()
34+
col4.alignment = 'CENTER'
35+
start_frame = item.BSEQ.start_end_frame[0]
36+
end_frame = item.BSEQ.start_end_frame[1]
3137
col1.prop(item, "name", text='', emboss=False)
3238
if item.BSEQ.enabled:
3339
col2.prop(item.BSEQ, "enabled", text="", icon="PLAY")
3440
col3.prop(item.BSEQ, "frame", text="")
41+
col4.label(text=str(start_frame) + '-' + str(end_frame))
3542
else:
3643
col2.prop(item.BSEQ, "enabled", text ="", icon="PAUSE")
3744
col3.label(text="", icon="BLANK1")
45+
col4.label(text=str(start_frame) + '-' + str(end_frame))
3846
else:
3947
# actually, I guess this line of code won't be executed?
4048
layout.label(text="", translate=False, icon_value=icon)
@@ -213,7 +221,8 @@ def draw(self, context):
213221

214222
col1.label(text="Import Settings")
215223

216-
# col2.prop(importer_prop, "use_blender_obj_import", text="Blender .obj Importer")
224+
col2.prop(importer_prop, "filter_string", text="Filter String")
225+
217226
col2.prop(importer_prop, "relative", text="Relative Path")
218227

219228
if importer_prop.relative:

bseq/properties.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,11 @@ class BSEQ_scene_property(bpy.types.PropertyGroup):
107107
description="Use Blender's built-in .obj import function (or meshio's .obj import function)",
108108
default=True,
109109
)
110+
111+
filter_string: bpy.props.StringProperty(name='Filter String',
112+
description='Filter string for file sequences',
113+
default='',
114+
)
110115

111116
class BSEQ_obj_property(bpy.types.PropertyGroup):
112117
init: bpy.props.BoolProperty(default=False)

0 commit comments

Comments
 (0)