Skip to content

Commit

Permalink
obj_path_resolve_test.py now includes randomising
Browse files Browse the repository at this point in the history
  • Loading branch information
ruaridhg committed Oct 3, 2023
1 parent f3f0145 commit 4b12faf
Show file tree
Hide file tree
Showing 4 changed files with 214 additions and 16 deletions.
2 changes: 1 addition & 1 deletion install_randomiser.sh
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,4 @@ source ~/.bash_profile

# zip randomiser, launch blender and install+enable
zip randomiser.zip -FS -r randomiser/
blender sample.blend --python install_and_enable_addons.py -- ./randomiser.zip
blender define_prop.blend --python install_and_enable_addons.py -- ./randomiser.zip
220 changes: 205 additions & 15 deletions randomiser/define_prop/obj_path_resolve_test.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,11 @@
import random

import bpy
import numpy as np

# from random import random
from mathutils import Vector

# %%
full_str = "bpy.data.objects['Cube'].location"
# path_prop, path_attr = full_str.rsplit("[]", 1)
Expand Down Expand Up @@ -103,22 +111,20 @@ def attr_get_type(obj, path):


def get_attr_only_str(full_str):
if "[" in full_str:
len_path = len(full_str.rsplit(".", 100)) - 1
print("len_path = ", len_path)
list_parent_nodes_str = full_str.rsplit(".", len_path - 3)
print("list_parent_nodes_str = ", list_parent_nodes_str)
attribute_only_str = full_str.replace(
list_parent_nodes_str[0] + ".", ""
)
if "data" in full_str:
mod = 0
elif "[" in full_str:
mod = 1
else:
len_path = len(full_str.rsplit(".", 100))
print("len_path = ", len_path)
list_parent_nodes_str = full_str.rsplit(".", len_path - 3)
print("list_parent_nodes_str = ", list_parent_nodes_str)
attribute_only_str = full_str.replace(
list_parent_nodes_str[0] + ".", ""
)
mod = 0

len_path = len(full_str.rsplit(".", 100)) - mod
print("len_path = ", len_path)

list_parent_nodes_str = full_str.rsplit(".", len_path - 3)
print("list_parent_nodes_str = ", list_parent_nodes_str)

attribute_only_str = full_str.replace(list_parent_nodes_str[0] + ".", "")

return attribute_only_str

Expand All @@ -130,3 +136,187 @@ def get_attr_only_str(full_str):
# full_str='bpy.context.scene.camera.location'
# attr_only_str=get_attr_only_str(full_str)
# print('attr_only_str = ', attr_only_str)


def attr_set_val(obj, path, min_val, max_val, UD_type):
if "." in path:
# gives us: ('modifiers["Subsurf"]', 'levels')
# len_path = len(full_str.rsplit(".", config.MAX_NUMBER_OF_SUBPANELS))
path_prop, path_attr = path.rsplit(".", 1)

# same as: prop = obj.modifiers["Subsurf"]
prop = obj.path_resolve(path_prop)
else:
prop = obj
# single attribute such as name, location... etc
path_attr = path

# same as: prop.levels = value

try:
getattr(prop, path_attr)
except Exception:
# print("Property does not exist")
pass
# action = getattr(prop, path_attr)

print("attr_set_val type ========================= ", UD_type)
# if UD_type==float:
# min_array = np.array(getattr(self, "min_" + m_str))
# max_array = np.array(getattr(self, "max_" + m_str))

if UD_type == float:
print("HELLO 1D FLOAT!!!!!!!")
# if rand_posx:
# getattr(context.scene.camera, value_str)[0] = uniform(
# loc_x_range[0], loc_x_range[1]
# )

# if rand_rotz:
# rand_z = uniform(rot_z_range[0], rot_z_range[1])
# else:
# rand_z = uniform(0.0, 0.0)

# vec = Vector([rand_x, rand_y, rand_z])

# bpy.data.objects["Camera"].rotation_euler[0] = vec[0]

value = random.uniform(min_val, max_val)
print(value)
elif UD_type == Vector:
print("HELLO 3D VECTOR FLOAT!!!!!")
value = random.uniform(min_val, max_val)
print(value)
else:
print("HELLO INTEGER!!!!!!!!!!!!!!!!!!!!")
value = random.randint(min_val, max_val)
print(value)

setattr(prop, path_attr, value)


#### camera.location
full_str = "bpy.context.scene.camera.location"
attr_only_str = get_attr_only_str(full_str)
print("attr_only_str = ", attr_only_str)

path_prop, path_attr = "camera.location".rsplit(".", 1)
prop = bpy.context.scene.path_resolve(path_prop)
action = getattr(prop, path_attr)
print("prop = ", prop)
print("action getattr = ", action)

prop_type, action, prop, path_attr = attr_get_type(
bpy.context.scene, attr_only_str
)


min_val = np.array(
getattr(
bpy.context.scene.socket_props_per_UD.collection[0],
"min_float_3d",
)
)

max_val = np.array(
getattr(
bpy.context.scene.socket_props_per_UD.collection[0],
"max_float_3d",
)
)
print(min_val)
print(max_val)
print(prop_type)
# min_val = bpy.context.scene.socket_props_per_UD.collection[0].min_float_1d[0]
# max_val = bpy.context.scene.socket_props_per_UD.collection[0].max_float_1d[0]
attr_set_val(
bpy.context.scene,
attr_only_str,
min_val,
max_val,
prop_type,
)


#### frame_current
full_str = "bpy.context.scene.frame_current"
attr_only_str = get_attr_only_str(full_str)
print("attr_only_str = ", attr_only_str)

prop = bpy.context.scene
# single attribute such as name, location... etc
path_attr = "frame_current"
action = getattr(prop, path_attr)
print("prop = ", prop)
print("action getattr = ", action)

prop_type, action, prop, path_attr = attr_get_type(
bpy.context.scene, attr_only_str
)


min_val = np.array(
getattr(
bpy.context.scene.socket_props_per_UD.collection[1],
"min_int_1d",
)
)

max_val = np.array(
getattr(
bpy.context.scene.socket_props_per_UD.collection[1],
"max_int_1d",
)
)
print(min_val)
print(max_val)
print(prop_type)
# min_val = bpy.context.scene.socket_props_per_UD.collection[0].min_float_1d[0]
# max_val = bpy.context.scene.socket_props_per_UD.collection[0].max_float_1d[0]
attr_set_val(
bpy.context.scene,
attr_only_str,
min_val,
max_val,
prop_type,
)

#### collision.absorption
full_str = "bpy.context.scene.objects['Cube'].collision.absorption"
attr_only_str = get_attr_only_str(full_str)
print("attr_only_str = ", attr_only_str)

path_prop, path_attr = "collision.absorption".rsplit(".", 1)
prop = bpy.context.scene.objects["Cube"].path_resolve(path_prop)
action = getattr(prop, path_attr)
print("prop = ", prop)
print("action getattr = ", action)

prop_type, action, prop, path_attr = attr_get_type(
bpy.data.objects["Cube"], attr_only_str
)


min_val = getattr(
bpy.context.scene.socket_props_per_UD.collection[2],
"min_float_1d",
)


max_val = np.array(
getattr(
bpy.context.scene.socket_props_per_UD.collection[2],
"max_float_1d",
)
)
print(min_val)
print(max_val)
print(prop_type)

attr_set_val(
bpy.data.objects["Cube"],
attr_only_str,
min_val,
max_val,
prop_type,
)
7 changes: 7 additions & 0 deletions randomiser/define_prop/operators.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import bpy
import numpy as np
from mathutils import Vector

from .ui import attr_get_type, get_attr_only_str

Expand Down Expand Up @@ -48,6 +49,8 @@ def attr_set_val(obj, path, min_val, max_val, UD_type):
# vec = Vector([rand_x, rand_y, rand_z])

# bpy.data.objects["Camera"].rotation_euler[0] = vec[0]
elif UD_type == Vector:
print("HELLO 3D VECTOR FLOAT!!!!!")
else:
print("HELLO INTEGER!!!!!!!!!!!!!!!!!!!!")
value = random.randint(min_val, max_val)
Expand Down Expand Up @@ -366,6 +369,8 @@ def execute(self, context):
)

if "bpy.context.scene" in full_str:
print("bpy.context.scene")
print("OPS EXECUTE attribute_only_str ", attribute_only_str)
attr_set_val(
bpy.context.scene,
attribute_only_str,
Expand All @@ -374,6 +379,8 @@ def execute(self, context):
attr_type,
)
elif "bpy.data.objects" in full_str:
print("bpy.context.scene")
print("OPS EXECUTE attribute_only_str ", attribute_only_str)
attr_set_val(
bpy.data.objects["Cube"],
attribute_only_str,
Expand Down
1 change: 1 addition & 0 deletions randomiser/define_prop/ui.py
Original file line number Diff line number Diff line change
Expand Up @@ -466,6 +466,7 @@ def poll(cls, context):

full_str = sockets_props_collection.name
attribute_only_str = get_attr_only_str(full_str)
print("SUBPANEL poll attribute_only_str ", attribute_only_str)

if "bpy.context.scene" in full_str:
prop_type, action, prop, path_attr = attr_get_type(
Expand Down

0 comments on commit 4b12faf

Please sign in to comment.