analitics

Pages

Wednesday, August 10, 2022

Python 3.10.7 : Manim python package - part 001.

Manim is an engine for precise programmatic animations, designed for creating explanatory math videos, see the official GitHub repo.
Let's install this python package.
python.exe -m pip install manim
...
Successfully installed Pillow-9.2.0 Pygments-2.12.0 certifi-2022.6.15 charset-no
rmalizer-2.1.0 click-8.1.3 click-default-group-1.2.2 cloup-0.13.1 colour-0.1.5 c
ommonmark-0.9.1 decorator-5.1.1 glcontext-2.3.6 idna-3.3 isosurfaces-0.1.0 manim
-0.16.0.post0 manimpango-0.4.1 mapbox-earcut-0.12.11 moderngl-5.6.4 moderngl-win
dow-2.4.1 multipledispatch-0.6.0 networkx-2.8.5 pycairo-1.21.0 pydub-0.25.1 pygl
et-1.5.26 pyrr-0.10.3 requests-2.28.1 rich-12.5.1 scipy-1.9.0 screeninfo-0.8 ski
a-pathops-0.7.2 srt-3.5.2 tqdm-4.64.0 urllib3-1.26.11 watchdog-2.1.9 
You need to install the ffmepg software and add this to the environment path.
The default source code for create a circle is this:
from manim import *

# a simple python class
class DefaultClassExample(Scene):
    def construct(self):
        # add a circle 
        circle = Circle()
        # create a animation 
        self.play(Create(circle))
Use this command to create a video with this source code
manim -pql manim_001.py test
Manim Community v0.16.0.post0
...
INFO     Previewed File at: 'C:\Python310\media\videos\manim_001\480p15\DefaultClassExample.mp4'
...
This is the result of this command:

Blender 3D and python scripting - part 024.

In this tutorial I will show you how to use GeometryNodes with python script and Blender A.P.I.
You can see the result in the next image.
The Object Info node gets information from objects. This can be useful to control parameters in the geometry node tree with an external object, either directly by using its geometry, or via its transformation properties. An Object Info node can be added quickly by dragging an object into the node editor.
Another information can be found on the manual link.
In the next script you can see I created a simple BezierCurve object.
The definition named new_GeometryNodes_group is used to create two nodes GroupInit and GroupOutput.
I commente the source code to see some steps.
# import python packages
import bpy
from mathutils import Vector

# create a simpple BezierCurve and rename it with 'BezierCurveGeormetryNode'
bpy.ops.curve.primitive_bezier_curve_add()
bpy.ops.object.modifier_add(type='NODES')  

curve = bpy.context.active_object
curve.name = 'BezierCurveGeormetryNode'

# define a function for GroupInit and GroupOutput
def new_GeometryNodes_group():
    ''' Create a new empty node group that can be used
        in a GeometryNodes modifier.
    '''
    node_group = bpy.data.node_groups.new('GeometryNodes', 'GeometryNodeTree')
    inNode = node_group.nodes.new('NodeGroupInput')
    inNode.outputs.new('NodeSocketGeometry', 'Geometry')
    outNode = node_group.nodes.new('NodeGroupOutput')
    outNode.inputs.new('NodeSocketGeometry', 'Geometry')
    node_group.links.new(inNode.outputs['Geometry'], outNode.inputs['Geometry'])
    # the -3.5 is value for how far will be set the GroupInit and GroupOutput in the area of GeormetryNodes
    inNode.location = Vector((-3.5*inNode.width, 0))
    outNode.location = Vector((3.5*outNode.width, 0))
    return node_group

# the default curve modifier has no node group set, you need to set :
if curve.modifiers[-1].node_group:
    node_group = curve.modifiers[-1].node_group    
else:
    node_group = new_GeometryNodes_group()
    curve.modifiers[-1].node_group = node_group

# set default grup node as nodes
nodes = node_group.nodes

# get both nodes for each one 
group_in = nodes.get('Group Input')
group_out = nodes.get('Group Output')

# add the GeometryNodeObjectInfo to the GeometryNode area 
new_node_obj = nodes.new('GeometryNodeObjectInfo')
new_node_obj.inputs[0].default_value = bpy.data.objects["BezierCurveGeormetryNode"]