I want to spawn my own custom primitive objects (in this case a sphere from an underlying cube mesh) and store it as an object in a variable.
I have managed to write a class that creates such objects, but I want to be able to store them inside a variable and then manipulate them using the class's methods. If you're wondering what the end goal is: I want to create a model of the solar system and this class is meant to create planets and stars.
My current code looks like this
import bpy import time import numpy start = time.time() # Reset scene after each execution bpy.ops.object.select_all(action='SELECT') bpy.ops.object.delete(use_global=False, confirm=False) # Been told this has less wrinkles in the mesh around the poles. # Hopefully this will make UV unwrapping and texturing easier down the line. class cube_sphere(): # r is radius of the sphere # R is the distance from the origin (location of the sun) def __init__(self,r,R): self.r = r self.R = R # adds the cube and fashions it into a sphere bpy.ops.mesh.primitive_uv_sphere_add(radius=self.r, enter_editmode=False, align='WORLD', location=(0, self.R, 0), scale=(1, 1, 1)) bpy.ops.object.subdivision_set(level=3, relative=False) bpy.context.object.modifiers["Subdivision"].levels = 3 bpy.context.object.modifiers["Subdivision"].render_levels = 3 bpy.ops.object.modifier_apply(modifier="Subdivision") bpy.ops.object.editmode_toggle() bpy.ops.transform.tosphere(value=1, mirror=True, use_proportional_edit=False, proportional_edit_falloff='SMOOTH', proportional_size=1, use_proportional_connected=False, use_proportional_projected=False) bpy.ops.object.editmode_toggle() bpy.ops.object.shade_smooth() # idk what is being put into variable a, but I'm almost certain it is not an object that inherits from the class that actually defines primitive meshes in blender. a = cube_sphere(1,0) end = time.time() print(end-start)
I've been told that using
bpy.ops is very slow since it always comes with a scene update. This is why I time the execution to see how quickly everything is running.
What I'd like to know is how I can store the actual cube object in a variable and then manipulate it by referencing the variable I stored it in. (not actual code)
a = bpy.objects.cube() a.set_edgelength(2) a.rotate(0,0,1)
Or in the case of my custom class:
class(bpy.objects.cube): def __init__(self,r,R): super().__init__() # I think that's how you do it if you specify a parent class in class(parent_class)? self.r = r self.R = R def set_something(self,something): self.something = something ...
Can this be done and where can I read up on that sort of stuff?