Created
June 1, 2016 08:23
-
-
Save peace098beat/1629ea5c8a7253d99db4269e81be52ec to your computer and use it in GitHub Desktop.
[Blender] ブレンダーで球を作るスクリプト
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| """ | |
| create_casing.py | |
| Tomoyuki Nohara 2016/06/01 | |
| TODO: Add Material | |
| """ | |
| import bpy | |
| # *************************************** | |
| # Setting | |
| # *************************************** | |
| # 単位系 | |
| bpy.data.scenes['Scene'].unit_settings.system = "METRIC" | |
| bpy.data.scenes['Scene'].unit_settings.system = "NONE" | |
| bpy.data.scenes['Scene'].unit_settings.system_rotation = "DEGREES" | |
| # 1[BU] = a[m] | |
| bpy.data.scenes['Scene'].unit_settings.scale_length = 1 | |
| # *************************************** | |
| # Parameter | |
| # *************************************** | |
| cm = 1e-2 | |
| # Casing | |
| case_radius_outer = 15 * cm # [m] | |
| case_tickness = 0.5 * cm # [m] | |
| case_radius_inner = case_radius_outer - case_tickness # [m] | |
| # Rib | |
| rib_radius_innter = 10 * cm # [m] | |
| rib_tickness = 1 * cm # [m] | |
| # *************************************** | |
| # Utilities | |
| # *************************************** | |
| def delete_all(): | |
| for item in bpy.context.scene.objects: | |
| bpy.context.scene.objects.unlink(item) | |
| for item in bpy.data.objects: | |
| bpy.data.objects.remove(item) | |
| for item in bpy.data.meshes: | |
| bpy.data.meshes.remove(item) | |
| for item in bpy.data.materials: | |
| bpy.data.materials.remove(item) | |
| def delete_object(item): | |
| bpy.context.scene.objects.unlink(item) | |
| bpy.data.objects.remove(item) | |
| # bpy.data.meshes.remove(item) | |
| def get_active_object(): | |
| bpy.ops.object.mode_set(mode='OBJECT') | |
| return bpy.context.active_object | |
| # *************************************** | |
| # Utilities | |
| # *************************************** | |
| def deg_to_rad(d): | |
| import math | |
| return d * math.pi / 180.0 | |
| # *************************************** | |
| # Utilities | |
| # *************************************** | |
| def rename(_name): | |
| obj = bpy.data.objects[0] | |
| obj.name = _name | |
| def get_objs(): | |
| return bpy.data.objects | |
| # *************************************** | |
| # Function | |
| # *************************************** | |
| def difference(target, opObj, delete=True): | |
| '''subtract opObj from the target''' | |
| # Deselect All | |
| bpy.ops.object.select_all(action='DESELECT') | |
| # Select the new object. | |
| target.select = True | |
| bpy.context.scene.objects.active = target | |
| # Add a modifier | |
| bpy.ops.object.modifier_add(type='BOOLEAN') | |
| mod = target.modifiers | |
| mod[0].name = "SubEmUp1" | |
| mod[0].object = opObj | |
| mod[0].operation = 'DIFFERENCE' | |
| # Apply modifier | |
| bpy.ops.object.modifier_apply(apply_as='DATA', modifier=mod[0].name) | |
| if delete: | |
| delete_object(opObj) | |
| return target | |
| def union(target, opObj, delete=True): | |
| # Deselect All | |
| bpy.ops.object.select_all(action='DESELECT') | |
| # Select the new object. | |
| target.select = True | |
| bpy.context.scene.objects.active = target | |
| # Add a modifier | |
| bpy.ops.object.modifier_add(type='BOOLEAN') | |
| mod = target.modifiers | |
| mod[0].name = "SubEmUp2" | |
| mod[0].object = opObj | |
| mod[0].operation = 'UNION' | |
| # Apply modifier | |
| bpy.ops.object.modifier_apply(apply_as='DATA', modifier=mod[0].name) | |
| # Delete operation object | |
| if delete: | |
| delete_object(opObj) | |
| return target | |
| def intersect(target, opObj, delete=True): | |
| # Deselect All | |
| bpy.ops.object.select_all(action='DESELECT') | |
| # Select the new object. | |
| target.select = True | |
| bpy.context.scene.objects.active = target | |
| # Add a modifier | |
| bpy.ops.object.modifier_add(type='BOOLEAN') | |
| mod = target.modifiers | |
| mod[0].name = "SubEmUp3" | |
| mod[0].object = opObj | |
| mod[0].operation = 'INTERSECT' | |
| # Apply modifier | |
| bpy.ops.object.modifier_apply(apply_as='DATA', modifier=mod[0].name) | |
| # Delete operation object | |
| if delete: | |
| delete_object(opObj) | |
| return target | |
| def add_ring_plane(name): | |
| # make plane | |
| bpy.ops.mesh.primitive_cylinder_add( | |
| vertices=64, | |
| radius=rib_radius_innter*1.5, | |
| depth=rib_tickness, | |
| end_fill_type='NGON', | |
| view_align=False, | |
| enter_editmode=False, | |
| location=(0, 0, 0), | |
| rotation=(0, 0, 0), | |
| layers=(True, False, False, False, False, False, False, False, False, False, | |
| False, False, False, False, False, False, False, False, False, False) | |
| ) | |
| bpy.data.objects[0].name = "tmp_rib1" | |
| # boolean inner | |
| bpy.ops.mesh.primitive_cylinder_add( | |
| vertices=64, | |
| radius=rib_radius_innter, | |
| depth=rib_tickness*2, | |
| end_fill_type='NGON', | |
| view_align=False, | |
| enter_editmode=False, | |
| location=(0, 0, 0), | |
| rotation=(0, 0, 0), | |
| layers=(True, False, False, False, False, False, False, False, False, False, | |
| False, False, False, False, False, False, False, False, False, False) | |
| ) | |
| bpy.data.objects[0].name = "tmp_rib2" | |
| # Diifer | |
| objs = bpy.data.objects | |
| target = objs["tmp_rib1"] | |
| opObj = objs["tmp_rib2"] | |
| target = difference(target, opObj) | |
| target.name = name | |
| return target | |
| def add_ribs(): | |
| rib1 = add_circle_plane("rib1") | |
| rib2 = add_circle_plane("rib2") | |
| rib2.location.z += case_radius_outer/2 | |
| rib3 = add_circle_plane("rib3") | |
| rib3.location.z -= case_radius_outer/2 | |
| return [rib1, rib2, rib3] | |
| def add_sphere(name): | |
| res = bpy.ops.mesh.primitive_uv_sphere_add( | |
| segments=32, | |
| ring_count=16, | |
| size=case_radius_outer, | |
| view_align=False, | |
| enter_editmode=True, | |
| location=(0, 0, 0), | |
| rotation=(0, 0, 0), | |
| layers=(True, False, False, False, False, False, False, False, False, False, | |
| False, False, False, False, False, False, False, False, False, False) | |
| ) | |
| obj = get_active_object() | |
| obj.name = 'case_radius_outer' | |
| res = bpy.ops.mesh.primitive_uv_sphere_add( | |
| segments=32, | |
| ring_count=16, | |
| size=case_radius_inner, | |
| view_align=False, | |
| enter_editmode=True, | |
| location=(0, 0, 0), | |
| rotation=(0, 0, 0), | |
| layers=(True, False, False, False, False, False, False, False, False, False, | |
| False, False, False, False, False, False, False, False, False, False) | |
| ) | |
| obj = get_active_object() | |
| obj.name = 'case_radius_inner' | |
| # Diffierencial | |
| objs = get_objs() | |
| target = objs["case_radius_outer"] | |
| opObj = objs["case_radius_inner"] | |
| target = difference(target, opObj) | |
| target.name = name | |
| return target | |
| def add_sphere_shell(radius, name): | |
| res = bpy.ops.mesh.primitive_uv_sphere_add( | |
| segments=32, | |
| ring_count=16, | |
| size=radius, | |
| view_align=False, | |
| enter_editmode=True, | |
| location=(0, 0, 0), | |
| rotation=(0, 0, 0), | |
| layers=(True, False, False, False, False, False, False, False, False, False, | |
| False, False, False, False, False, False, False, False, False, False) | |
| ) | |
| obj = get_active_object() | |
| obj.name = name | |
| return obj | |
| def add_circle_plane(name): | |
| # make plane | |
| bpy.ops.mesh.primitive_cylinder_add( | |
| vertices=64, | |
| radius=rib_radius_innter*1.5, | |
| depth=rib_tickness, | |
| end_fill_type='NGON', | |
| view_align=False, | |
| enter_editmode=True, | |
| location=(0, 0, 0), | |
| rotation=(0, 0, 0), | |
| layers=(True, False, False, False, False, False, False, False, False, False, | |
| False, False, False, False, False, False, False, False, False, False) | |
| ) | |
| obj = get_active_object() | |
| obj.name = name | |
| objs = get_objs() | |
| target = objs[name] | |
| return target | |
| def make_casing(): | |
| # Case Sphere | |
| case_sphere = add_sphere("case_sphere") | |
| # case_sphere.location.x += case_radius_outer/2 | |
| # Rib Plane 3 | |
| rib_plane1 = add_circle_plane("rib_plane1") | |
| rib_plane2 = add_circle_plane("rib_plane2") | |
| rib_plane2.location.z += case_radius_outer/2 | |
| rib_plane3 = add_circle_plane("rib_plane3") | |
| rib_plane3.location.z -= case_radius_outer/2 | |
| # Triming for Rib Plane | |
| tmp_sphere_shell = add_sphere_shell( | |
| case_radius_outer-case_tickness/10., "tmp_shell") | |
| intersect(rib_plane1, tmp_sphere_shell, False) | |
| intersect(rib_plane2, tmp_sphere_shell, False) | |
| intersect(rib_plane3, tmp_sphere_shell, True) | |
| target = union(case_sphere, rib_plane1) | |
| target = union(case_sphere, rib_plane2) | |
| target = union(case_sphere, rib_plane3) | |
| # Triming Inner Sphere | |
| tmp_inner_shell = add_sphere_shell( | |
| radius=rib_radius_innter, name="tmp_inner_shell") | |
| difference(case_sphere, tmp_inner_shell) | |
| # D A N M E N | |
| bpy.ops.mesh.primitive_cube_add( | |
| radius= 1, | |
| view_align=False, | |
| enter_editmode=True, | |
| location=(1, 0, 0), | |
| ) | |
| obj=get_active_object() | |
| difference(case_sphere, obj) | |
| # *************************************** | |
| # main | |
| # *************************************** | |
| if __name__ == '__main__': | |
| delete_all() | |
| make_casing() | |
| os = bpy.data.objects | |
| for o in os: | |
| print(o) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment