Skip to content

Weekly Update #3

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Jul 14, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
56 changes: 28 additions & 28 deletions __init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,53 +20,53 @@
if bpy.context.preferences.filepaths.use_relative_paths == True:
bpy.context.preferences.filepaths.use_relative_paths = False

from simloader import *
from bseq import *

classes = [
SIMLOADER_obj_property,
SIMLOADER_scene_property,
SIMLOADER_mesh_property,
SIMLOADER_OT_load,
SIMLOADER_OT_edit,
SIMLOADER_OT_resetpt,
SIMLOADER_OT_resetins,
SIMLOADER_OT_resetmesh,
SIMLOADER_Import,
SIMLOADER_List_Panel,
SIMLOADER_UL_Obj_List,
SIMLOADER_Settings,
SIMLOADER_Templates,
SIMLOADER_UL_Att_List,
SIMLOADER_OT_set_as_split_norm,
SIMLOADER_OT_remove_split_norm,
SIMLOADER_OT_disable_selected,
SIMLOADER_OT_enable_selected,
SIMLOADER_OT_refresh_seq,
BSEQ_obj_property,
BSEQ_scene_property,
BSEQ_mesh_property,
BSEQ_OT_load,
BSEQ_OT_edit,
BSEQ_OT_resetpt,
BSEQ_OT_resetins,
BSEQ_OT_resetmesh,
BSEQ_Import,
BSEQ_List_Panel,
BSEQ_UL_Obj_List,
BSEQ_Settings,
BSEQ_Templates,
BSEQ_UL_Att_List,
BSEQ_OT_set_as_split_norm,
BSEQ_OT_remove_split_norm,
BSEQ_OT_disable_selected,
BSEQ_OT_enable_selected,
BSEQ_OT_refresh_seq,
]


def register():
bpy.app.handlers.load_post.append(SIMLOADER_initialize)
bpy.app.handlers.load_post.append(BSEQ_initialize)
for cls in classes:
bpy.utils.register_class(cls)
bpy.types.TEXT_MT_templates.append(draw_template)
bpy.types.Scene.SIMLOADER = bpy.props.PointerProperty(type=SIMLOADER_scene_property)
bpy.types.Object.SIMLOADER = bpy.props.PointerProperty(type=SIMLOADER_obj_property)
bpy.types.Mesh.SIMLOADER = bpy.props.PointerProperty(type=SIMLOADER_mesh_property)
bpy.types.Scene.BSEQ = bpy.props.PointerProperty(type=BSEQ_scene_property)
bpy.types.Object.BSEQ = bpy.props.PointerProperty(type=BSEQ_obj_property)
bpy.types.Mesh.BSEQ = bpy.props.PointerProperty(type=BSEQ_mesh_property)


# manually call this function once
# so when addon being installed, it can run correctly
# because scene is not used, so pass None into it
SIMLOADER_initialize(None)
BSEQ_initialize(None)

def unregister():
for cls in classes:
bpy.utils.unregister_class(cls)
bpy.types.TEXT_MT_templates.remove(draw_template)
del bpy.types.Scene.SIMLOADER
del bpy.types.Object.SIMLOADER
bpy.app.handlers.load_post.remove(SIMLOADER_initialize)
del bpy.types.Scene.BSEQ
del bpy.types.Object.BSEQ
bpy.app.handlers.load_post.remove(BSEQ_initialize)
unsubscribe_to_selected()


Expand Down
16 changes: 16 additions & 0 deletions additional_file_formats/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
Here you can find an example if you want to import customized file formats.

1. create an `example.py`
2. implement a function as following
```python
def read_func(filepath):
# open the filepath linked file
# construct the meshio object
return meshio.Mesh
```
3. call `meshio.register_format("name", [".extenstion"], read_func, {".extenstion": None})`in global environment
4. add `from . import example` in `__init__.py`

You can check `bgeo.py` as an example, which reads the partiles-only [.bgeo](https://github.com/wdas/partio) file.

For more details how to construct the `meshio.Mesh` object, you can check [here](https://github.com/nschloe/meshio/wiki/meshio-Mesh()-data-structure) for the details about the data strucutre, and [here](https://github.com/nschloe/meshio/wiki/Node-ordering-in-cells) more details about the vertex ordering.
10 changes: 2 additions & 8 deletions additional_file_formats/__init__.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,2 @@
from .mzd import readMZD_to_bpymesh, readMZD_to_meshio
from .bgeo import readbgeo_to_meshio

additional_format_loader = {'bgeo': readbgeo_to_meshio, 'mzd': readMZD_to_meshio}

__all__ = [
readMZD_to_bpymesh, readMZD_to_meshio, readbgeo_to_meshio, additional_format_loader
]
from . import bgeo
from . import mzd
3 changes: 3 additions & 0 deletions additional_file_formats/bgeo.py
Original file line number Diff line number Diff line change
Expand Up @@ -105,3 +105,6 @@ def readbgeo_to_meshio(filepath):
raise Exception("file didn't end")
return meshio.Mesh(position, [('vertex', [])], point_data=point_attributes)


# no need for write function
meshio.register_format("bgeo", [".bgeo"], readbgeo_to_meshio, {".bgeo": None})
7 changes: 6 additions & 1 deletion additional_file_formats/mzd.py
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,7 @@ def readMZD_to_meshio(filepath):
pass
return meshio.Mesh(out_vertPositions.reshape((out_numVertices, 3)), cells, point_data)


def readMZD_to_bpymesh(filepath, mesh):
shade_scheme = False
if mesh.polygons:
Expand Down Expand Up @@ -308,4 +309,8 @@ def readMZD_to_bpymesh(filepath, mesh):
else:
# print(name)
file.seek(size, 1)
pass
pass


# no need for write function
meshio.register_format("mzd", [".mzd"], readMZD_to_meshio, {".mzd": None})
64 changes: 64 additions & 0 deletions bseq/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
from .operators import BSEQ_OT_load, BSEQ_OT_edit, BSEQ_OT_resetpt, BSEQ_OT_resetmesh, BSEQ_OT_resetins,BSEQ_OT_set_as_split_norm,BSEQ_OT_remove_split_norm,BSEQ_OT_disable_selected,BSEQ_OT_enable_selected,BSEQ_OT_refresh_seq
from .properties import BSEQ_scene_property, BSEQ_obj_property,BSEQ_mesh_property
from .panels import BSEQ_UL_Obj_List, BSEQ_List_Panel, BSEQ_Settings, BSEQ_Import, BSEQ_Templates, BSEQ_UL_Att_List, draw_template
from .messenger import subscribe_to_selected, unsubscribe_to_selected
import bpy
from bpy.app.handlers import persistent
from .importer import update_obj
from datetime import datetime


def print_information(scene):
if not bpy.context.scene.BSEQ.print:
return
now = datetime.now()
path = bpy.context.scene.render.filepath
path = bpy.path.abspath(path)
filepath = path + '/bseq_' + now.strftime("%Y-%m-%d_%H-%M")
with open(filepath, 'w') as file:
file.write("Render Time: {}\n".format(now.strftime("%Y-%m-%d_%H-%M")))
file.write("bseq Objects in the scene:\n\n")
for obj in bpy.data.objects:
bseq_prop = obj.BSEQ
if bseq_prop.init:
file.write("Object name: {}\n".format(obj.name))
file.write("Is it being animated: {}\n".format(bseq_prop.enabled))
file.write("Filepath: {}\n".format(bseq_prop.pattern))
file.write("Is it relative path: {}\n".format(bseq_prop.use_relative))
file.write("\n\n")


@persistent
def BSEQ_initialize(scene):
if update_obj not in bpy.app.handlers.frame_change_post:
bpy.app.handlers.frame_change_post.append(update_obj)
subscribe_to_selected()
if print_information not in bpy.app.handlers.render_init:
bpy.app.handlers.render_init.append(print_information)


__all__ = [
"BSEQ_OT_edit",
"BSEQ_OT_load",
"BSEQ_obj_property",
"BSEQ_initialize",
"BSEQ_Import",
"BSEQ_List_Panel",
"BSEQ_UL_Obj_List",
"BSEQ_scene_property",
"BSEQ_Templates",
"BSEQ_Settings",
"BSEQ_UL_Att_List",
"subscribe_to_selected",
"BSEQ_OT_resetpt",
"BSEQ_OT_resetmesh",
"BSEQ_OT_resetins",
"draw_template",
"unsubscribe_to_selected",
"BSEQ_OT_set_as_split_norm",
"BSEQ_mesh_property",
"BSEQ_OT_remove_split_norm",
"BSEQ_OT_disable_selected",
"BSEQ_OT_enable_selected",
"BSEQ_OT_refresh_seq",
]
12 changes: 6 additions & 6 deletions simloader/callback.py → bseq/callback.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,17 @@

def update_path(self, context):
# When the path has been changed, reset the selected sequence to None
context.scene.SIMLOADER['fileseq'] = 1
context.scene.SIMLOADER.use_pattern = False
context.scene.SIMLOADER.pattern = ""
context.scene.BSEQ['fileseq'] = 1
context.scene.BSEQ.use_pattern = False
context.scene.BSEQ.pattern = ""


def item_fileseq(self, context):
'''
Detects all the file sequences in the directory
'''

p = context.scene.SIMLOADER.path
p = context.scene.BSEQ.path
try:
f = fileseq.findSequencesOnDisk(p)
except:
Expand All @@ -38,10 +38,10 @@ def item_fileseq(self, context):
def update_selected_obj_num(self, context):

# Here is when select sequences, then change the corresponding object to active object
index = context.scene.SIMLOADER.selected_obj_num
index = context.scene.BSEQ.selected_obj_num
obj = bpy.data.objects[index]

if context.scene.SIMLOADER.selected_obj_deselectall_flag:
if context.scene.BSEQ.selected_obj_deselectall_flag:
bpy.ops.object.select_all(action="DESELECT")
obj.select_set(True)
context.view_layer.objects.active = obj
Expand Down
39 changes: 16 additions & 23 deletions simloader/importer.py → bseq/importer.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
from .utils import show_message_box
import numpy as np
from mathutils import Matrix
# this import is not useless
import additional_file_formats


Expand Down Expand Up @@ -131,7 +132,7 @@ def update_mesh(meshio_mesh, mesh):
attribute.data.foreach_set(name_string, v.ravel())

# set as split norm
if mesh.SIMLOADER.split_norm_att_name and mesh.SIMLOADER.split_norm_att_name == k:
if mesh.BSEQ.split_norm_att_name and mesh.BSEQ.split_norm_att_name == k:
mesh.use_auto_smooth = True
mesh.normals_split_custom_set_from_vertices(v)

Expand All @@ -144,11 +145,7 @@ def create_obj(fileseq, use_relaitve, transform_matrix=Matrix([[1, 0, 0, 0], [0,
meshio_mesh = None
enabled = True
try:
ext = fileseq.extension().split('.')[-1]
if ext in additional_file_formats.additional_format_loader:
meshio_mesh = additional_file_formats.additional_format_loader[ext](filepath)
else:
meshio_mesh = meshio.read(filepath)
meshio_mesh = meshio.read(filepath)
except Exception as e:
show_message_box("Error when reading: " + filepath + ",\n" + traceback.format_exc(),
"Meshio Loading Error" + str(e),
Expand All @@ -159,13 +156,13 @@ def create_obj(fileseq, use_relaitve, transform_matrix=Matrix([[1, 0, 0, 0], [0,
name = fileseq.basename() + "@" + fileseq.extension()
mesh = bpy.data.meshes.new(name)
object = bpy.data.objects.new(name, mesh)
object.SIMLOADER.use_relative = use_relaitve
object.BSEQ.use_relative = use_relaitve
if use_relaitve:
object.SIMLOADER.pattern = bpy.path.relpath(str(fileseq))
object.BSEQ.pattern = bpy.path.relpath(str(fileseq))
else:
object.SIMLOADER.pattern = str(fileseq)
object.SIMLOADER.init = True
object.SIMLOADER.enabled = enabled
object.BSEQ.pattern = str(fileseq)
object.BSEQ.init = True
object.BSEQ.enabled = enabled
object.matrix_world = transform_matrix
if enabled:
update_mesh(meshio_mesh, object.data)
Expand All @@ -180,25 +177,25 @@ def update_obj(scene, depsgraph=None):
current_frame = bpy.context.scene.frame_current

for obj in bpy.data.objects:
if obj.SIMLOADER.init == False:
if obj.BSEQ.init == False:
continue
if obj.SIMLOADER.enabled == False:
if obj.BSEQ.enabled == False:
continue

meshio_mesh = None
pattern = obj.SIMLOADER.pattern
if obj.SIMLOADER.use_relative:
pattern = obj.BSEQ.pattern
if obj.BSEQ.use_relative:
pattern = bpy.path.abspath(pattern)
# in case the blender file was created on windows system, but opened in linux system
pattern = bpy.path.native_pathsep(pattern)
fs = fileseq.FileSequence(pattern)

if obj.SIMLOADER.use_advance and obj.SIMLOADER.script_name:
script = bpy.data.texts[obj.SIMLOADER.script_name]
if obj.BSEQ.use_advance and obj.BSEQ.script_name:
script = bpy.data.texts[obj.BSEQ.script_name]
try:
exec(script.as_string())
except Exception as e:
show_message_box(traceback.format_exc(), "running script: " + obj.SIMLOADER.script_name + " failed: " + str(e),
show_message_box(traceback.format_exc(), "running script: " + obj.BSEQ.script_name + " failed: " + str(e),
"ERROR")
continue

Expand All @@ -225,11 +222,7 @@ def update_obj(scene, depsgraph=None):
else:
filepath = fs[current_frame % len(fs)]
try:
ext = fs.extension().split('.')[-1]
if ext in additional_file_formats.additional_format_loader:
meshio_mesh = additional_file_formats.additional_format_loader[ext](filepath)
else:
meshio_mesh = meshio.read(filepath)
meshio_mesh = meshio.read(filepath)
except Exception as e:
show_message_box("Error when reading: " + filepath + ",\n" + traceback.format_exc(),
"Meshio Loading Error" + str(e),
Expand Down
18 changes: 9 additions & 9 deletions simloader/messenger.py → bseq/messenger.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,28 +7,28 @@ def selected_callback():
name = bpy.context.active_object.name
idx = bpy.data.objects.find(name)
if idx >= 0:
bpy.context.scene.SIMLOADER.selected_obj_deselectall_flag = False
bpy.context.scene.SIMLOADER.selected_obj_num = idx
bpy.context.scene.SIMLOADER.selected_obj_deselectall_flag = True
bpy.context.scene.BSEQ.selected_obj_deselectall_flag = False
bpy.context.scene.BSEQ.selected_obj_num = idx
bpy.context.scene.BSEQ.selected_obj_deselectall_flag = True


def subscribe_to_selected():
import simloader
import bseq

# because current implementation may subscribe twice
# so clear once to avoid duplication
bpy.msgbus.clear_by_owner(simloader)
bpy.msgbus.clear_by_owner(bseq)

bpy.msgbus.subscribe_rna(
key=(bpy.types.LayerObjects, 'active'),
# don't know why it needs this owner, so I set owner to this module `simloader`
owner=simloader,
# don't know why it needs this owner, so I set owner to this module `bseq`
owner=bseq,
# no args
args=(()),
notify=selected_callback,
)


def unsubscribe_to_selected():
import simloader
bpy.msgbus.clear_by_owner(simloader)
import bseq
bpy.msgbus.clear_by_owner(bseq)
Loading