You are on page 1of 5

import bpy

import pdb
import random
import math
from math import sin, cos, radians
from bpy.props import *

class MyOperator(bpy.types.Operator):
""" for cube art """
bl_idname = "object.my_operator"
bl_label = "My cube art operator"
bl_options = {'REGISTER', 'UNDO'}

# properties
spacing : FloatProperty(
name="Spacing",
description="Spacing between cubes",
default=2.1,
min=0,
max=7.0
)
cube_count : IntProperty(
name="cube count",
description="A number of cubes",
default=25,
min=1,
max=100
)

width_of_spiral : FloatProperty(
name="Width of spiral",
description="Width of the spiral",
default=1,
min=0,
max=999.0
)
height_of_spiral : FloatProperty(
name="Height of spiral",
description="Height of the spiral",
default=1,
min=0.1,
max=999.0
)

def spiral_equation( self, t ):


""" returns (x, y, z) coordinates of a spiral """
x = self.width_of_spiral*cos(t) * self.spacing
y = self.width_of_spiral*sin(t) * self.spacing
z = self.height_of_spiral*t * self.spacing
return (x, y, z)

def generate_random_bright_color():
""" generates random bright color """
# Generate random RGB values between 0 and 1
r = random.random()
g = random.random()
b = random.random()

# Increase brightness by setting the maximum channel value to 1


max_val = max(r, g, b)
r /= max_val
g /= max_val
b /= max_val

return (r, g, b)

def get_material( self, input_name ,input_color ):


""" Creates material if doesn't exist """
material = bpy.data.materials.get(input_name)
if material is None:
material = bpy.data.materials.new(name=input_name)
material.diffuse_color = input_color
return material

def spawn_art( self, collecion_name ):


""" creates a cubes and lights art """
art_collection = bpy.data.collections.new(collecion_name) # create a
collection
bpy.context.scene.collection.children.link(art_collection) # child of the
scene collection

material_1 = self.get_material("Material.001", (0, .905, .702)) # RGB


values for #00E7B3
material_2 = self.get_material("Material.002", (.098, .216, .247)) # RGB
values for #18383F

for t in range(0, self.cube_count):


# agle = radians( random.randint(0, 360) )
# for x in range(5):
# z = abs( sin(agle) * 4) * random.random() * 1.2
coor = self.spiral_equation(t)

# Create a cube object


bpy.ops.mesh.primitive_cube_add(size=2, location=coor)
cube_object = bpy.context.object

# Unlink the cube from the Scene collection


bpy.context.collection.objects.unlink(cube_object)
# Assign the cube to current collection
art_collection.objects.link(cube_object)

# Assign a random material to the cube


if random.random() < 0.33:
cube_object.data.materials.append(material_1)
else:
cube_object.data.materials.append(material_2)
# agle += radians(9*1)

def remove_collection(collection_name):
pdb.set_trace()
# Get the collection
collection = bpy.data.collections.get(collection_name)

if collection is not None:


# Delete objects in the collection
for obj in collection.objects:
bpy.data.objects.remove(obj, do_unlink=True)
# Delete lights in the collection (not Main lights)
lights_to_remove = [obj for obj in bpy.data.objects if obj.type ==
'LIGHT' and not obj.name.startswith("Main_light")]
for light_obj in lights_to_remove:
bpy.data.lights.remove(light_obj.data, do_unlink=True)
bpy.data.objects.remove(light_obj, do_unlink=True)

# Unlink and remove the collection


bpy.data.collections.remove(collection, do_unlink=True)

def execute(self, context):


# self.remove_collection("Art")
self.spawn_art("Art")

return {'FINISHED'}

def menu_func(self, context):


self.layout.operator(MyOperator.bl_idname, text=MyOperator.bl_label)

# Register and add to the "object" menu (required to also use F3 search "Simple
Object Operator" for quick access).
def register():
bpy.utils.register_class(MyOperator)
bpy.types.VIEW3D_MT_object.append(menu_func)

def unregister():
bpy.utils.unregister_class(MyOperator)
bpy.types.VIEW3D_MT_object.remove(menu_func)

if __name__ == "__main__":
register()

# test call
bpy.ops.object.my_operator()
import bpy
import random

SPACING = 2
def delete_cylinders():
# Iterate over all objects in the scene
for obj in bpy.context.scene.objects:
if obj.type == 'MESH' and obj.data.name.startswith('Cylinder'):
bpy.data.objects.remove(obj, do_unlink=True)

def rock_chunk(x_s , y_s , z_s):


hexagons = []
for x in range(2):
for y in range(15):
loc = (x_s + x * SPACING, y_s + y * SPACING, random.random() * 2 +
z_s)
bpy.ops.mesh.primitive_cylinder_add( location=loc, scale=(1, 1, 9),
vertices = 6)
if (y + 1) % 10:
loc = location=(x_s + x * SPACING, y_s + y * SPACING,
random.random() * 2 +z_s)
bpy.ops.mesh.primitive_cylinder_add( location=loc, scale=(1, 1, 9),
vertices = 6)
hexagon = bpy.context.object
hexagons.append(hexagon)

bpy.ops.object.select_all(action='DESELECT')
if hexagons is not []:
for hexagon in hexagons:
hexagon.select_set(True)

bpy.context.view_layer.objects.active = hexagons[0]
bpy.ops.object.join()

dal = 50

delete_cylinders()

for y in range(4):
rock_chunk(5 , y*dal , 0)
rock_chunk(-5 , y*dal , 0)

rock_chunk(4 , y*dal , -15)


rock_chunk(-4 , y*dal , -15)

rock_chunk(3 , y*dal , 20)


rock_chunk(-3 , y*dal , 20)
rock_chunk(2 , y*dal , 15)
rock_chunk(-2 , y*dal , 15)

rock_chunk(3 , 5+y*dal, -20)


rock_chunk(-3, 5+y*dal, -20)

You might also like