-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.py
166 lines (135 loc) · 5.58 KB
/
utils.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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
import sys, random, os
import bpy, bpy_extras
"""
Some utility functions for interacting with Blender
"""
def version_supported(version):
return (2, 9, 3) <= version <= (3, 0, 0)
def extract_args(input_argv=None):
"""
Pull out command-line arguments after "--". Blender ignores command-line flags
after --, so this lets us forward command line arguments from the blender
invocation to our own script.
"""
if input_argv is None:
input_argv = sys.argv
output_argv = []
if '--' in input_argv:
idx = input_argv.index('--')
output_argv = input_argv[(idx + 1):]
return output_argv
def parse_args(parser, argv=None):
return parser.parse_args(extract_args(argv))
def delete_object(obj):
""" Delete a specified blender object """
for o in bpy.data.objects:
o.select_set(state=False)
obj.select_set(state=True)
bpy.ops.object.delete()
def get_camera_coords(cam, pos):
"""
For a specified point, get both the 3D coordinates and 2D pixel-space
coordinates of the point from the perspective of the camera.
Inputs:
- cam: Camera object
- pos: Vector giving 3D world-space position
Returns a tuple of:
- (px, py, pz): px and py give 2D image-space coordinates; pz gives depth
in the range [-1, 1]
"""
scene = bpy.context.scene
x, y, z = bpy_extras.object_utils.world_to_camera_view(scene, cam, pos)
scale = scene.render.resolution_percentage / 100.0
w = int(scale * scene.render.resolution_x)
h = int(scale * scene.render.resolution_y)
px = int(round(x * w))
py = int(round(h - y * h))
return (px, py, z)
def set_layer(obj, layer_idx):
""" Move an object to a particular layer """
# Set the target layer to True first because an object must always be on
# at least one layer.
obj.layers[layer_idx] = True
for i in range(len(obj.layers)):
obj.layers[i] = (i == layer_idx)
def add_object(object_dir, name, scale, loc, theta=0):
"""
Load an object from a file. We assume that in the directory object_dir, there
is a file named "$name.blend" which contains a single object named "$name"
that has unit size and is centered at the origin.
- scale: scalar giving the size that the object should be in the scene
- loc: tuple (x, y) giving the coordinates on the ground plane where the
object should be placed.
"""
# First figure out how many of this object are already in the scene so we can
# give the new object a unique name
count = 0
for obj in bpy.data.objects:
if obj.name.startswith(name):
count += 1
filename = os.path.join(object_dir, '%s.blend' % name, 'Object', name)
bpy.ops.wm.append(filename=filename)
# Give it a new name to avoid conflicts
new_name = '%s_%d' % (name, count)
bpy.data.objects[name].name = new_name
# Set the new object as active, then rotate, scale, and translate it
x, y, z = loc
bpy.context.view_layer.objects.active = bpy.data.objects[new_name]
bpy.context.object.rotation_euler[2] = theta
bpy.ops.transform.resize(value=(scale, scale, scale))
bpy.ops.transform.translate(value=(x, y, z + scale))
def load_materials(material_dir):
"""
Load materials from a directory. We assume that the directory contains .blend
files with one material each. The file X.blend has a single NodeTree item named
X; this NodeTree item must have a "Color" input that accepts an RGBA value.
"""
for fn in os.listdir(material_dir):
if not fn.endswith('.blend'):
continue
name = os.path.splitext(fn)[0]
filepath = os.path.join(material_dir, fn, 'NodeTree', name)
bpy.ops.wm.append(filename=filepath)
def add_material(name, **properties):
"""
Create a new material and assign it to the active object. "name" should be the
name of a material that has been previously loaded using load_materials.
"""
# Figure out how many materials are already in the scene
mat_count = len(bpy.data.materials)
# Create a new material; it is not attached to anything and
# it will be called "Material"
bpy.ops.material.new()
# Get a reference to the material we just created and rename it;
# then the next time we make a new material it will still be called
# "Material" and we will still be able to look it up by name
mat = bpy.data.materials['Material']
mat.name = 'Material_%d' % mat_count
# Attach the new material to the active object
# Make sure it doesn't already have materials
obj = bpy.context.active_object
assert len(obj.data.materials) == 0
obj.data.materials.append(mat)
# Find the output node of the new material
output_node = None
for n in mat.node_tree.nodes:
if n.name == 'Material Output':
output_node = n
break
# Add a new GroupNode to the node tree of the active material,
# and copy the node tree from the preloaded node group to the
# new group node. This copying seems to happen by-value, so
# we can create multiple materials of the same type without them
# clobbering each other
group_node = mat.node_tree.nodes.new('ShaderNodeGroup')
group_node.node_tree = bpy.data.node_groups[name]
# Find and set the "Color" input of the new group node
for inp in group_node.inputs:
if inp.name in properties:
inp.default_value = properties[inp.name]
# Wire the output of the new group node to the input of
# the MaterialOutput node
mat.node_tree.links.new(
group_node.outputs['Shader'],
output_node.inputs['Surface'],
)