refactor: cleanup utils.py
This commit is contained in:
@ -79,11 +79,11 @@ class BlArmature(BlDatablock):
|
||||
data = utils.dump_datablock(pointer, 4)
|
||||
|
||||
#get the parent Object
|
||||
object_users = utils.get_users(pointer)[0]
|
||||
object_users = utils.get_datablock_users(pointer)[0]
|
||||
data['user'] = object_users.name
|
||||
|
||||
#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_scene'] = [item.name for item in container_users if isinstance(item,bpy.types.Scene)]
|
||||
return data
|
||||
|
Submodule libs/replication updated: 5c7123f50c...5c8920522f
114
utils.py
114
utils.py
@ -12,15 +12,11 @@ import mathutils
|
||||
from . import presence, environment
|
||||
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.setLevel(logging.DEBUG)
|
||||
|
||||
|
||||
def get_users(datablock):
|
||||
def get_datablock_users(datablock):
|
||||
users = []
|
||||
supported_types = bpy.context.window_manager.session.supported_datablock
|
||||
if hasattr(datablock, 'users_collection') and datablock.users_collection:
|
||||
@ -38,8 +34,6 @@ def get_users(datablock):
|
||||
users.append(item)
|
||||
return users
|
||||
|
||||
# UTILITY FUNCTIONS
|
||||
|
||||
|
||||
def random_string_digits(stringLength=6):
|
||||
"""Generate a random string of letters and digits """
|
||||
@ -48,14 +42,13 @@ def random_string_digits(stringLength=6):
|
||||
|
||||
|
||||
def clean_scene():
|
||||
for datablock in BPY_TYPES:
|
||||
datablock_ref = getattr(bpy.data, BPY_TYPES[datablock])
|
||||
for item in datablock_ref:
|
||||
try:
|
||||
datablock_ref.remove(item)
|
||||
# Catch last scene remove
|
||||
except RuntimeError:
|
||||
pass
|
||||
for type_name in dir(bpy.data):
|
||||
try:
|
||||
type_collection = getattr(bpy.data, type_name)
|
||||
for item in type_collection:
|
||||
type_collection.remove(item)
|
||||
except:
|
||||
continue
|
||||
|
||||
|
||||
def revers(d):
|
||||
@ -107,80 +100,6 @@ def load_dict(src_dict, target):
|
||||
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):
|
||||
if datablock:
|
||||
dumper = dump_anything.Dumper()
|
||||
@ -212,19 +131,4 @@ def dump_datablock_attibutes(datablock=None, attributes=[], depth=1, dickt=None)
|
||||
except:
|
||||
pass
|
||||
|
||||
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
|
||||
return data
|
Reference in New Issue
Block a user