-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathcreate_planet.py
156 lines (109 loc) · 3.72 KB
/
create_planet.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
import bpy
import os
import csv
import math
# This file is a template for the workshop on
# creating planets in Blender. Adjust, rewrite and
# extend the functions according to your own needs.
def delete_planets():
"""Delete objects with names matching 'Planet-*'"""
bpy.ops.object.select_all(action='DESELECT')
bpy.ops.object.select_pattern(pattern="Planet-*")
n = len(bpy.context.selected_objects)
bpy.ops.object.delete()
print("%d planet(s) were deleted." % n)
return
def delete_unused_materials():
"""Delete all unused materials (also done automatically after reload)"""
i = 0
for mat in bpy.data.materials:
if mat.users == 0:
name = mat.name
bpy.data.materials.remove(mat)
i = i + 1
print("Deleted material ", name)
print("%d materials were deleted." % i)
return
def delete_unused_textures():
"""Delete all unused textures"""
# Be careful, since this really deletes all textures which are currently
# not used. It may thus delete more than you intended.
i = 0
for tex in bpy.data.textures:
if tex.users == 0:
name = tex.name
bpy.data.textures.remove(tex)
i = i + 1
print("Deleted texture ", name)
print("%d textures were deleted." % i)
return
def add_texture(mat, imgname):
"""Add image texture to given material, map as sphere
mat -- material
imgname -- name of texture image file
"""
# create texture, add image
img = bpy.data.images.load(imgname)
tex = bpy.data.textures.new(imgname, type='IMAGE')
tex.image = img
# add texture to material, set mapping
mtex = mat.texture_slots.add()
mtex.texture = tex
mtex.texture_coords = 'ORCO'
mtex.mapping = 'SPHERE'
return
def add_material(obj, name):
"""Add material to an object
obj -- object to which the material is appended
name -- basename to be added to material name
"""
# create a material
matname = "Material-" + name
mat = bpy.data.materials.new(matname)
mat.diffuse_color = [0,0,1]
mat.specular_intensity = 0.1
# add material to the object
obj.data.materials.append(mat)
return mat
def add_sphere(name, location):
"""Add smooth sphere to current scene at given location
name -- name for new sphere
location -- location for the new sphere
"""
# add object
bpy.ops.mesh.primitive_uv_sphere_add(
segments=48, ring_count=24, size=1.0,
location=location, rotation=[0,0,0])
# get object
obj = bpy.context.object
# set name and smooth shading
obj.name = name
bpy.ops.object.shade_smooth()
print("Sphere '%s' created." % name)
return obj
if __name__ == '__main__':
# set current directory, in case we need it to load files
dir = os.path.dirname(bpy.data.filepath) + os.sep
# delete objects that were previously created by this script
delete_planets()
# also delete unused materials and textures
delete_unused_materials()
delete_unused_textures()
# This would be a good place to start a loop over all planets.
location = [2.5,0,0]
name = "Earth"
objname = "Planet-" + name
# create planet as sphere object
obj = add_sphere(objname, location)
# add a material to the object
mat = add_material(obj, name)
# add texture to the material
#imgname = dir + "textures/earth.jpg"
#add_texture(mat, imgname)
# add flattening of planet-sphere
# add axial tilt
# add orbit paths
# add orbit animation
# add rotation animation
# add rings for some planets
# end of loop