refactor: cleanup utils.py

This commit is contained in:
Swann
2019-09-26 22:57:39 +02:00
parent e1657f1184
commit 35667cbd5a
3 changed files with 12 additions and 108 deletions

View File

@ -79,11 +79,11 @@ class BlArmature(BlDatablock):
data = utils.dump_datablock(pointer, 4) data = utils.dump_datablock(pointer, 4)
#get the parent Object #get the parent Object
object_users = utils.get_users(pointer)[0] object_users = utils.get_datablock_users(pointer)[0]
data['user'] = object_users.name data['user'] = object_users.name
#get parent collection #get parent collection
container_users = utils.get_users(object_users) container_users = utils.get_datablock_users(object_users)
data['user_collection'] = [item.name for item in container_users if isinstance(item,bpy.types.Collection)] data['user_collection'] = [item.name for item in container_users if isinstance(item,bpy.types.Collection)]
data['user_scene'] = [item.name for item in container_users if isinstance(item,bpy.types.Scene)] data['user_scene'] = [item.name for item in container_users if isinstance(item,bpy.types.Scene)]
return data return data

114
utils.py
View File

@ -12,15 +12,11 @@ import mathutils
from . import presence, environment from . import presence, environment
from .libs import dump_anything from .libs import dump_anything
# TODO: replace hardcoded values...
BPY_TYPES = {'Image': 'images', 'Texture': 'textures', 'Material': 'materials', 'GreasePencil': 'grease_pencils', 'Curve': 'curves', 'Collection': 'collections', 'Mesh': 'meshes', 'Object': 'objects',
'Scene': 'scenes', 'Light': 'lights', 'SunLight': 'lights', 'SpotLight': 'lights', 'AreaLight': 'lights', 'PointLight': 'lights', 'Camera': 'cameras', 'Action': 'actions', 'Armature': 'armatures'}
logger = logging.getLogger(__name__) logger = logging.getLogger(__name__)
logger.setLevel(logging.DEBUG) logger.setLevel(logging.DEBUG)
def get_users(datablock): def get_datablock_users(datablock):
users = [] users = []
supported_types = bpy.context.window_manager.session.supported_datablock supported_types = bpy.context.window_manager.session.supported_datablock
if hasattr(datablock, 'users_collection') and datablock.users_collection: if hasattr(datablock, 'users_collection') and datablock.users_collection:
@ -38,8 +34,6 @@ def get_users(datablock):
users.append(item) users.append(item)
return users return users
# UTILITY FUNCTIONS
def random_string_digits(stringLength=6): def random_string_digits(stringLength=6):
"""Generate a random string of letters and digits """ """Generate a random string of letters and digits """
@ -48,14 +42,13 @@ def random_string_digits(stringLength=6):
def clean_scene(): def clean_scene():
for datablock in BPY_TYPES: for type_name in dir(bpy.data):
datablock_ref = getattr(bpy.data, BPY_TYPES[datablock]) try:
for item in datablock_ref: type_collection = getattr(bpy.data, type_name)
try: for item in type_collection:
datablock_ref.remove(item) type_collection.remove(item)
# Catch last scene remove except:
except RuntimeError: continue
pass
def revers(d): def revers(d):
@ -107,80 +100,6 @@ def load_dict(src_dict, target):
pass pass
def resolve_bpy_path(path):
"""
Get bpy property value from path
"""
item = None
try:
path = path.split('/')
item = getattr(bpy.data, BPY_TYPES[path[0]])[path[1]]
except:
pass
return item
def load_client(client=None, data=None):
C = bpy.context
D = bpy.data
net_settings = C.window_manager.session
if client and data:
if net_settings.enable_presence:
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:
target = bpy.data.armatures.new(data['name'])
dump_anything.load(target, data)
with open(file, 'w') as fp:
json.dump(data, fp)
fp.close()
target.id = data['id']
else:
# Construct a correct execution context
file = "cache_{}.json".format(target.name)
with open(file, 'r') as fp:
data = json.load(fp)
if data:
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.edit_bones.new[eb]
dump_anything.load(target_new_b, data['bones'][eb])
logger.debug(eb)
bpy.ops.object.mode_set(mode='OBJECT', toggle=False)
fp.close()
import os
os.remove(file)
def dump_datablock(datablock, depth): def dump_datablock(datablock, depth):
if datablock: if datablock:
dumper = dump_anything.Dumper() dumper = dump_anything.Dumper()
@ -212,19 +131,4 @@ def dump_datablock_attibutes(datablock=None, attributes=[], depth=1, dickt=None)
except: except:
pass pass
return data return data
def init_client(key=None):
client_dict = {}
C = bpy.context
Net = C.window_manager.session
client_dict['uuid'] = str(uuid4())
client_dict['location'] = [[0, 0, 0], [0, 0, 0], [0, 0, 0], [0, 0, 0]]
client_dict['color'] = [Net.client_color.r,
Net.client_color.g, Net.client_color.b, 1]
client_dict['active_objects'] = get_selected_objects(C.view_layer)
return client_dict