feat(helpers): fix mesh material loading

This commit is contained in:
Swann Martinez 2019-05-03 18:52:49 +02:00
parent 31a72f5001
commit 76efb0ed47
No known key found for this signature in database
GPG Key ID: 414CCAFD8DA720E1
4 changed files with 80 additions and 44 deletions

View File

@ -1 +0,0 @@
{"use_deform_delay": false, "show_names": false, "is_dirty": false, "is_evaluated": false, "tag": false, "pose_position": "POSE", "bones": {}, "id": "user_IM3xRe", "layers": [true, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false], "use_auto_ik": false, "show_bone_custom_shapes": true, "show_group_colors": true, "is_editmode": false, "users": 0, "name_full": "Armature.001", "use_mirror_x": false, "display_type": "OCTAHEDRAL", "layers_protected": [false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false], "use_fake_user": false, "name": "Armature.001", "is_library_indirect": false, "show_axes": false, "edit_bones": {}, "original": {"use_deform_delay": false, "show_names": false, "is_dirty": false, "is_evaluated": false, "tag": false, "pose_position": "POSE", "bones": {}, "id": "user_IM3xRe", "layers": [true, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false], "use_auto_ik": false, "show_bone_custom_shapes": true, "show_group_colors": true, "is_editmode": false, "users": 0, "name_full": "Armature.001", "use_mirror_x": false, "display_type": "OCTAHEDRAL", "layers_protected": [false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false], "use_fake_user": false, "name": "Armature.001", "is_library_indirect": false, "show_axes": false, "edit_bones": {}, "original": {"use_deform_delay": false, "show_names": false, "is_dirty": false, "is_evaluated": false, "tag": false, "pose_position": "POSE", "bones": {}, "id": "user_IM3xRe", "layers": [true, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false], "use_auto_ik": false, "show_bone_custom_shapes": true, "show_group_colors": true, "is_editmode": false, "users": 0, "name_full": "Armature.001", "use_mirror_x": false, "display_type": "OCTAHEDRAL", "layers_protected": [false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false], "use_fake_user": false, "name": "Armature.001", "is_library_indirect": false, "show_axes": false, "edit_bones": {}, "original": {"is_editmode": false, "name": "Armature.001", "is_library_indirect": false, "use_auto_ik": false, "use_mirror_x": false, "name_full": "Armature.001", "use_deform_delay": false, "show_bone_custom_shapes": true, "original": "Armature.001", "show_names": false, "is_dirty": false, "is_evaluated": false, "show_group_colors": true, "display_type": "OCTAHEDRAL", "pose_position": "POSE", "show_axes": false, "tag": false, "id": "user_IM3xRe", "use_fake_user": false, "users": 0}}}}

View File

@ -84,7 +84,7 @@ class RCFClient(object):
# Sync Watchdog
self.watchdog_agent = threading.Thread(
target=watchdog_worker, args=(self.serial_feed, 2, self.stop_event), name="watchdog-agent")
target=watchdog_worker, args=(self.serial_feed, 0.2, self.stop_event), name="watchdog-agent")
self.watchdog_agent.daemon = True
self.watchdog_agent.start()

View File

@ -18,10 +18,16 @@ SUPPORTED_TYPES = [ 'Armature', 'Material',
logger = logging.getLogger(__name__)
logging.basicConfig(level=logging.DEBUG)
# UTILITY FUNCTIONS
def revers(d):
l = []
for i in d:
l.append(i)
return l[::-1]
def refresh_window():
import bpy
bpy.ops.wm.redraw_timer(type='DRAW_WIN_SWAP', iterations=1)
def get_armature_edition_context(armature):
@ -102,7 +108,7 @@ def load(key, value):
load_default(target=target, data=value,
create=True, type=target_type)
elif target_type == 'Armature':
preload_armature(target=target, data=value,
load_armature(target=target, data=value,
create=True)
elif target_type == 'Client':
load_client(key.split('/')[1], value)
@ -134,10 +140,12 @@ def load_client(client=None, data=None):
draw.renderer.draw_client(data)
draw.renderer.draw_client_selected_objects(data)
def load_armature(target=None, data=None, create=False):
file = "cache_{}.json".format(data['name'])
context = bpy.context
if not target or not target.is_editmode:
if not target:
target = bpy.data.armatures.new(data['name'])
dump_anything.load(target, data)
@ -148,32 +156,43 @@ def load_armature(target=None, data=None, create=False):
target.id = data['id']
else:
# Construct a correct execution context
context = get_armature_edition_context(target)
# Construct a correct execution context
file = "cache_{}.json".format(target.name)
with open(file, 'r') as fp:
data = json.load(fp)
if data:
for b in data['bones']:
if b in target.edit_bones.keys():
# Update the bone
pass
else:
# Add new edit bone and load it
bpy.ops.armature.bone_primitive_add(context)
ob = None
for o in bpy.data.objects:
if o.data == target:
ob = o
if ob:
bpy.context.view_layer.objects.active = ob
bpy.ops.object.mode_set(mode='EDIT', toggle=False)
for eb in data['edit_bones']:
if eb in target.edit_bones.keys():
# Update the bone
pass
else:
# Add new edit bone and load it
target_new_b = target.bones[b]
dump_anything.load(target_new_b, data['bones'][b])
target_new_b = target.edit_bones.new[eb]
dump_anything.load(target_new_b, data['bones'][eb])
logger.info(eb)
bpy.ops.object.mode_set(mode='OBJECT', toggle=False)
fp.close()
import os
os.remove(file)
logger.info(b)
def load_mesh(target=None, data=None, create=False):
import bmesh
if not target or not target.is_editmode:
# TODO: handle error
# LOAD GEOMETRY
mesh_buffer = bmesh.new()
for i in data["vertices"]:
@ -193,16 +212,29 @@ def load_mesh(target=None, data=None, create=False):
verts.append(mesh_buffer.verts[v])
if len(verts) > 0:
mesh_buffer.faces.new(verts)
f = mesh_buffer.faces.new(verts)
f.material_index= data["polygons"][p]['material_index']
if target is None and create:
target = bpy.data.meshes.new(data["name"])
mesh_buffer.to_mesh(target)
# Load other meshes metadata
# LOAD METADATA
dump_anything.load(target, data)
material_to_load = []
material_to_load = revers(data["materials"])
target.materials.clear()
# SLots
i = 0
while len(material_to_load) > len(target.materials):
target.materials.append(bpy.data.materials[material_to_load[i]])
i+=1
target.id = data['id']
else:
logger.debug("Mesh can't be loaded")
@ -245,10 +277,6 @@ def load_object(target=None, data=None, create=False):
else:
target.hide_select = True
#Post load
if pointer and pointer.__class__.__name__ == 'Armature':
load_armature(pointer)
except Exception as e:
logger.error("Object {} loading error: {} ".format(data["name"],e))
@ -450,8 +478,10 @@ def dump(key):
elif target_type == 'Light':
data = dump_datablock(target, 1)
elif target_type == 'Mesh':
data = dump_datablock_attibute(
target, ['name', 'polygons', 'edges', 'vertices', 'id'], 6)
data = dump_datablock(target, 2)
dump_datablock_attibute(
target, ['name', 'polygons', 'edges', 'vertices', 'id'], 6,data)
elif target_type == 'Object':
data = dump_datablock(target, 1)
elif target_type == 'Collection':
@ -459,8 +489,8 @@ def dump(key):
elif target_type == 'Scene':
data = dump_datablock_attibute(
target, ['name', 'collection', 'id', 'camera', 'grease_pencil'], 4)
elif target_type == 'Armature':
data = dump_datablock(target, 4)
# elif target_type == 'Armature':
# data = dump_datablock(target, 4)
return data
@ -479,7 +509,7 @@ def dump_datablock(datablock, depth):
return data
def dump_datablock_attibute(datablock, attributes, depth=1):
def dump_datablock_attibute(datablock, attributes, depth=1,dickt=None):
if datablock:
dumper = dump_anything.Dumper()
dumper.type_subset = dumper.match_subset_all
@ -489,6 +519,9 @@ def dump_datablock_attibute(datablock, attributes, depth=1):
key = "{}/{}".format(datablock_type, datablock.name)
data = {}
if dickt:
data = dickt
for attr in attributes:
try:
data[attr] = dumper.dump(getattr(datablock, attr))

View File

@ -498,22 +498,26 @@ def exist(update):
def depsgraph_update(scene):
global client_instance
global client_keys
global client_state
if client_instance and client_instance.net_agent.is_alive():
updates = bpy.context.depsgraph.updates
username = bpy.context.scene.session_settings.username
update_selected_object(bpy.context)
selected_objects = helpers.get_selected_objects(scene)
# if len(selected_objects) > 0:
# for updated_data in updates:
# if updated_data.id.name in selected_objects:
# if updated_data.is_updated_transform or updated_data.is_updated_geometry:
# client_instance.set(
# "{}/{}".format(updated_data.id.bl_rna.name, updated_data.id.name))
for update in reversed(updates):
if update.id.id == username:
getattr(bpy.data, helpers.CORRESPONDANCE[update.id.__class__.__name__])[update.id.name].is_dirty= True
if client_state == 3:
if bpy.context.mode == 'OBJECT':
updates = bpy.context.depsgraph.updates
username = bpy.context.scene.session_settings.username
update_selected_object(bpy.context)
selected_objects = helpers.get_selected_objects(scene)
# if len(selected_objects) > 0:
# for updated_data in updates:
# if updated_data.id.name in selected_objects:
# if updated_data.is_updated_transform or updated_data.is_updated_geometry:
# client_instance.set(
# "{}/{}".format(updated_data.id.bl_rna.name, updated_data.id.name))
for update in reversed(updates):
if update.id.id == username:
getattr(bpy.data, helpers.CORRESPONDANCE[update.id.__class__.__name__])[update.id.name].is_dirty= True
def register():
from bpy.utils import register_class