-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathtest_script.py
More file actions
59 lines (43 loc) · 1.61 KB
/
test_script.py
File metadata and controls
59 lines (43 loc) · 1.61 KB
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
import bpy
import os
# Create a material with a texture.
bpy.ops.mesh.primitive_ico_sphere_add(radius=1, enter_editmode=False, location=(0, 0, 2))
mymesh = bpy.context.active_object
# Create a material and link it to the mesh
mymat = bpy.data.materials.new("mymat")
mymesh.active_material = mymat
# Not necessary, but demonstrates how to get a material from a mesh.
mymat = mymesh.active_material
# Setup nodes in the material.
mymat.use_nodes = True
node_tree = mymat.node_tree
# Add an image
img_node = node_tree.nodes.new('ShaderNodeTexImage')
# Link the image node to the bsdf shader.
diff_node = node_tree.nodes['Principled BSDF']
node_tree.links.new(img_node.outputs['Color'], diff_node.inputs['Base Color'])
# Open an image.
bpy.ops.image.open(filepath="/path/to/mytex.png")
# Set the image on the object.
img_node.image = bpy.data.images['mytex.png']
def get_view3d_area():
'''Gets the 3d viewport. It is relative to whatever area the python script runs in.'''
for ar in bpy.context.screen.areas.values():
if ar.type == 'VIEW_3D':
return ar
def set_shading_to_mat():
'''Sets the viewport shading to 'rendered material'.'''
area = get_view3d_area()
area.spaces[0].shading.type = 'MATERIAL'
def set_shading_col_img():
'''Sets the viewport colour to '(unshaded) texture'.'''
area = get_view3d_area()
area.spaces[0].shading.color_type = 'TEXTURE'
# Get current objects
bpy.context.scene.objects.values()
# Activate texture shading.
set_shading_col_img()
# Get the active object
bpy.context.view_layer.objects.active = mymesh
# Set an object as selected.
mymesh.select_set(True)