This repository has been archived by the owner on Aug 1, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 82
/
Copy pathblender_snippets.py
152 lines (109 loc) · 3.98 KB
/
blender_snippets.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
from typing import Any, List, Optional
def active_layer_collection(
collection: str, scene: Optional[str] = "Scene", viewlayer: Optional[str] = "View Layer"
) -> str:
return f"""
import bpy
viewlayer = bpy.data.scenes["{scene}"].view_layers["{viewlayer}"]
collection = viewlayer.layer_collection.children["{collection}"]
viewlayer.active_layer_collection = collection
"""
def active_layer_master_collection(scene: Optional[str] = "Scene", viewlayer: Optional[str] = "View Layer") -> str:
"""
Select master collection
"""
return f"""
import bpy
viewlayer = bpy.data.scenes["{scene}"].view_layers["{viewlayer}"]
viewlayer.active_layer_collection = viewlayer.layer_collection
"""
def data_objects_remove(name: str) -> str:
return f"""
import bpy
object = bpy.data.objects["{name}"]
bpy.data.objects.remove(object)
"""
def data_new(collection_name: str, *args: List[Any]) -> str:
def string(x: Any):
if isinstance(x, str):
return f"'{x}'"
else:
return str(x)
s = ",".join([string(x) for x in args])
return f"""
import bpy
data = bpy.data.{collection_name}.new({s})
"""
def data_collections_new(name: str) -> str:
return data_new("collections", name)
def data_collections_remove(collection_name: str) -> List[str]:
return f"""
import bpy
collection = bpy.data.collections["{collection_name}"]
bpy.data.collections.remove(collection)
"""
def data_collections_rename(old_name: str, new_name: str) -> str:
return data_rename("collections", old_name, new_name)
def data_lights_update(name: str, property_update: str) -> str:
return data_update("objects", name, property_update)
def data_lights_rename(old_name: str, new_name: str) -> str:
return data_rename("lights", old_name, new_name)
def data_objects_new(*args: List[Any]) -> str:
return data_new("objects", *args)
def data_objects_rename(old_name: str, new_name: str) -> str:
return data_rename("objects", old_name, new_name)
def data_objects_update(name: str, property_update: str) -> str:
return data_update("objects", name, property_update)
def data_scenes_rename(old_name: str, new_name: str) -> str:
return data_rename("scenes", old_name, new_name)
def data_rename(collection_name: str, old_name: str, new_name: str) -> str:
return f"""
import bpy
bpy.data.{collection_name}["{old_name}"].name = "{new_name}"
"""
def data_update(collection_name: str, name: str, property_update: str) -> str:
return f"""
import bpy
bpy.data.{collection_name}["{name}"]{property_update}
"""
def ops_objects_light_add(light_type: Optional[str] = "POINT", location: Optional[str] = "0.0, 0.0, 0.0") -> str:
return f"""
import bpy
bpy.ops.object.light_add(type="{light_type}", location=({location}))
"""
def scene_collection_objects_unilink(object_name: str, scene_name: Optional[str] = "Scene") -> str:
return f"""
import bpy
scene = bpy.data.scenes["{scene_name}"]
object = bpy.data.objects["{object_name}"]
scene.collection.objects.unlink(object)
"""
def scene_collection_children_link(collection_name: str, scene_name: Optional[str] = "Scene") -> str:
return f"""
import bpy
scene = bpy.data.scenes["{scene_name}"]
collection = bpy.data.collections["{collection_name}"]
scene.collection.children.link(collection)
"""
def scene_collection_children_unlink(collection_name: str, scene_name: Optional[str] = "Scene") -> str:
return f"""
import bpy
scene = bpy.data.scenes["{scene_name}"]
collection = bpy.data.collections["{collection_name}"]
scene.collection.children.unlink(collection)
"""
def trigger_scene_update(scene_name: str = "Scene") -> str:
# Changing anything will trigger an update
return f"""
import bpy
scene = bpy.data.scenes["{scene_name}"]
uuid = scene.mixer_uuid
scene.mixer_uuid = uuid
"""
def collection_objects_unlink(object_name: str, collection_name: str) -> str:
return f"""
import bpy
object = bpy.data.objects["{object_name}"]
collection = bpy.data.collections["{collection_name}"]
collection.objects.unlink(object)
"""