182 lines
5.1 KiB
Python
Raw Normal View History

2020-03-20 14:56:50 +01:00
# ##### BEGIN GPL LICENSE BLOCK #####
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <https://www.gnu.org/licenses/>.
#
# ##### END GPL LICENSE BLOCK #####
2019-05-03 16:20:40 +02:00
import json
2019-09-30 13:35:50 +02:00
import logging
2019-08-08 15:00:07 +02:00
import os
2019-08-08 15:35:43 +02:00
import random
2019-09-30 13:35:50 +02:00
import string
import sys
2020-03-23 17:55:10 +01:00
import time
2019-09-30 13:35:50 +02:00
from uuid import uuid4
from collections.abc import Iterable
2019-05-02 17:58:37 +02:00
import bpy
import mathutils
2019-09-30 13:35:50 +02:00
from . import environment, presence
2019-05-02 17:58:37 +02:00
from .libs import dump_anything
2019-04-10 17:01:21 +02:00
2019-04-19 15:46:54 +02:00
logger = logging.getLogger(__name__)
logger.setLevel(logging.WARNING)
2019-08-08 15:00:07 +02:00
def has_action(target):
return (hasattr(target, 'animation_data')
and target.animation_data
and target.animation_data.action)
def has_driver(target):
return (hasattr(target, 'animation_data')
and target.animation_data
and target.animation_data.drivers)
2020-01-22 14:33:34 +01:00
2019-10-07 17:32:56 +02:00
def find_from_attr(attr_name, attr_value, list):
for item in list:
if getattr(item, attr_name, None) == attr_value:
return item
return None
2019-09-26 22:57:39 +02:00
def get_datablock_users(datablock):
users = []
supported_types = get_preferences().supported_datablocks
2019-09-30 13:35:50 +02:00
if hasattr(datablock, 'users_collection') and datablock.users_collection:
users.extend(list(datablock.users_collection))
2019-09-30 13:35:50 +02:00
if hasattr(datablock, 'users_scene') and datablock.users_scene:
users.extend(list(datablock.users_scene))
2019-09-30 13:35:50 +02:00
if hasattr(datablock, 'users_group') and datablock.users_scene:
users.extend(list(datablock.users_scene))
for datatype in supported_types:
if datatype.bl_name != 'users':
2019-09-30 13:35:50 +02:00
root = getattr(bpy.data, datatype.bl_name)
for item in root:
if hasattr(item, 'data') and datablock == item.data or \
2019-09-30 13:35:50 +02:00
datatype.bl_name != 'collections' and hasattr(item, 'children') and datablock in item.children:
users.append(item)
return users
2019-08-08 15:35:43 +02:00
def random_string_digits(stringLength=6):
"""Generate a random string of letters and digits """
lettersAndDigits = string.ascii_letters + string.digits
2020-01-22 14:33:34 +01:00
return ''.join(random.choices(lettersAndDigits, k=stringLength))
2019-08-08 15:35:43 +02:00
def randomColor():
r = random.random()
v = random.random()
b = random.random()
return [r, v, b]
2019-08-08 15:35:43 +02:00
def clean_scene():
2019-09-26 22:57:39 +02:00
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
2019-08-08 15:00:07 +02:00
def revers(d):
l = []
for i in d:
l.append(i)
2019-05-08 17:06:52 +02:00
return l[::-1]
2019-05-02 17:58:37 +02:00
2019-05-08 17:06:52 +02:00
def get_armature_edition_context(armature):
2019-05-08 17:06:52 +02:00
2019-05-03 16:20:40 +02:00
override = {}
# Set correct area
2019-05-08 17:06:52 +02:00
for area in bpy.data.window_managers[0].windows[0].screen.areas:
if area.type == 'VIEW_3D':
2019-05-03 16:20:40 +02:00
override = bpy.context.copy()
override['area'] = area
break
# Set correct armature settings
2019-05-08 17:06:52 +02:00
override['window'] = bpy.data.window_managers[0].windows[0]
2019-05-03 16:20:40 +02:00
override['screen'] = bpy.data.window_managers[0].windows[0].screen
override['mode'] = 'EDIT_ARMATURE'
override['active_object'] = armature
override['selected_objects'] = [armature]
for o in bpy.data.objects:
if o.data == armature:
2019-05-03 16:20:40 +02:00
override['edit_object'] = o
2019-05-08 17:06:52 +02:00
break
return override
2019-04-18 15:05:48 +02:00
2019-05-08 17:06:52 +02:00
def get_selected_objects(scene, active_view_layer):
return [obj.uuid for obj in scene.objects if obj.select_get(view_layer=active_view_layer)]
2019-04-18 15:05:48 +02:00
2019-08-08 15:00:07 +02:00
def load_dict(src_dict, target):
try:
for item in src_dict:
# attr =
setattr(target, item, src_dict[item])
except Exception as e:
logger.error(e)
pass
2019-08-08 15:00:07 +02:00
def dump_datablock_attibutes(datablock=None, attributes=[], depth=1, dickt=None):
2019-04-10 18:01:55 +02:00
if datablock:
dumper = dump_anything.Dumper()
dumper.type_subset = dumper.match_subset_all
dumper.depth = depth
2019-05-13 11:49:46 +02:00
datablock_type = datablock.bl_rna.name
2019-04-10 18:01:55 +02:00
data = {}
2019-05-08 17:06:52 +02:00
if dickt:
data = dickt
2019-04-10 18:01:55 +02:00
for attr in attributes:
try:
data[attr] = dumper.dump(getattr(datablock, attr))
except:
pass
2019-09-30 13:35:50 +02:00
return data
2020-01-22 14:33:34 +01:00
def resolve_from_id(id, optionnal_type=None):
for category in dir(bpy.data):
root = getattr(bpy.data, category)
if isinstance(root, Iterable):
if id in root and ((optionnal_type is None) or (optionnal_type.lower() in root[id].__class__.__name__.lower())):
return root[id]
return None
def get_preferences():
2020-03-23 17:55:10 +01:00
return bpy.context.preferences.addons[__package__].preferences
def current_milli_time():
return int(round(time.time() * 1000))