1
$\begingroup$

I'm trying to generate random rock shapes and then export the objects as .obj. If I enter what's in the following script one line at a time in the console all the objects come out looking different, but when running the script with the loop I end up several that are exact copies.

import bpy
import os
from random import randrange, uniform

mat = bpy.data.materials['Material']
bpy.data.materials["Material"].node_tree.nodes["Principled BSDF"].inputs[0].default_value = (0.02, 0.02, 0.02, 1)

for x in range(5):

    frand = uniform(1,3)

    # Generate synthetic shape
    bpy.ops.mesh.add_mesh_rock(preset_values='1', scale_X=(0.5, 1.25), skew_X=-0.5, scale_Y=(0.5, 1.25), skew_Y=-0.5, scale_Z=(0.5, 1.25), skew_Z=-0.5, use_scale_dis=False, scale_fac=(1, 1, 1), deform=frand, rough=2, detail=3, display_detail=3, smooth_fac=2, smooth_it=2, use_generate=True, use_random_seed=True, user_seed=1)
    
    # Add material
    obj = bpy.context.active_object
    obj.data.materials.append(mat)

    # Filepath for OBJ
    blend_file_path = bpy.data.filepath
    directory = os.path.dirname(blend_file_path)
    target_file = os.path.join(directory, 'GeneratedShapes/myfile%s.obj' % x)

    # Export as OBJ
    bpy.ops.export_scene.obj(filepath=target_file)
    
    # Delete object
    bpy.ops.object.delete(use_global=False)
$\endgroup$

1 Answer 1

2
$\begingroup$

I suggest you use num_of_rocks=5 parameter :

bpy.ops.mesh.add_mesh_rock(preset_values='1', num_of_rocks=5, scale_X=(0.5, 1.25), skew_X=-0.5, scale_Y=(0.5, 1.25), skew_Y=-0.5, scale_Z=(0.5, 1.25), skew_Z=-0.5, use_scale_dis=False, scale_fac=(1, 1, 1), deform=1, rough=2, detail=3, display_detail=3, smooth_fac=2, smooth_it=2, use_generate=True, use_random_seed=True)

The reason it's not working as intended is because the seeding is based on time and for some reason it defaults to the last complete second so you can't have "true" random seed if you execute the operator less than one second after the last iteration.

Link to the culprit couple of lines of code :

https://github.com/sobotka/blender-addons/blob/f6d72972320a891b00d3d9e4519f89f24963bae6/add_mesh_extra_objects/add_mesh_rocks/rockgen.py#L749-L750

If you want to solve it, navigate to your local blender installation folder \ 3.1 (or whatever version you're using) \ scripts\addons\add_mesh_extra_objects\add_mesh_rocks\rockgen.py and replace line 750 with

seed()

(Take care to copy all the extra spaces, or else it won't work)

or simply remove everything between the two parentheses after seed

$\endgroup$

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .