0
$\begingroup$

Help me please to translate generated random values in integer panel property.

I understand that the execute function create a new object every time, but for this simple example will be fine if the values for the last object are saved in panel for edit it.

(Redo-panel already can do this task, but it shows all values that exist. Need to some of values was be generated private.)

enter image description here

import bpy
from random import randint

class VIEW3D_PT_monkeygen(bpy.types.Panel):
    bl_space_type = 'VIEW_3D'
    bl_region_type = 'UI'
    bl_category = "Panel"
    bl_label = "UI"

    def draw(self, context): 
        scene = context.scene
        layout = self.layout
        
        ops = layout.operator('mesh.monkeygen', text='Generate random monkey')
        ops.co_x = randint(1,10)
        ops.co_y = randint(1,10)
        ops.size = randint(1,3)
        
        layout.prop(scene.monkeyprops, "co_x")
        layout.prop(scene.monkeyprops, "co_y")
        layout.prop(scene.monkeyprops, "size")
        
        #ops2 = layout.operator('mesh.monkeygen', text='Monkey from values') 
        #ops2.co_x = scene.monkeyprops.co_x
        #ops2.co_y = scene.monkeyprops.co_y
        #ops2.size = scene.monkeyprops.size

class MESH_OT_monkeygen(bpy.types.Operator):
    bl_idname = "mesh.monkeygen"
    bl_label = "Monkey Generator"
    #bl_options = {'REGISTER', 'UNDO'}

    co_x = bpy.props.IntProperty(
        name="Pos x", default=1, min=1, max=10,
    )
    co_y = bpy.props.IntProperty(
        name="Pos y", default=1, min=1, max=10,
    )
    size = bpy.props.IntProperty(
        name="Size", default=1, min=1, max=3,
    )
    def execute(self, context):
        #do something
        bpy.ops.mesh.primitive_monkey_add(size=self.size, location=(self.co_x, self.co_y, 0))
        return {'FINISHED'}

class MonkeyProperty(bpy.types.PropertyGroup):
    co_x = bpy.props.IntProperty(
        name="Pos x", default=1, min=1, max=10,
    )
    co_y = bpy.props.IntProperty(
        name="Pos y", default=1, min=1, max=10,
    )
    size = bpy.props.IntProperty(
        name="Size", default=1, min=1, max=3,
    )

blender_classes = [
    MESH_OT_monkeygen,
    VIEW3D_PT_monkeygen,
    MonkeyProperty,
]

def register():
    for bpy_class in blender_classes:
        bpy.utils.register_class(bpy_class)
    bpy.types.Scene.monkeyprops = bpy.props.PointerProperty(type=MonkeyProperty)

def unregister():
    for bpy_class in blender_classes:
        bpy.utils.unregister_class(bpy_class)
    del bpy.types.Scene.MonkeyProperty

if __name__ == "__main__":
    register()
$\endgroup$
2
  • $\begingroup$ Save your pos in scene as you access the scene property in panel. Then for each time you create a random new item, write back those values. And those properties will contain a set() function that every time you change the value in panel, modify the last object (either by the last object data or use an additional pointer property to remember it) $\endgroup$
    – HikariTW
    Sep 27 '20 at 14:27
  • $\begingroup$ Thank @HikariTW, you tip let me to find solution, though it was a little easier. My hair turned gray while I was dealing with it. $\endgroup$
    – day9
    Sep 28 '20 at 12:05
0
$\begingroup$

Solution, that generate random values, save it and allow to edit 2 neccesary values from 3 created.

So, value editing not execute in real time, changes is required apply from button (is good for my script with highpoly mesh objects)

import bpy
from random import randint

class VIEW3D_PT_monkeygen(bpy.types.Panel):
    bl_space_type = 'VIEW_3D'
    bl_region_type = 'UI'
    bl_category = "Panel"
    bl_label = "UI"

    def draw(self, context): 
        scene = context.scene
        layout = self.layout
        
        # Generating button
        # nonstop random values generating in draw function
        # values sending to execute function
        ops = layout.operator('mesh.monkeygen', text='Generate random monkey')
        # all existing values
        ops.op_x = randint(1,10)
        ops.op_y = randint(1,10)
        ops.op_size = randint(1,5) # private value for example
        
        # Buttons x y, for control defined values in PropertyGroup
        layout.prop(scene.monkeyprops,'op2_x')
        layout.prop(scene.monkeyprops, "op2_y")
        
        # Apply defined values from PropertyGroup and sending to execute function
        ops2 = layout.operator('mesh.monkeygen', text='From values')
        # only changeable values
        ops2.op_x = scene.monkeyprops.op2_x
        ops2.op_y = scene.monkeyprops.op2_y

class MESH_OT_monkeygen(bpy.types.Operator):
    bl_idname = "mesh.monkeygen"
    bl_label = "Monkey Generator"
    #bl_options = {'REGISTER', 'UNDO'}
    
    op_x = bpy.props.IntProperty(
        name="Pos x", default=1, min=1, max=10,
    )
    op_y = bpy.props.IntProperty(
        name="Pos y", default=1, min=1, max=10,
    )
    op_size = bpy.props.IntProperty(
        name="Pos y", default=1, min=1, max=4,
    )
    
    def execute(self, context):
        meshes = bpy.data.meshes
        # remove previos mesh
        for mesh in meshes:
            if 'Suzanne' in mesh.name:
                meshes.remove(meshes[mesh.name])
        
        #do something
        bpy.ops.mesh.primitive_monkey_add(size=self.op_size, location=(self.op_x, self.op_y, 0))
        
        # set random values to defined values
        bpy.data.scenes['Scene'].monkeyprops.op2_x = self.op_x
        bpy.data.scenes['Scene'].monkeyprops.op2_y = self.op_y
        
        return {'FINISHED'}

class MonkeyProperty(bpy.types.PropertyGroup):
    
    op2_x = bpy.props.IntProperty(
        name="Pos x", default=1, min=1, max=10,
    )
    op2_y = bpy.props.IntProperty(
        name="Pos y", default=1, min=1, max=10,
    )

blender_classes = [
    MESH_OT_monkeygen,
    VIEW3D_PT_monkeygen,
    MonkeyProperty,
]

def register():
    for bpy_class in blender_classes:
        bpy.utils.register_class(bpy_class)
    bpy.types.Scene.monkeyprops = bpy.props.PointerProperty(type=MonkeyProperty)

def unregister():
    for bpy_class in blender_classes:
        bpy.utils.unregister_class(bpy_class)
    del bpy.types.Scene.monkeyprops

if __name__ == "__main__":
    register()
$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.