python random

cycles

Attachments




Attachments


Cool man! :RocknRoll: What algorithm did you use to generate that?

it looks awesome ,the blue one can be used as a desktop wallpaper :slight_smile:

    • create Bezier Curve
    • go in Edit Mode
    • select one point (vertex)
    • run script
    • add Bevel Object

import bpy
import random
dx = 0
dy = 0
dz = 0
for j in range(1,1200):
    x = random.uniform(-1, 1)
    y = random.uniform(-1, 1)
    z = random.uniform(-1, 1)
    dx = dx + x
    dy = dy + y
    dz = dz + z
    if ((dx <= 5) and (dx >= -5)):
        if ((dy <= 5) and (dy >= -5)):
            if ((dz <= 1) and (dz >= -1)):
                bpy.ops.curve.extrude_move(CURVE_OT_extrude={"mode":'TRANSLATION'}, TRANSFORM_OT_translate={"value":(x, y, z), "constraint_axis":(True, True, True), "constraint_orientation":'LOCAL', "mirror":False, "proportional":'DISABLED', "proportional_edit_falloff":'SMOOTH', "proportional_size":1, "snap":False, "snap_target":'CLOSEST', "snap_point":(0, 0, 0), "snap_align":False, "snap_normal":(0, 0, 0), "gpencil_strokes":False, "texture_space":False, "remove_on_cancel":False, "release_confirm":False})
                bpy.ops.transform.rotate(value=2, axis=(x,y,z), constraint_axis=(False, False, False), constraint_orientation='GLOBAL', mirror=False, proportional='DISABLED', proportional_edit_falloff='SMOOTH', proportional_size=1)
            else:
                dx = dx - x
                dy = dy - y
                dz = dz - z
        else:
            dx = dx - x
            dy = dy - y
            dz = dz - z
    else:
        dx = dx - x
        dy = dy - y
        dz = dz - z

Cheater! No really that’s pretty cool…thanks for sharing :slight_smile:

different look

Attachments


Attachments


Your script inspired this :wink:

Install it like an add-on and play around with it if you like.


import random
import math
import time


import bpy
from bpy.props import IntProperty


bl_info = {
    "name": "Python Random Generator",
    "author": "Jake Dube",
    "version": (1, 0),
    "blender": (2, 77, 0),
    "location": "3D View > Tools > Random",
    "description": "Generates various random meshes with Python.",
    "wiki_url": "",
    "category": "Add Mesh",
}




class RandomGeneratorPanel(bpy.types.Panel):
    bl_label = "Random Generator"
    bl_idname = "3D_VIEW_PT_layout_RandomGenerator"
    bl_space_type = 'VIEW_3D'
    bl_region_type = 'TOOLS'
    bl_context = "objectmode"
    bl_category = 'Random'


    def draw(self, context):
        scene = context.scene
        layout = self.layout
        
        row = layout.row()
        row.scale_y = 1.5
        row.operator("randomgen.generate_cuboid", icon="MOD_BUILD")
        
        col = layout.column()
        col.prop(scene, 'rg_loops')
        col.prop(scene, 'rg_resets')
        col.prop(scene, 'rg_size')
        


class RandomGeneratorCuboid(bpy.types.Operator):
    bl_label = "Generate Cuboid"
    bl_idname = "randomgen.generate_cuboid"
    bl_description = "Generates cool cuboid ;)"
    bl_options = {'REGISTER', 'UNDO'}


    def execute(self, context):
        print("Starting Generation...")
        MAX_RESETS = bpy.context.scene.rg_resets
        MAX_LOOPS = bpy.context.scene.rg_loops


        # setup base
        bpy.ops.mesh.primitive_cube_add(radius=0.5, view_align=False, enter_editmode=False, location=(0, 0, 0))
        bpy.ops.object.mode_set(mode='EDIT')


        # setup record of locations
        extrusions = [(0,0,0)]
        resets = 0
        for i in range(0,MAX_LOOPS):
            # duplicate cube to new location
            choices = [(1,0,0), (0,1,0), (0,0,1), (-1,0,0), (0,-1,0), (0,0,-1)]
            ver_choices = []
            for c in choices:
                loc = transform_loc(extrusions[-1],c)
                if verify(loc, extrusions):
                    ver_choices += [c]
            
            if ver_choices:
                xyz = random.choice(ver_choices)
                bpy.ops.mesh.duplicate_move(MESH_OT_duplicate={"mode":1}, TRANSFORM_OT_translate={"value":(xyz), "mirror":False, "proportional":'DISABLED', "proportional_edit_falloff":'SMOOTH', "proportional_size":1, "snap":False, "snap_target":'CLOSEST', "snap_point":(0, 0, 0), "snap_align":False, "snap_normal":(0, 0, 0), "gpencil_strokes":False, "texture_space":False, "remove_on_cancel":False, "release_confirm":False})
                
                # record new location
                extrusions += [transform_loc(extrusions[-1], xyz)]
            else:
                resets += 1
                print("Resets:", resets, "Loops:", i)
                if resets >= MAX_RESETS:
                    break
                # reached a dead end so go to a random spot and keep going
                extrusions += [transform_loc(extrusions[-1], random.choice(extrusions))]
                
                
        # cleanup
        bpy.ops.mesh.select_all(action='SELECT')
        bpy.ops.mesh.remove_doubles()
        bpy.ops.object.mode_set(mode='OBJECT')
        
        return {'FINISHED'}




def verify(choice, extrusions):
    max = bpy.context.scene.rg_size
    x1,y1,z1 = choice
    close = 0
    for x2,y2,z2 in extrusions:
        distance = math.sqrt((x2-x1)**2 + (y2-y1)**2 + (z2-z1)**2)
        if distance < 1.5:
            close += 1
        if close > 2:
            return False
    if x1 < 25 and y1 < 25 and z1 < 25:
        return True


def transform_loc(base,transform):
    x1,y1,z1 = base
    x2,y2,z2 = transform
    return (x1+x2,y1+y2,z1+z2)


classes = [RandomGeneratorPanel, RandomGeneratorCuboid]


def register():
    for i in classes:
        bpy.utils.register_class(i)
    
    bpy.types.Scene.rg_resets = IntProperty(name="Resets", default=1000, min=0, max=100000000)
    bpy.types.Scene.rg_loops = IntProperty(name="Loops", default=10000, min=1, max=1000000000)
    bpy.types.Scene.rg_size = IntProperty(name="Size", default=10, min=1, max=1000)
        
def unregister():
    for i in classes:
        bpy.utils.unregister_class(i)
    
    del bpy.types.Scene.rg_resets
    del bpy.types.Scene.rg_loops
    del bpy.types.Scene.rg_size
        
if __name__ == "__main__":
    register()



1 Like

cool script @Graphics_Dev

Attachments


Nice render. :slight_smile:



cycle + PS

Cool stuff, gunarsb. That blue hairball is really interesting. How do you make the sphere shape instead of the square one.

Killer man! Sort of gives a bokeh look…sweet job on the DOF. :wink:

space where calculates points is like cube with dimensions x = (-1,1), y = (-1,1) and z = (-1,1) only you need to change camera position to different angle

    • create Bezier Curve
    • go in Edit Mode
    • select one point (vertex)
    • run script
    • add Bevel Object
import bpy
import random
dx = 0
dy = 0
dz = 0
for j in range(1,3000):
    x = random.uniform(-1, 1)
    y = random.uniform(-1, 1)
    z = random.uniform(-1, 1)
    dx = dx + x
    dy = dy + y
    dz = dz + z
    if ((dx <= 1) and (dx >= -1)):
        if ((dy <= 1) and (dy >= -1)):
            if ((dz <= 1) and (dz >= -1)):
                bpy.ops.curve.extrude_move(CURVE_OT_extrude={"mode":'TRANSLATION'}, TRANSFORM_OT_translate={"value":(x, y, z), "constraint_axis":(True, True, True), "constraint_orientation":'LOCAL', "mirror":False, "proportional":'DISABLED', "proportional_edit_falloff":'SMOOTH', "proportional_size":1, "snap":False, "snap_target":'CLOSEST', "snap_point":(0, 0, 0), "snap_align":False, "snap_normal":(0, 0, 0), "gpencil_strokes":False, "texture_space":False, "remove_on_cancel":False, "release_confirm":False})
                bpy.ops.transform.rotate(value=2, axis=(x,y,z), constraint_axis=(False, False, False), constraint_orientation='GLOBAL', mirror=False, proportional='DISABLED', proportional_edit_falloff='SMOOTH', proportional_size=1)
            else:
                dx = dx - x
                dy = dy - y
                dz = dz - z
        else:
            dx = dx - x
            dy = dy - y
            dz = dz - z
    else:
        dx = dx - x
        dy = dy - y
        dz = dz - z
1 Like


import bpy
import random
import math
dx = 0
dy = 0
dz = 0
for j in range(1,90000):
    x = random.uniform(-2, 2)
    y = random.uniform(-2, 2)
    z = random.uniform(-2, 2)
    dx = dx + x
    dy = dy + y
    dz = dz + z
    if ((dx <= 2) and (dx >= -2)):
        if ((dy <= 2) and (dy >= -2)):
            if ((dz <= 2) and (dz >= -2)):
                if (math.pow(dx,2) + math.pow(dy,2) + math.pow(dz,2) <= math.pow(2,2)
                and math.pow(dx,2) + math.pow(dy,2) + math.pow(dz,2) >= math.pow(1.95,2)):
                    bpy.ops.curve.extrude_move(CURVE_OT_extrude={"mode":'TRANSLATION'}, TRANSFORM_OT_translate={"value":(x, y, z), "constraint_axis":(True, True, True), "constraint_orientation":'LOCAL', "mirror":False, "proportional":'DISABLED', "proportional_edit_falloff":'SMOOTH', "proportional_size":1, "snap":False, "snap_target":'CLOSEST', "snap_point":(0, 0, 0), "snap_align":False, "snap_normal":(0, 0, 0), "gpencil_strokes":False, "texture_space":False, "remove_on_cancel":False, "release_confirm":False})
                    bpy.ops.transform.rotate(value=10, axis=(x,y,z), constraint_axis=(False, False, False), constraint_orientation='GLOBAL', mirror=False, proportional='DISABLED', proportional_edit_falloff='SMOOTH', proportional_size=1)
                else:
                    dx = dx - x
                    dy = dy - y
                    dz = dz - z
            else:
                dx = dx - x
                dy = dy - y
                dz = dz - z
        else:
            dx = dx - x
            dy = dy - y
            dz = dz - z
    else:
        dx = dx - x
        dy = dy - y
        dz = dz - z


python + cycles

@gunarsb

  • What are your cube material and vines material nodes?
  • Is it possible to make fractals with this script ?