Newer
Older
if not material:
material = bpy.data.materials.new(name=name_compat)
material.use_nodes = True
if self.use_transparency:
material.blend_method = 'BLEND'
node_tree = material.node_tree
out_node = clean_node_tree(node_tree)
tex_image = self.create_cycles_texnode(context, node_tree, img_spec)
if self.shader == 'PRINCIPLED':
core_shader = node_tree.nodes.new('ShaderNodeBsdfPrincipled')
elif self.shader == 'SHADELESS':
core_shader = get_shadeless_node(node_tree)
else: # Emission Shading
core_shader = node_tree.nodes.new('ShaderNodeEmission')
core_shader.inputs['Strength'].default_value = self.emit_strength
# Connect color from texture
node_tree.links.new(core_shader.inputs[0], tex_image.outputs['Color'])
Bastien Montagne
committed
if self.shader == 'PRINCIPLED':
node_tree.links.new(core_shader.inputs['Alpha'], tex_image.outputs['Alpha'])
Bastien Montagne
committed
else:
bsdf_transparent = node_tree.nodes.new('ShaderNodeBsdfTransparent')
mix_shader = node_tree.nodes.new('ShaderNodeMixShader')
node_tree.links.new(mix_shader.inputs['Fac'], tex_image.outputs['Alpha'])
node_tree.links.new(mix_shader.inputs[1], bsdf_transparent.outputs['BSDF'])
Bastien Montagne
committed
node_tree.links.new(mix_shader.inputs[2], core_shader.outputs[0])
core_shader = mix_shader
node_tree.links.new(out_node.inputs['Surface'], core_shader.outputs[0])
auto_align_nodes(node_tree)
return material
# -------------------------------------------------------------------------
# Geometry Creation
def create_image_plane(self, context, name, img_spec):
width, height = self.compute_plane_size(context, img_spec)
# Create new mesh
bpy.ops.mesh.primitive_plane_add('INVOKE_REGION_WIN')
# Why does mesh.primitive_plane_add leave the object in edit mode???
bpy.ops.object.mode_set(mode='OBJECT')
plane.dimensions = width, height, 0.0
plane.data.name = plane.name = name
bpy.ops.object.transform_apply(location=False, rotation=False, scale=True)
# If sizing for camera, also insert into the camera's field of view
if self.size_mode == 'CAMERA':
offset_axis = self.axis_id_to_vector[self.offset_axis]
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
translate_axis = [0 if offset_axis[i] else 1 for i in (0, 1)]
center_in_camera(context.scene, context.scene.camera, plane, translate_axis)
self.align_plane(context, plane)
return plane
def compute_plane_size(self, context, img_spec):
"""Given the image size in pixels and location, determine size of plane"""
px, py = img_spec.size
# can't load data
if px == 0 or py == 0:
px = py = 1
if self.size_mode == 'ABSOLUTE':
y = self.height
x = px / py * y
elif self.size_mode == 'CAMERA':
x, y = compute_camera_size(
context, context.scene.cursor.location,
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
self.fill_mode, px / py
)
elif self.size_mode == 'DPI':
fact = 1 / self.factor / context.scene.unit_settings.scale_length * 0.0254
x = px * fact
y = py * fact
else: # elif self.size_mode == 'DPBU'
fact = 1 / self.factor
x = px * fact
y = py * fact
return x, y
def align_plane(self, context, plane):
"""Pick an axis and align the plane to it"""
if 'CAM' in self.align_axis:
# Camera-aligned
camera = context.scene.camera
if (camera):
# Find the axis that best corresponds to the camera's view direction
Vector((0, 0, 1)) - camera.matrix_world.col[3].xyz
# pick the axis with the greatest magnitude
mag = max(map(abs, axis))
# And use that axis & direction
axis = Vector([
n / mag if abs(n) == mag else 0.0
for n in axis
])
else:
# No camera? Just face Z axis
axis = Vector((0, 0, 1))
self.align_axis = 'Z+'
axis = self.axis_id_to_vector[self.align_axis]
# rotate accordingly for x/y axiis
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
if not axis.z:
plane.rotation_euler.x = pi / 2
if axis.y > 0:
plane.rotation_euler.z = pi
elif axis.y < 0:
plane.rotation_euler.z = 0
elif axis.x > 0:
plane.rotation_euler.z = pi / 2
elif axis.x < 0:
plane.rotation_euler.z = -pi / 2
# or flip 180 degrees for negative z
elif axis.z < 0:
plane.rotation_euler.y = pi
if self.align_axis == 'CAM':
constraint = plane.constraints.new('COPY_ROTATION')
constraint.target = camera
constraint.use_x = constraint.use_y = constraint.use_z = True
if not self.align_track:
bpy.ops.object.visual_transform_apply()
plane.constraints.clear()
if self.align_axis == 'CAM_AX' and self.align_track:
constraint = plane.constraints.new('LOCKED_TRACK')
constraint.target = camera
constraint.track_axis = 'TRACK_Z'
constraint.lock_axis = 'LOCK_Y'
# -----------------------------------------------------------------------------
# Register
Bastien Montagne
committed
Florian Meyer
committed
def import_images_button(self, context):
Bastien Montagne
committed
self.layout.operator(IMPORT_IMAGE_OT_to_plane.bl_idname, text="Images as Planes", icon='TEXTURE')
Campbell Barton
committed
classes = (
IMPORT_IMAGE_OT_to_plane,
)
for cls in classes:
bpy.utils.register_class(cls)
Brecht Van Lommel
committed
bpy.types.TOPBAR_MT_file_import.append(import_images_button)
bpy.types.VIEW3D_MT_image_add.append(import_images_button)
bpy.app.handlers.load_post.append(register_driver)
register_driver()
Campbell Barton
committed
Brecht Van Lommel
committed
bpy.types.TOPBAR_MT_file_import.remove(import_images_button)
bpy.types.VIEW3D_MT_image_add.remove(import_images_button)
# This will only exist if drivers are active
if check_drivers in bpy.app.handlers.depsgraph_update_post:
bpy.app.handlers.depsgraph_update_post.remove(check_drivers)
Bastien Montagne
committed
bpy.app.handlers.load_post.remove(register_driver)
del bpy.app.driver_namespace['import_image__find_plane_corner']
for cls in classes:
bpy.utils.unregister_class(cls)
Campbell Barton
committed
# Run simple doc tests
import doctest
doctest.testmod()
unregister()